How to do it...

The necessary steps include the following:

  1. Add these import statements to your project:
import java.util.Arrays;
import java.util.List;
import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.TreePrint;
  1. Add the following to the main method of your project to load a model and declare a sentence:
LexicalizedParser lexicalizedParser = 
LexicalizedParser.loadModel("englishPCFG.ser.gz");
List<String> wordList = Arrays.asList("The", "old man", "sat",
"down", "beside", "the", "tree", ".");

  1. Next, add the following to parse the sentence and display the results:
Tree tree = lexicalizedParser.parseStrings(wordList);
tree.pennPrint();
  1. Execute the program. You will get the following output:
(ROOT
(S
(NP (DT The) (NN old man))
(VP (VBD sat)
(PRT (RP down))
(PP (IN beside)
(NP (DT the) (NN tree))))
(. .)))
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.98.177