2017-09-01 71 views
0

我想從句子中刪除停用詞。 我已經這段代碼:「」刪除句子中的停用詞

splitted = text.split() 

for index, word in enumerate(splitted): 
    if word in self.stopWords: 
     del splitted[index] 

text = " ".join(splitted) 

停用詞與該指令self.stopWords.update(['.', ',', "\"", "\'", '?', '!', ':', ';', '(', ')', '[', ']', '{', '}', '),', '],', '},', '",', "',", '")', '"]', '"}', "-", "--", '\".', "\'.", '/', ').', '-', '--', '%', '°\'', '(-', '("', '."', '.),', ');', '–', '$', 'a'])但是,例如,字母更新的「A」,如,或‘;’不會從句子中刪除。

我該怎麼辦?

+1

同時通過它迭代不從列表中刪除。創建一個新的,而不是過濾 – MooingRawr

+1

你的'self.stopWords'包含**字符**而不是**字符**,想想兩者之間的區別 – stovfl

回答

1

我覺得它更容易使用列表理解(或發電機表達,因爲我在這裏做的):

' '.join(w for w in text.split() if w not in stop_words)