编程语言
首页 > 编程语言> > python-从二元语法列表中删除uni-gram

python-从二元语法列表中删除uni-gram

作者:互联网

我设法从文本文档创建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个单词组合,而不是单个单词停止.

解决方法:

您可以分步进行.首先定义一个辅助函数:

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] 

标签:n-gram,file-handling,python,list
来源: https://codeday.me/bug/20191012/1900218.html