How can I integrate stanford parser software in my

2019-03-21 15:23发布

问题:

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