示例#1
0
        public void DescendingStalenessSort()
        {
            var chatScores = new Dictionary <Chat, double>();

            chatScores.Add(Chat.Create("c1").Staleness(3.1), 1);
            chatScores.Add(Chat.Create("c2").Staleness(3), 1);
            chatScores.Add(Chat.Create("c3").Staleness(1.1), 1);
            chatScores.Add(Chat.Create("c4"), 0);
            chatScores.Add(Chat.Create("c5"), 10);

            var list = FuzzySearch.DescendingStalenessRandomizedSort(chatScores);

            for (int i = 1; i < list.Count; i++)
            {
                Assert.That(list[i].Value <= list[i - 1].Value, Is.True);
            }

            //list.ForEach((kvp) => Console.WriteLine(kvp.Key + " -> " + kvp.Value));

            var chats = (from kvp in list select kvp.Key).ToList();

            Assert.That(chats[0].text, Is.EqualTo("c5"));
            Assert.That(chats[1].text, Is.EqualTo("c3"));
            Assert.That(chats[2].text, Is.EqualTo("c2"));
            Assert.That(chats[3].text, Is.EqualTo("c1"));
            Assert.That(chats[4].text, Is.EqualTo("c4"));
        }
示例#2
0
        /// <summary>
        /// Clear all chats and reset the runtime state
        /// </summary>
        public void Reset(List <Chat> theChats = null)
        {
            this.resolver    = new Resolver(this);
            this.parser      = new ChatParser(this);
            this.search      = new FuzzySearch(this);
            this.scheduler   = new ChatScheduler(this);
            this.appEvents   = new AppEventHandler(this);
            this.chatEvents  = new ChatEventHandler(this);
            this.chats       = new Dictionary <string, Chat>();
            this.choiceCache = new Dictionary <string, Choice>();

            if (!theChats.IsNullOrEmpty())
            {
                AppendChats(theChats);
            }
        }
示例#3
0
        public void DescendingLastRunAtSort()
        {
            var chatScores = new Dictionary <Chat, double>();

            chatScores.Add(Chat.Create("c1").LastRunAt(Util.EpochMs()), 1);
            chatScores.Add(Chat.Create("c2").LastRunAt(Util.EpochMs()), 2);
            chatScores.Add(Chat.Create("c3").LastRunAt(Util.EpochMs()), 3);
            chatScores.Add(Chat.Create("c4").LastRunAt(Util.EpochMs()), 4);
            chatScores.Add(Chat.Create("c5").LastRunAt(Util.EpochMs()), 5);

            var chats = FuzzySearch.DescendingScoreLastRunAtRandomizedSort(chatScores);

            //chats.ForEach((obj) => Console.WriteLine(obj.Key.text));

            for (int i = 0; i < 5; i++)
            {
                Assert.That(chats[i].Key.text, Is.EqualTo("c" + (5 - i)));
            }

            ////////////////////////////////////////////////////////////////////

            chatScores = new Dictionary <Chat, double>();
            chatScores.Add(Chat.Create("c1").LastRunAt(Util.EpochMs()), 1);
            chatScores.Add(Chat.Create("c2").LastRunAt(Util.EpochMs()), 2);
            chatScores.Add(Chat.Create("c3").LastRunAt(Util.EpochMs()), 3);
            chatScores.Add(Chat.Create("c4").LastRunAt(Util.EpochMs()), 5);
            chatScores.Add(Chat.Create("c5").LastRunAt(Util.EpochMs() - 1), 5);

            chats = FuzzySearch.DescendingScoreLastRunAtRandomizedSort(chatScores);
            //chats.ForEach((obj) => Console.WriteLine(obj.Key.text));

            for (int i = 0; i < 5; i++)
            {
                Assert.That(chats[i].Key.text, Is.EqualTo("c" + (5 - i)));
            }

            ////////////////////////////////////////////////////////////////////

            chatScores = new Dictionary <Chat, double>();
            chatScores.Add(Chat.Create("c1").LastRunAt(Util.EpochMs()), 1);
            chatScores.Add(Chat.Create("c2").LastRunAt(Util.EpochMs()), 2);
            chatScores.Add(Chat.Create("c3").LastRunAt(Util.EpochMs()), 3);
            chatScores.Add(Chat.Create("c4").LastRunAt(Util.EpochMs()), 5);
            chatScores.Add(Chat.Create("c5").LastRunAt(-1), 5);

            chats = FuzzySearch.DescendingScoreLastRunAtRandomizedSort(chatScores);
            //chats.ForEach((obj) => Console.WriteLine(obj.Key.text));

            for (int i = 0; i < 5; i++)
            {
                Assert.That(chats[i].Key.text, Is.EqualTo("c" + (5 - i)));
            }

            //////////////////////////////////////////////////////////////////////

            chatScores = new Dictionary <Chat, double>();
            chatScores.Add(Chat.Create("c1").LastRunAt(Util.EpochMs()), 1);
            chatScores.Add(Chat.Create("c2").LastRunAt(Util.EpochMs()), 2);
            chatScores.Add(Chat.Create("c3").LastRunAt(Util.EpochMs()), 3);
            chatScores.Add(Chat.Create("c4").LastRunAt(-1), 5);
            chatScores.Add(Chat.Create("c5").LastRunAt(-1), 5);

            for (int j = 0; j < 10; j++) // repeat a few times
            {
                chats = FuzzySearch.DescendingScoreLastRunAtRandomizedSort(chatScores);
                //chats.ForEach(obj => Console.Write(obj.Key.text+ ", "));Console.WriteLine();
                Assert.That(chats[0].Key.text, Is.EqualTo("c5").Or.EqualTo("c4"));
                Assert.That(chats[0].Key.text, Is.EqualTo("c4").Or.EqualTo("c5"));
                Assert.That(chats[2].Key.text, Is.EqualTo("c3"));
                Assert.That(chats[3].Key.text, Is.EqualTo("c2"));
                Assert.That(chats[4].Key.text, Is.EqualTo("c1"));
            }

            //////////////////////////////////////////////////////////////////////

            chatScores = new Dictionary <Chat, double>();

            chatScores.Add(Chat.Create("c1").LastRunAt(Util.EpochMs()), 1);
            chatScores.Add(Chat.Create("c2").LastRunAt(Util.EpochMs() - 1000), 1);
            chatScores.Add(Chat.Create("c3").LastRunAt(Util.EpochMs() - 2000), 1);
            chatScores.Add(Chat.Create("c4"), 0);
            chatScores.Add(Chat.Create("c5"), 10);

            chats = FuzzySearch.DescendingScoreLastRunAtRandomizedSort(chatScores);
            for (int i = 0; i < chats.Count; i++)
            {
                //Console.WriteLine(chats[i].Key + "(" + chats[i].Key.lastRunAt + ") -> " + chats[i].Value);
                if (i + 1 < chats.Count)
                {
                    Assert.That(chats[i + 1].Value <= chats[i].Value, Is.True);
                }
            }

            Assert.That(chats[0].Key.text, Is.EqualTo("c5"));
            Assert.That(chats[1].Key.text, Is.EqualTo("c3"));
            Assert.That(chats[2].Key.text, Is.EqualTo("c2"));
            Assert.That(chats[3].Key.text, Is.EqualTo("c1"));
            Assert.That(chats[4].Key.text, Is.EqualTo("c4"));
        }