2017-03-16 39 views
6

mam zdanie John zobaczył jaskrawy kapelusz w sklepie
Jak do reprezentowania w postaci drzewa zależności jak poniżej?Zależność parsowania drzewa Spacy

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

Mam ten skrypt z 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] 

otrzymuję następujący ale szukam formacie drzewa (NLTK).

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

Odpowiedz

3

reprezentacje tekstowe na bok, co starasz się osiągnąć to, aby uzyskać okręgową drzewo z wykresu zależności. Przykładem pożądanego wyniku jest klasyczne drzewo elektoratu (jak w gramatyce struktury fraz, w przeciwieństwie do gramatyki zależności).

Podczas konwersji drzew z okręgów na wykresy zależności jest mniej więcej zautomatyzowanym zadaniem (np. http://www.mathcs.emory.edu/~choi/doc/clear-dependency-2012.pdf), a innym kierunkiem nie jest. Były prace nad tym, sprawdź projekt PAD https://github.com/ikekonglp/PAD i dokument opisujący algorytm: http://homes.cs.washington.edu/~nasmith/papers/kong+rush+smith.naacl15.pdf.

Można też ponownie rozważyć, czy naprawdę potrzebujemy parse okręgową, tutaj jest dobrym argumentem: https://linguistics.stackexchange.com/questions/7280/why-is-constituency-needed-since-dependency-gets-the-job-done-more-easily-and-e

3

Aby ponownie utworzyć drzewo NLTK stylu dla Spacy parsowań zależność, spróbuj zastosować metodę draw od nltk.tree zamiast 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() 

Należy pamiętać, że tylko dlatego, przestronne obsługuje obecnie zależność analizowania i tagowanie na słowo i poziomu rzeczownik-frazy, przestronne drzewa nie będzie tak głęboko strukturalny jako tych, które można uzyskać z, na przykład, parser Stanford, który można al więc wyobrazić jak drzewo:

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

Teraz, jeśli prowadzimy zarówno nltk_spacy_tree("John saw a flashy hat at the store.") będzie produkować this image i nltk_stanford_tree("John saw a flashy hat at the store.") będzie produkować this one.