-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.java
More file actions
47 lines (39 loc) · 1.7 KB
/
Parser.java
File metadata and controls
47 lines (39 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package gui;
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);
int an=657583,dp=6877,ar=6568,at=6582,ka=7571;
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();
}
}