2017-06-29 11 views
0

他の質問に対してすでに回答があり、さらにコードには常にエラーが返されます。コンパイル選択したファイルを書き込むときにUnicodeEncodeErrorが発生する

import os 
import nlpnet 
import codecs 

TAGGER = nlpnet.POSTagger('pos-pt', language='pt') 


# You could have a function that tagged and verified if a 
# sentence meets the criteria for storage. 

def is_worth_saving(text, pos, pos_count): 
    # tagged sentences are lists of tagged words, which in 
    # nlpnet are (word, pos) tuples. Tagged texts may contain 
    # several sentences. 
    pos_words = [word for sentence in TAGGER.tag(text) 
      for word in sentence 
      if word[1] == pos] 
    return len(pos_words) >= pos_count 



with codecs.open('dataset.txt', encoding='utf8') as original_file: 
with codecs.open('dataset_new.txt', 'w') as output_file: 
    for text in original_file: 
     # For example, only save sentences with more than 5 verbs in it 
     if is_worth_saving(text, 'V', 5): 
      output_file.write(text + os.linesep) 

エラー: このコードの唯一の目的は、単に、よりお好みの特定のPOSのNの発生よりも含まれている文章ファイルへの文書の文章にタグを入れて、ダンプすることです。

Traceback (most recent call last): 
    File "D:/Word Sorter/Classifier.py", line 31, in <module> 
    output_file.write(text + os.linesep) 
UnicodeEncodeError: 'ascii' codec can't encode characters in position 161-162: ordinal not in range(128) 

答えて

関連する問題