2017-03-16 20 views
6

문장이 있습니다. John이 가게에서 화려한 모자를 보았습니다.
어떻게하면 아래와 같이 종속성 트리로 나타낼 수 있습니까? Spacy의 종속성 파싱 트리

(S 
     (NP (NNP John)) 
     (VP 
     (VBD saw) 
     (NP (DT a) (JJ flashy) (NN hat)) 
     (PP (IN at) (NP (DT the) (NN store))))) 

나는 here

import spacy 
from nltk import Tree 
en_nlp = spacy.load('en') 

doc = en_nlp("John saw a flashy hat at the store") 

def to_nltk_tree(node): 
    if node.n_lefts + node.n_rights > 0: 
     return Tree(node.orth_, [to_nltk_tree(child) for child in node.children]) 
    else: 
     return node.orth_ 


[to_nltk_tree(sent.root).pretty_print() for sent in doc.sents] 

나는 다음하지만 난 나무 (NLTK) 형식을 찾고 무엇입니까에서이 스크립트를 얻었다.

 saw     
    ____|_______________  
|  |   at 
|  |   | 
|  hat  store 
|  ___|____  | 
John a  flashy the 

답변

3

텍스트 표현을 제외하고, 당신이 달성하려고하는 것은 종속성 그래프 밖으로 선거구 트리를 얻는 것입니다. 원하는 출력 결과는 고전 문학 구조 트리입니다 (예 : 문법 구조 문법과 마찬가지로 의존성 문법).

constituency 트리에서 종속성 그래프로 변환하는 것이 자동화 된 작업 (예 : http://www.mathcs.emory.edu/~choi/doc/clear-dependency-2012.pdf)이지만 다른 방향은 아닙니다. PAD 프로젝트 https://github.com/ikekonglp/PAD과 기본 알고리즘을 설명하는 문서 인 http://homes.cs.washington.edu/~nasmith/papers/kong+rush+smith.naacl15.pdf을 확인하십시오.

당신은 또한 당신이 정말로 선거 구민 구문 분석을해야하는 경우 재고 할 수 있습니다

, 여기에 좋은 인수입니다 : https://linguistics.stackexchange.com/questions/7280/why-is-constituency-needed-since-dependency-gets-the-job-done-more-easily-and-e

3

, 적응 종속 구문 분석에 대한 NLTK 스타일의 나무를 다시 만들려면 nltk.tree에서 draw 방법을 사용하여 시도 대신 pretty_print의 : 적응은 현재 종속 구문 분석을 지원하며, 단어와 명사 구문 수준에서 태그 있기 때문에, 적응 나무가, 당신은에서 얻을하려는 것만 큼 깊게 구조화 수 없음을

import spacy 
from nltk.tree import Tree 

spacy_nlp = spacy.load("en") 

def nltk_spacy_tree(sent): 
    """ 
    Visualize the SpaCy dependency tree with nltk.tree 
    """ 
    doc = spacy_nlp(sent) 
    def token_format(token): 
     return "_".join([token.orth_, token.tag_, token.dep_]) 

    def to_nltk_tree(node): 
     if node.n_lefts + node.n_rights > 0: 
      return Tree(token_format(node), 
         [to_nltk_tree(child) 
         for child in node.children] 
        ) 
     else: 
      return token_format(node) 

    tree = [to_nltk_tree(sent.root) for sent in doc.sents] 
    # The first item in the list is the full tree 
    tree[0].draw() 

주 예를 들어, 스탠포드 파서는 우리는 모두를 실행하면 이제 nltk_spacy_tree("John saw a flashy hat at the store.")this image를 생성합니다

from nltk.tree import Tree 
from nltk.parse.stanford import StanfordParser 

# Note: Download Stanford jar dependencies first 
# See https://stackoverflow.com/questions/13883277/stanford-parser-and-nltk 
stanford_parser = StanfordParser(
    model_path="edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz" 
) 

def nltk_stanford_tree(sent): 
    """ 
    Visualize the Stanford dependency tree with nltk.tree 
    """ 
    parse = stanford_parser.raw_parse(sent) 
    tree = list(parse) 
    # The first item in the list is the full tree 
    tree[0].draw() 

nltk_stanford_tree("John saw a flashy hat at the store.")this one를 생성합니다 : 그래서 트리로 시각화.