/// <summary>Some basic testing of the ClassifierCombiner.</summary> /// <param name="args">Command-line arguments as properties: -loadClassifier1 serializedFile -loadClassifier2 serializedFile</param> /// <exception cref="System.Exception">If IO or serialization error loading classifiers</exception> public static void Main(string[] args) { Properties props = StringUtils.ArgsToProperties(args); Edu.Stanford.Nlp.IE.ClassifierCombiner ec = new Edu.Stanford.Nlp.IE.ClassifierCombiner(props); log.Info(ec.ClassifyToString("Marketing : Sony Hopes to Win Much Bigger Market For Wide Range of Small-Video Products --- By Andrew B. Cohen Staff Reporter of The Wall Street Journal")); }
// static method for getting a ClassifierCombiner from a string path /// <exception cref="System.IO.IOException"/> /// <exception cref="System.TypeLoadException"/> /// <exception cref="System.InvalidCastException"/> public static Edu.Stanford.Nlp.IE.ClassifierCombiner GetClassifier(string loadPath, Properties props) { ObjectInputStream ois = IOUtils.ReadStreamFromString(loadPath); Edu.Stanford.Nlp.IE.ClassifierCombiner returnCC = GetClassifier(ois, props); IOUtils.CloseIgnoringExceptions(ois); return(returnCC); }
// show some info about a ClassifierCombiner public static void ShowCCInfo(Edu.Stanford.Nlp.IE.ClassifierCombiner cc) { log.Info(string.Empty); log.Info("classifiers used:"); log.Info(string.Empty); if (cc.initLoadPaths.Count == cc.baseClassifiers.Count) { for (int i = 0; i < cc.initLoadPaths.Count; i++) { log.Info("baseClassifiers index " + i + " : " + cc.initLoadPaths[i]); } } else { for (int i = 0; i < cc.initLoadPaths.Count; i++) { log.Info("baseClassifiers index " + i); } } log.Info(string.Empty); log.Info("combinationMode: " + cc.combinationMode); log.Info(string.Empty); }
// run a particular CRF of this ClassifierCombiner on a testFile // user can say -crfToExamine 0 to get 1st element or -crfToExamine /edu/stanford/models/muc7.crf.ser.gz // this does not currently support drill down on CMM's /// <exception cref="System.Exception"/> public static void ExamineCRF(Edu.Stanford.Nlp.IE.ClassifierCombiner cc, string crfNameOrIndex, SeqClassifierFlags flags, string testFile, string testFiles, IDocumentReaderAndWriter <CoreLabel> readerAndWriter) { CRFClassifier <CoreLabel> crf; // potential index into baseClassifiers int ci; // set ci with the following rules // 1. first see if ci is an index into baseClassifiers // 2. if its not an integer or wrong size, see if its a file name of a loadPath try { ci = System.Convert.ToInt32(crfNameOrIndex); if (ci < 0 || ci >= cc.baseClassifiers.Count) { // ci is not an int corresponding to an element in baseClassifiers, see if name of a crf loadPath ci = cc.initLoadPaths.IndexOf(crfNameOrIndex); } } catch (NumberFormatException) { // cannot interpret crfNameOrIndex as an integer, see if name of a crf loadPath ci = cc.initLoadPaths.IndexOf(crfNameOrIndex); } // if ci corresponds to an index in baseClassifiers, get the crf at that index, otherwise set crf to null if (ci >= 0 && ci < cc.baseClassifiers.Count) { // TODO: this will break if baseClassifiers contains something that is not a CRF crf = (CRFClassifier <CoreLabel>)cc.baseClassifiers[ci]; } else { crf = null; } // if you can get a specific crf, generate the appropriate report, if null do nothing if (crf != null) { // if there is a crf and testFile was set , do the crf stuff for a single testFile if (testFile != null) { if (flags.searchGraphPrefix != null) { crf.ClassifyAndWriteViterbiSearchGraph(testFile, flags.searchGraphPrefix, crf.MakeReaderAndWriter()); } else { if (flags.printFirstOrderProbs) { crf.PrintFirstOrderProbs(testFile, readerAndWriter); } else { if (flags.printFactorTable) { crf.PrintFactorTable(testFile, readerAndWriter); } else { if (flags.printProbs) { crf.PrintProbs(testFile, readerAndWriter); } else { if (flags.useKBest) { // TO DO: handle if user doesn't provide kBest int k = flags.kBest; crf.ClassifyAndWriteAnswersKBest(testFile, k, readerAndWriter); } else { if (flags.printLabelValue) { crf.PrintLabelInformation(testFile, readerAndWriter); } else { // no crf test flag provided log.Info("Warning: no crf test flag was provided, running classify and write answers"); crf.ClassifyAndWriteAnswers(testFile, readerAndWriter, true); } } } } } } } else { if (testFiles != null) { // if there is a crf and testFiles was set , do the crf stuff for testFiles // if testFile was set as well, testFile overrides IList <File> files = Arrays.Stream(testFiles.Split(",")).Map(null).Collect(Collectors.ToList()); if (flags.printProbs) { // there is a crf and printProbs crf.PrintProbs(files, crf.DefaultReaderAndWriter()); } else { log.Info("Warning: no crf test flag was provided, running classify files and write answers"); crf.ClassifyFilesAndWriteAnswers(files, crf.DefaultReaderAndWriter(), true); } } } } }