2017-12-14 24 views
0

This 게시물은 Spacy의 태그 작성자로 Conll 형식의 텍스트 블록의 종속성을 얻는 방법을 보여줍니다. 이 솔루션을 게시입니다 : 내가 doc.sents를 사용하지 않고 동일한 출력을 좀하고 싶습니다Spacy의 문장 분리자를 사용하지 않고 Spacy에서 Conll 형식으로

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 
       )) 

그것은이 블록을 출력합니다.

사실, 저는 제 자신의 문장 분리기가 있습니다. 나는 이것을 사용하고 싶습니다. 그리고 Spacy에게 한 번에 한 문장 씩주고 POS, NER, 의존성을 얻고 싶습니다.

POS, NER 및 Spacy의 문장 분리기를 사용하지 않고 Spacy로 Conll 형식의 한 문장의 종속성을 어떻게 얻을 수 있습니까?

답변

0

This post는 spacy sentence boundary detection을 사용하여 예기치 않은 문장이 나오는 사용자에 관한 것입니다. Spacy의 개발자가 제안한 솔루션 중 하나는 자신의 문장 경계 검색 규칙을 추가 할 수있는 유연성을 추가하는 것입니다. 이 문제는 Spacy가 아닌 종속성 분석과 함께 해결됩니다. 그러므로 나는 당신이 찾고있는 것이 Spacy에 의해 현재 지원되고 있다고 생각하지는 않습니다 만, 가까운 장래에있을 것입니다. DocumentsPacy에서

0

이 반복 가능하고, 문서에 이상이 그것을 반복 상태 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 형식에 따라 각 문장 다음에 줄 바꿈을 인쇄해야합니다.