如何在我的java程序中集成stanford解析器软件? [英] How can I integrate stanford parser software in my java program?

查看:228
本文介绍了如何在我的java程序中集成stanford解析器软件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在java中开发一个项目,该项目使用斯坦福解析器来分隔句子,并且必须生成一个图表来显示句子中单词之间的关系。以
为例:俄亥俄州位于美国。
输出:

I have to develop a project in java that uses a Stanford parser to separate the sentences and has to generate a graph that shows the relation between the words in a sentence. for example: Ohio is located in America. Output:

图像显示图形。但输出不必相同,但它必须显示图形形式的单词之间的关系。可以使用Jgraph,Jung生成图表。但最初我必须将解析器软件集成到我的程序中。那么如何整合解析器?

the image shows the graph. But the output need not be the same but it has to show relation between the words in graph form. The graph can be generated using Jgraph, Jung. But initially I have to integrate the parser software into my program. So how can I integrate a parser??

推荐答案


  • 下载Stanford Parser zip

  • 添加 jars 到项目的构建路径(包括模型文件)

  • 使用以下代码段解析句子并返回选区树: (可以通过检查树的结构来构建依赖树)

    • Download the Stanford Parser zip:
    • Add jars to the build path of your project (include the model files)
    • Use the following snippet to parse sentences and return the constituency trees:(dependency trees can be built by inspecting the structure of the tree)

      import java.io.StringReader;
      import java.util.List;
      
      import edu.stanford.nlp.ling.CoreLabel;
      import edu.stanford.nlp.process.TokenizerFactory;
      import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
      import edu.stanford.nlp.process.CoreLabelTokenFactory;
      import edu.stanford.nlp.process.PTBTokenizer;
      import edu.stanford.nlp.process.Tokenizer;
      import edu.stanford.nlp.trees.Tree;
      
      class Parser {
      
          private final static String PCG_MODEL = "edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz";        
      
          private final TokenizerFactory<CoreLabel> tokenizerFactory = PTBTokenizer.factory(new CoreLabelTokenFactory(), "invertible=true");
      
          private final LexicalizedParser parser = LexicalizedParser.loadModel(PCG_MODEL);
      
          public Tree parse(String str) {                
              List<CoreLabel> tokens = tokenize(str);
              Tree tree = parser.apply(tokens);
              return tree;
          }
      
          private List<CoreLabel> tokenize(String str) {
              Tokenizer<CoreLabel> tokenizer =
                  tokenizerFactory.getTokenizer(
                      new StringReader(str));    
              return tokenizer.tokenize();
          }
      
          public static void main(String[] args) { 
              String str = "My dog also likes eating sausage.";
              Parser parser = new Parser(); 
              Tree tree = parser.parse(str);  
      
              List<Tree> leaves = tree.getLeaves();
              // Print words and Pos Tags
              for (Tree leaf : leaves) { 
                  Tree parent = leaf.parent(tree);
                  System.out.print(leaf.label().value() + "-" + parent.label().value() + " ");
              }
              System.out.println();               
          }
      }
      


    • 这篇关于如何在我的java程序中集成stanford解析器软件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆