Remove uni-grams from a list of bi-grams

别来无恙 提交于 2019-12-02 01:51:48

You can do it in steps. First define a helper function:

def removeStop(bigram, stops):
    return ' '.join(w for w in bigram.split() if not w in stops)

And then:

[removeStop(i,new_stops) for i in new_keywords] 

assuming you have the 2 lists this will do what you want:

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)

This will create a list like you posted:

['nike shoes', 'nike ', 'nike ', 'nike ']

To eliminate the doubles do this:

new_keywords = list(set(new_keywords))

So the final list looks like this:

['nike shoes', 'nike ']

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!