【问题标题】:How to parse Penn Tree Bank and get all the child trees using stanford NLP?如何使用 stanford NLP 解析 Penn Tree Bank 并获取所有子树?
【发布时间】:2016-05-26 00:32:52
【问题描述】:

有没有办法解析下面的PTB树得到所有的子树 例如:

Text   :  Today is a nice day.
PTB : (3 (2 Today) (3 (3 (2 is) (3 (2 a) (3 (3 nice) (2 day)))) (2 .)))

需要所有可能的子树

Output  : 
(3 (2 Today) (3 (3 (2 is) (3 (2 a) (3 (3 nice) (2 day)))) (2 .)))
(2 Today)
(3 (3 (2 is) (3 (2 a) (3 (3 nice) (2 day)))) (2 .))
(3 (2 is) (3 (2 a) (3 (3 nice) (2 day))))
(3 (2 is) (3 (2 a) (3 (3 nice) (2 day))))
(2 is)
(3 (2 a) (3 (3 nice) (2 day)))
(2 a)
(3 (3 nice) (2 day))
(3 nice)
(2 day)
(2 .)

【问题讨论】:

    标签: java parsing nlp stanford-nlp


    【解决方案1】:

    此演示的输入文件应该是每行一棵树的一个字符串表示形式。此示例打印出第一棵树的子树。

    Stanford CoreNLP 感兴趣的课程是 Tree。

    import edu.stanford.nlp.trees.*;
    
    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.io.*;
    
    public class TreeLoadExample {
    
        public static void printSubTrees(Tree t) {
            if (t.isLeaf())
                return;
            System.out.println(t);
            for (Tree subTree : t.children()) {
                printSubTrees(subTree);
            }
        }
    
    
        public static void main(String[] args) throws IOException, FileNotFoundException,
                UnsupportedEncodingException {
            TreeFactory tf = new LabeledScoredTreeFactory();
            Reader r = new BufferedReader(new InputStreamReader(new FileInputStream(args[0]), "UTF-8"));
            TreeReader tr = new PennTreeReader(r, tf);
            Tree t = tr.readTree();
            printSubTrees(t);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多