2017-12-14 12 views
0

This投稿は、Spacyのタグ付きConll形式のテキストブロックの依存関係を取得する方法を示しています。これは解決策が掲載されて:私はdoc.sentsを使用せずに同じ出力を取得したいと思いSpacyの文分割器を使用しないConll形式のSpacy

1 Bob bob NNP PERSON 2 nsubj 
2 bought buy VBD  0 ROOT 
3 the the DT  4 det 
4 pizza pizza NN  2 dobj 
5 to to IN  2 dative 
6 Alice alice NNP PERSON 5 pobj 

import spacy 
nlp_en = spacy.load('en') 
doc = nlp_en(u'Bob bought the pizza to Alice') 
for sent in doc.sents: 
     for i, word in enumerate(sent): 
       if word.head == word: 
       head_idx = 0 
       else: 
       head_idx = word.head.i - sent[0].i + 1 
       print("%d\t%s\t%s\t%s\t%s\t%s\t%s"%(
       i+1, # There's a word.i attr that's position in *doc* 
        word, 
        word.lemma_, 
        word.tag_, # Fine-grained tag 
        word.ent_type_, 
        str(head_idx), 
        word.dep_ # Relation 
       )) 

それは、このブロックを出力します。

実際、私は自分の文分割器を持っています。私はそれを使い、POS、NER、依存関係を得るために一度に1つの文をSpacyに与えたいと思っています。

Spacyの文分割器を使用せずに、Spacyを使用して1つの文のPOS、NER、および依存関係をConll形式で取得するにはどうすればよいですか?

答えて

0

This postは、予期せぬ文章に直面しているユーザーが、spacy sentence boundary detectionを使用していないことを示しています。 Spacyの開発者によって提案されたソリューションの1つ(ポスト同様)は、独自の文境界検出ルールを追加する柔軟性を追加することです。この問題は、Spacyによる依存関係の解析と組み合わせて解決されています。したがって、あなたが探しているものはSpacyによって今サポートされているとは思えませんが、近い将来には可能性があります。

0

sPacyDocumentが反復可能で、ドキュメントに、それは反復するToken

| __iter__(...) 
|  Iterate over `Token` objects, from which the annotations can be 
|  easily accessed. This is the main way of accessing `Token` objects, 
|  which are the main way annotations are accessed from Python. If faster- 
|  than-Python speeds are required, you can instead access the annotations 
|  as a numpy array, or access the underlying C data directly from Cython. 
|  
|  EXAMPLE: 
|   >>> for token in doc 

にわたりそのため、私はあなただけで分割され、あなたの文章のそれぞれについてDocumentをしなければならないと考えていると述べています

def printConll(split_sentence_text): 
    doc = nlp(split_sentence_text) 
    for i, word in enumerate(doc): 
      if word.head == word: 
      head_idx = 0 
      else: 
      head_idx = word.head.i - sent[0].i + 1 
      print("%d\t%s\t%s\t%s\t%s\t%s\t%s"%(
      i+1, # There's a word.i attr that's position in *doc* 
       word, 
       word.lemma_, 
       word.tag_, # Fine-grained tag 
       word.ent_type_, 
       str(head_idx), 
       word.dep_ # Relation 
      )) 

もちろん、CoNLL形式に従えば、各文の後に改行を印刷する必要があります。

関連する問題