Beispiel #1
0
        internal async Task RespondWithAnswer(String[] dataset, String start = "", String user = "")
        {
            int chainLength = 2;

            if (IsNumber(start) && start != "")
            {
                chainLength = int.Parse(start);
                start       = "";
            }
            StringMarkov model = new StringMarkov(chainLength);

            model.Learn(dataset);
            Random  rnd1          = new Random();
            String  sentence      = "";
            int     startSpawn    = 0;
            Boolean acceptable    = false;
            int     totalAttempts = 0;

            while (sentence.Split('c').Length < 4 && !acceptable)
            {
                acceptable = false;
                sentence   = start;
                int i = 0;
                startSpawn += 1;
                while (i < 30)
                {
                    try
                    {
                        String[] newWord      = model.GetMatches(sentence.Trim()).ToArray();
                        Double   len          = Convert.ToDouble(newWord.Length - 1);
                        Double   rnd          = Convert.ToDouble(rnd1.NextDouble());
                        Double   decision     = Math.Floor(rnd * len);
                        String   selectedWord = newWord[Convert.ToInt32(decision)];
                        if (selectedWord == null)
                        {
                            break;
                        }
                        sentence = sentence + " " + selectedWord;

                        i += 1;
                    }
                    catch (Exception exc)
                    {
                        i              = 31;
                        totalAttempts += 1;
                        if (totalAttempts > 1000)
                        {
                            await ReplyAsync("Sorry, no can do - " + user + " doesn't talk much");

                            return;
                        }
                    }
                    totalAttempts += 1;
                    if (totalAttempts > 1000)
                    {
                        await ReplyAsync("Sorry, no can do - " + user + " doesn't talk much");

                        return;
                    }
                }
                totalAttempts += 1;
                if (totalAttempts > 1000)
                {
                    await ReplyAsync("Sorry, no can do - " + user + " doesn't talk much");

                    return;
                }
                if (!dataset.Contains(sentence.Trim(' ')))
                {
                    acceptable = true;
                }
            }
            await ReplyAsync("```" + sentence + "``` - " + user);
        }