문제

I am using the stanford parser in python by doing the following:

import os
sentence = "Did Matt win the men slalom?"
os.popen("echo '"+sentence+"' > ~/stanfordtemp.txt")
parser_out = os.popen("~/stanford-parser-2012-11-12/lexparser.sh  
  ~/stanfordtemp.txt").readlines()

for tree in parser_out:
    print tree

However, I dont know how I can access the leaves of the tree being returned by the parser.Can you help me with this? I also have to write a code which will be able to generate sql queries from english sentences. Any tips on this? Any help will be much appreciated. I am also using nltk for doing all the operations.

도움이 되었습니까?

해결책

Here's an example of building a tree and then recursively building a list of the leaves. The sample text is take from the online standford parser.

# class for tree nodes
class Node:
    def __init__(self,start):
        self.start = start
        self.children = []
        self.text = ''

# make a tree        
def make_tree(s):
    stack = []
    nodes = []
    cur = None
    root = None    

    for i, c in enumerate(s):
        if c == '(':
            cur = Node(i)
            if stack:
                stack[-1].children.append(cur)
            stack.append(cur)

            if root is None:
                root = cur

        elif c == ')' and stack:
            topnode = stack.pop()

            text = s[topnode.start + 1: i]
            topnode.text = text

    return root

# list of leaves
def list_of_leaves(node):
    result = []
    for child in node.children:
        result.extend(list_of_leaves(child))
    if not result:
        return [node]

    return result

s = """(ROOT
  (SQ (VBD Did)
    (NP (NNP Matt))
    (VP (VB win)
      (NP (DT the) (NNS men) (NN slalom)))
    (. ?)))"""

root = make_tree(s)    

for node in list_of_leaves(root):
    print node.text

다른 팁

How to extract individual clauses with a sentence as sub-trees?. So whenever a clause starts(S,SBAR, SBARQ etc), extract as a subtree until another clause is encountered. For the last clause its up to end of the sentence.

Here is an example:

(ROOT (S (S (NP (NNP John)) (VP (VBZ lives) (PP (IN in) (NP (NNP New) (NNP York) (NN city))))) (, ,) (CC but) (S (SBAR (WHADVP (WRB whenever)) (S (NP (PRP he)) (VP (VBZ travels) (S (VP (TO to) (VP (VB work))))))) (, ,) (NP (PRP he)) (VP (VBZ travels) (ADVP (RB very) (RB far)) (PP (TO to) (NP (PRP$ his) (NN work) (NN place))))) (. .)))

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top