Example #1
0
        public LBT(LBT other)
        {
            if (other != null)
            {
                // clone stroke list
                strokes = new List <Stroke>();
                if (other.strokes != null)
                {
                    foreach (Stroke s in other.strokes)
                    {
                        strokes.Add(s);
                    }
                }
                // clone tree
                root = new LBTNode(other.root);

                // rebuild dictionary
                stroke_to_node = new Dictionary <Stroke, LBTNode>();
                Stack stack = new Stack();
                stack.Push(root);
                while (stack.Count > 0)
                {
                    LBTNode node = (LBTNode)stack.Pop();
                    if (node != root)
                    {
                        stroke_to_node.Add(node.stroke, node);
                    }
                    foreach (LBTNode ln in node.children)
                    {
                        stack.Push(ln);
                    }
                }
                raw_data = null;
            }
        }
        // this node assumed to be the root!
        public string ToInkML(string ui_annotation)
        {
            StringBuilder         sb       = new StringBuilder();
            List <SymbolTreeNode> children = new List <SymbolTreeNode>();

            PopulateChildrenLexerResults(children);
            List <Stroke> all_strokes = new List <Stroke>();
            Dictionary <string, InkML.Trace> traces = new Dictionary <string, InkML.Trace>();
            InkML inkml = new InkML();

            inkml.mathml_expression        = new InkML.MathML();
            inkml.mathml_expression.mathml = "<math xmlns='http://www.w3.org/1998/Math/MathML'><mrow>";

            foreach (SymbolTreeNode stn in children)
            {
                foreach (Stroke s in stn.symbolData.segment.strokes)
                {
                    all_strokes.Add(s);
                }
            }
            foreach (Stroke s in all_strokes.OrderBy(s => int.Parse(s.stroke_id)))
            {
                InkML.Trace t = new InkML.Trace {
                    id = s.stroke_id, points = s.points
                };
                traces.Add(s.stroke_id, t);
                inkml.traces.Add(t);
            }
            foreach (SymbolTreeNode stn in children)
            {
                InkML.TraceGroup tg = new InkML.TraceGroup();
                foreach (var s in stn.symbolData.segment.strokes)
                {
                    tg.trace_views.Add(traces[s.stroke_id]);
                }
                tg.truth       = stn.nodeType;
                tg.mathml_href = stn.id;

                inkml.trace_groups.Add(tg);
                // inkml.classification_attributes.Add(
                // just add to one mathml row for now

                //inkml.mathml_expression.mathml += "<mi>" + tg.truth + "</mi>";
            }

            inkml.mathml_expression.mathml = ToMathML();
            inkml.annotations["UI"]        = ui_annotation;

            return(inkml.ToInkML(null));
        }
Example #3
0
        public LBT(InkML in_data, AdjacentCriterion adjacent)
        {
            raw_data = in_data;
            // sort boxes by left hand extent
            // starting from left, move right and evaluate which strokes have something to the left
            // use filtering function to evaluate if is valid
            strokes        = new List <Stroke>();
            stroke_to_node = new Dictionary <Stroke, LBTNode>();

            foreach (InkML.Trace tr in raw_data.traces)
            {
                strokes.Add(tr.ToStroke());
            }

            GraphFromStrokeList(this, strokes, adjacent);
        }
Example #4
0
        /// <summary>
        /// Populates a new InkML object from a .inkml file.
        /// </summary>
        /// <param name="file">
        /// A <see cref="System.String" /> containing the location of the file to read.
        /// </param>
        /// <returns>
        /// An <see cref="InkML" /> object, populated with data from the file.
        /// </returns>
        /// <remarks>
        /// - The stroke data contained in the segment objects created by this method are
        /// references to the corresponding stroke objects in the stroke collection.
        /// </remarks>
        public static InkML NewFromFile(string file)
        {
            if (!File.Exists(file))
            {
                return(null);
            }

            InkML obj = new InkML();

            obj.filename = System.IO.Path.GetFileName(file);

            XDocument doc = XDocument.Load(file);

            // pull out annotations
            foreach (var el in doc.Element(_ns + "ink").Elements(_ns + "annotation"))
            {
                string type = el.Attribute("type").Value;
                string val  = el.Value;

                obj.annotations[type] = val;
            }

            // get traces
            foreach (var el in doc.Element(_ns + "ink").Elements(_ns + "trace"))
            {
                try
                {
                    Trace trace = new Trace();
                    trace.id = el.Attribute("id").Value;

                    string raw_coords = el.Value;
                    foreach (string raw_coordpair in raw_coords.Split(new char[] { ',' }))
                    {
                        string[] coordpair = raw_coordpair.Trim().Split(new char[] { ' ' });
                        float    x_coord   = float.Parse(coordpair[0].Trim());
                        float    y_coord   = float.Parse(coordpair[1].Trim());
                        trace.points.Add(new Vector2(x_coord, y_coord));
                    }

                    obj.traces.Add(trace);
                    obj.trace_id_to_trace[trace.id] = trace;
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine(e.ToString());
                }
            }

            // get trace groups
            var trace_group_elements = doc.Element(_ns + "ink").Elements(_ns + "traceGroup");

            if (trace_group_elements.Count() > 0)
            {
                var trace_groups = trace_group_elements.Last();
                foreach (var trace_group in trace_groups.Elements())
                {
                    TraceGroup tg = new TraceGroup();
                    tg.truth = (from c in trace_group.Elements(_ns + "annotation")
                                where c.Attribute("type").Value.Equals("truth")
                                select c.Value).LastOrDefault();


                    tg.mathml_href = (from c in trace_group.Elements(_ns + "annotationXML")
                                      select c.Attribute("href").Value).LastOrDefault();



                    if (tg.truth == null || tg.mathml_href == null)
                    {
                        continue;
                    }
                    obj.mathml_id_to_tracegroup.Add(tg.mathml_href, tg);
                    foreach (var trace_view in trace_group.Elements(_ns + "traceView"))
                    {
                        try
                        {
                            string trace_data_ref = trace_view.Attribute("traceDataRef").Value;
                            tg.trace_views.Add(obj.trace_id_to_trace[trace_data_ref]);
                            obj.trace_id_to_tracegroup[trace_data_ref] = tg;

                            //obj.stroke_truths.Add( trace_data_ref, segment.ann_truth );
                        }
                        catch (Exception e)
                        {
                            Console.Error.WriteLine(e.ToString());
                        }
                    }
                    obj.trace_groups.Add(tg);
                }
            }

            // get mathml
            var mathml = doc.Descendants(_mathml_ns + "math").FirstOrDefault();

            if (mathml != null)
            {
                obj.mathml_expression = new MathML {
                    mathml = mathml.ToString()
                };
            }

            return(obj);
        }