繁体   English   中英

NLTK:情绪分析和词干分析

[英]NLTK: Sentiment Analysis and Stemming

我正在为情感分析编写代码。 现在,我想在代码段中使用Stemmer,但是当我使用打印功能时,结果表明词干无法正常工作。 你知道我在做什么错吗? 这是我的代码段:

pos_data = []
with open('Positive.txt') as f:  
    for line in f:
        pos_data.append([format_sentence(line), 'pos'])
    for line in f:
        stemmer.stem(pos_data)
print (pos_data)

您既需要将文件拆分成几行,又需要将这些行拆分成多个单词(可以被标记化)

>>> import nltk
>>> from nltk import PorterStemmer
>>> test = 'this sentence is just a tester set of words'
>>> test_tokenize = nltk.word_tokenize(test)
>>> test_tokenize
['this', 'sentence', 'is', 'just', 'a', 'tester', 'set', 'of', 'words']
>>> port = PorterStemmer()
>>> for word in test_tokenize:
...     print port.stem(word)
... 
thi
sentenc
is
just
a
tester
set
of
word



with open('Positive.txt', 'rb') as f:
    for line in f.readlines():
        words = nltk.word_tokenize(line)
        for word in words:
            print port.stem(word)

您似乎没有正确调用Stemmer API,因为它一次只需要一个令牌。 这意味着您应该首先标记您的句子。 在此处查看文档http://www.nltk.org/howto/stem.html

另外,为了将来参考,您应该包括完整的工作代码,以及错误的导入和堆栈跟踪。

with open('Positive.txt') as f:  
    for line in f:
        tokens = format_sentence(line).split() # tokenize using spaces
        stem_sentence = ' '.join([stemmer.stem(token) for token in tokens])
        pos_data.append([stem_sentence, 'pos'])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM