/** * 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()); } }
public void setCorrectTags(List<? extends HasTag> sentence) { int len = sentence.size(); correctTags = new String[len]; for (int i = 0; i < len; i++) { correctTags[i] = sentence.get(i).tag(); } }
/** * 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()); } } } }
/** * Create a new <code>TaggedWord</code>. * * @param oldLabel A Label. If it implements the HasWord and/or * HasTag interface, then the corresponding value will be set */ public TaggedWord(Label oldLabel) { super(oldLabel.value()); if (oldLabel instanceof HasTag) { this.tag = ((HasTag) oldLabel).tag(); } }
((HasTag) token).setTag(toks[1]);
public <E extends Label & HasTag> WordTag(E word) { this(word.value(), word.tag()); }
/** * 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); } } }
String posStr = normalizePreterminal(t); t.setValue(posStr); if(t.label() instanceof HasTag) ((HasTag) t.label()).setTag(posStr);
/** * Creates a new CategoryWordTag label from an existing label. * The oldLabel value() -- i.e., category -- is used for the new label. * The tag and word * are initialized iff the current label implements HasTag and HasWord * respectively. * * @param oldLabel The label to use as a basis of this Label */ public CategoryWordTag(Label oldLabel) { super(oldLabel); if (oldLabel instanceof HasTag) { this.tag = ((HasTag) oldLabel).tag(); } if (oldLabel instanceof HasWord) { this.word = ((HasWord) oldLabel).word(); } }
/** * 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; }
@Override public boolean test(Dependency<G, D, N> d) { /* log.info("DRF: Checking " + d + ": hasTag?: " + (d.dependent() instanceof HasTag) + "; value: " + ((d.dependent() instanceof HasTag)? ((HasTag) d.dependent()).tag(): null)); */ if (d == null) { return false; } if ( ! (d.dependent() instanceof HasTag)) { return false; } String tag = ((HasTag) d.dependent()).tag(); return tagRejectFilter.test(tag); }
ht.setTag(currentTree.label().value());
hw.getClass()); String tag = ((HasTag) hw).tag(); sb.append(" <word wid=\"").append(wordIndex).append("\" pos=\"").append(XMLUtils.escapeAttributeXML(tag)).append("\""); if (outputLemmas && hasCoreLabels) {
@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; }
/** * Construct a fall through tree in case we can't parse this sentence. * * @param words Words of the sentence that didn't parse * @return A tree with X for all the internal nodes. * Preterminals have the right tag if the words are tagged. */ public static Tree xTree(List<? extends HasWord> words) { TreeFactory treeFactory = new LabeledScoredTreeFactory(); List<Tree> lst2 = new ArrayList<>(); for (HasWord obj : words) { String s = obj.word(); Tree t = treeFactory.newLeaf(s); String tag = "XX"; if (obj instanceof HasTag) { if (((HasTag) obj).tag() != null) { tag = ((HasTag) obj).tag(); } } Tree t2 = treeFactory.newTreeNode(tag, Collections.singletonList(t)); lst2.add(t2); } return treeFactory.newTreeNode("X", lst2); }
/** * 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; }