我很難過。UnicodeDecodeError與在Python中詞幹在
我有一對夫婦的千言萬語
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]
而且我得到這個錯誤,這是明確的Ë詞幹包和Unicode莫名其妙:
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)
我已經嘗試了一切,用codecs.open
,試圖每個單詞進行明確編碼爲utf8
- 仍然會產生同樣的錯誤。
請指教。
編輯:
我應該提到,這個代碼在我運行Ubuntu的PC上工作完美。我最近有一個macbook pro,我得到這個錯誤。我檢查了我的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
您可能有一個多字節的UTF8字符。如果不是太長,您是否可以按照原樣從代碼中複製粘貼您的_full_數組定義? – 2014-08-29 10:01:08
你有沒有拉丁字符? – 2014-08-29 10:22:50
你在這裏混合了完全不同的字符集。如果可以的話,當您將數據拉入程序時,請將單詞屬於不同的語言(或更好的 - 不同的字符集)放在不同的列表中,因爲這會讓您的生活更輕鬆。然後,您可以將這些字符串的二進制文件解碼爲每個列表中適當的字符集。 – 2014-08-29 11:02:11