【发布时间】:2017-06-05 17:15:47
【问题描述】:
我有以下功能:
def preprocessText (data):
stemmer = nltk.stem.porter.PorterStemmer()
preprocessed = []
for each in data:
tokens = nltk.word_tokenize(each.lower().translate(string.punctuation))
filtered = [word for word in tokens if word not in nltk.corpus.stopwords.words('english')]
preprocessed.append([stemmer.stem(item) for item in filtered])
print(Counter(tokens).most_common(10))
return (np.array(preprocessed))
应该使用 Porter Stemmer 删除标点符号、标记、删除停用词和词干。但是,它不能正常工作。例如,当我运行这段代码时:
s = ["The cow and of.", "and of dog the."]
print (Counter(preprocessText(s)))
它产生这个输出:
[('and', 1), ('.', 1), ('dog', 1), ('the', 1), ('of', 1)]
不会删除标点符号或停用词。
【问题讨论】:
标签: python string nltk preprocessor