简体   繁体   中英

Tagging spanish text with Unicode characters not possible with NLTK?

I'm trying to parse some spanish sentences that contain non-ascii characters (mostly accents in words...for instance: película (film), atención (attention), etc).

I'm reading the lines from a file encoded with utf-8. Here is a sample of my script:

# -*- 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()

At this line:

output_line = tagger.uni.tag(s.split())

I'm getting this error:

/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)

Here is some output for a simple sentence:

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)]

If I understood correctly from this chapter ...the process is correct...I decode the line from utf-8 to Unicode, tag, and then encode from Unicode to utf-8 again...I don't understand this error

Any idea what I'm doing wrong?

Thanks, Alejandro

EDIT: found the problem...basically the spanish cess_esp corpus is encoded with Latin-2 encoding. See the code below to see how to be able to train the tagger correctly.

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

A better way would be to use the CorpusReader class to ask for the corpus encoding, thus you don't need to know it before-hand.

Possibly something is wrong with your tagger object or how your file is read. I re-wrote part of your code and it runs without error:

# -*- 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]:

http://pastebin.com/n0NK574a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM