Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

54
LINES

< > BotCompany Repo | #1008487 // Stanford Parser Test [WORKS]

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Uses 6125K of libraries. Click here for Pure Java version (438L/4K/13K).

1  
!7
2  
3  
lib 1008485 // stanford parser
4  
lib 1008486 // english model
5  
6  
import edu.stanford.nlp.ling.CoreLabel;
7  
import edu.stanford.nlp.process.TokenizerFactory;
8  
import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
9  
import edu.stanford.nlp.process.CoreLabelTokenFactory;
10  
import edu.stanford.nlp.process.PTBTokenizer;
11  
import edu.stanford.nlp.process.Tokenizer;
12  
import edu.stanford.nlp.trees.Tree;
13  
14  
sS PCG_MODEL = "edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz";
15  
static TokenizerFactory<CoreLabel> tokenizerFactory;
16  
static LexicalizedParser parser;
17  
18  
static Tree parse(S str) {
19  
  List<CoreLabel> tokens = tokenize(str);
20  
  ret parser.apply(tokens);
21  
}
22  
23  
static L<CoreLabel> tokenize(String str) {
24  
  Tokenizer<CoreLabel> tokenizer = tokenizerFactory.getTokenizer(
25  
    new StringReader(str));
26  
  ret tokenizer.tokenize();
27  
}
28  
29  
p {
30  
  tokenizerFactory = PTBTokenizer.factory(new CoreLabelTokenFactory(), "invertible=true");
31  
  parser = LexicalizedParser.loadModel(PCG_MODEL);
32  
  S str = "My dog also likes eating sausage.";
33  
  Tree tree = parse(str);
34  
35  
  L<Tree> leaves = tree.getLeaves();
36  
  // Print words and Pos Tags
37  
  for (Tree leaf : leaves) {
38  
    Tree parent = leaf.parent(tree);
39  
    print("  " + leaf.label().value() + " [" + parent.label().value() + "]");
40  
  }
41  
  print();
42  
  printTree(tree);
43  
}
44  
45  
svoid printTree(Tree tree) {
46  
  printTree("", tree);
47  
}
48  
49  
svoid printTree(S indent, Tree tree) {
50  
  //if (l(indent) > 4) ret;
51  
  print(indent + tree.label().value());
52  
  for (Tree child : tree.getChildrenAsList())
53  
    printTree(indent + "  ", child);
54  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1008487
Snippet name: Stanford Parser Test [WORKS]
Eternal ID of this version: #1008487/7
Text MD5: 165c55ac305a3dfc53f2ee1a476f5fa3
Transpilation MD5: 260168e69f45a65543a4225fdc150f6d
Author: stefan
Category: javax / a.i. parsing
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-05-17 17:30:56
Source code size: 1589 bytes / 54 lines
Pitched / IR pitched: No / No
Views / Downloads: 389 / 1301
Version history: 6 change(s)
Referenced in: [show references]