private bool TrainPattern(EROIBW8 trainROI, ref EImageBW8 trainImage, EPatternType patternType, float lightBalance = 0, bool autoTransitionThickness = true, int transitionThickness = 6) { bool reSuccess = false; try { lightBalance = lightBalance > 1 ? 1 : lightBalance; lightBalance = lightBalance < -1 ? -1 : lightBalance; Base.PatternType = patternType; Base.LightBalance = lightBalance; Base.AutoTransitionThickness = autoTransitionThickness; Base.TransitionThickness = 6; Base.Learn(trainROI); Base.CopyLearntPattern(trainImage); reSuccess = true; } catch (EException exc) { StackFrame[] stackFrames = new StackTrace(true).GetFrames(); clsLogFile.LogTryCatch(stackFrames, exc.Message, true, true); } catch (Exception ex) { StackFrame[] stackFrames = new StackTrace(true).GetFrames(); clsLogFile.LogTryCatch(stackFrames, ex.Message, true, true); } return(reSuccess); }
private void ReadXmlDocument(XmlDocument xmlDoc) { XmlNode rootXmlNode = xmlDoc.DocumentElement; //make sure it is actually an xml node if (rootXmlNode == null || rootXmlNode.NodeType != XmlNodeType.Element) { return; } //eat up the name of that xml node var strElementName = rootXmlNode.Name; if ("bulletml" != strElementName) { //The first node HAS to be bulletml throw new Exception("Error reading \"" + Filename + "\": XML root node needs to be \"bulletml\", found \"" + strElementName + "\" instead"); } //Create the root node of the bulletml tree RootNode = new BulletMLNode(ENodeName.bulletml); //Read in the whole bulletml tree RootNode.Parse(rootXmlNode, null); //Find what kind of pattern this is: horizontal or vertical XmlNamedNodeMap mapAttributes = rootXmlNode.Attributes; if (mapAttributes == null) { return; } for (var i = 0; i < mapAttributes.Count; i++) { //will only have the name attribute var strName = mapAttributes.Item(i).Name; var strValue = mapAttributes.Item(i).Value; if ("type" == strName) { //if this is a top level node, "type" will be vertical or horizontal Orientation = StringToPatternType(strValue); } } }
public bool TrainPattern(EPatternType patternType, float lightBalance = 0) { bool reSuccess = false; try { reSuccess = TrainPattern(trainROI, ref trainImage, patternType, lightBalance); } catch (EException exc) { StackFrame[] stackFrames = new StackTrace(true).GetFrames(); clsLogFile.LogTryCatch(stackFrames, exc.Message, true, true); } catch (Exception ex) { StackFrame[] stackFrames = new StackTrace(true).GetFrames(); clsLogFile.LogTryCatch(stackFrames, ex.Message, true, true); } return(reSuccess); }
static void Main(string[] args) { string input = @"3+(2*4+5*(-2+4))"; //var tokenizer = new BasicTokenizer(); //var splitter = new BasicSplitter(); //splitter.CharacterCategories["operator"] = new HashSet<char>(new char[] //{ // '+', '-', '*', '/', '(', ')', '=', '.', '{', '}', '<', '>' //}); //splitter.CharacterCategories["eos"] = new HashSet<char>(new char[] //{ // ';' //}); //splitter.CharacterCategories["number"] = new HashSet<char>(Enumerable.Range(0, 10).Select(x => (char)('0' + x))); //splitter.CharacterCategories["name"] = new HashSet<char>(Enumerable.Range(0, ('z'-'a')).Select(x => (char)('a' + x))); //var tokens = tokenizer.Tokenize(input).ToArray(); //tokens = splitter.Split(tokens).ToArray(); int linenum = 0; int col = 0; var tokens = input.Select(value => { var res = ToToken(value, linenum, col); if (value == '\n') { linenum++; col = 0; } else { col++; } return(res); }); Console.WriteLine("Reading Grammar"); var productions = new List <Production>(); var lines = File.ReadAllLines("MathGrammar.txt"); foreach (var line in lines) { if (line.Trim().Length == 0 || line.Trim().StartsWith("#")) { continue; } var caretPosition = line.IndexOf("->"); var productionName = line.Substring(0, caretPosition).Trim(); int priority = 0; if (productionName.StartsWith("[")) { priority = int.Parse(productionName.Substring(1, productionName.IndexOf(']') - 1)); productionName = productionName.Substring(productionName.IndexOf(']') + 1).Trim(); } var spl = line.Substring(caretPosition + 2).Split(' ').Select(x => x.Trim()).Where(x => !string.IsNullOrEmpty(x)).ToArray(); var patterns = new StackItemPattern[spl.Length]; for (var i = 0; i < spl.Length; i++) { var value = spl[i]; EPatternType pType = EPatternType.Literal; if (value.StartsWith("{") && value.EndsWith("}")) { pType = EPatternType.Production; value = value.Substring(1, value.Length - 2); } else if (value.StartsWith("<") && value.EndsWith(">")) { pType = EPatternType.TokenType; value = value.Substring(1, value.Length - 2); } else if (value == "$$") { pType = EPatternType.EndOfInput; value = ""; } patterns[i] = new StackItemPattern(pType, value); } productions.Add(new Production(productionName, patterns) { Precedence = priority }); } foreach (var production in productions) { Console.WriteLine(production); } Console.WriteLine("Next: Build Parse Table [press enter]"); Console.ReadLine(); ParseTable parseTable = new ParseTable(productions); Console.WriteLine(parseTable); File.WriteAllText("output.txt", parseTable.ToString()); Console.ReadLine(); Console.WriteLine("Next: Parse"); Console.WriteLine(input); Console.ReadLine(); Parser p = new Parser(parseTable, tokens); var result = p.Step(); bool shouldSkip = false; while (result == null) { if (!shouldSkip) { PrintState(p.State); Console.WriteLine("(Type SKIP to skip parsing)"); shouldSkip = Console.ReadLine().ToUpper() == "SKIP"; } result = p.Step(); } PrintRecursive(result); Console.ReadLine(); }
/// <summary> /// Parses a bulletml document into this bullet pattern /// </summary> /// <param name="xmlFileName">Xml file name.</param> public bool ParseXML(string xmlFileName) { XmlReaderSettings settings = new XmlReaderSettings(); settings.DtdProcessing = DtdProcessing.Ignore; using (XmlReader reader = XmlReader.Create(xmlFileName, settings)) { //Open the file. XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(reader); XmlNode rootXmlNode = xmlDoc.DocumentElement; //make sure it is actually an xml node if (rootXmlNode.NodeType == XmlNodeType.Element) { //eat up the name of that xml node string strElementName = rootXmlNode.Name; if (("bulletml" != strElementName) || !rootXmlNode.HasChildNodes) { //The first node HAS to be bulletml Debug.Assert(false); return(false); } //Create the root node of the bulletml tree RootNode = new BulletMLNode(); //Read in the whole bulletml tree if (!RootNode.Parse(rootXmlNode, null)) { //an error ocurred reading in the tree return(false); } Debug.Assert(ENodeName.bulletml == RootNode.Name); //Find what kind of pattern this is: horizontal or vertical XmlNamedNodeMap mapAttributes = rootXmlNode.Attributes; for (int i = 0; i < mapAttributes.Count; i++) { //will only have the name attribute string strName = mapAttributes.Item(i).Name; string strValue = mapAttributes.Item(i).Value; if ("type" == strName) { //if this is a top level node, "type" will be veritcal or horizontal Orientation = StringToPatternType(strValue); } } } else { //should be an xml node!!! Debug.Assert(false); return(false); } } //grab that filename Filename = xmlFileName; return(true); }
/// <summary> /// Initializes a new instance of the <see cref="BulletMLLib.BulletPattern"/> class. /// </summary> public BulletPattern() { RootNode = null; Orientation = EPatternType.none; // default }
internal void ParseXmlStream(Stream xmlStream) { // initialise schema for validating the document if (schema == null) { using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("BulletMLLib.Content.bulletml.xsd")) { if (stream == null) { throw new InvalidBulletPatternException("Could not find XML schema."); } schema = XmlSchema.Read(stream, null); } } XmlReaderSettings settings = new XmlReaderSettings(); settings.DtdProcessing = DtdProcessing.Ignore; settings.ValidationType = ValidationType.Schema; settings.Schemas.Add(schema); using (XmlReader reader = XmlReader.Create(xmlStream, settings)) { try { //Open the file. XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(reader); XmlNode rootXmlNode = xmlDoc.DocumentElement; //make sure it is actually an xml node if (rootXmlNode.NodeType == XmlNodeType.Element) { //eat up the name of that xml node string strElementName = rootXmlNode.Name; if ("bulletml" != strElementName) { //The first node HAS to be bulletml throw new InvalidBulletPatternException("Root XML element should be '<bulletml>'."); } //Create the root node of the bulletml tree RootNode = new BulletMLNode(ENodeName.bulletml); //Read in the whole bulletml tree RootNode.Parse(rootXmlNode, null); //Find what kind of pattern this is: horizontal or vertical XmlNamedNodeMap mapAttributes = rootXmlNode.Attributes; for (int i = 0; i < mapAttributes.Count; i++) { //will only have the name attribute string strName = mapAttributes.Item(i).Name; string strValue = mapAttributes.Item(i).Value; if ("type" == strName) { //if this is a top level node, "type" will be veritcal or horizontal Orientation = StringToPatternType(strValue); } } } } catch (Exception ex) { //an error ocurred reading in the tree throw new InvalidBulletPatternException("Could not read XML.", ex); } } //validate that the bullet nodes are all valid try { RootNode.ValidateNode(); } catch (Exception ex) { //an error ocurred reading in the tree throw new InvalidBulletPatternException("XML did not validate.", ex); } }
public StackItemPattern(EPatternType type, string value) { PatternType = type; Value = value; }
public PatternInfo(string PatternId, EPatternType PatternType) { this.PatternId = PatternId; this.PatternType = PatternType; }
/// <summary> /// Parses a bulletml document into this bullet pattern /// </summary> /// <param name="xmlFileName">Xml file name.</param> public void ParseXML(string xmlFileName) { #if NETFX_CORE XmlReaderSettings settings = new XmlReaderSettings(); settings.DtdProcessing = DtdProcessing.Ignore; #else XmlReaderSettings settings = new XmlReaderSettings(); settings.ValidationType = ValidationType.None; settings.DtdProcessing = DtdProcessing.Parse; settings.ValidationEventHandler += new ValidationEventHandler(MyValidationEventHandler); #endif try { using (XmlReader reader = XmlReader.Create(xmlFileName, settings)) { //Open the file. XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(reader); XmlNode rootXmlNode = xmlDoc.DocumentElement; //make sure it is actually an xml node if (rootXmlNode.NodeType == XmlNodeType.Element) { //eat up the name of that xml node string strElementName = rootXmlNode.Name; if ("bulletml" != strElementName) { //The first node HAS to be bulletml throw new Exception("Error reading \"" + xmlFileName + "\": XML root node needs to be \"bulletml\", found \"" + strElementName + "\" instead"); } //Create the root node of the bulletml tree RootNode = new BulletMLNode(ENodeName.bulletml); //Read in the whole bulletml tree RootNode.Parse(rootXmlNode, null); //Find what kind of pattern this is: horizontal or vertical XmlNamedNodeMap mapAttributes = rootXmlNode.Attributes; for (int i = 0; i < mapAttributes.Count; i++) { //will only have the name attribute string strName = mapAttributes.Item(i).Name; string strValue = mapAttributes.Item(i).Value; if ("type" == strName) { //if this is a top level node, "type" will be veritcal or horizontal Orientation = StringToPatternType(strValue); } } } } } catch (Exception ex) { //an error ocurred reading in the tree throw new Exception("Error reading \"" + xmlFileName + "\"", ex); } //grab that filename Filename = xmlFileName; //validate that the bullet nodes are all valid try { RootNode.ValidateNode(); } catch (Exception ex) { //an error ocurred reading in the tree throw new Exception("Error reading \"" + xmlFileName + "\"", ex); } }