繁体   English   中英

使用NLTK从停用词算法中删除重音词

[英]Removing accented words from stop words algorithm with NLTK

我正在尝试在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”不起作用,有人知道我可以用来解决此问题的解决方案吗?

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

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("é") )。

暂无
暂无

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

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