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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在NLTK和scikit-learn中结合文本词干和删除标点符号相关的知识,希望对你有一定的参考价值。

我正在使用NLTK和scikit-learnCountVectorizer组合来阻止单词和标记化。

下面是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

上面的代码可以工作,但它很慢,因为它循环遍历同一个文本多次:

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

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

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

以上是关于在NLTK和scikit-learn中结合文本词干和删除标点符号的主要内容,如果未能解决你的问题,请参考以下文章

朴素贝叶斯的 nltk 词干和停用词

02 NLTK 分句分词词干提取词型还原

获取 nltk semcor 语料库词的意义词干

NLTK 和 scikit-learn 中的伯努利朴素贝叶斯结果不同

整理了25个Python文本处理案例,收藏!

火炉炼AI机器学习036-NLP词形还原