edu.stanford.nlp.patterns.surface.ScorePatternsF1 Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of stanford-corenlp Show documentation
Show all versions of stanford-corenlp Show documentation
Stanford CoreNLP provides a set of natural language analysis tools which can take raw English language text input and give the base forms of words, their parts of speech, whether they are names of companies, people, etc., normalize dates, times, and numeric quantities, mark up the structure of sentences in terms of phrases and word dependencies, and indicate which noun phrases refer to the same entities. It provides the foundational building blocks for higher level text understanding applications.
package edu.stanford.nlp.patterns.surface;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import edu.stanford.nlp.patterns.CandidatePhrase;
import edu.stanford.nlp.patterns.ConstantsAndVariables;
import edu.stanford.nlp.patterns.GetPatternsFromDataMultiClass.PatternScoring;
import edu.stanford.nlp.patterns.ScorePatterns;
import edu.stanford.nlp.stats.ClassicCounter;
import edu.stanford.nlp.stats.Counter;
import edu.stanford.nlp.stats.Counters;
import edu.stanford.nlp.stats.TwoDimensionalCounter;
import edu.stanford.nlp.util.CollectionUtils;
/**
* Used if patternScoring flag is set to F1 with the seed pattern. See {@link PatternScoring} enum.
*
* @author Sonal Gupta ([email protected])
*
*/
public class ScorePatternsF1 extends ScorePatterns {
Counter p0Set = null;
E p0;
public ScorePatternsF1(ConstantsAndVariables constVars,
PatternScoring patternScoring,
String label, Set allCandidatePhrases,
TwoDimensionalCounter patternsandWords4Label,
TwoDimensionalCounter negPatternsandWords4Label,
TwoDimensionalCounter unLabeledPatternsandWords4Label,
Properties props, Counter p0Set, E p0){
super(constVars,
patternScoring, label, allCandidatePhrases, patternsandWords4Label,
negPatternsandWords4Label, unLabeledPatternsandWords4Label,
props);
this.p0 = p0;
this.p0Set =p0Set;
}
@Override
public void setUp(Properties props){}
@Override
public Counter score() {
Counter specificity = new ClassicCounter<>();
Counter sensitivity = new ClassicCounter<>();
if (p0Set.keySet().size() == 0)
throw new RuntimeException("how come p0set size is empty for " + p0
+ "?");
for (Entry> en : patternsandWords4Label
.entrySet()) {
int common = CollectionUtils.intersection(en.getValue().keySet(),
p0Set.keySet()).size();
if (common == 0)
continue;
if (en.getValue().keySet().size() == 0)
throw new RuntimeException("how come counter for " + en.getKey()
+ " is empty?");
specificity.setCount(en.getKey(), common
/ (double) en.getValue().keySet().size());
sensitivity.setCount(en.getKey(), common / (double) p0Set.size());
}
Counters.retainNonZeros(specificity);
Counters.retainNonZeros(sensitivity);
Counter add = Counters.add(sensitivity, specificity);
Counter product = Counters.product(sensitivity,
specificity);
Counters.retainNonZeros(product);
Counters.retainKeys(product, add.keySet());
Counter finalPat = Counters.scale(
Counters.division(product, add), 2);
return finalPat;
}
}