//********************************************************************************************** // BuildSpecificRule // // Starting with a generic size char rule, and a word with identifiers (which also determines // the rule size to build), build the specific rule. // Example: // Rule: Hello #001# -> #001# , growleft empty, growright #002# // Varword: #004##005##006# // Output patterns: Hello #004##005##006# -> #004##005##006# //********************************************************************************************** private static void BuildSpecificRule(GenericRule rule, string varWord, out string inputPattern, out string outputPattern) { int ruleSize = varWord.Length; int nextId = 900; string id = CharGenericRules.GenerateId(nextId++); string inputIdWord = id; outputPattern = rule.Output.Replace(rule.Id1, id); // Grow the rule as needed for (int i = 2; i <= ruleSize; i++) { id = CharGenericRules.GenerateId(nextId++); inputIdWord = inputIdWord + id; string growLeft = rule.GrowToLeft.Replace(rule.Id2, id); string growRight = rule.GrowToRight.Replace(rule.Id2, id); outputPattern = growLeft + outputPattern + growRight; } outputPattern += rule.Ending; inputPattern = rule.Input.Replace(rule.Id1, inputIdWord); }
//********************************************************************************************* // GetRule //********************************************************************************************* private GenericRule GetRule(string patternInput) { GenericRule ruleFound = null; // Find a matching rule in the list of rules foreach (GenericRule rule in rules) { if (patternInput == rule.Input) { ruleFound = rule; break; } } return(ruleFound); }
//********************************************************************************************** // SentenceMatchesPattern // // Verify whether a string matches the rule input string with factIDs inside. // With the twist of allowing rules of multiple word sizes. //********************************************************************************************** private static bool SentenceMatchesPattern(GenericRule rule, string inputText, bool useAllInput, out string varWord) { varWord = string.Empty; // If it has end of sentence character, take it out -we don't want it tied to the last word. string empty = rule.Ending; Syntax.RemoveEndCharIfPresent(ref inputText, ref empty, rule.Ending.Trim()); Dictionary <string, char> seenIDs = new Dictionary <string, char>(); // Separate all the strings into words string[] inputWords = Syntax.GetWords(inputText); string[] patternWords = Syntax.GetWords(rule.Input); // Check the input sentences against the pattern // Not the same number of words in the line, not match -if we are supposed to use all input if (useAllInput && inputWords.Length != patternWords.Length) { return(false); } // Either way, the input has to be at least the size of the pattern if (inputWords.Length < patternWords.Length) { return(false); } bool match = true; for (int word = 0; word < patternWords.Length && match; word++) { // It is not a variable word, and it is not the same, not match if (!CharGenericRules.IsIdWord(patternWords[word])) { if (inputWords[word] != patternWords[word]) { match = false; } } else { // ##### // Is it a VARIABLE SIZE variable word? if (patternWords[word] == rule.Id1) { varWord = inputWords[word]; } else { // ##### // It is a variable word, check each of its characters int p = 0; for (int c = 0; c < inputWords[word].Length && match; c++) { string id = CharGenericRules.ExtractIdOrChar(patternWords[word], p); if (id.Length == 0) { // We ran out of pattern!! match = false; } else if (id.Length == 1) { // Not really an ID! Must match the input if (inputWords[word][c] != id[0]) { match = false; } } else { // It IS an ID - Check if the char has been seen before or is new if (seenIDs.ContainsKey(id)) { if (inputWords[word][c] != seenIDs[id]) { match = false; } } else { seenIDs.Add(id, inputWords[word][c]); } } p += id.Length; } // Input is shorter than pattern! if (p != patternWords[word].Length) { match = false; } } } } return(match); }