public void AddSuffix(SuffixWord suf) { int index = suffixes.FindIndex(s => s.value == suf.value); if (index != -1) { suffixes[index].multiplicity++; } else { suffixes.Add(suf); } }
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("*")); }
public PreSuffixWordEntry(string pre, SuffixWord suf) { prefix = pre; AddSuffix(suf); }