在NLTK和scikit-learn中结合文本词干和删除标点符号
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在NLTK和scikit-learn中结合文本词干和删除标点符号相关的知识,希望对你有一定的参考价值。
我正在使用NLTK和scikit-learn
的CountVectorizer
组合来阻止单词和标记化。
下面是CountVectorizer
的简单用法示例:
from sklearn.feature_extraction.text import CountVectorizer
vocab = ['The swimmer likes swimming so he swims.']
vec = CountVectorizer().fit(vocab)
sentence1 = vec.transform(['The swimmer likes swimming.'])
sentence2 = vec.transform(['The swimmer swims.'])
print('Vocabulary: %s' %vec.get_feature_names())
print('Sentence 1: %s' %sentence1.toarray())
print('Sentence 2: %s' %sentence2.toarray())
哪个会打印
Vocabulary: ['he', 'likes', 'so', 'swimmer', 'swimming', 'swims', 'the']
Sentence 1: [[0 1 0 1 1 0 1]]
Sentence 2: [[0 0 0 1 0 1 1]]
现在,让我们说我想删除停用词并阻止这些词。一种选择是这样做:
from nltk import word_tokenize
from nltk.stem.porter import PorterStemmer
#######
# based on http://www.cs.duke.edu/courses/spring14/compsci290/assignments/lab02.html
stemmer = PorterStemmer()
def stem_tokens(tokens, stemmer):
stemmed = []
for item in tokens:
stemmed.append(stemmer.stem(item))
return stemmed
def tokenize(text):
tokens = nltk.word_tokenize(text)
stems = stem_tokens(tokens, stemmer)
return stems
########
vect = CountVectorizer(tokenizer=tokenize, stop_words='english')
vect.fit(vocab)
sentence1 = vect.transform(['The swimmer likes swimming.'])
sentence2 = vect.transform(['The swimmer swims.'])
print('Vocabulary: %s' %vect.get_feature_names())
print('Sentence 1: %s' %sentence1.toarray())
print('Sentence 2: %s' %sentence2.toarray())
哪个印刷品:
Vocabulary: ['.', 'like', 'swim', 'swimmer']
Sentence 1: [[1 1 1 1]]
Sentence 2: [[1 0 1 1]]
但是,如何才能最好地摆脱第二版中的标点字符呢?
答案
有几个选项,请在标记化之前尝试删除标点符号。但这意味着don't
- > dont
import string
def tokenize(text):
text = "".join([ch for ch in text if ch not in string.punctuation])
tokens = nltk.word_tokenize(text)
stems = stem_tokens(tokens, stemmer)
return stems
或者尝试在标记化后删除标点符号。
def tokenize(text):
tokens = nltk.word_tokenize(text)
tokens = [i for i in tokens if i not in string.punctuation]
stems = stem_tokens(tokens, stemmer)
return stems
EDITED
上面的代码可以工作,但它很慢,因为它循环遍历同一个文本多次:
- 一旦删除标点符号
- 第二次标记化
- 第三次干活。
如果您有更多步骤,例如删除数字或删除停用词或小写等。
将这些步骤尽可能地集中在一起会更好,如果您的数据需要更多的预处理步骤,这里有几个更好的答案:
- Applying NLTK-based text pre-proccessing on a pandas dataframe
- Why is my NLTK function slow when processing the DataFrame?
- https://www.kaggle.com/alvations/basic-nlp-with-nltk
以上是关于在NLTK和scikit-learn中结合文本词干和删除标点符号的主要内容,如果未能解决你的问题,请参考以下文章