The necessary steps include the following:
- 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;
- 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", ".");
- Next, add the following to parse the sentence and display the results:
Tree tree = lexicalizedParser.parseStrings(wordList);
tree.pennPrint();
- 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))))
(. .)))