簡體   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