我设法从文本文档创建了2个列表。第一个是我的双语列表:
keywords = ['nike shoes','nike clothing', 'nike black', 'nike white']
和停用词列表:
stops = ['clothing','black','white']
我想从关键字列表中删除停止。使用上面的示例,我之后的输出应该如下所示:
new_keywords = ['nike shoes','nike', 'nike', 'nike'] --> eventually I'd like to remove those dupes.
这是我到目前为止所做的:
keywords = open("keywords.txt", "r")
new_keywords = keywords.read().split(",")
stops = open("stops.txt","r")
new_stops = stops.read().split(",")
[i for i in new_keywords if i not in new_stops]
我遇到的问题是它正在寻找2个单词组合而不是单个单词停止....
答案 0 :(得分:1)
您可以分步进行。首先定义一个辅助函数:
def removeStop(bigram, stops):
return ' '.join(w for w in bigram.split() if not w in stops)
然后:
[removeStop(i,new_stops) for i in new_keywords]
答案 1 :(得分:1)
假设你有2个列表,这将做你想要的:
new_keywords = []
for k in keywords:
temp = False
for s in stops:
if s in k:
new_keywords.append(k.replace(s,""))
temp = True
if temp == False:
new_keywords.append(k)
这将创建一个像你发布的列表:
['nike shoes', 'nike ', 'nike ', 'nike ']
要消除双打,请执行以下操作:
new_keywords = list(set(new_keywords))
所以最终的清单如下:
['nike shoes', 'nike ']