public static void Main(string[] args) { if (args.Length != 4) { Console.WriteLine("Usage: ParserTest grammarFile inputInkML outputInkML 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 segmentFile = args[3]; InkML inputFileML = InkML.NewFromFile(inputInkMLFile); if (inputFileML == null) { Console.Error.WriteLine("IO error opening file {0}", args[1]); return; } // The InkML type contains some iterable fields, e.g. over 'traces' (strokes) strokeList = new List <Stroke>(); foreach (InkML.Trace tr in inputFileML.traces) { strokeList.Add(tr.ToStroke()); } LexerMain lexer = new LexerMain("part1.stats.csv", "http://saskatoon.cs.rit.edu:1500/", 0, segmentFile, strokeList); }
public ParserMain(Grammar grammar, InkML inkml_file, int topn, int neighbors, string classify_url, double prob_threshold, string stats_file) { TOP_N = topn; MAX_NEIGHBORS = neighbors; // YUCK!! COPY AND PASTE BELOW. // Set symbol tables and input file. Instantiate the lexer. Create empty valid parse list. //initializeTables(symbolDefFile); lexer = new LexerMain(stats_file, classify_url, prob_threshold); validParses = new List <ValidParseTree>(); this.grammar = grammar; layoutClasses = grammar.GetLayoutClasses(); // Obtain LBT, stroke information. inputInkML = inkml_file; initLBT = new LBT(inputInkML, LBT.DefaultAdjacentCriterion); treeRoot = new ParseTreeNode(); treeRoot.nodeType = "S"; treeRoot.lbt = initLBT; // treeRoot.lexResult ??? //strokeList = lbt.strokes; // unused? currentSymbol = null; minRequiredStrokes = 1; unusedInputStrokes = initLBT.strokes.Count; parseStateStack = new Stack(); //Console.WriteLine(tree.ToDOT()); // Create root node. //rootAll = new ParseTreeNode("S", tree); // HACK: note that the ParseState constructor will initialize the parser data as well. //ParseState initState = new ParseState(rootAll, tree); //, 5); // MAGIC accesses = 0; hits = 0; // Invoke the parse #if DEBUG //Console.WriteLine("Starting parse..."); #endif //Parse(initState, true, MAX_NEIGHBORS); //validParses.Sort(); }
public static void Main(string[] args) { List <string> filenames = new List <string>(); using (FileStream fs = new FileStream(args[0], FileMode.Open)) { StreamReader sr = new StreamReader(fs); for (string line = sr.ReadLine(); line != null; line = sr.ReadLine()) { filenames.Add(line); } } Dictionary <string, string> truth_to_layout = new Dictionary <string, string>(); // load symbol classes info using (FileStream fs = new FileStream(args[1], FileMode.Open)) { StreamReader sr = new StreamReader(fs); for (string s = sr.ReadLine(); s.Trim() != "##LAYOUT"; s = sr.ReadLine()) { ; } for (string s = sr.ReadLine(); s != null; s = sr.ReadLine()) { s = s.Trim(); string[] tokens = s.Split(' ', '\t'); for (int k = 1; k < tokens.Length; k++) { truth_to_layout[tokens[k]] = tokens[0]; } } } Console.WriteLine("String:ChildTruth,Class:Relationship,String:ParentTruth,String:Child,String:Parent,Feature:Top,Feature:Bottom,Feature:Left,Feature:Right"); foreach (string s in filenames) { InkML inkml = InkML.NewFromFile(s); // parse the math expression tree MathExpression me = new MathExpression(inkml.mathml_expression.mathml); foreach (var edge in me.edge_list) { // root node is irrelevant here if (edge.Item3 == "root") { continue; } InkML.TraceGroup child = inkml.mathml_id_to_tracegroup[edge.Item1]; MathExpression.NodeRelationship relationship = edge.Item2; InkML.TraceGroup parent = inkml.mathml_id_to_tracegroup[edge.Item3]; Console.Write(child.truth); Console.Write(", "); Console.Write(relationship); Console.Write(", "); Console.Write(parent.truth); Console.Write(", "); Console.Write(truth_to_layout[child.truth]); Console.Write(", "); Console.Write(truth_to_layout[parent.truth]); Console.Write(", "); AABB child_box = child.aabb; AABB parent_box = parent.aabb; float top = (child_box.Top - parent_box.Top) / parent_box.Height; float bottom = (child_box.Bottom - parent_box.Top) / parent_box.Height; float left = (child_box.Left - parent_box.Right) / parent_box.Width; float right = (child_box.Right - parent_box.Right) / parent_box.Width; Console.Write(top); Console.Write(", "); Console.Write(bottom); Console.Write(", "); Console.Write(left); Console.Write(", "); Console.Write(right); Console.WriteLine(); } } }
// RZ: New constructor -> replaces the lexer. public ParserMain(Grammar grammar, InkML inkml_file, int topn, int neighbors, string classify_url, double prob_threshold, string stats_file, string segmentFile, List <Stroke> strokeData) : this(grammar, inkml_file, topn, neighbors, classify_url, prob_threshold, stats_file) { lexer = new LexerMain(stats_file, classify_url, prob_threshold, segmentFile, strokeData); }
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()); }