/** * Set the tags of the original tokens and the leaves if they * aren't already set. */ private static void setMissingTags(CoreMap sentence, Tree tree) { List<TaggedWord> taggedWords = null; List<Label> leaves = null; List<CoreLabel> tokens = sentence.get(CoreAnnotations.TokensAnnotation.class); for (int i = 0, size = tokens.size(); i < size; ++i) { CoreLabel token = tokens.get(i); if (token.tag() == null) { if (taggedWords == null) { taggedWords = tree.taggedYield(); } if (leaves == null) { leaves = tree.yield(); } token.setTag(taggedWords.get(i).tag()); Label leaf = leaves.get(i); if (leaf instanceof HasTag) { ((HasTag) leaf).setTag(taggedWords.get(i).tag()); } } } }
((HasTag) token).setTag(toks[1]);
String posStr = normalizePreterminal(t); t.setValue(posStr); if(t.label() instanceof HasTag) ((HasTag) t.label()).setTag(posStr);
/** * Returns the input sentence for the parser. */ private List<CoreLabel> getInputSentence(Tree t) { if (op.testOptions.forceTags) { if (op.testOptions.preTag) { List<TaggedWord> s = tagger.apply(t.yieldWords()); if(op.testOptions.verbose) { log.info("Guess tags: "+Arrays.toString(s.toArray())); log.info("Gold tags: "+t.labeledYield().toString()); } return SentenceUtils.toCoreLabelList(s); } else if(op.testOptions.noFunctionalForcing) { ArrayList<? extends HasWord> s = t.taggedYield(); for (HasWord word : s) { String tag = ((HasTag) word).tag(); tag = tag.split("-")[0]; ((HasTag) word).setTag(tag); } return SentenceUtils.toCoreLabelList(s); } else { return SentenceUtils.toCoreLabelList(t.taggedYield()); } } else { return SentenceUtils.toCoreLabelList(t.yieldWords()); } }
ht.setTag(currentTree.label().value());
@Override public Tree transformTree(Tree t, Tree root) { String baseCat = t.value(); StringBuilder newCategory = new StringBuilder(); //Add manual state splits for (Pair<TregexPattern,Function<TregexMatcher,String>> e : activeAnnotations) { TregexMatcher m = e.first().matcher(root); if (m.matchesAt(t)) newCategory.append(e.second().apply(m)); } // WSGDEBUG //Add morphosyntactic features if this is a POS tag if(t.isPreTerminal() && tagSpec != null) { if( !(t.firstChild().label() instanceof CoreLabel) || ((CoreLabel) t.firstChild().label()).originalText() == null ) throw new RuntimeException(String.format("%s: Term lacks morpho analysis: %s",this.getClass().getName(),t.toString())); String morphoStr = ((CoreLabel) t.firstChild().label()).originalText(); MorphoFeatures feats = tagSpec.strToFeatures(morphoStr); baseCat = feats.getTag(baseCat); } //Update the label(s) String newCat = baseCat + newCategory.toString(); t.setValue(newCat); if (t.isPreTerminal() && t.label() instanceof HasTag) ((HasTag) t.label()).setTag(newCat); return t; }
/** * This method does language-specific tree transformations such as annotating particular nodes with language-relevant * features. Such parameterizations should be inside the specific TreebankLangParserParams class. This method is * recursively applied to each node in the tree (depth first, left-to-right), so you shouldn't write this method to * apply recursively to tree members. This method is allowed to (and in some cases does) destructively change the * input tree <code>t</code>. It changes both labels and the tree shape. * * @param t The input tree (with non-language specific annotation already done, so you need to strip back to basic * categories) * @param root The root of the current tree (can be null for words) * @return The fully annotated tree node (with daughters still as you want them in the final result) */ @Override public Tree transformTree(Tree t, Tree root) { String newCat = t.value() + getAnnotationString(t, root); t.setValue(newCat); if (t.isPreTerminal() && t.label() instanceof HasTag) ((HasTag) t.label()).setTag(newCat); return t; }
((HasTag) tagNode.label()).setTag(tagNode.label().value());
/** * Build a parse tree node corresponding to the word in the given XML node. */ private Tree buildWordNode(Node root) { Element eRoot = (Element) root; String posStr = getPOS(eRoot); posStr = treeNormalizer.normalizeNonterminal(posStr); String lemma = eRoot.getAttribute(ATTR_LEMMA); String word = getWord(eRoot); String leafStr = treeNormalizer.normalizeTerminal(word); Tree leafNode = treeFactory.newLeaf(leafStr); if (leafNode.label() instanceof HasWord) ((HasWord) leafNode.label()).setWord(leafStr); if (leafNode.label() instanceof HasLemma && lemma != null) ((HasLemma) leafNode.label()).setLemma(lemma); List<Tree> kids = new ArrayList<>(); kids.add(leafNode); Tree t = treeFactory.newTreeNode(posStr, kids); if (t.label() instanceof HasTag) ((HasTag) t.label()).setTag(posStr); return t; }
log.warn(String.format("%s: missing tag for %s",this.getClass().getName(),t.pennString())); } else if(t.label() instanceof HasTag) { ((HasTag) t.label()).setTag(t.value());
@Override public Tree transformTree(Tree t, Tree root) { // Perform tregex-powered annotations t = super.transformTree(t, root); String cat = t.value(); //Add morphosyntactic features if this is a POS tag if(t.isPreTerminal() && tagSpec != null) { if( !(t.firstChild().label() instanceof CoreLabel) || ((CoreLabel) t.firstChild().label()).originalText() == null ) throw new RuntimeException(String.format("%s: Term lacks morpho analysis: %s",this.getClass().getName(),t.toString())); String morphoStr = ((CoreLabel) t.firstChild().label()).originalText(); Pair<String,String> lemmaMorph = MorphoFeatureSpecification.splitMorphString("", morphoStr); MorphoFeatures feats = tagSpec.strToFeatures(lemmaMorph.second()); cat = feats.getTag(cat); } //Update the label(s) t.setValue(cat); if (t.isPreTerminal() && t.label() instanceof HasTag) ((HasTag) t.label()).setTag(cat); return t; }
node.setScore(tree.score()); if(node.label() instanceof HasTag) ((HasTag) node.label()).setTag(s);
/** * Given a tree, set the tags on the leaf nodes if they are not * already set. Do this by using the preterminal's value as a tag. */ public static void setLeafTagsIfUnset(Tree tree) { if (tree.isPreTerminal()) { Tree leaf = tree.children()[0]; if (!(leaf.label() instanceof HasTag)) { return; } HasTag label = (HasTag) leaf.label(); if (label.tag() == null) { label.setTag(tree.value()); } } else { for (Tree child : tree.children()) { setLeafTagsIfUnset(child); } } }
/** * Convert a constituency label to a dependency label. Options are provided for selecting annotations * to copy. * * @param oldLabel * @param copyLabel * @param copyIndex * @param copyPosTag */ private static Label makeDependencyLabel(Label oldLabel, boolean copyLabel, boolean copyIndex, boolean copyPosTag) { if ( ! copyLabel) return oldLabel; String wordForm = (oldLabel instanceof HasWord) ? ((HasWord) oldLabel).word() : oldLabel.value(); Label newLabel = oldLabel.labelFactory().newLabel(wordForm); if (newLabel instanceof HasWord) ((HasWord) newLabel).setWord(wordForm); if (copyPosTag && newLabel instanceof HasTag && oldLabel instanceof HasTag) { String tag = ((HasTag) oldLabel).tag(); ((HasTag) newLabel).setTag(tag); } if (copyIndex && newLabel instanceof HasIndex && oldLabel instanceof HasIndex) { int index = ((HasIndex) oldLabel).index(); ((HasIndex) newLabel).setIndex(index); } return newLabel; }
((HasTag) tagNode.label()).setTag(tagNode.label().value());
((HasTag) newNode.label()).setTag(MW_TAG);
((HasTag) nodeLabel).setTag(headTag);
/** * This method does language-specific tree transformations such as annotating particular nodes with language-relevant * features. Such parameterizations should be inside the specific TreebankLangParserParams class. This method is * recursively applied to each node in the tree (depth first, left-to-right), so you shouldn't write this method to * apply recursively to tree members. This method is allowed to (and in some cases does) destructively change the * input tree <code>t</code>. It changes both labels and the tree shape. * * @param t The input tree (with non-language specific annotation already done, so you need to strip back to basic * categories) * @param root The root of the current tree (can be null for words) * @return The fully annotated tree node (with daughters still as you want them in the final result) */ @Override public Tree transformTree(Tree t, Tree root) { String newCat = t.value() + getAnnotationString(t, root); t.setValue(newCat); if (t.isPreTerminal() && t.label() instanceof HasTag) ((HasTag) t.label()).setTag(newCat); return t; }