How can I integrate stanford parser software in my java program? [closed]

依然范特西╮ 提交于 2019-12-21 02:43:29

问题


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:

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??


回答1:


  • 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();               
        }
    }
    


来源:https://stackoverflow.com/questions/19429106/how-can-i-integrate-stanford-parser-software-in-my-java-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!