2

我想在文件中进行词干处理。当我在终端中使用它时它工作正常,但是当我将它应用到文本文件中时,它不起作用。终端代码:

print PorterStemmer().stem_word('complications')

功能代码:

def stemming_text_1():
    with open('test.txt', 'r') as f:
        text = f.read()
        print text
        singles = []    

        stemmer = PorterStemmer() #problem from HERE
        for plural in text:
            singles.append(stemmer.stem(plural))
        print singles

输入test.txt

126211 crashes bookmarks runs error logged debug core bookmarks
126262 manual change crashes bookmarks propagated ion view bookmarks

期望/预期输出

126211 crash bookmark runs error logged debug core bookmark
126262 manual change crash bookmark propagated ion view bookmark

任何建议将不胜感激,谢谢:)

4

1 回答 1

2

您需要将文本拆分为单词,以便词干分析器工作。目前,该变量text包含整个文件作为一个大字符串。循环for plural in text:将每个字符分配textplural.

试试for plural in text.split():吧。

[编辑]要获得所需格式的输出,您需要逐行读取文件,而不是一次全部读取:

def stemming_text_1():
    with open('test.txt', 'r') as f:
        for line in f:
            print line
            singles = []

            stemmer = PorterStemmer() #problem from HERE
            for plural in line.split():
                singles.append(stemmer.stem(plural))
            print ' '.join(singles)
于 2013-05-30T12:55:16.590 回答