法语文本中最常见的单词

rom*_*rom 2 python text nltk stop-words

我正在使用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]
Run Code Online (Sandbox Code Playgroud)

这是输出:

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)]
Run Code Online (Sandbox Code Playgroud)

我的问题是stopwords不丢弃所有无意义的词.例如','不是单词,应该删除,'les'是一篇文章,应该删除.

如何解决问题?

我使用的文本可以在这个页面找到:http: //www.elysee.fr/la-presidence/discours-d-investiture-de-nicolas-sarkozy/

use*_*743 6

通常,最好使用自己的停用词列表.为此,您可以从此处获取法语停用词列表.文章"les"也在列表中.创建它们的文本文件,并使用该文件从语料库中删除停用词.然后为了标点符号,你必须写一个标点删除功能.你应该怎么写,高度取决于你的应用程序.但只是为了向您展示一些可以帮助您入门的示例,您可以写道:

import string
t = "hello, eric! how are you?"
print t.translate(string.maketrans("",""), string.punctuation)
Run Code Online (Sandbox Code Playgroud)

输出是:

hello eric how are you
Run Code Online (Sandbox Code Playgroud)

或者,另一种方式是简单地写:

t = t.split()
for w in t:
    w = w.strip('\'"?,.!_+=-')
    print w
Run Code Online (Sandbox Code Playgroud)

所以,它实际上取决于你需要如何删除它们.在某些情况下,这些方法可能不会产生您想要的结果.但是,你可以建立它们.如果您有任何其他问题,请与我们联系.