public static void Main(string[] args) { if (args.Length < 10) { Console.WriteLine("Usage: classify grammar.txt filelist.txt output_dir result_list.txt"); Console.WriteLine(" grammar.txt String - filename for grammar to use"); Console.WriteLine(" filelist.txt String - list of files"); Console.WriteLine(" output_dir String - destination to save result"); Console.WriteLine(" result_list.txt String - file to save pairs of inputs/outputs for evaluation"); Console.WriteLine(" thread_count int - number of threads to use for execution"); Console.WriteLine(" proc_timeout int - maximum time a thread can run (in minutes)"); Console.WriteLine(" neighbors (k) int - number to use when doing k-nearest-neighbor analyses"); Console.WriteLine(" classify_url String - url of classifier (use \"-\" for default, saskatoon)"); Console.WriteLine(" prob_threshold double - classification probability pruning threshold"); Console.WriteLine(" layout_stats_file String - filename for stats file to use"); return; } string grammar_txt = args[0]; string file_list = args[1]; string output_dir = Path.GetFullPath(args[2]); string result_list = args[3]; string thread_count_str = args[4]; int thread_count; if (!int.TryParse(thread_count_str, out thread_count)) { Console.Error.WriteLine("Could not parse argument thread_count, using default value of {0}.", thread_count_default); thread_count = thread_count_default; } string proc_timeout_str = args[5]; int proc_timeout; if (!int.TryParse(proc_timeout_str, out proc_timeout)) { Console.Error.WriteLine("Could not parse argument proc_timeout, using default value of {0}.", proc_timeout_default); proc_timeout = proc_timeout_default; } string neighbors_str = args[6]; int neighbors; if (!int.TryParse(neighbors_str, out neighbors)) { Console.Error.WriteLine("Could not parse argument neighbors, using default value of {0}.", neighbors_default); neighbors = neighbors_default; } string classify_url = args[7]; if (classify_url == "-") { classify_url = classify_url_default; } string prob_threshold_str = args[8]; double prob_threshold; if (!double.TryParse(prob_threshold_str, out prob_threshold)) { Console.Error.WriteLine("Could not parse argument prob_threshold, using default value of {0}.", prob_threshold_default); prob_threshold = prob_threshold_default; } string stats_file = args[9]; Grammar grammar = Grammar.Load(grammar_txt); List <string> input_files = new List <string>(); using (FileStream fs = new FileStream(file_list, FileMode.Open)) { StreamReader sr = new StreamReader(fs); for (string s = sr.ReadLine(); s != null; s = sr.ReadLine()) { input_files.Add(s); } } Semaphore sema = new Semaphore(thread_count, thread_count); Dictionary <string, string> inputfile_to_output_inkml = new Dictionary <string, string>(); // number of files completed int complete_counter = 0; for (int k = 0; k < input_files.Count; k++) { string filename = input_files[k]; Console.WriteLine("Evaluating " + filename); sema.WaitOne(); Thread thread = new Thread(delegate() { InkML inkml_file = InkML.NewFromFile(filename); ParserMain pm = new ParserMain(grammar, inkml_file, top_n, neighbors, classify_url, prob_threshold, stats_file); Thread threadToKill = null; Action action = delegate() { threadToKill = Thread.CurrentThread; pm.topLevelParser(); }; IAsyncResult async_result = action.BeginInvoke(null, null); // wait minutes if (async_result.AsyncWaitHandle.WaitOne(proc_timeout * 60 * 1000)) // proc_timeout is in minutes { action.EndInvoke(async_result); if (pm.validParses.Count > 0) { string inkml_string = pm.validParses[0].root.ToInkML(inkml_file.annotations["UI"]); string new_inkml = output_dir + Path.DirectorySeparatorChar + "result." + Path.GetFileName(filename); inputfile_to_output_inkml.Add(filename, new_inkml); using (FileStream fs = new FileStream(new_inkml, FileMode.Create)) { StreamWriter sw = new StreamWriter(fs); sw.Write(inkml_string); sw.Close(); } Console.WriteLine("Done with " + filename + "; valid parse:\n" + pm.validParseTreeString(1)); } else { Console.WriteLine("Done with " + filename + "; invalid parse"); } } else { threadToKill.Abort(); Console.WriteLine("Done with " + filename + "; aborted"); } sema.Release(); Interlocked.Increment(ref complete_counter); }); //thread. thread.Start(); while (thread.IsAlive == false) { ; } } while (complete_counter != input_files.Count) { Thread.Sleep(100); } using (FileStream fs = new FileStream(result_list, FileMode.Create)) { StreamWriter sw = new StreamWriter(fs); foreach (string filename in input_files) { string new_inkml; if (inputfile_to_output_inkml.TryGetValue(filename, out new_inkml)) { sw.Write(filename); sw.Write('\t'); sw.WriteLine(new_inkml); } } sw.Close(); } }
public static void Main(string[] args) { if (args.Length < 4) { Console.WriteLine("Usage: ParserTest grammarFile inputInkML outputInkML adjStats [segmentFile]"); return; } // Note syntax for embedding subsequent arguments in output string {<index} string grammarFile = args[0]; string inputInkMLFile = args[1]; string outputInkMLFile = args[2]; string adjStatsFile = args[3]; string segmentSet = null; if (args.Length > 4) { segmentSet = args[4]; } InkML inputFileML = InkML.NewFromFile(inputInkMLFile); if (inputFileML == null) { Console.Error.WriteLine("error opening file {0}", args[1]); return; } // The InkML type contains some iterable fields, e.g. over 'traces' (strokes) List <Stroke> strokeList = new List <Stroke>(); foreach (InkML.Trace tr in inputFileML.traces) { strokeList.Add(tr.ToStroke()); } //Console.WriteLine("****************** SECOND PARSE ************************"); // port 1501: Part 1 (parallel implementation) // port 1503: Part 2 (parallel implementation) Grammar grammar = Grammar.Load(grammarFile); ParserMain secondParser = null; if (args.Length > 3) { secondParser = new ParserMain(grammar, inputFileML, 1, 2, "http://saskatoon.cs.rit.edu:1501/", 0, adjStatsFile, segmentSet, strokeList); } else { // NOTE: currently the behavior for Next() and Start() has been modified (likely will not execute properly). secondParser = new ParserMain(grammar, inputFileML, 1, 2, "http://saskatoon.cs.rit.edu:1501/", 0, adjStatsFile); } secondParser.topLevelParser(); //Console.WriteLine(secondParser.validParseTreeString(10)); if (secondParser.validParses.Count > 0) { string annotationui = inputFileML.annotations.ContainsKey("UI") ? inputFileML.annotations["UI"] : "NO_UI"; //Console.WriteLine(secondParser.validParses[0].root.ToInkML( annotationui ) ); System.IO.File.WriteAllText(outputInkMLFile, secondParser.validParses[0].root.ToInkML(annotationui)); } Console.WriteLine("Applied Rules: " + secondParser.apply_rule_counter); // inkml output //Console.WriteLine(secondParser.treeRoot.ToInkML()); }