Stanford Parser and NLTK

后端 未结 18 2375
既然无缘
既然无缘 2020-11-22 01:32

Is it possible to use Stanford Parser in NLTK? (I am not talking about Stanford POS.)

18条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 02:37

    You can use the Stanford Parsers output to create a Tree in nltk (nltk.tree.Tree).

    Assuming the stanford parser gives you a file in which there is exactly one parse tree for every sentence. Then this example works, though it might not look very pythonic:

    f = open(sys.argv[1]+".output"+".30"+".stp", "r")
    parse_trees_text=[]
    tree = ""
    for line in f:
      if line.isspace():
        parse_trees_text.append(tree)
    tree = ""
      elif "(. ...))" in line:
    #print "YES"
    tree = tree+')'
    parse_trees_text.append(tree)
    tree = ""
      else:
    tree = tree + line
    
    parse_trees=[]
    for t in parse_trees_text:
      tree = nltk.Tree(t)
      tree.__delitem__(len(tree)-1) #delete "(. .))" from tree (you don't need that)
      s = traverse(tree)
      parse_trees.append(tree)
    

提交回复
热议问题