2016-09-16 5 views
0

nltkモジュールを使用してセンテンスの極性を検出するカスタムコーパスを作成しました。ここではコーパスの階層は次のとおりです。NLTKでカスタム作成コーパスを読んでいるときのUnicodeDecodeError

極性
--polar
---- polar_tweets.txt
--nonpolar
---- nonpolar_tweets.txt

そして、ここではどのようにあります私は、ソースコードでそのコーパスをインポートしています:

polarity = LazyCorpusLoader('polar', CategorizedPlaintextCorpusReader, r'(?!\.).*\.txt', cat_pattern=r'(polar|nonpolar)/.*', encoding='utf-8') 
corpus = polarity 
print(corpus.words(fileids=['nonpolar/non-polar.txt'])) 

が、それは次のエラー発生します

01ファイル polar_tweets.txtnonpolar_tweets.txtを作成している間
Traceback (most recent call last): 
    File "E:/Analytics Practice/Social Media Analytics/analyticsPlatform/DataAnalysis/SentimentAnalysis/data/training_testing_data.py", line 9, in <module> 
    print(corpus.words(fileids=['nonpolar/nonpolar_tweets.txt'])) 
    File "E:\Analytics Practice\Social Media Analytics\analyticsPlatform\lib\site-packages\nltk\util.py", line 765, in __repr__ 
    for elt in self: 
    File "E:\Analytics Practice\Social Media Analytics\analyticsPlatform\lib\site-packages\nltk\corpus\reader\util.py", line 291, in iterate_from 
    tokens = self.read_block(self._stream) 
    File "E:\Analytics Practice\Social Media Analytics\analyticsPlatform\lib\site-packages\nltk\corpus\reader\plaintext.py", line 122, in _read_word_block 
    words.extend(self._word_tokenizer.tokenize(stream.readline())) 
    File "E:\Analytics Practice\Social Media Analytics\analyticsPlatform\lib\site-packages\nltk\data.py", line 1135, in readline 
    new_chars = self._read(readsize) 
    File "E:\Analytics Practice\Social Media Analytics\analyticsPlatform\lib\site-packages\nltk\data.py", line 1367, in _read 
    chars, bytes_decoded = self._incr_decode(bytes) 
    File "E:\Analytics Practice\Social Media Analytics\analyticsPlatform\lib\site-packages\nltk\data.py", line 1398, in _incr_decode 
    return self.decode(bytes, 'strict') 
    File "C:\Users\prabhjot.rai\AppData\Local\Continuum\Anaconda3\lib\encodings\utf_8.py", line 16, in decode 
    return codecs.utf_8_decode(input, errors, True) 
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc2 in position 269: invalid continuation byte 

、私は、ファイルにutf-8からuncleaned_polar_tweets.txtをデコードして、ファイルpolar_tweets.txtにそれを書いています。

with open(path_to_file, "rb") as file: 
    output_corpus = clean_text(file.read().decode('utf-8'))['cleaned_corpus'] 

output_file = open(output_path, "w") 
output_file.write(output_corpus) 
output_file.close() 

output_fileのはpolar_tweets.txtnonpolar_tweets.txtである:ここではそのためのコードです。 どこにエラーがありますか?私はencoding='latin-1'encoding='utf-8'を交換した場合、私が最初にutf-8にエンコードし、行

polarity = LazyCorpusLoader('polar', CategorizedPlaintextCorpusReader, r'(?!\.).*\.txt', cat_pattern=r'(polar|nonpolar)/.*', encoding='utf-8') 

によってutf-8にも読んでいますので、コードは完璧に動作します。問題はどこですか?コーパスを作成中にもutf-8でデコードする必要がありますか?

+0

用語がオフです。読んでいるときは、*何かからデコードします。このエラーは、コーパス(またはその一部)が有効なUTF-8ではないことを示しています。問題のデータにアクセスすることができなければ、推測しかできません。 'LC_ALL = C grep -m 1 $ '\ xC2' nonpolar_tweets.txt'は何を生成しますか? (おそらくパイプを 'xxd'または同様のバイトに正確に見るために) – tripleee

+0

...または同等のPython - 違反行を読み、' repr() 'を調べてください。 – tripleee

答えて

1

Pythonのモデルでは、unicodeは一種のデータですが、utf-8エンコーディングであることを理解する必要があります。彼らはまったく同じものではありません。あなたは原文を読んでいます。これは明らかにutf-8です。それを掃除し、エンコーディングを指定せずに新しいコーパスに書き出します。あなたはそれを書いています...エンコーディングを知っている人。見つけてください、エンコーディングを指定してコーパスをもう一度きれいにして生成してください。

私はこのすべてをPython 3でやっていることを願っています。そうでない場合は、ここで停止してPython 3に切り替えてください。

output_file = open(output_path, "w", encoding="utf-8") 
output_file.write(output_corpus) 
output_file.close() 
+0

ありがとうございます:) –

関連する問題