我正在尝试识别字符串句子中识别的特定单词(从列表中)。
我已经成功导入了一个(不适当的)单词列表,然后将其与输入句子进行比较,看看该单词是否在句子中(在基本的 if 循环中使用) - 它运行良好(下面的代码),但现在我需要确定实际上发现哪个单词用作输出的一部分。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from inappropriate_words import inappropriate # a list of inappropriate words
import sys
message = ' '.join(sys.argv[1:]) # the input message already converted to lowercase
message = message.replace(".", "") # to remove the full stop as well
#print (message) #to test if needed
if any(word in message.split() for word in inappropriate):
print "SAMPLE WORD is inappropriate."
一个例子是:
输入:“你喜欢cookie吗”
流程:Cookies 位于不适当的列表中,因此它被识别并触发 if 循环
输出:“Cookie 是不合适的。” # 我喜欢 cookies SBTW
最佳答案
我会使用一个集合来存储不适当的单词,然后简单地进行查找,这是O(1)
,而不是使用列表O(n)
:
st = set(inappropriate)
message = ' '.join(sys.argv[1:]) # the input message already converted to lowercase
message = message.replace(".", "") # to remove the full stop as well
for word in message.split():
if word in st:
print "{} is inappropriate.".format(word)
如果您想查看是否有任何单词匹配,请添加一个中断,以查看所有匹配的单词按原样使用。
您还可以使用 set.intersection 查找所有常见单词:
comm = st.intersection(message.split())
最后,您可以去掉单词中的标点符号并使用 argv[1:]
,而不是加入和替换:
from string import punctuation
from inappropriate_words import inappropriate # a list of inappropriate words
import sys
for word in sys.argv[1:]:
if word.strip(punctuation) in st:
print "{} is inappropriate.".format(word)
关于python - 输出在字符串中找到的特定单词(从列表中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31904098/