public float randomProbabilityForWord(int gn = 0) { float prob = (float)MyBot.randomDoubleRange(minWordAfterProbList[gn] * 0.8, maxWordAfterProbList[gn] * 1.2); // 0.8 and 1.2 here because we want an even distribution of chance for the case that a word has only 1 wordAfter return(prob); }
//THE IMPORTANT HEURISTIC METHOD (for random sentences only) public string nextChosenWord(int gn = 0) { //returns a word of possible //otherwise returns null string returnString = null; int maxLoopAttempts = 7; if (wordAfterLists[gn].Count() == 0) { return(returnString); } if (wordAfterLists[gn].Count() == 1 && (MyBot.randomDoubleRange(0, 100) > 50)) { return(returnString); } int currentBestIndex = -1; float currentSmallestDifference = 99; for (int i = 0; i < maxLoopAttempts; i++) { //pick the wordAfter with probability greater and closest to prob float prob = randomProbabilityForWord(gn); var x = this; for (int j = 0; j < wordAfterLists[gn].Count(); j++) { float thisDifference = wordAfterLists[gn][j].prob - prob; if (thisDifference < 0)//if lower, ignore { continue; } if (thisDifference < currentSmallestDifference) { currentSmallestDifference = thisDifference; currentBestIndex = j; } } if (currentBestIndex != -1) { returnString = wordAfterLists[gn][currentBestIndex].wordString; break; } } return(returnString); }
public int selectRandomIndex(List <IndexProbPair> list) { int index = 0; float sumProbs = 0; foreach (IndexProbPair ipp in list) { sumProbs += ipp.prob; } if (sumProbs == 0) { return(0); } while (index == 0) { index = (int)MyBot.randomDoubleRange(0, list.Count()); if (list[index].prob == 0) //&& sumProbs != 0 { index = 0; //try again } } return(list[index].index); }
public static float randomProbabilityForSentence() { float prob = (float)MyBot.randomDoubleRange(minVocabWordProb * 0.2, maxVocabWordProb * 1.2); return(prob); }
public Word selectRandomWord() { int index = (int)MyBot.randomDoubleRange(1, vocabulary.Count());//skips first testing word return(vocabulary.ElementAt(index).Value); }
public string generateNewBiGramSentence(string input = null) { List <List <Word> > sentences = new List <List <Word> >(); sentences.Add(new List <Word>());//first sentence string returnString = ""; //input handle if (input == null) { //choose first word sentences.Last().Add(selectRandomWord()); returnString += " " + sentences.Last().Last(); } else { //TODO: make it choose a good word from the input if possible string[] inputArray = input.Split(' '); string inputWord = inputArray.Last(); for (int i = 0; i < inputArray.Count(); i++) { if (vocabulary.ContainsKey(inputArray[i])) { sentences.Last().Add(vocabulary[inputArray[i]]); } else { sentences.Last().Add(new Word(inputArray[i])); } returnString += " " + sentences.Last().Last(); } } int allSentenceLength = (int)MyBot.randomDoubleRange(minSentenceLength, Math.Min(maxSentenceLength * 0.6, 25)); while (CountWordsOfSentences(sentences) < allSentenceLength) { int sentenceLength = (int)MyBot.randomDoubleRange(minSentenceLength, allSentenceLength); //loop to build sentence while (sentences.Last().Count() <= sentenceLength) { if (returnString.Last().Equals('.') || returnString.Last().Equals('!') || returnString.Last().Equals('?')) { if (sentences.Last().Count() == 1 && returnString.Last().Equals('.'))//if sentence is a single word { sentences.RemoveAt(sentences.Count() - 1); } sentences.Add(new List <Word>()); //add a new sentence sentences.Last().Add(selectRandomWord()); //start the new sentence returnString += " " + sentences.Last().Last(); break; } int bestIndex = 0; List <IndexProbPair> currentBestIndices = new List <IndexProbPair>(); int loopAttempts = 7; while (loopAttempts > 0) { for (int i = 0; i < vocabulary.Count(); i++) { float newProb = vocabulary.ElementAt(i).Value.probabilityGivenSentence(sentences.Last()); tryAddBestIndex(currentBestIndices, i, newProb); } bestIndex = selectRandomIndex(currentBestIndices); if (bestIndex == 0) { loopAttempts--; } else { break; } } //now add the word found if (bestIndex != 0)//it found a word other than the first default one { sentences.Last().Add(vocabulary.ElementAt(bestIndex).Value); returnString += " " + sentences.Last().Last(); } else { //it failed to find a proper word. add punctuation if necessary and choose new random word if (!Char.IsPunctuation(returnString.Last())) { returnString += (MyBot.randomDoubleRange(0, 100) > 50) ? "," : "."; } //add a comma or period to it since it failed, if the last character isn't already a punctuation sentences.Add(new List <Word>()); //add a new sentence sentences.Last().Add(selectRandomWord()); //start the new sentence returnString += " " + sentences.Last().Last(); break; } } } //sentence cleanup returnString = formatSentence(returnString); Console.WriteLine("NEW BAYES SENTENCE: " + returnString); return(returnString); }
//GENERATE NEW SENTENCE public string generateNewSentence(string input = null) { //build list of words List <Word> sentence = new List <Word>(); string returnString = ""; int sentenceLength = (int)MyBot.randomDoubleRange(minSentenceLength, maxSentenceLength * 0.6); if (input == null) { sentence.Add(selectVocabularyWord());//start random generation returnString += " " + sentence.Last(); } else//handle starting sentence from input { string[] inputArray = input.Split(' '); string inputWord = inputArray.Last(); if (!vocabulary.ContainsKey(inputWord)) { sentence.Add(selectVocabularyWord());//if not in vocab, just start random sentence } else { for (int i = 0; i < inputArray.Count() - 1; i++) { returnString += " " + inputArray[i]; } Word inputStartingWord = vocabulary[inputWord]; sentence.Add(inputStartingWord); returnString += " " + sentence.Last(); } } while (sentence.Count() < sentenceLength) { string newWordKey = sentence.Last().nextChosenWord(); if (newWordKey != null) { sentence.Add(vocabulary[newWordKey]); returnString += " " + sentence.Last(); } else { if (sentence.Count() > 0)//possibility that sentence has failed on first try, in which case, don't do this { if (!Char.IsPunctuation(returnString.Last())) { returnString += (MyBot.randomDoubleRange(0, 100) > 50) ? "," : "."; } //add a comma or period to it since it failed, if the last character isn't already a punctuation Word nextWord = null; while (nextWord == null) { nextWord = selectVocabularyWord(); } sentence.Add(selectVocabularyWord()); returnString += " " + sentence.Last(); } } } returnString = formatSentence(returnString); Console.WriteLine("NEW SENTENCE: " + returnString); return(returnString); }