/** * Finds and returns the key in the Counter with the largest count. Returning * null if count is empty. * * @param c The Counter * @return The key in the Counter with the largest count. */ public static <E> E argmax(Counter<E> c) { return argmax(c, (x, y) -> 0, null); }
/** * Finds and returns the key in the Counter with the largest count. Returning * null if count is empty. * * @param c The Counter * @param tieBreaker the tie breaker for when elements have the same value. * @return The key in the Counter with the largest count. */ public static <E> E argmax(Counter<E> c, Comparator<E> tieBreaker) { return argmax(c, tieBreaker, (E) null); }
public E argmax() { return Counters.argmax(counter); }
@Override @Deprecated public L classOf(RVFDatum<L, F> example) { Counter<L> scores = scoresOf(example); return Counters.argmax(scores); }
@Override public L classOf(Datum<L, F> example) { Counter<L> scores = scoresOf(example); if (scores != null) { return Counters.argmax(scores); } else { return defaultLabel; } }
/** * Runs the Viterbi algorithm on the sequence model * in order to find the best sequence. * This sequence finder only works on SequenceModel's with rightWindow == 0. * * @return An array containing the int tags of the best sequence */ @Override public int[] bestSequence(SequenceModel ts) { return Counters.argmax(kBestSequences(ts, 1)); }
private L classOfRVFDatum(RVFDatum<L, F> example) { Counter<L> scores = scoresOfRVFDatum(example); return Counters.argmax(scores); }
@Override public E argmax() { return Counters.argmax(Counters.linearCombination(this.counter, 1.0, prior.counter, priorMultiplier)); }
@Override public L classOf(Datum<L, F> example) { return Counters.argmax(scoresOf(example)); }
public L classOf(RVFDatum<L, F> example) { Counter<L> scores = scoresOf(example); return Counters.argmax(scores); }
private String getTag(String word) { int iW = wordIndex.addToIndex(word); ensureProbs(iW, false); return Counters.argmax(logProbs); }
@Override public L classOf(Datum<L, F> example) { if(example instanceof RVFDatum<?, ?>)return classOfRVFDatum((RVFDatum<L,F>)example); Counter<L> scores = scoresOf(example); return Counters.argmax(scores); }
/** * A utility to get useful information out of a CorefMention. In particular, it returns the CoreLabels which are * associated with this mention, and it returns a score for how much we think this mention should be the canonical * mention. * * @param doc The document this mention is referenced into. * @param mention The mention itself. * @return A pair of the tokens in the mention, and a score for how much we like this mention as the canonical mention. */ private static Pair<List<CoreLabel>, Double> grokCorefMention(Annotation doc, CorefChain.CorefMention mention) { List<CoreLabel> tokens = doc.get(CoreAnnotations.SentencesAnnotation.class).get(mention.sentNum - 1).get(CoreAnnotations.TokensAnnotation.class); List<CoreLabel> mentionAsTokens = tokens.subList(mention.startIndex - 1, mention.endIndex - 1); // Try to assess this mention's NER type Counter<String> nerVotes = new ClassicCounter<>(); mentionAsTokens.stream().filter(token -> token.ner() != null && !"O".equals(token.ner())).forEach(token -> nerVotes.incrementCount(token.ner())); String ner = Counters.argmax(nerVotes, (o1, o2) -> o1 == null ? 0 : o1.compareTo(o2)); double nerCount = nerVotes.getCount(ner); double nerScore = nerCount * nerCount / ((double) mentionAsTokens.size()); // Return return Pair.makePair(mentionAsTokens, nerScore); }
/** * Select the most common element of the given type in the given span. * This is useful for, e.g., finding the most likely NER span of a given span, or the most * likely POS tag of a given span. * Null entries are removed. * * @param span The span of the sentence to find the mode element in. This must be entirely contained in the sentence. * @param selector The property of the sentence we are getting the mode of. For example, <code>Sentence::posTags</code> * @param <E> The type of the element we are getting. * @return The most common element of the given property in the sentence. */ public <E> E modeInSpan(Span span, Function<Sentence, List<E>> selector) { if (!Span.fromValues(0, sentence.length()).contains(span)) { throw new IllegalArgumentException("Span must be entirely contained in the sentence: " + span + " (sentence length=" + sentence.length() + ")"); } Counter<E> candidates = new ClassicCounter<>(); for (int i : span) { candidates.incrementCount(selector.apply(sentence).get(i)); } candidates.remove(null); return Counters.argmax(candidates); }
/** * Mostly just an alias, but make sure our featurizer is serializable! */ public interface Featurizer extends Function<Triple<ClauseSplitterSearchProblem.State, ClauseSplitterSearchProblem.Action, ClauseSplitterSearchProblem.State>, Counter<String>>, Serializable { boolean isSimpleSplit(Counter<String> feats); }
/** * Score the given input, returning both the classification decision and the * probability of that decision. * Note that this method will not return a relation which does not type check. * * * @param input The input to classify. * @return A pair with the relation we classified into, along with its confidence. */ public Pair<String,Double> classify(KBPInput input) { RVFDatum<String, String> datum = new RVFDatum<>(features(input)); Counter<String> scores = classifier.scoresOf(datum); Counters.expInPlace(scores); Counters.normalize(scores); String best = Counters.argmax(scores); // While it doesn't type check, continue going down the list. // NO_RELATION is always an option somewhere in there, so safe to keep going... while (!NO_RELATION.equals(best) && scores.size() > 1 && (!KBPRelationExtractor.RelationType.fromString(best).get().validNamedEntityLabels.contains(input.objectType) || RelationType.fromString(best).get().entityType != input.subjectType) ) { scores.remove(best); Counters.normalize(scores); best = Counters.argmax(scores); } return Pair.makePair(best, scores.getCount(best)); }
/** * Attempt to infer the part of speech of the given preterminal node, which * was created during the expansion of a multi-word token. */ private static String inferPOS(Tree t, Tree parent, TwoDimensionalCounter<String, String> unigramTagger) { String word = t.firstChild().value(); String containingPhraseStr = getContainingPhrase(t, parent); // Overrides: let the manual POS model handle a few special cases first String overrideTag = ManualUWModel.getOverrideTag(word, containingPhraseStr); if (overrideTag != null) return overrideTag; Set<String> unigramTaggerKeys = unigramTagger.firstKeySet(); // Try treating this word as a verb and stripping any clitic // pronouns. If the stripped version exists in the unigram // tagger, then stick with the verb hypothesis SpanishVerbStripper.StrippedVerb strippedVerb = verbStripper.separatePronouns(word); if (strippedVerb != null && unigramTaggerKeys.contains(strippedVerb.getStem())) { String pos = Counters.argmax(unigramTagger.getCounter(strippedVerb.getStem())); if (pos.startsWith("v")) return pos; } if (unigramTagger.firstKeySet().contains(word)) return Counters.argmax(unigramTagger.getCounter(word), new POSTieBreaker()); return ManualUWModel.getTag(word, containingPhraseStr); }
private static void modifyUsingCoreNLPNER(Annotation doc) { Properties ann = new Properties(); ann.setProperty("annotators", "pos, lemma, ner"); StanfordCoreNLP pipeline = new StanfordCoreNLP(ann, false); pipeline.annotate(doc); for (CoreMap sentence : doc.get(CoreAnnotations.SentencesAnnotation.class)) { List<EntityMention> entities = sentence.get(MachineReadingAnnotations.EntityMentionsAnnotation.class); if (entities != null) { List<CoreLabel> tokens = sentence.get(CoreAnnotations.TokensAnnotation.class); for (EntityMention en : entities) { //System.out.println("old ner tag for " + en.getExtentString() + " was " + en.getType()); Span s = en.getExtent(); Counter<String> allNertagforSpan = new ClassicCounter<>(); for (int i = s.start(); i < s.end(); i++) { allNertagforSpan.incrementCount(tokens.get(i).ner()); } String entityNertag = Counters.argmax(allNertagforSpan); en.setType(entityNertag); //System.out.println("new ner tag is " + entityNertag); } } } }
/** * TODO(gabor) JavaDoc * * @param tokens * @param span * @return */ public static String guessNER(List<CoreLabel> tokens, Span span) { Counter<String> nerGuesses = new ClassicCounter<>(); for (int i : span) { nerGuesses.incrementCount(tokens.get(i).ner()); } nerGuesses.remove("O"); nerGuesses.remove(null); if (nerGuesses.size() > 0 && Counters.max(nerGuesses) >= span.size() / 2) { return Counters.argmax(nerGuesses); } else { return "O"; } }
int antID = Counters.argmax(probs);