【发布时间】:2015-02-01 11:18:21
【问题描述】:
我正在使用 python nltk 包来查找法语文本中最常用的单词。我发现它真的不起作用......
这是我的代码:
#-*- coding: utf-8 -*-
#nltk: package for text analysis
from nltk.probability import FreqDist
from nltk.corpus import stopwords
import nltk
import tokenize
import codecs
import unicodedata
#output French accents correctly
def convert_accents(text):
return unicodedata.normalize('NFKD', text).encode('ascii', 'ignore')
### MAIN ###
#openfile
text_temp=codecs.open('text.txt','r','utf-8').readlines()
#put content in a list
text=[]
for word in text_temp:
word=word.strip().lower()
if word!="":
text.append(convert_accents(word))
#tokenize the list
text=nltk.tokenize.word_tokenize(str(text))
#use FreqDist to get the most frequents words
fdist = FreqDist()
for word in text:
fdist.inc( word )
print "BEFORE removing meaningless words"
print fdist.items()[:10]
#use stopwords to remove articles and other meaningless words
for sw in stopwords.words("french"):
if fdist.has_key(sw):
fdist.pop(sw)
print "AFTER removing meaningless words"
print fdist.items()[:10]
这是输出:
BEFORE removing meaningless words
[(',', 85), ('"', 64), ('de', 59), ('la', 47), ('a', 45), ('et', 40), ('qui', 39), ('que', 33), ('les', 30), ('je', 24)]
AFTER removing meaningless words
[(',', 85), ('"', 64), ('a', 45), ('les', 30), ('parce', 15), ('veut', 14), ('exigence', 12), ('aussi', 11), ('pense', 11), ('france', 10)]
我的问题是stopwords 没有丢弃所有无意义的词。
例如,“,”不是单词,应该删除,“les”是文章,应该删除。
如何解决问题?
我使用的文本可以在这个页面找到: http://www.elysee.fr/la-presidence/discours-d-investiture-de-nicolas-sarkozy/
【问题讨论】:
-
如果
nltk提供的stopwords不适合您,那么您应该自己列出要删除的停用词,或者寻找其他库。至于逗号,,您可以在全文中尝试newstr = oldstr.replace(",", ""),然后再进行任何其他工作。 -
接受您的建议。但是为什么
nltk stopwords功能不能完成它应该做的工作呢?!!! -
我看了
nltk的法语stopwords,我想说它很完整(我也说法语)。再多几个词,比如“ils”、“elles”、“les”、“leurs”(主要是复数),就可以了。我猜想写nltk中使用的Stopwords Corpus的人不太懂法语。但这也不是我们可以抱怨的,毕竟他们免费给了我们一个很棒的图书馆! -
好的,谢谢,
user823743也给出了另一个很好的解释;)。
标签: python text nltk stop-words