public void GenerationWithAllSubstitutions() { var g = new Grammar(); var subject = new Symbol("SUBJECT"); var animal = new Symbol("ANIMAL"); var person = new Symbol("PERSON"); g.AddSubstitution(new Substitution(subject, new Sentence("{ANIMAL}"))); g.AddSubstitution(new Substitution(subject, new Sentence("{PERSON}"))); g.AddSubstitution(new Substitution(animal, new Sentence("a dog"))); g.AddSubstitution(new Substitution(animal, new Sentence("a cat"))); g.AddSubstitution(new Substitution(person, new Sentence("John"))); var startSentence = new Sentence("This is {SUBJECT}."); var results = new string[100]; for (int i = 0; i < 100; i++) { var result = g.ApplyAllSubstitutions(startSentence); results[i] = result.FinalOutput; } Assert.True(results.Contains("This is a dog.")); Assert.True(results.Contains("This is a cat.")); Assert.True(results.Contains("This is John.")); }
public void AddSubstitution() { var g = new Grammar(); var subject = new Symbol("SUBJECT"); g.AddSubstitution(subject, new Sentence("John")); Assert.Equal(1, g.Symbols.Count); Assert.Equal(1, g.Substitutions.Count); Assert.Equal(subject, g.Substitutions[0].Symbol); }
public void SimpleGenerationWithVerticalBar() { var g = new Grammar(); var substitution = new Substitution(new Symbol("VERTICAL_BAR"), new Sentence("this is a vertical bar: ||.")); g.AddSubstitution(substitution); var startSentence = new Sentence("A vertical bar test: {VERTICAL_BAR}"); var result = g.ApplySubstitution(startSentence, substitution); Assert.Equal("A vertical bar test: this is a vertical bar: |.", result.FinalOutput); }
public void SimpleGenerationWithBrace() { var g = new Grammar(); var subject = new Symbol("SUBJECT"); var substitution = new Substitution(subject, new Sentence("John")); g.AddSubstitution(substitution); var startSentence = new Sentence("{SUBJECT} is a subject {{brace test: {{SUBJECT}}, {{HELLO!}}}}."); var result = g.ApplySubstitution(startSentence, substitution); Assert.Equal("John is a subject {brace test: {SUBJECT}, {HELLO!}}.", result.FinalOutput); }
public Grammar GrammarFromText(string content) { var result = new Grammar(); var helper = new SubstitutionHelper(); content = content.Replace("\r\n", "\n"); var lines = content.Split('\n'); var lineCount = 0; foreach (var line in lines) { lineCount++; if (string.IsNullOrWhiteSpace(line) || line.StartsWith("#")) { continue; } if (!line.Contains(":")) { throw new TinyGrammarException(string.Format("Error on line {0}: missing \":\".", lineCount)); } var symbolEndPosition = line.IndexOf(":"); var symbolName = line.Substring(0, symbolEndPosition); var sentenceExpression = line.Substring(symbolEndPosition + 1, line.Length - 1 - symbolEndPosition); sentenceExpression = helper.HandleSpecialCharacters(sentenceExpression); foreach (var altSentenceExpression in helper.GetAlternativeExpressions(sentenceExpression)) { result.AddSubstitution( helper.CleanupSymbolName(symbolName), helper.UnHandleSpecialCharacters(altSentenceExpression) ); } } return(result); }