public static void eval(MaxentModel model, Reader r, Evalable e, bool verbose) { float totPos = 0, truePos = 0, falsePos = 0; Event[] events = (e.getEventCollector(r)).getEvents(true); //MaxentModel model = e.getModel(dir, name); string negOutcome = e.NegativeOutcome; foreach (Event @event in events) { string guess = model.getBestOutcome(model.eval(@event.Context)); string ans = @event.Outcome; if (verbose) { Console.WriteLine(ans + " " + guess); } if (!ans.Equals(negOutcome)) { totPos++; } if (!guess.Equals(negOutcome) && !guess.Equals(ans)) { falsePos++; } else if (ans.Equals(guess)) { truePos++; } } Console.WriteLine("Precision: " + truePos / (truePos + falsePos)); Console.WriteLine("Recall: " + truePos / totPos); }
/// <summary> /// Replace the old model with a new one, forcing the replacement to wait until /// all threads using the old model have finished using it. /// </summary> /// <param name="model"> /// The new model which is being swapped in. </param> public virtual void replaceModel(MaxentModel model) { lock (this) { replacementThread = Thread.CurrentThread; while (!replacementCanProceed) { Thread.Yield(); } setter.setModel(model); replacementThread = null; } }
public static void eval(MaxentModel model, Reader r, Evalable e) { eval(model, r, e, false); }
/// <summary> /// Sets the model for the given domain. /// </summary> /// <param name="domain"> /// The ModelDomain object which keys to the model. </param> /// <param name="model"> /// The MaxentModel trained for the domain. </param> public virtual void setModelForDomain(ModelDomain domain, MaxentModel model) { map[domain] = model; }
public ModelApplier(MaxentModel m) { _model = m; }
/// <summary> /// Main method. Call as follows: /// <para> /// java ModelApplier modelFile dataFile /// </para> /// </summary> public static void Main(string[] args) { string dataFileName, modelFileName; bool real = false; string type = "maxent"; int ai = 0; if (args.Length == 0) { usage(); } if (args.Length > 0) { while (args[ai].StartsWith("-", StringComparison.Ordinal)) { if (args[ai].Equals("-real")) { real = true; } else if (args[ai].Equals("-perceptron")) { type = "perceptron"; } else { usage(); } ai++; } modelFileName = args[ai++]; dataFileName = args[ai++]; ModelApplier predictor = null; try { MaxentModel m = (new GenericModelReader(new Jfile(modelFileName))).Model; predictor = new ModelApplier(m); } catch (Exception e) { Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); Environment.Exit(0); } try { EventStream es = new BasicEventStream(new PlainTextByLineDataStream(new FileReader(new Jfile(dataFileName))), ","); while (es.hasNext()) { predictor.eval(es.next(), real); } return; } catch (Exception e) { Console.WriteLine("Unable to read from specified file: " + modelFileName); Console.WriteLine(); Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); } } }