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); }
static void Main(string[] args) { Console.WriteLine("_LambHoot Discord Bot v1.4 Full nGram & James Bond_"); Console.WriteLine("_Discord Bot_"); MyBot lambhootBot = new MyBot(); //Console.WriteLine("_Partial BiGram_"); //PartialBiGram myPartialBiGram = new PartialBiGram(); //Console.WriteLine("_vocab done_"); //Word the = myPartialBiGram.vocabulary["the"]; //Word letter = myPartialBiGram.vocabulary["letter"]; //Word seven = myPartialBiGram.vocabulary["7/10."]; //Word game = myPartialBiGram.vocabulary["game"]; //Word same = myPartialBiGram.vocabulary["same"]; //List<Word> sentence = new List<Word>(); //sentence.Add(the); //var low = letter.probabilityGivenSentence(sentence); //var high = game.probabilityGivenSentence(sentence); //var high2 = same.probabilityGivenSentence(sentence); //var idk = the.probabilityGivenSentence(sentence); //sentence.Add(letter); //var low2 = seven.probabilityGivenSentence(sentence); //var a = letter.ProbabilityOfWordgivenB(the, 0); //var b = seven.ProbabilityOfWordgivenB(the, 1); //var c = game.ProbabilityOfWordgivenB(the, 0); //var d = same.ProbabilityOfWordgivenB(the, 0); //var e = the.ProbabilityOfWordgivenB(the, 0); //string s0 = myPartialBiGram.generateNewBiGramSentence("the"); //Console.WriteLine("_____"); //string s00 = myPartialBiGram.generateNewBiGramSentence("the"); //Console.WriteLine("_____"); //string s1 = myPartialBiGram.generateNewBiGramSentence("holy"); //Console.WriteLine("_____"); //string s2 = myPartialBiGram.generateNewBiGramSentence("astrology is"); //Console.WriteLine("_____"); //string s3 = myPartialBiGram.generateNewBiGramSentence("Denis is"); //Console.WriteLine("_____"); //string s4 = myPartialBiGram.generateNewBiGramSentence("applesauce"); //Console.WriteLine("_____"); //string s5 = myPartialBiGram.generateNewBiGramSentence(); //Console.WriteLine("_____"); //string s6 = myPartialBiGram.generateNewBiGramSentence(); //Console.WriteLine("_____"); //string s7 = myPartialBiGram.generateNewBiGramSentence(); //var x = 0; }
//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); }