Python:NLTK 和 TextBlob(法语) [英] Python: NLTK and TextBlob in french

查看:74
本文介绍了Python:NLTK 和 TextBlob(法语)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 NLTK 和 TextBlob 在文本中查找名词和名词短语:

I'm using NLTK and TextBlob to find nouns and noun phrases in a text:

from textblob import TextBlob 
import nltk

blob = TextBlob(text)
print(blob.noun_phrases)
tokenized = nltk.word_tokenize(text)
nouns = [word for (word, pos) in nltk.pos_tag(tokenized) if is_noun(pos)]
print(nouns)

如果我的文字是英文的,这很好用,但如果我的文字是法文就不行了.

This works fine if my text is in english but it's not good anymore if my text is in french.

我找不到如何将此代码改编为法语,我该怎么做?

I was unable to find how to adapt this code for french language, how do I do that?

是否有可以解析的所有语言的列表?

And is there a list somewhere of all the languages that are possible to parse?

推荐答案

默认情况下 NLTK 使用英语分词器,这在法语中会有奇怪或未定义的行为.

By default NLTK uses the English tokenizer, which will have strange or undefined behavior in French.

@fpierron 是正确的.如果您阅读了它提到的文章,您只需加载正确的分词器语言模型并在您的程序中使用它.

@fpierron is correct. If you read the article it mentions, you simply have to load the correct tokenizer language model and use it in your program.

import nltk.data
#chargement du tokenizer
tokenizer = nltk.data.load('tokenizers/punkt/french.pickle')
tokens = tokenizer.tokenize("Jadis, une nuit, je fus un papillon, voltigeant, content de son sort. Puis, je m’éveillai, étant Tchouang-tseu. Qui suis-je en réalité ? Un papillon qui rêve qu’il est Tchouang-tseu ou Tchouang qui s’imagine qu’il fut papillon ?")

print(tokens) 

['Jadis, une nuit, je fus un papillon, voltigeant, content de son sort.', 'Puis, je m’éveillai, étant Tchouang-tseu.', 'Qui suis-je en réalité ?', 'Un papillon qui rêve qu’il est Tchouang-tseu ou Tchouang qui s’imagine qu’il fut papillon ?']

如果您没有正确的文件,您可以使用nltk.download()";下载正确的法语模型.

If you don't have the correct file you can use "nltk.download()" to download the correct model for french.

如果您查看分词器上的 NLTK 网站,还有一些其他示例.http://www.nltk.org/api/nltk.tokenize.html

if you look at NLTKs website on the tokenizer, there are some other examples. http://www.nltk.org/api/nltk.tokenize.html

这篇关于Python:NLTK 和 TextBlob(法语)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆