2017-04-16 6 views
0

ファイルを読み込み、ファイルのテキストのステムトークンを格納するためにPorterStemmerを使用しようとしましたが、このエラーが発生しました。ポーターステマーとPythonでエンコーディングをする方法を手に入れよう

tokens=preprocessTokens(line) 
    File "/home/fl/git/KNN/preprocessDoc.py", line 20, in preprocessTokens 
line=line+' '+ps.stem(w) 
    File "/usr/local/lib/python2.7/dist-packages/nltk/stem/porter.py", line 664, in stem 
    stem = self._step1a(stem) 
    File "/usr/local/lib/python2.7/dist-packages/nltk/stem/porter.py", line 289, in _step1a 
    if word.endswith('ies') and len(word) == 4: 
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 0: ordinal not in range(128) 

私のコードにこれらの2行を追加し、それを解決してから

reload(sys) 
sys.setdefaultencoding('ISO-8859-15') 

を無視しかし、私はいくつかのファイルのために、次のエラーを得たために。それから、エンコーディングを 'utf-8'に変更しようとしましたが、同じエラーが発生しました。

tokens=preprocessTokens(line.encode('ascii',errors='ignore')) 
    File "/home/fl/git/KNN/preprocessDoc.py", line 20, in preprocessTokens 
    line=line+' '+ps.stem(w) 
    File "/usr/local/lib/python2.7/dist-packages/nltk/stem/porter.py", line 665, in stem 
    stem = self._step1b(stem) 
    File "/usr/local/lib/python2.7/dist-packages/nltk/stem/porter.py", line 376, in _step1b 
    lambda stem: (self._measure(stem) == 1 and 
    File "/usr/local/lib/python2.7/dist-packages/nltk/stem/porter.py", line 258, in _apply_rule_list 
    if suffix == '*d' and self._ends_double_consonant(word): 
    File "/usr/local/lib/python2.7/dist-packages/nltk/stem/porter.py", line 214, in _ends_double_consonant 
    word[-1] == word[-2] and 
IndexError: string index out of range 

答えて

0

エラーメッセージが文字で問題を抱えていることは回避策として、私は、入力文字列の文字文字ずつと、その文字とを取る、だから、127より大きい値127(すなわち:C & 127)を置きますその文字を文字列に戻します。言い換えれば、文字列を再構築し、すべての文字を127 asciiで変換してから、その入力文字列を処理します。 これは私の問題の解決策です。

関連する問題