List of usage examples for edu.stanford.nlp.trees LabeledScoredTreeNode LabeledScoredTreeNode
public LabeledScoredTreeNode()
From source file:elkfed.expletives.EF_Tree.java
License:Apache License
public static Tree tagged_word(String word, String tag) { LabelFactory lf = new StringLabelFactory(); Tree result = new LabeledScoredTreeNode(); result.setLabel(lf.newLabel(tag));/* ww w .j ava 2 s . co m*/ Tree[] dtrs = new Tree[1]; dtrs[0] = new LabeledScoredTreeNode(lf.newLabel(word)); result.setChildren(dtrs); return result; }
From source file:elkfed.expletives.EF_Tree.java
License:Apache License
public static Tree tree_pred(Tree node) { LabelFactory lf = new StringLabelFactory(); Tree result = new LabeledScoredTreeNode(); result.setLabel(lf.newLabel(node.value() + "-PRD")); if (node.value().equals("PP") && node.children().length == 2 && node.children()[0].value().equals("IN")) { Tree[] dtrs = new Tree[2]; dtrs[0] = node.children()[0];//from w w w .j a va 2 s.co m dtrs[1] = node.children()[1].headPreTerminal(new ModCollinsHeadFinder()); result.setChildren(dtrs); } Tree[] dtrs = new Tree[1]; dtrs[0] = node.headPreTerminal(new ModCollinsHeadFinder()); result.setChildren(dtrs); return result; }
From source file:elkfed.expletives.EF_Tree.java
License:Apache License
/** constructs a marked subtree for a subclause * outside the path to the pronoun//from w ww . j av a 2 s .c o m * @param node the starting point * @return a marked subtree for the tree starting with node */ public static Tree tree_pruned(Tree node) { LabelFactory lf = new StringLabelFactory(); Tree result = new LabeledScoredTreeNode(); result.setLabel(lf.newLabel(node.value() + "-X")); List<Tree> dtrs = new ArrayList<Tree>(); boolean cpl_seen = false; if (node.value().matches("S|SBAR|VP")) { for (Tree t : node.children()) { // modals are copied verbatim String cat = t.value(); if (cat.matches("TO|MD|IN")) { dtrs.add(t); cpl_seen = true; } else if (cat.startsWith("WH")) { Tree dtr = tagged_word(cat, "WH"); cpl_seen = true; } else if (t.value().startsWith("VB")) { break; } else if (t.value().matches("S|SBAR|VP")) { if (cpl_seen) { //ignore } else { dtrs.add(tree_pruned(t)); } } } } result.setChildren(dtrs); return result; }
From source file:elkfed.expletives.EF_Tree.java
License:Apache License
/** constructs a marked subtree for parts which are * outside the path to the pronoun/* w ww. j a v a 2 s. com*/ * @param node the starting point * @return a marked subtree for the tree starting with node */ public static Tree tree_outside(Tree node) { LabelFactory lf = new StringLabelFactory(); // verbs and modals are copied verbatim if (node.value().matches("VB[DZPNG]?")) { return tagged_word(Morphology.stemStatic(node.children()[0].value(), node.value()).value(), "VBX"); //return node; } else if (node.value().matches("TO|MD|IN|RB")) { return node; } Tree result = new LabeledScoredTreeNode(); result.setLabel(lf.newLabel(node.value())); if (node.value().matches("VP")) { List<Tree> dtrs = new ArrayList<Tree>(); dtrs_inside(node, dtrs); result.setChildren(dtrs); } else { List<Tree> dtrs = null; result.setChildren(dtrs); } return result; }
From source file:elkfed.expletives.EF_Tree.java
License:Apache License
/** * constructs a marked subtree for the part where the * pronoun is <i>inside</i> the subtree * @param node the starting point/* ww w . j a v a 2 s .c o m*/ * @param pron our pronoun * @return a marked subtree for the tree starting with node */ public static Tree tree_inside(Tree node, Tree pron) { LabelFactory lf = new StringLabelFactory(); int pron_left = pron.leftCharEdge(node); int pron_right = pron.rightCharEdge(node); List<Tree> dtrs = new ArrayList<Tree>(node.children().length); boolean node_seen = false; for (Tree t : node.children()) { if (t == pron) { dtrs.add(t); node_seen = true; } else if (t.dominates(pron)) { dtrs.add(tree_inside(t, pron)); node_seen = true; } else { String cat = t.value(); if (cat.matches("S|SBAR")) { dtrs.add(tree_pruned(t)); } else { dtrs.add(tree_outside(t)); } } } Tree result = new LabeledScoredTreeNode(); result.setLabel(lf.newLabel(node.value() + "-I")); result.setChildren(dtrs); return result; }
From source file:elkfed.expletives.EF_Tree.java
License:Apache License
public static Tree tree_markonly(Tree node, Tree pron) { LabelFactory lf = new StringLabelFactory(); List<Tree> dtrs = new ArrayList<Tree>(node.children().length); for (Tree t : node.children()) { if (t == pron) { dtrs.add(t);/* w ww . j av a 2 s .co m*/ } else if (t.dominates(pron)) { dtrs.add(tree_markonly(t, pron)); } else { dtrs.add(t); } } Tree result = new LabeledScoredTreeNode(); result.setLabel(lf.newLabel(node.value() + "-I")); result.setChildren(dtrs); return result; }
From source file:elkfed.mmax.pipeline.Parser.java
License:Apache License
/** Add parser, part of speech, and chunk markables */ protected void addMarkables() { final StringBuffer markableBuffer = new StringBuffer(); List<Markable> sentences = null; try {/*from ww w . j av a 2s . c o m*/ sentences = DiscourseUtils.getSentences(currentDocument); } catch (Exception mmax2e) { mmax2e.printStackTrace(); } for (int sentence = 0; sentence < sentences.size(); sentence++) { /** Add the parse tree markables */ final Map<String, String> attributes = new HashMap<String, String>(levelAttributes); attributes.put(TAG_ATTRIBUTE, forest.get(sentence).replaceAll("&", "&")); markableBuffer.setLength(0); Markable sent_m = sentences.get(sentence); int start = sent_m.getLeftmostDiscoursePosition(); int end = sent_m.getRightmostDiscoursePosition(); currentLevel.addMarkable(start, end, attributes); /** Retrieve chunk tags from the parse tree and add chunk markables */ boolean inNP = false; int startNP = -1; int wordLoc = 0; int depth = 0; for (String tok : forest.get(sentence).replaceAll("\\)", ") ").split("\\s+")) { if (tok.matches("\\(NP")) { inNP = true; startNP = wordLoc; depth = 0; } if ((inNP) && (tok.matches(".*\\)"))) { depth--; } if ((inNP) && (tok.matches("\\(.*"))) { depth++; } if (tok.matches(".+\\)")) { wordLoc++; } if ((depth == 0) && (inNP)) { inNP = false; final Map<String, String> cAttributes = new HashMap<String, String>(chunkAttributes); markableBuffer.setLength(0); cAttributes.put(TAG_ATTRIBUTE, "np"); //TODO: check if it's not start+wordLoc-1 ? chunkLevel.addMarkable(start + startNP, start + wordLoc - 1, cAttributes); } } /** Create a tree object from the current sentence */ Tree currentTree = new LabeledScoredTreeNode(); // System.err.println("processing sentence: "+forest.get(sentence)); currentTree = (LabeledScoredTreeNode) Tree.valueOf(forest.get(sentence)); /** Retrieve POS tags from the parse tree */ List<Label> taggedSent = new ArrayList<Label>(currentTree.preTerminalYield()); for (int i = 0; i < taggedSent.size(); i++) { posTags.add(taggedSent.get(i).value()); } } /** Add POS tag markables */ for (int pos = 0; pos < posTags.size(); pos++) { final HashMap<String, String> attributes = new HashMap<String, String>(posAttributes); attributes.put(TAG_ATTRIBUTE, posTags.get(pos).toLowerCase()); posLevel.addMarkable(pos, pos, attributes); } }
From source file:nlpedit.ui.NLPTree.java
License:Open Source License
public Tree getTree() { Tree ntree = new LabeledScoredTreeNode(); try {/* w w w . j a va 2s .com*/ ntree = (new PennTreeReader(new StringReader(getTreeString()), new LabeledScoredTreeFactory(new StringLabelFactory()))).readTree(); } catch (IOException e) { e.printStackTrace(); } return ntree; }