public string GenerateStory() { string result = ""; int wordCount = 0; PrefixWord currentPrefix = new PrefixWord(2); SuffixWord currentSuffix; do { currentSuffix = table.GetSuffixOf(currentPrefix); if (currentSuffix.value != "*") { result += currentSuffix.value + " "; } else { result += '\n'; } currentPrefix.Add(currentSuffix.value); wordCount++; } while (/*currentSuffix.value != "*" && */ wordCount < maxWords); return(result); }
void AddToDatabase(PrefixWord prefix, SuffixWord suffix) { string prefixValue = prefix.GetConcatenatedPrefix(); if (prefixValue == "*|*" && suffix.value == "*") { // Don't add [*|* -> *] return; } int index = table.database.FindIndex(p => (p.prefix == prefixValue)); if (index != -1) { table.database[index].AddSuffix(suffix); } else { table.database.Add(new PreSuffixWordEntry(prefixValue, suffix)); } }
public void BuildDatabase() { PrefixWord currentPrefix = new PrefixWord(2); char[] delimiterChars = { ' ', '\t', '\n', '\r' }; string[] words = sample.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries); //string pattern = @"\s?"; //string[] words = Regex.Split (sample, pattern); for (int i = 0; i <= words.Length; i++) { string next = i < words.Length ? words[i] : "*"; SuffixWord currentSuffix = new SuffixWord(next); AddToDatabase(currentPrefix, currentSuffix); currentPrefix.Add(next); } SaveDatabase(); LoggerTool.Post("Finished building story database!"); }
public SuffixWord GetSuffixOf(PrefixWord prefix) { int index = database.FindIndex(p => (p.prefix == prefix.GetConcatenatedPrefix())); if (index != -1) { int multiplicitySum = database[index].suffixes.Select(m => m.multiplicity).ToArray().Sum(); int roll = Random.Range(0, multiplicitySum); int cumulative = 0; for (int i = 0; i < database[index].suffixes.Count; i++) { SuffixWord suffix = database[index].suffixes[i]; cumulative += suffix.multiplicity; if (roll < cumulative) { return(suffix); } } } return(new SuffixWord("*")); }