/// <summary> /// Capitalizes the first letter of the first word of the phrase, and converts any words that shouldn't be caplitalized to full lowercase. /// </summary> public static List <TemplateWord> EditCapitalization(List <TemplateWord> phrase) { List <TemplateWord> phraseCopy = phrase; if (phraseCopy.Count > 1) { bool foundFirstWord = false; TemplateWord firstWord = null; for (int i = 0; i < phraseCopy.Count && !foundFirstWord; i++) { Word word = phraseCopy[i].Word; if (!(word.Value.Length == 1 && (TemplateParser.separators.Contains(word.Value[0]) || TemplateParser.phraseSeparators.Contains(word.Value[0])))) { foundFirstWord = true; firstWord = phraseCopy[i]; } } if (firstWord != null) { firstWord.Word = new Word(CapitalizeFirstLetter(firstWord.Word.Value), firstWord.Word.PoS, firstWord.Word.Frequency); } } for (int i = 1; i < phraseCopy.Count; i++) { if (!capitalizedPosTags.Contains(phraseCopy[i].Word.PoS.Tag.ToLower())) { phraseCopy[i].Word = new Word(phraseCopy[i].Word.Value.ToLowerInvariant(), phraseCopy[i].Word.PoS, phraseCopy[i].Word.Frequency); } } return(phraseCopy); }
public override string ToString() { string result = ""; foreach (TemplatePhrase phrase in phrases) { result += "<"; for (int i = 0; i < phrase.words.Count; i++) { TemplateWord word = phrase.words[i]; result += word.ToString() + "{" + word.PoS + "}"; if (i < phrase.words.Count - 1) { result += "|"; } } result += ">"; } return(result); }
public void Populate(Dictionary <int, Word> populatedGroupWords, BigramDictionaryLookup bigramLookup, WordDictionaryLookup wordLookup) { foreach (TemplateWord word in words) { if (word.IsHook) { word.Word = null; } } List <int> indicesOfCurrentPassToExecute = new List <int>(); do { foreach (int index in indicesOfCurrentPassToExecute) { TemplateWord currentWord = words[index]; TemplateWord wordBefore = (index > 0 && !words[index - 1].NeedsHookPopulated) ? words[index - 1] : null; TemplateWord wordAfter = (index < words.Count - 1 && !words[index + 1].NeedsHookPopulated) ? words[index + 1] : null; bool populatedByGroup = false; if (currentWord.group != 0 && populatedGroupWords.Count > 0) { Word wordForGroup = null; if (populatedGroupWords.TryGetValue(currentWord.group, out wordForGroup)) { populatedByGroup = true; currentWord.Word = wordForGroup; } } if (!populatedByGroup) { if (wordBefore != null && wordAfter != null) { currentWord.Word = bigramLookup.GetRandomBetweenTwoWords(wordBefore.Word, currentWord.PoS, wordAfter.Word); } else if (wordBefore != null) { currentWord.Word = bigramLookup.GetRandomSecondWord(wordBefore.Word, currentWord.PoS); } else if (wordAfter != null) { currentWord.Word = bigramLookup.GetRandomFirstWord(currentWord.PoS, wordAfter.Word); } else { currentWord.Word = wordLookup.GetRandomWordByPOS(currentWord.PoS); } if (currentWord.group != 0) { populatedGroupWords.Add(currentWord.group, currentWord.Word); } } //TODO: yield return for coroutine implementation if needed } indicesOfCurrentPassToExecute.Clear(); bool areStillHooks = false; bool lastWordWasHook = false; for (int i = 0; i < words.Count; i++) { TemplateWord tWord = words[i]; bool currentWordIsHook = tWord.NeedsHookPopulated; areStillHooks |= currentWordIsHook; if (currentWordIsHook ^ lastWordWasHook) { if (currentWordIsHook && i > 0) { indicesOfCurrentPassToExecute.Add(i); } if (lastWordWasHook) { indicesOfCurrentPassToExecute.Add(i - 1); } } lastWordWasHook = currentWordIsHook; } if (areStillHooks && indicesOfCurrentPassToExecute.Count == 0) { int times = (int)(rand.NextDouble() * (words.Count - 1)) + 1; for (int i = 0; i < times; i++) { indicesOfCurrentPassToExecute.Add((int)(rand.NextDouble() * words.Count)); } } }while (indicesOfCurrentPassToExecute.Count != 0); }
public void AddWord(TemplateWord word) { words.Add(word); }