The question is stored as a simple string:
String question = "Who is the 32nd president of the United States?";
We will use the LexicalizedParser class, as developed in the Finding word dependencies using the GrammaticalStructure class section. The relevant code is duplicated here for your convenience:
String parserModel = ".../englishPCFG.ser.gz"; LexicalizedParser lexicalizedParser = LexicalizedParser.loadModel(parserModel); TokenizerFactory<CoreLabel> tokenizerFactory = PTBTokenizer.factory(new CoreLabelTokenFactory(), ""); Tokenizer<CoreLabel> tokenizer = tokenizerFactory.getTokenizer(new StringReader(question)); List<CoreLabel> wordList = tokenizer.tokenize(); Tree parseTree = lexicalizedParser.apply(wordList); TreebankLanguagePack tlp = lexicalizedParser.treebankLanguagePack(); GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory(); GrammaticalStructure gs = gsf.newGrammaticalStructure(parseTree); List<TypedDependency> tdl = gs.typedDependenciesCCprocessed(); System.out.println(tdl); for (TypedDependency dependency : tdl) { System.out.println("Governor Word: [" + dependency.gov() + "] Relation: [" + dependency.reln().getLongName() + "] Dependent Word: [" + dependency.dep() + "]"); }
When executed with the question, we get the following output:
[root(ROOT-0, Who-1), cop(Who-1, is-2), det(president-5, the-3), amod(president-5, 32nd-4), nsubj(Who-1, president-5), det(States-9, the-7), nn(States-9, United-8), prep_of(president-5, States-9)] Governor Word: [ROOT] Relation: [root] Dependent Word: [Who/WP] Governor Word: [Who/WP] Relation: [copula] Dependent Word: [is/VBZ] Governor Word: [president/NN] Relation: [determiner] Dependent Word: [the/DT] Governor Word: [president/NN] Relation: [adjectival modifier] Dependent Word: [32nd/JJ] Governor Word: [Who/WP] Relation: [nominal subject] Dependent Word: [president/NN] Governor Word: [States/NNPS] Relation: [determiner] Dependent Word: [the/DT] Governor Word: [States/NNPS] Relation: [nn modifier] Dependent Word: [United/NNP] Governor Word: [president/NN] Relation: [prep_collapsed] Dependent Word: [States/NNPS]
This information provides the foundation to determine the type of question.