Python 提取文件中的单词

标签 python nltk stemming

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

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

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

最佳答案

您需要将文本拆分为单词,词干分析器才能工作。目前,变量 text 将整个文件包含为一个大字符串。循环 forplural in text:text 中的每个字符分配给 plural

尝试使用在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)

关于Python 提取文件中的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16835372/

相关文章:

python - 如何使用 Google API Python 客户端在 OAUTH 之后获取用户电子邮件

python - 我的权限设置正确吗? (Python)

python - 将 python NLTK 解析树保存到图像文件

python - 将连字符与换行符相结合

java - 如何使用R语言tm(文本挖掘)包中的stemDocument?

python - 解析未声明的参数

python - Postgre/SQLAlchemy UUID 插入但无法比较

python - 使用 NLTK 对 POS 标记词进行词形还原?

r - 如何在 R 中将文本拆分为两个有意义的词

nlp - 雪球词干 : defining Regions