2014-08-29 6 views
2

나는 너무 혼란 스럽다.파이썬에서 단어 형태소 분석을 사용하는 UnicodeDecodeError

내가 천 마디 말

이제
x = ['company', 'arriving', 'wednesday', 'and', 'then', 'beach', 'how', 'are', 'you', 'any', 'warmer', 'there', 'enjoy', 'your', 'day', 'follow', 'back', 'please', 'everyone', 'go', 'watch', 's', 'new', 'video', 'you', 'know', 'the', 'deal', 'make', 'sure', 'to', 'subscribe', 'and', 'like', '<http>', 'you', 'said', 'next', 'week', 'you', 'will', 'be', 'the', 'one', 'picking', 'me', 'up', 'lol', 'hindi', 'na', 'tl', 'huehue', 'that', 'works', 'you', 'said', 'everyone', 'of', 'us', 'my', 'little', 'cousin', 'keeps', 'asking', 'if', 'i', 'wanna', 'play', 'and', "i'm", 'like', 'yes', 'but', 'with', 'my', 'pals', 'not', 'you', "you're", 'welcome', 'pas', 'quand', 'tu', 'es', 'vers', '<num>', 'i', 'never', 'get', 'good', 'mornng', 'texts', 'sad', 'sad', 'moment', 'i', 'think', 'ima', 'go', 'get', 'a', 'glass', 'of', 'milk', 'ahah', 'for', 'the', 'first', 'time', 'i', 'actually', 'know', 'what', 'their', 'doing', 'd', 'thank', 'you', 'happy', 'birthday', 'hope', "you're"...........] 

부부의 목록을 가지고, 나는

types = [] 
for word in x: 
    a.append(type(word)) 
print set(a) 

>>>set([<type 'str'>]) 

지금, 내가 시도 할 문자열을 수 있도록이 목록의 각 요소의 유형을 확인했습니다 NLTK의 포터 줄기를 사용하여 각 단어를 스템 핑합니다.

import nltk 
porter = nltk.PorterStemmer() 
stemmed_x = [porter.stem(word) for word in x] 

그리고이 오류는 분명히 th 전자 든 패키지와 유니 코드를 형태소 : 내가 명시 적으로 utf8 각 단어를 인코딩하기 위해 노력하고, codecs.open를 사용하여, 모든 것을 시도

File "/Library/Python/2.7/site-packages/nltk-3.0.0b2-py2.7.egg/nltk/stem/porter.py", line 633, in stem 
    stem = self.stem_word(word.lower(), 0, len(word) - 1) 
    File "/Library/Python/2.7/site-packages/nltk-3.0.0b2-py2.7.egg/nltk/stem/porter.py", line 591, in stem_word 
    word = self._step1ab(word) 
    File "/Library/Python/2.7/site-packages/nltk-3.0.0b2-py2.7.egg/nltk/stem/porter.py", line 289, in _step1ab 
    if word.endswith("ied"): 
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 12: ordinal not in range(128) 

- 여전히 같은 오류가 발생합니다.

알려 주시기 바랍니다.

편집 :

이 코드는 우분투를 실행 내 PC에 완벽하게 일을 언급해야한다. 최근에 맥북 프로가 있는데이 오류가 나타납니다. 내 Mac에서 터미널 설정을 확인하고 utf8 인코딩으로 설정되어 있습니다.

편집 2 : 단어의 끝 부분에

for w in x: 
    try: 
     porter.stem(w) 
    except UnicodeDecodeError: 
     print w 

#sagittarius” 
#instadane… 
#bleedblue” 
#pr챕cieux 
#على_شرفة_الماضي 
#exploringsf… 
#fishing… 
#sindhubestfriend… 
#الإستعداد_لإنهيار_ال_سعود 
#jaredpreslar… 
#femalepains” 
#gobillings” 
#juicing… 
#instamood… 

는 모두 공통점이 무엇인지 것 같다 있습니다 추가 구두점 :이 코드 조각,

재미있는, 나는 문제의 단어를 격리했다 , 단어 #pr-cieux를 제외합니다

+0

아마도 멀티 바이트 UTF8 문자가 주위에 숨어 있습니다. 너무 길지 않다면 코드에서 _full_ 배열 정의를 "있는 그대로"복사하여 붙여 넣을 수 있습니까? –

+0

거기에 라틴 문자가 있습니까? –

+1

완전히 다른 문자 세트가 여기에 있습니다. 가능한 경우, 프로그램에 데이터를 가져올 때 다른 언어 (또는 더 나은 아직 다른 문자 세트)에 속한 단어를 별도의 목록에 보관하면 훨씬 쉽게 사용할 수 있습니다. 그런 다음이 문자열의 바이너리를 목록 당 적절한 문자 집합으로 디코딩 할 수 있습니다. –

답변

1

아마도 0xe216-bit codepoint encoded as UTF-8에서 가능한 첫 번째 바이트 중 하나이기 때문에 멀티 바이트 UTF8 문자가 주위에 숨어있을 수 있습니다. 귀하의 프로그램이 0x00에서 0x7F까지의 유효한 인코딩 값을 가진 ASCII 문자로 가정 할 때이 값은 거부됩니다.

(나는 당신의 희망은 ASCII 문자 세트를 처리하여 데이터에서 가정으로) 그런 다음, 간단한 이해하여 "나쁜"값을 식별 손으로 해결할 수 있습니다

print [value for value in x if '\xe2' in x] 
+0

그 시도, 빈 목록을 반환 – user1452494

+0

@ user1452494 당신은 확실히 당신에게 원시 데이터를 제공해야합니다. 파일을 어딘가에 업로드 하시겠습니까? –

+0

편집을 참조하십시오. 오류가 발생하는 목록의 단어가 식별되었습니다. – user1452494

0

word.decode('utf-8') 사용 이 오류를 해결해야합니다.

import nltk 
porter = nltk.PorterStemmer() 
stemmed_x = [porter.stem(word.decode('utf-8')) for word in x] 
관련 문제