【问题标题】:Removing accented words from stop words algorithm with NLTK使用 NLTK 从停用词算法中删除重音词
【发布时间】:2018-12-06 14:29:17
【问题描述】:

我正在尝试在 Python 中开发一个简单的算法来从文本中删除停用词,但我遇到了带有重音的单词的问题。我正在使用以下代码:

import io
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from unicodedata import normalize
import sys

reload(sys)
sys.setdefaultencoding('utf8')

stop_words = set(stopwords.words('portuguese'))
file1 = open("C:\Users\Desktop\Test.txt")
print("File open")
line = file1.read()
words = line.split()
#convert the words to lower case
words = [word.lower() for word in words]
print("Running!")
for r in words:
    if r not in stop_words:
            appendFile = open('finalText.txt','a')
            appendFile.writelines(" "+r)
            appendFile.close()

print("Finished!")

使用以下测试文件运行代码时:

E É Á A O Ó U Ú

我有这个输出:

 É Á Ó Ú

它似乎无法识别重读单词,并且对 utf-8 使用“setdefaultencoding”不起作用,有谁知道我可以用来解决这个问题的解决方案吗?

【问题讨论】:

    标签: python nltk


    【解决方案1】:

    这不是编码或重音问题。这些只是不在列表中的单词:

    from nltk.corpus import stopwords
    stop_words = set(stopwords.words('portuguese'))
    
    print(" ".join([w for w in stop_words if len(w) == 1]))
    # >>> e à o a
    # -> does not contain á é ó ú
    
    print("À".lower() in stop_words)
    # >>> True
    

    如果需要,您可以将单词添加到集合 (stop_words.add("é"))。

    【讨论】:

      猜你喜欢
      • 2013-05-12
      • 1970-01-01
      • 2013-10-08
      • 2015-01-20
      • 2021-02-02
      • 2016-01-19
      • 2016-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多