我想在一个文件中做词干分析。当我在终端中使用它时,它工作得很好,但当我将它应用于文本文件时,它就不能工作了。终端代码:
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任何建议都将非常感谢,谢谢:)
发布于 2013-05-30 20:55:17
您需要将文本拆分成单词,词干分析器才能工作。目前,变量text将整个文件包含为一个大字符串。循环for plural in text:将text中的每个字符分配给plural。
请尝试使用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)https://stackoverflow.com/questions/16835372
复制相似问题