2017-12-11 18 views
0

私は私のコーパスで文章チャンクを実行しようとしています。最初に私のタグ付きデータをロードして、そのタグ付きコーパスでチャンクを実行しようとしていました。TypeError:シーケンスアイテム352:予期されるstrインスタンス、NoneTypeが見つかりました

def load_corpus(): 
    corpus_root = os.path.abspath('../nlp1/dumpfiles') 
    mycorpus = nltk.corpus.reader.TaggedCorpusReader(corpus_root,'.*') 
    return mycorpus.tagged_sents() 

def sents_chunks(tagg_sents, pos_tag_pattern): 
    chunk_freq_dict = defaultdict(int) 
    chunker = nltk.RegexpParser(pos_tag_pattern) 
    for sent in tagg_sents: 
     if not all(sent): 
      print("NoneType object in \"{}\": {}".format(sent.label(),sent)) 
      sent = cast_to_tree_function(filter(bool, sent)) 
     for chk in chunker.parse(sent).subtrees(): 
      if str(chk).startswith('(NP'): 
       phrase = chk.__unicode__()[4:-1] 
       #print(phrase) 
       if '\n' in phrase: 
        phrase = ' '.join(phrase.split()) 
        #print(phrase) 
       chunk_freq_dict[phrase] += 1 
    #print(chunk_freq_dict) 
    return chunk_freq_dict 

どこで、なぜ私は誰もが問題とどのように私はそれを解決することができているかを知るknow.doesいけない私は、どこかで私のコーパスでのエラーを取得していますか?これはエラーです:

Traceback (most recent call last): 
    File "multiwords1.py", line 184, in <module> 
    candidates = main(domain_corpus, PATTERN,MIN_FREQ,MIN_CVAL) 
    File "multiwords1.py", line 156, in main 
    chunks_freqs = sents_chunks(domain_sents, pos_tag_pattern) 
    File "multiwords1.py", line 23, in sents_chunks 
    for chk in chunker.parse(sent).subtrees(): 
    File "/usr/local/lib/python3.5/dist-packages/nltk/chunk/regexp.py", line 1208, in parse 
    chunk_struct = parser.parse(chunk_struct, trace=trace) 
    File "/usr/local/lib/python3.5/dist-packages/nltk/chunk/regexp.py", line 1023, in parse 
    chunkstr = ChunkString(chunk_struct) 
    File "/usr/local/lib/python3.5/dist-packages/nltk/chunk/regexp.py", line 98, in __init__ 
    self._str = '<' + '><'.join(tags) + '>' 
TypeError: sequence item 352: expected str instance, NoneType found 

答えて

0

あなたはTypeError実行を持っています。メッセージアイテム352がタグ(NoneType)でないタグから呼び出された場合、sent(ntlk.tree.Tree class)にNoneTypeオブジェクトが順番に存在することを意味します。

This line is the reason for the exceptionstr.joinは、strとなるためです。あなたはチェックが必要sentiterableの各項目をstr typeの所属にチェックする必要があります。

これにはfilter組み込み関数を使用できますが、結果はTree typeにキャストする必要があります。

filter(bool, sent) # return a iterator with valid items 

反復可能なオブジェクトは、あなたがこれを行うことができますNoneType項目があり、チェック用:

if not all(sent): 
    print("NoneType object in \"{}\": {}".format(sent.label(), sent)) 
    sent = cast_to_tree_function(filter(bool, sent)) # update set object to valid items 
+1

は私がどこのフィルタを使用してもそこにするかわからないreplying.butをありがとうございました私のコーパスに500ファイルありますが、どのようにエラーがどこにあるのですか?どのファイルにこのエラーがありますか? – user3778289

+0

[q](https://stackoverflow.com/questions/47758858/typeerror-sequence-item-352-expected-str-instance-nonetype-found?answertab=active#comment82480654_47759650)|> ビルトインを使用できますfunction [all](https://docs.python.org/3/library/functions.html#all)をクリックします。私のバージョンのソリューションが投稿に追加されます。 –

+0

私はこれを行いましたが、tagg_sentsで送信された場合でも同じエラーが発生しました: すべてではない場合は、 print(送信) print( "NoneTypeオブジェクトは\" {} \ ":{} chunker.parse(送信).subtrees(): – user3778289

関連する問題