在NLTK和scikit-learn中结合文本词干和删除标点符号

时间:2014-09-30 17:14:33

标签: python text scikit-learn nltk

我正在使用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]]

但是,如何才能最好地摆脱第二版中的标点字符?

1 个答案:

答案 0 :(得分:26)

有几个选项,请在标记化之前尝试删除标点符号。但这意味着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

上面的代码可以正常运行但速度相当慢,因为它会循环遍历同一个文本多次:

  • 删除标点符号
  • 第二次标记化
  • 第三次干活。

如果您有更多步骤,例如删除数字或删除停用词或小写等等。

最好将这些步骤尽可能地集中在一起,如果您的数据需要更多的预处理步骤,这里有几个更好的答案: