gpt4 book ai didi

Python 机器人,尝试扫描特定的独立单词

转载 作者:太空宇宙 更新时间:2023-11-03 17:19:58 25 4
gpt4 key购买 nike

我正在创建一个 reddit 机器人,用于搜索评论中的特定单词。假设我扫描“美国”这个词,我只希望它找到美国作为一个独立的词(我不确定这个词是什么)。如果有人说“美国”,我希望机器人忽略它。

words_to_match = ['america']

for comment in comments:
comment_text = comment.body.lower()
isMatch = any(string in comment_text for string in words_to_match)

这就是我到目前为止所拥有的。

编辑:实际上,我刚刚意识到我可以创建一个新的要忽略的单词列表,至少对于这个特定的机器人来说是这样。

words_to_ignore = ['american']

最佳答案

如果您只想搜索该单词,可以使用 re.findall()

data = '''
I'm creating a reddit bot that searches for a specific word in comments.
Lets say i scan for the word "america", i only want it to find america as
a standalone word (I'm not sure what the word for this is). If someone says
"american" i want the bot to ignore it.
'''
def is_match(st):
# checks if word America, regardless of case, appears in
# text as a standalone word
import re
if re.findall(r'\bamerica\b', st, flags=re.IGNORECASE):
return True
else:
return False

print(is_match(data))

True

关于Python 机器人,尝试扫描特定的独立单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33270192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com