private void addPOSView(List<String> pos, TextAnnotation ta) { // POS View TokenLabelView posView = new TokenLabelView(ViewNames.POS, "GoldStandard", ta, 1.0); for (int j = 0; j < pos.size(); j++) posView.addTokenLabel(j, pos.get(j), 1.0); ta.addView(ViewNames.POS, posView); }
/** * create a Lemma view in the TextAnnotation argument, and return a reference to that View. */ public View createLemmaView(TextAnnotation inputTa) throws IOException { String[] toks = inputTa.getTokens(); TokenLabelView lemmaView = new TokenLabelView(ViewNames.LEMMA, NAME, inputTa, 1.0); for (int i = 0; i < toks.length; ++i) { String lemma = getLemma(inputTa, i); Constituent lemmaConstituent = new Constituent(lemma, ViewNames.LEMMA, inputTa, i, i + 1); lemmaView.addConstituent(lemmaConstituent); } inputTa.addView(ViewNames.LEMMA, lemmaView); return lemmaView; }
public static String getPOS(TextAnnotation ta, int tokenId) { return ((TokenLabelView) (ta.getView(ViewNames.POS))).getLabel(tokenId); } }
/** * Helper for create a TokenLabelView from a stream of token labels */ private static int createTokenLabelView( Stream<Pair<Integer, String>> tokenLabels, TextAnnotation ta, String viewName) { TokenLabelView view = new TokenLabelView(viewName, "GoldStandard", ta, 1.0); tokenLabels.forEach(label -> view.addTokenLabel(label.getFirst(), label.getSecond(), 1.0)); ta.addView(viewName, view); return view.count(); }
@Override public String toString() { StringBuilder sb = new StringBuilder(); for (int i = 0; i < this.textAnnotation.size(); i++) { String label = this.getLabel(i); if (label.length() == 0) sb.append(this.getTextAnnotation().getToken(i)).append(" "); else sb.append("(").append(this.getLabel(i)).append(" ") .append(this.textAnnotation.getToken(i)).append(") "); } return sb.toString(); } }
/** * Aligns a {@link Labeling} to a {@link TokenLabelView}. * * @return A TokenLabelView */ public static TokenLabelView alignLabelingToTokenLabelView(String viewName, TextAnnotation ta, Labeling labeling) { List<Span> labels = labeling.getLabels(); double score = labeling.getScore(); String generator = labeling.getSource(); TokenLabelView view = new TokenLabelView(viewName, generator, ta, score); for (Span span : labels) { int tokenId = ta.getTokenIdFromCharacterOffset(span.getStart()); int endTokenId = ta.getTokenIdFromCharacterOffset(span.getEnding()); if (tokenId == endTokenId) endTokenId++; for (int i = tokenId; i < endTokenId; i++) { view.addTokenLabel(i, span.getLabel(), span.getScore()); if (span.isSetAttributes() && span.getAttributes().size() > 0) { Constituent c = view.getConstituentAtToken(i); copyAttributesToConstituent(span, c); } } } return view; }
public TokenLabelView getPrediction(TextAnnotation ta) throws Exception { log.debug("Input: {}", ta.getText()); List<Constituent> predicates = manager.getPredicateDetector().getPredicates(ta); // If there are no verb identified, return an empty TokenLabelView if (predicates.isEmpty()) return new TokenLabelView(SenseManager.getPredictedViewName(), VerbSenseConstants.systemIdentifier, ta, 1.0); ILPSolverFactory solver = new ILPSolverFactory(ILPSolverFactory.SolverType.JLISCuttingPlaneGurobi); ILPInference inference = manager.getInference(solver, predicates); return inference.getOutputView(); } }
private void addAnnotation(TextAnnotation ta) { String goldViewName = SenseManager.getGoldViewName(); Tree<String> tree = ParseHelper.getParseTree(ViewNames.PARSE_GOLD, ta, 0); Tree<Pair<String, IntPair>> spanLabeledTree = ParseUtils.getSpanLabeledTree(tree); List<Tree<Pair<String, IntPair>>> yield = spanLabeledTree.getYield(); TokenLabelView view = new TokenLabelView(goldViewName, "AnnotatedTreebank", ta, 1.0); Set<Integer> predicates = new HashSet<>(); for (PropbankFields fields : goldFields.get(ta.getId())) { int start = fields.getPredicateStart(yield); if (predicates.contains(start)) continue; predicates.add(start); view.addTokenLabel(start, fields.getSense(), 1.0); try { view.addTokenAttribute(start, LemmaIdentifier, fields.getLemma()); } catch (Exception e) { // XXX Maybe log the exception? e.printStackTrace(); } } if (view.getConstituents().size() > 0) ta.addView(goldViewName, view); } }
public void addTokenAttribute(int tokenId, String attributeKey, String attributeValue) throws Exception { Constituent c = getConstituentAtToken(tokenId); if (c == null) throw new Exception("Trying to add attribute to non-existent constituent at token " + tokenId); c.addAttribute(attributeKey, attributeValue); }
/** * Aligns a {@link edu.illinois.cs.cogcomp.thrift.base.Labeling} to a * {@link edu.illinois.cs.cogcomp.core.datastructures.textannotation.TokenLabelView}. * * <b>NOTE:</b> must correct for one-past-the-end labeling when calling * {@link TextAnnotation#getTokenIdFromCharacterOffset(int)}. * * @return A TokenLabelView */ public static TokenLabelView alignLabelingToTokenLabelView(String viewName, TextAnnotation ta, Labeling labeling) { List<Span> labels = labeling.getLabels(); double score = labeling.getScore(); String generator = labeling.getSource(); TokenLabelView view = new TokenLabelView(viewName, generator, ta, score); for (Span span : labels) { int tokenId = ta.getTokenIdFromCharacterOffset(span.getStart()); int endTokenId = ta.getTokenIdFromCharacterOffset(span.getEnding() - 1); if (tokenId == endTokenId) endTokenId++; for (int i = tokenId; i < endTokenId; i++) { view.addTokenLabel(i, span.getLabel(), span.getScore()); if (span.isSetAttributes() && span.getAttributes().size() > 0) { Constituent c = view.getConstituentAtToken(i); copyAttributesToConstituent(span, c); } } } return view; }
public TokenLabelView getPrediction(TextAnnotation ta) throws Exception { log.debug("Input: {}", ta.getText()); List<Constituent> predicates = manager.getPredicateDetector().getPredicates(ta); // If there are no verb identified, return an empty TokenLabelView if (predicates.isEmpty()) return new TokenLabelView(SenseManager.getPredictedViewName(), VerbSenseConstants.systemIdentifier, ta, 1.0); ILPSolverFactory solver = new ILPSolverFactory(ILPSolverFactory.SolverType.JLISCuttingPlaneGurobi); ILPInference inference = manager.getInference(solver, predicates); return inference.getOutputView(); } }
private void addAnnotation(TextAnnotation ta) { String goldViewName = SenseManager.getGoldViewName(); Tree<String> tree = ParseHelper.getParseTree(ViewNames.PARSE_GOLD, ta, 0); Tree<Pair<String, IntPair>> spanLabeledTree = ParseUtils.getSpanLabeledTree(tree); List<Tree<Pair<String, IntPair>>> yield = spanLabeledTree.getYield(); TokenLabelView view = new TokenLabelView(goldViewName, "AnnotatedTreebank", ta, 1.0); Set<Integer> predicates = new HashSet<>(); for (PropbankFields fields : goldFields.get(ta.getId())) { int start = fields.getPredicateStart(yield); if (predicates.contains(start)) continue; predicates.add(start); view.addTokenLabel(start, fields.getSense(), 1.0); try { view.addTokenAttribute(start, LemmaIdentifier, fields.getLemma()); } catch (Exception e) { // XXX Maybe log the exception? e.printStackTrace(); } } if (view.getConstituents().size() > 0) ta.addView(goldViewName, view); } }
/** * Helper for create a TokenLabelView from a stream of token labels */ private static int createTokenLabelView( Stream<Pair<Integer, String>> tokenLabels, TextAnnotation ta, String viewName) { TokenLabelView view = new TokenLabelView(viewName, "GoldStandard", ta, 1.0); tokenLabels.forEach(label -> view.addTokenLabel(label.getFirst(), label.getSecond(), 1.0)); ta.addView(viewName, view); return view.count(); }
public String getTokenAttribute(int tokenId, String key) { Constituent c = getConstituentAtToken(tokenId); if (c == null) return null; return c.getAttribute(key); }
@Override public String toString() { StringBuilder sb = new StringBuilder(); for (int i = 0; i < this.textAnnotation.size(); i++) { String label = this.getLabel(i); if (label.length() == 0) sb.append(this.getTextAnnotation().getToken(i)).append(" "); else sb.append("(").append(this.getLabel(i)).append(" ") .append(this.textAnnotation.getToken(i)).append(") "); } return sb.toString(); } }
private void addPOSView(List<String> pos, TextAnnotation ta) { // POS View TokenLabelView posView = new TokenLabelView(ViewNames.POS, "GoldStandard", ta, 1.0); for (int j = 0; j < pos.size(); j++) posView.addTokenLabel(j, pos.get(j), 1.0); ta.addView(ViewNames.POS, posView); }
/** * create a Lemma view in the TextAnnotation argument, and return a reference to that View. */ public View createLemmaView(TextAnnotation inputTa) throws IOException { String[] toks = inputTa.getTokens(); TokenLabelView lemmaView = new TokenLabelView(ViewNames.LEMMA, NAME, inputTa, 1.0); for (int i = 0; i < toks.length; ++i) { String lemma = getLemma(inputTa, i); Constituent lemmaConstituent = new Constituent(lemma, ViewNames.LEMMA, inputTa, i, i + 1); lemmaView.addConstituent(lemmaConstituent); } inputTa.addView(ViewNames.LEMMA, lemmaView); return lemmaView; }
/** * Aligns a {@link edu.illinois.cs.cogcomp.thrift.base.Labeling} to a * {@link edu.illinois.cs.cogcomp.core.datastructures.textannotation.TokenLabelView}. * * <b>NOTE:</b> must correct for one-past-the-end labeling when calling * {@link TextAnnotation#getTokenIdFromCharacterOffset(int)}. * * @return A TokenLabelView */ public static TokenLabelView alignLabelingToTokenLabelView(String viewName, TextAnnotation ta, Labeling labeling) { List<Span> labels = labeling.getLabels(); double score = labeling.getScore(); String generator = labeling.getSource(); TokenLabelView view = new TokenLabelView(viewName, generator, ta, score); for (Span span : labels) { int tokenId = ta.getTokenIdFromCharacterOffset(span.getStart()); int endTokenId = ta.getTokenIdFromCharacterOffset(span.getEnding() - 1); if (tokenId == endTokenId) endTokenId++; for (int i = tokenId; i < endTokenId; i++) { view.addTokenLabel(i, span.getLabel(), span.getScore()); if (span.isSetAttributes() && span.getAttributes().size() > 0) { Constituent c = view.getConstituentAtToken(i); copyAttributesToConstituent(span, c); } } } return view; }
@Override protected void addView(TextAnnotation ta) throws AnnotatorException { View v = new TokenLabelView(ViewNames.TRANSLITERATION, this.getClass().getName(), ta, 1.0); int index = 0; for(String tok : ta.getTokens()){ try { TopList<Double, String> ll = model.Generate(tok.toLowerCase()); if(ll.size() > 0) { Pair<Double, String> toppair = ll.getFirst(); Constituent c = new Constituent(toppair.getSecond(), toppair.getFirst(), ViewNames.TRANSLITERATION, ta, index, index + 1); v.addConstituent(c); } } catch (Exception e) { // print that this word has failed... e.printStackTrace(); } index++; } ta.addView(ViewNames.TRANSLITERATION, v); } }
public static String getPOS(TextAnnotation ta, int tokenId) { return ((TokenLabelView) (ta.getView(ViewNames.POS))).getLabel(tokenId); } }