public double[] computeAnswerTypeProbs(Parse question) { String[] context = atcg.getContext(question);//<co id="atc.context"/> return model.eval(context, probs);//<co id="atc.evaluate"/> } /*
public String computeAnswerType(Parse question) { double[] probs = computeAnswerTypeProbs(question);//<co id="atc.getprobs"/> return model.getBestOutcome(probs);//<co id="atc.outcome"/> }
@Override public Set<String> getOutcomeNames() { if (outcomeNames == null) { outcomeNames = new TreeSet<String>(); for (int i = 0; i < this.model.getNumOutcomes(); i++) { outcomeNames.add(this.model.getOutcome(i)); } } return outcomeNames; }
private void eval(Event event, boolean real) { String outcome = event.getOutcome(); // Is ignored String[] context = event.getContext(); double[] ocs; if (!real) { ocs = _model.eval(context); } else { float[] values = RealValueFileEventStream.parseContexts(context); ocs = _model.eval(context, values); } int numOutcomes = ocs.length; DoubleStringPair[] result = new DoubleStringPair[numOutcomes]; for (int i=0; i<numOutcomes; i++) result[i] = new DoubleStringPair(ocs[i], _model.getOutcome(i)); java.util.Arrays.sort(result); // Print the most likely outcome first, down to the least likely. for (int i=numOutcomes-1; i>=0; i--) System.out.print(result[i].stringValue + " " + result[i].doubleValue + " "); System.out.println(); }
public OUTCOME_TYPE classify(List<Feature> features) throws CleartkProcessingException { ContextValues contextValues = this.featuresEncoder.encodeAll(features); String encodedOutcome = this.model.getBestOutcome(this.model.eval( contextValues.getContext(), contextValues.getValues())); return outcomeEncoder.decode(encodedOutcome); }
@Override public Map<OUTCOME_TYPE, Double> score(List<Feature> features) throws CleartkProcessingException { ContextValues contextValues = this.featuresEncoder.encodeAll(features); double[] evalResults = this.model.eval(contextValues.getContext(), contextValues.getValues()); String[] encodedOutcomes = (String[]) this.model.getDataStructures()[2]; Map<OUTCOME_TYPE, Double> returnValues = Maps.newHashMap(); for (int i = 0; i < evalResults.length; i++) { returnValues.put(outcomeEncoder.decode(encodedOutcomes[i]), evalResults[i]); } return returnValues; }
public BeamSearch(int size, BeamSearchContextGenerator<T> cg, MaxentModel model, SequenceValidator<T> validator, int cacheSize) { this.size = size; this.cg = cg; this.model = model; this.validator = validator; if (cacheSize > 0) { contextsCache = new Cache(cacheSize); } this.probs = new double[model.getNumOutcomes()]; }
@SuppressWarnings("unchecked") private void initialise() { Object[] dataStructures = maxentModel.getDataStructures(); outcomeNames = (String[]) dataStructures[2]; TreeSet<String> outcomeSet = new TreeSet<String>(); for (String outcome : outcomeNames) outcomeSet.add(outcome); outcomeList.addAll(outcomeSet); this.predicateTable = (IndexHashTable<String>) dataStructures[1]; predicates = new String[predicateTable.size()]; predicateTable.toArray(predicates); modelParameters = (Context[]) dataStructures[0]; }
private void eval(Event event, boolean real) { String outcome = event.getOutcome(); // Is ignored String[] context = event.getContext(); double[] ocs; if (!real) { ocs = _model.eval(context); } else { float[] values = RealValueFileEventStream.parseContexts(context); ocs = _model.eval(context, values); } int numOutcomes = ocs.length; DoubleStringPair[] result = new DoubleStringPair[numOutcomes]; for (int i=0; i<numOutcomes; i++) result[i] = new DoubleStringPair(ocs[i], _model.getOutcome(i)); java.util.Arrays.sort(result); // Print the most likely outcome first, down to the least likely. for (int i=numOutcomes-1; i>=0; i--) System.out.print(result[i].stringValue + " " + result[i].doubleValue + " "); System.out.println(); }
public static void eval(MaxentModel model, Reader r, Evalable e, boolean verbose) { float totPos=0, truePos=0, falsePos=0; Event[] events = (e.getEventCollector(r)).getEvents(true); //MaxentModel model = e.getModel(dir, name); String negOutcome = e.getNegativeOutcome(); for (Event event : events) { String guess = model.getBestOutcome(model.eval(event.getContext())); String ans = event.getOutcome(); if (verbose) System.out.println(ans + " " + guess); if (!ans.equals(negOutcome)) totPos++; if (!guess.equals(negOutcome) && !guess.equals(ans)) falsePos++; else if (ans.equals(guess)) truePos++; } System.out.println("Precision: " + truePos/(truePos+falsePos)); System.out.println("Recall: " + truePos/totPos); }
public void init(NamedList initArgs) { SolrParams params = SolrParams.toSolrParams(initArgs); String modelDirectory = params.get("modelDirectory", System.getProperty("model.dir"));//<co id="qqpp.model"/> String wordnetDirectory = params.get("wordnetDirectory", System.getProperty("wordnet.dir"));//<co id="qqpp.wordnet"/> if (modelDirectory != null) { File modelsDir = new File(modelDirectory); try { InputStream chunkerStream = new FileInputStream( new File(modelsDir,"en-chunker.bin")); ChunkerModel chunkerModel = new ChunkerModel(chunkerStream); chunker = new ChunkerME(chunkerModel); //<co id="qqpp.chunker"/> InputStream posStream = new FileInputStream( new File(modelsDir,"en-pos-maxent.bin")); POSModel posModel = new POSModel(posStream); tagger = new POSTaggerME(posModel); //<co id="qqpp.tagger"/> model = new DoccatModel(new FileInputStream( //<co id="qqpp.theModel"/> new File(modelDirectory,"en-answer.bin"))) .getChunkerModel(); probs = new double[model.getNumOutcomes()]; atcg = new AnswerTypeContextGenerator( new File(wordnetDirectory, "dict"));//<co id="qqpp.context"/> } catch (IOException e) { throw new RuntimeException(e); } } } /*
@Override public List<Decision> decide(List<FeatureResult<?>> featureResults) { List<String> contextList = new ArrayList<String>(); List<Float> weightList = new ArrayList<Float>(); OpenNLPDecisionMaker.prepareData(featureResults, contextList, weightList); String[] contexts = new String[contextList.size()]; float[] weights = new float[weightList.size()]; int i = 0; for (String context : contextList) { contexts[i++] = context; } i = 0; for (Float weight : weightList) { weights[i++] = weight; } double[] probs = model.eval(contexts, weights); String[] outcomes = new String[probs.length]; for (i = 0; i < probs.length; i++) outcomes[i] = model.getOutcome(i); TreeSet<Decision> outcomeSet = new TreeSet<Decision>(); for (i = 0; i < probs.length; i++) { Decision decision = new Decision(outcomes[i], probs[i]); outcomeSet.add(decision); } List<Decision> decisions = new ArrayList<Decision>(outcomeSet); return decisions; }
public static void eval(MaxentModel model, Reader r, Evalable e, boolean verbose) { float totPos=0, truePos=0, falsePos=0; Event[] events = (e.getEventCollector(r)).getEvents(true); //MaxentModel model = e.getModel(dir, name); String negOutcome = e.getNegativeOutcome(); for (Event event : events) { String guess = model.getBestOutcome(model.eval(event.getContext())); String ans = event.getOutcome(); if (verbose) System.out.println(ans + " " + guess); if (!ans.equals(negOutcome)) totPos++; if (!guess.equals(negOutcome) && !guess.equals(ans)) falsePos++; else if (ans.equals(guess)) truePos++; } System.out.println("Precision: " + truePos/(truePos+falsePos)); System.out.println("Recall: " + truePos/totPos); }
/** * Returns a number between 0 and 1 which represents the models belief that the specified mentions are compatible. * Value closer to 1 are more compatible, while values closer to 0 are less compatible. * @param mention1 The first mention to be considered. * @param mention2 The second mention to be considered. * @return a number between 0 and 1 which represents the models belief that the specified mentions are compatible. */ public double compatible(Context mention1, Context mention2) { List<String> feats = getFeatures(mention1, mention2); if (debugOn) System.err.println("SimilarityModel.compatible: feats="+feats); return (testModel.eval(feats.toArray(new String[feats.size()]))[SAME_INDEX]); }
logger.info(Arrays.asList(context).toString()); float[] values = RealValueFileEventStream.parseContexts(context); double[] ocs = model.eval(context, values); int numOutcomes = ocs.length; DoubleStringPair[] result = new DoubleStringPair[numOutcomes]; for (int i = 0; i < numOutcomes; i++) { result[i] = new DoubleStringPair(ocs[i], model.getOutcome(i));
double[] outcomeProbs = loadedMaxentModel.eval(context); String outcome = loadedMaxentModel.getBestOutcome(outcomeProbs);
public double[] genderDistribution(Context np1) { List<String> features = getFeatures(np1); if (debugOn) { //System.err.println("GenderModel.genderDistribution: "+features); } return testModel.eval(features.toArray(new String[features.size()])); }
scores = (double[]) contextsCache.get(contexts); if (scores == null) { scores = model.eval(contexts, probs); contextsCache.put(contexts,scores); scores = model.eval(contexts, probs); if (scores[p] < min) continue; //only advance first "size" outcomes String out = model.getOutcome(p); if (validSequence(i, sequence, outcomes, out)) { Sequence ns = new Sequence(top, out, scores[p]); String out = model.getOutcome(p); if (validSequence(i, sequence, outcomes, out)) { Sequence ns = new Sequence(top, out, scores[p]);
public double[] numberDist(Context c) { List<String> feats = getFeatures(c); return testModel.eval(feats.toArray(new String[feats.size()])); }
public double getNonReferentialProbability(MentionContext mention) { List<String> features = getFeatures(mention); double r = model.eval(features.toArray(new String[features.size()]))[nonRefIndex]; if (debugOn) System.err.println(this +" " + mention.toText() + " -> null " + r + " " + features); return r; }