[英]Python NLTK - Preventing stop word removal from removing EVERY word
我正在使用很短的單詞串,其中有些很愚蠢。 假設地,我可以有一個字符串“ you a a”,如果我刪除停用詞,那么該字符串將為空白。 由於我是在循環中進行分類,因此如果涉及到空白字符串,它只會因錯誤而停止。 我創建了以下代碼來解決此問題:
def title_features(words):
filter_words = [word for word in words.split() if word not in stopwords.words('english')]
features={}
if len(filter_words) >= 1:
features['First word'] = ''.join(filter_words[0])
else:
features['First word'] = ''.join(words.split()[0])
return features
這樣可以確保沒有錯誤,但是我想知道是否有更有效的方法來解決。 或者采取一種方法來解決所有單詞(如果它們都是停用詞)不會消失的情況。
最簡單的解決方案是檢查過濾結果,並在必要時還原完整的單詞列表。 然后,其余代碼可以使用單個變量而不進行檢查。
def title_features(words):
filter_words = [word for word in words.split() if word not in stopwords.words('english')]
if not filter_words: # Use full list if necessary
filter_words = words
features={}
features['First word'] = filter_words[0]
features[...] = ...
return features
您可以將其重寫為:
def title_features(words):
filtered = [word for word in words.split() if word not in stopwords.words('english')]
return {'First word': (filtered or words.split(None, 1) or [''])[0]}
如果不為空(例如,具有一個或多個長度或一個或多個),或者為空,則將對其進行filtered
,然后繼續拆分原始文件,如果為空,則默認為一個帶空的元素列表串。 然后,您將使用選擇的任何一個中的[0]
作為第一個元素(第一個不間斷字,字符串的第一個字或空字符串)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.