Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Unicode字符标记西班牙语文本无法使用NLTK?_Python_Unicode_Encoding_Utf 8_Nltk - Fatal编程技术网

Python 使用Unicode字符标记西班牙语文本无法使用NLTK?

Python 使用Unicode字符标记西班牙语文本无法使用NLTK?,python,unicode,encoding,utf-8,nltk,Python,Unicode,Encoding,Utf 8,Nltk,我试图解析一些包含非ascii字符的西班牙语句子(主要是单词中的重音符号……例如:película(电影)、atención(注意)等) 我正在读取一个用utf-8编码的文件中的行。以下是我的脚本示例: # -*- coding: utf-8 -*- import nltk import sys from nltk.corpus import cess_esp as cess from nltk import UnigramTagger as ut from nltk import Bigra

我试图解析一些包含非ascii字符的西班牙语句子(主要是单词中的重音符号……例如:película(电影)、atención(注意)等)

我正在读取一个用utf-8编码的文件中的行。以下是我的脚本示例:

# -*- coding: utf-8 -*-

import nltk
import sys
from nltk.corpus import cess_esp as cess
from nltk import UnigramTagger as ut
from nltk import BigramTagger as bt

f = codecs.open('spanish_sentences', encoding='utf-8')
results_file = codecs.open('tagging_results', encoding='utf-8', mode='w+')

for line in iter(f):

    output_line =  "Current line contents before tagging->" + str(line.decode('utf-8', 'replace'))
    print output_line
    results_file.write(output_line.encode('utf8'))

    output_line = "Unigram tagger->"
    print output_line
    results_file.write(output_line)

    s = line.decode('utf-8', 'replace')
    output_line = tagger.uni.tag(s.split())
    print output_line
    results_file.write(str(output_line).encode('utf8'))

f.close()
results_file.close()
在这一行:

output_line = tagger.uni.tag(s.split())
我得到了这个错误:

/usr/local/lib/python2.7/dist-packages/nltk-2.0.4-py2.7.egg/nltk/tag/sequential.py:138: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
return self._context_to_tag.get(context)
下面是一个简单句子的一些输出:

Current line contents before tagging->tengo una queja y cada que hablo a atención me dejan en la linea media hora y cortan la llamada!!

Unigram tagger->
[(u'tengo', 'vmip1s0'), (u'una', 'di0fs0'), (u'queja', 'ncfs000'), (u'y', 'cc'), (u'cada', 'di0cs0'), (u'que', 'pr0cn000'), (u'hablo', 'vmip1s0'), (u'a', 'sps00'), (u'atenci\xf3n', None), (u'me', 'pp1cs000'), (u'dejan', 'vmip3p0'), (u'en', 'sps00'), (u'la', 'da0fs0'), (u'linea', None), (u'media', 'dn0fs0'), (u'hora', 'ncfs000'), (u'y', 'cc'), (u'cortan', None), (u'la', 'da0fs0'), (u'llamada!!', None)]
如果我正确理解了…过程是正确的…我将行从utf-8解码为Unicode,标记,然后再次从Unicode编码为utf-8…我不理解这个错误

知道我做错了什么吗

谢谢, 亚历杭德罗

EDIT:发现了问题……基本上,西班牙语cess_esp语料库是用拉丁-2编码的。请参阅下面的代码,了解如何正确培训标签工。

tagged_sents = (
[(word.decode('Latin2'), tag) for (word, tag) in sent]
for sent in cess.tagged_sents()
)
tagger = UT(tagged_sents)  # training a tagger

更好的方法是使用类请求语料库编码,因此您不需要事先知道。

可能是tagger对象或文件读取方式有问题。我重新编写了部分代码,它运行时没有错误:

tagged_sents = (
[(word.decode('Latin2'), tag) for (word, tag) in sent]
for sent in cess.tagged_sents()
)
tagger = UT(tagged_sents)  # training a tagger
# -*- coding: utf-8 -*-

import urllib2, codecs

from nltk.corpus import cess_esp as cess
from nltk import word_tokenize
from nltk import UnigramTagger as ut
from nltk import BigramTagger as bt

tagger = ut(cess.tagged_sents())

url = 'https://db.tt/42Lt5M5K'
fin = urllib2.urlopen(url).read().strip().decode('utf8')
fout = codecs.open('tagger.out', 'w', 'utf8')
for line in fin.split('\n'):
    print>>fout, "Current line contents before tagging->", line
    print>>fout, "Unigram tagger->",
    print>>fout, tagger.tag(word_tokenize(line))
    print>>fout, ""
[out]:


你能把你的
西班牙语句子
文件上传到网上的某个地方吗?你好,阿尔瓦斯!当然,在这里:您的代码丢失了
tagger
?我想你是在cess语料库上训练的?你好,阿尔瓦斯,刚刚复制并粘贴了你的代码,但一旦发现一个非ascii的单词,仍然会出现相同的错误/usr/local/lib/python2.7/dist packages/nltk-2.0.4-py2.7.egg/nltk/tag/sequential.py:138:UnicodeWarning:Unicode相等比较无法将两个参数转换为Unicode-将它们解释为不相等的返回自我。_context_to_tag.get(context)我想知道这是否与我的nltk版本有关…您使用的是哪一个?谢谢我甚至在Ubuntu 13.04的一个新的virtualbox图像上尝试过这个,只是为了丢弃与我的机器相关的任何东西,结果得到了完全相同的问题……我很困惑!do
sudo pip安装-U pyyaml nltk
要求已经更新:pyyaml-in/usr/local/lib/python2.7/dist-packages/pyyaml-3.11-py2.7-linux-x86_64.egg要求已经更新:nltk-in/usr/local/lib/python2.7/dist-packages/nltk-2.0.4-py2.7.7.egg清理…您的nltk版本是什么?