Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #49427

Re: MeCab UTF-8 Decoding Problem

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsfeed.datemas.de!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '16,': 0.03; 'syntax': 0.04; 'string.': 0.05; '-*-': 0.07; 'continuation': 0.07; 'indicated': 0.07; 'linux,': 0.07; 'string': 0.09; 'ascii': 0.09; 'output,': 0.09; 'properly.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'throws': 0.09; 'true)': 0.09; 'url:googlecode': 0.09; 'python': 0.11; 'jan': 0.12; '2.7': 0.14; 'codec': 0.16; 'encoding.': 0.16; 'exited': 0.16; 'fine.': 0.16; 'hex': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject:Problem': 0.16; 'subject:UTF': 0.16; 'sure.': 0.16; 'url:svn': 0.16; 'followed': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'normally': 0.19; "skip:' 30": 0.19; 'example': 0.22; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'byte': 0.24; 'bytes': 0.24; 'text.': 0.24; 'unicode': 0.24; 'looks': 0.24; 'software.': 0.24; 'question': 0.24; 'nearly': 0.26; 'skip:" 40': 0.26; 'code:': 0.26; 'asking': 0.27; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'appear': 0.29; 'am,': 0.29; 'character': 0.29; 'generally': 0.29; 'url:code': 0.29; 'characters': 0.30; 'prints': 0.31; 'file': 0.32; 'themselves': 0.32; 'another': 0.32; 'text': 0.33; '(most': 0.33; 'skip:# 10': 0.33; 'maybe': 0.34; 'problem': 0.35; "can't": 0.35; 'display': 0.35; 'case,': 0.35; 'but': 0.35; 'add': 0.35; 'marks': 0.36; 'representing': 0.36; 'set.': 0.36; 'surely': 0.36; 'hi,': 0.36; 'error.': 0.37; 'to:addr:python-list': 0.38; 'skip:- 10': 0.38; 'recent': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'called': 0.40; 'skip:u 10': 0.60; 'dave': 0.60; 'received:173': 0.61; 'skip:* 10': 0.61; 'email addr:gmail.com': 0.63; 'url:index': 0.63; 'skip:n 10': 0.64; 'url:p': 0.64; 'more': 0.64; 'here': 0.66; 'invalid': 0.68; 'press': 0.70; '8bit%:100': 0.72; 'analysis': 0.75; '8bit%:77': 0.84; "it'd": 0.84; 'received:fios.verizon.net': 0.84; 'skip:\xef 10': 0.84; 'angel': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: MeCab UTF-8 Decoding Problem
Date Sat, 29 Jun 2013 11:32:48 -0400
References <f4fe97e3-5949-4c52-97d7-4995b8891efd@googlegroups.com> <51CEE910.4090701@davea.name>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding quoted-printable
X-Gmane-NNTP-Posting-Host pool-173-75-251-66.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130620 Thunderbird/17.0.7
In-Reply-To <51CEE910.4090701@davea.name>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3994.1372519986.3114.python-list@python.org> (permalink)
Lines 84
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1372519986 news.xs4all.nl 15918 [2001:888:2000:d::a6]:45902
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:49427

Show key headers only | View raw


On 6/29/2013 10:02 AM, Dave Angel wrote:
> On 06/29/2013 07:29 AM, fobos3@gmail.com wrote:
>> Hi,
>
> Using Python 2.7 on Linux, presumably?  It'd be better to be explicit.
>
>>
>> I am trying to use a program called MeCab, which does syntax analysis
>> on Japanese text.

It is generally nice to give a link when asking about 3rd party 
software.  https://code.google.com/p/mecab/
In this case, nearly all the non-boilerplate text is Japanese ;-(.

 >> The problem I am having is that it returns a byte string

and the problem with bytes is that they can have any encoding.
In Python 2 (indicated by your print *statements*), a byte string is 
just a string.

>> and if I try to print it, it prints question marks for almost
>> all characters. However, if I try to use .decide, it throws an error.
>> Here is my code:
>
> What do the MeCab docs say the tagger.parse byte string represents?
> Maybe it's not text at all.  But surely it's not utf-8.

https://mecab.googlecode.com/svn/trunk/mecab/doc/index.html
MeCab: Yet Another Part-of-Speech and Morphological Analyzer
followed by Japanese.

>> #!/usr/bin/python
>> # -*- coding:utf-8 -*-
>>
>> import MeCab
>> tagger = MeCab.Tagger("-Owakati")
>> text = 'MeCabで遊んでみよう!'

Parts of this appear in the output, as indicated by spaces.
'MeCabで遊 んで みよ う!'

>> result = tagger.parse(text)
>> print result
>>
>> result = result.decode('utf-8')
>> print result
>>
>> And here is the output:
>>
>> MeCab �� �� ��んで�� �� ��う!

Python normally prints bytes with ascii chars representing either 
themselves or other values with hex escapes. This looks more like 
unicode sent to a terminal with a limited character set. I would add

print type(result)

to be sure.

>> Traceback (most recent call last):
>>    File "test.py", line 11, in <module>
>>      result = result.decode('utf-8')
>>    File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
>>      return codecs.utf_8_decode(input, errors, True)
>> UnicodeDecodeError: 'utf8' codec can't decode bytes in position 6-7:
>> invalid continuation byte
>>
>>
>> ------------------
>> (program exited with code: 1)
>> Press return to continue
>>
>> Also my terminal is able to display Japanese characters properly. For
>> example print '日本語' works perfectly fine.


-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

MeCab UTF-8 Decoding Problem fobos3@gmail.com - 2013-06-29 04:29 -0700
  Re: MeCab UTF-8 Decoding Problem Giorgos Tzampanakis <giorgos.tzampanakis@gmail.com> - 2013-06-29 13:46 +0000
  Re: MeCab UTF-8 Decoding Problem Dave Angel <d@davea.name> - 2013-06-29 10:02 -0400
  Re: MeCab UTF-8 Decoding Problem Terry Reedy <tjreedy@udel.edu> - 2013-06-29 11:32 -0400
  Re: MeCab UTF-8 Decoding Problem Terry Reedy <tjreedy@udel.edu> - 2013-06-29 11:55 -0400
  Re: MeCab UTF-8 Decoding Problem MRAB <python@mrabarnett.plus.com> - 2013-06-29 17:12 +0100
  Re: MeCab UTF-8 Decoding Problem Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-29 16:20 +0000

csiph-web