如何精确匹配一个句子,而不是子串匹配
How to match a sentence exactly, rather than substring match
所以我使用 PRAW 创建了一个简单的 reddit 机器人:
import praw
import time
from praw.helpers import comment_stream
r = praw.Reddit("han_solo_response")
r.login()
target_text = "I love you"
response_text = "I know"
processed = []
while True:
for c in comment_stream(r, 'all'):
if target_text in c.body and c.id not in processed:
#if find comment not in cache
c.reply(response_text)
processed.append(c.id) #then push the response
time.sleep(20)
每当有人在 reddit 上的任何地方根据著名的星球大战电影交流发布“我爱你”时,它都会生成 han solo 回复“我知道”。它现在正在工作,但它正在捕获任何包含 "i love you" 的内容,例如我不得不删除一条评论,其中那个人说“我爱你的车”。
我怎样才能将其触发精确地限制为 "I love you",而不是 "your" 或任何其他包含 "you" 的单词,或者就此而言,使其仅触发响应通过独立的短语,而不是当它出现在句子的上下文中时。
这一行:
if target_text in c.body and c.id not in processed:
应该是:
if target_text == c.body and c.id not in processed:
如果你想让它不区分大小写:
target_text = "i love you"
和
if target_text == c.body.lower() and c.id not in processed:
所以我使用 PRAW 创建了一个简单的 reddit 机器人:
import praw
import time
from praw.helpers import comment_stream
r = praw.Reddit("han_solo_response")
r.login()
target_text = "I love you"
response_text = "I know"
processed = []
while True:
for c in comment_stream(r, 'all'):
if target_text in c.body and c.id not in processed:
#if find comment not in cache
c.reply(response_text)
processed.append(c.id) #then push the response
time.sleep(20)
每当有人在 reddit 上的任何地方根据著名的星球大战电影交流发布“我爱你”时,它都会生成 han solo 回复“我知道”。它现在正在工作,但它正在捕获任何包含 "i love you" 的内容,例如我不得不删除一条评论,其中那个人说“我爱你的车”。
我怎样才能将其触发精确地限制为 "I love you",而不是 "your" 或任何其他包含 "you" 的单词,或者就此而言,使其仅触发响应通过独立的短语,而不是当它出现在句子的上下文中时。
这一行:
if target_text in c.body and c.id not in processed:
应该是:
if target_text == c.body and c.id not in processed:
如果你想让它不区分大小写:
target_text = "i love you"
和
if target_text == c.body.lower() and c.id not in processed: