本文主要研究下如何使用opennlp進行依存句法分析html
opennlp主要使用Parser來進行依存句法分析,其模型爲ParserModelapache
@Test public void testParserTool() throws IOException { try (InputStream modelInputStream = this.getClass().getClassLoader().getResourceAsStream("chunker/en-parser-chunking.bin")) { ParserModel model = new ParserModel(modelInputStream); Parser parser = ParserFactory.create(model); String sentence = "The cow jumped over the moon"; // Used to demonstrate difference between NER and Parser // sentence = "He was the last person to see Fred."; Parse parses[] = ParserTool.parseLine(sentence, parser, 3); for (Parse parse : parses) { parse.show(); } } catch (IOException ex) { ex.printStackTrace(); } }
這裏使用en-parser-chunking.bin這個訓練好的模型來進行分析
第一句輸出以下this
(TOP (PP (S (NP (DT The) (NN cow)) (PP (VP (VBD jumped) (PRT (RP over))))) (NP (DT the) (NN moon)))) (TOP (NP (NP (DT The) (NN cow)) (PP (S (VP (VBN jumped) (PP (IN over) (NP (DT the) (NN moon)))))))) (TOP (NP (NP (DT The) (NN cow)) (SBAR (S (VP (VBN jumped) (PP (IN over) (NP (DT the) (NN moon))))))))
第二句輸出以下code
(TOP (FRAG (FRAG (S (NP (PRP He)) (VP (VBD was) (NP (NP (DT the) (JJ last) (NN person)) (SBAR (S (VP (TO to) (VP (VB see))))))))) (: Fred.))) (TOP (S (S (NP (PRP He)) (VP (VBD was) (NP (NP (DT the) (JJ last) (NN person)) (PP (VP (TO to) (VP (VB see))))))) (: Fred.))) (TOP (S (FRAG (S (NP (PRP He)) (VP (VBD was) (NP (NP (DT the) (JJ last) (NN person)) (SBAR (S (VP (TO to) (VP (VB see))))))))) (: Fred.)))
opennlp也支持依存句法分析,不過根節點的表示,stanford nlp使用的是ROOT,而opennlp使用的是TOP。htm