//DOCUMENT ME!!! /// <summary> Generates a new sentence from the analyzed words and tags. </summary> public string generate() { //Eventually choose a tense based on the previous sentences tense... string selectedTense = verbTenses [Program.rndm.Next(verbTenses.Length)]; List <Word> generatedPhrase = new List <Word> (); string[] sentenceModel = Markov.writeSyntax(); if (Program.showAnalysis) { Console.Write("SYNTAX: "); foreach (string syn in sentenceModel) { Console.Write("{0} ", syn); } Console.WriteLine(); } bool printedSubject = false; foreach (string syntaxType in sentenceModel) { if ((syntaxType == "name" || syntaxType == "pronoun" || syntaxType == "noun") && !printedSubject) { if (subjectFollowsTags()) { generatedPhrase.Add(subject); } else { generatedPhrase.Add(wordThatFollowsTags(syntaxType)); if (Program.showAnalysis) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("FAIL: Subject did not have enough to do with the sentence!"); Console.ForegroundColor = Program.colour; } } printedSubject = true; } else { generatedPhrase.Add(wordThatFollowsTags(syntaxType)); } } string phrase = ""; for (int i = 0; i < generatedPhrase.Count; i++) { if (sentenceModel [i] == "Verb") { phrase += Conjugator.conjugate(generatedPhrase [i].name, selectedTense) + " "; } else { phrase += generatedPhrase [i].name + " "; } } return(phrase); }
public MarkovLine(string line) { int c = 0; key = ""; while (line [c] != ':') { key += line [c]; c++; } line = line.Remove(0, key.Length + 1); possibleFollowings = Markov.arrayFromLine(line); }
/// <summary> Guess the syntax of the sentence based on known words... </summary> public string[] detectSyntax() { string syntaxTypes = "article pronoun adjective adverb noun Verb preposition conjunction exclamation name"; string[] syntax = new string[words.Length]; for (int i = 0; i < syntax.Length; i++) { syntax [i] = "???"; } for (int i = 0; i < words.Length; i++) { List <string> syntaxTags = new List <string> (); string substring = ""; for (int ind = 0; ind < words[i].tags.Length; ind++) { string wordStops = " .,:;?!"; if (wordStops.Contains(words[i].tags [ind].ToString()) && substring != "") { if (syntaxTypes.Contains(substring)) { syntaxTags.Add(substring); } substring = ""; } else if (ind == words[i].tags.Length - 1) { substring += words[i].tags [ind]; if (syntaxTypes.Contains(substring)) { syntaxTags.Add(substring); } substring = ""; } else { if (!wordStops.Contains(words [i].tags [ind].ToString())) { substring += words[i].tags [ind]; } } } //Decide what syntaxType should go at this position int tries = 0; while (syntaxTags.Count > 1) { if (i > 0 && syntaxTags.Contains("noun") && syntax [i - 1] == "article") { syntaxTags.Clear(); syntaxTags.Add("noun"); } else if (i > 0 && syntaxTags.Contains("Verb") && syntax [i - 1] == "adverb") { syntaxTags.Clear(); syntaxTags.Add("Verb"); } else if (i > 0 && syntaxTags.Contains("adjective") && syntax [i - 1] == "adjective") { syntaxTags.Remove("adjective"); } else if (i > 0 && syntaxTags.Contains("adverb") && syntax [i - 1] == "adjective") { syntaxTags.Remove("adverb"); } else if (i > 0 && syntaxTags.Contains("Verb") && syntax [i - 1] == "Verb") { syntaxTags.Remove("Verb"); } else if (syntaxTags.Contains("name") && syntaxTags.Contains("noun")) { syntaxTags.Remove("name"); } else if (i == 0 && syntaxTags.Contains("adjective")) { syntaxTags.Clear(); syntaxTags.Add("adjective"); } //to avoid infinite loops tries++; if (tries > syntaxTags.Count) { Log.Write("Sentence.cs", "BUG", "Failed to narrow down syntax for word: " + words [i].name); break; } } syntax [i] = syntaxTags [0]; } Markov.readSyntax(syntax); return(syntax); }