示例#1
0
        public void TestEvaluate()
        {
            using (var bayes = new BFEngine(new ConsolePlatformServices()))
            {
                // Train 5 times to reach MinTokenOccurrence.
                bayes.TrainAsGood($"a b c d e f g h i j k l m n o p");
                bayes.TrainAsGood($"a b c d e f g h i j k l m n o p");
                bayes.TrainAsGood($"a b c d e f g h i j k l m n o p");
                bayes.TrainAsGood($"a b c d e f g h i j k l m n o p");
                bayes.TrainAsGood($"a b c d e f g h i j k l m n o p");

                // Train 5 times to reach MinTokenOccurrence.
                bayes.TrainAsBad("q r s t u v w x y z");
                bayes.TrainAsBad("q r s t u v w x y z");
                bayes.TrainAsBad("q r s t u v w x y z");
                bayes.TrainAsBad("q r s t u v w x y z");
                bayes.TrainAsBad("q r s t u v w x y z");

                Assert.AreEqual(bayes.GoodEventCount, 5);
                Assert.AreEqual(bayes.BadEventCount, 5);
                Assert.AreEqual(bayes.TokenCount, 26);

                string test;
                double val;

                test = "a";
                Assert.AreEqual(0, bayes.GetBadProbability(test));

                test = "A";
                Assert.AreEqual(0, bayes.GetBadProbability(test));

                test = "z";
                Assert.AreEqual(1, bayes.GetBadProbability(test));

                test = "a z";
                Assert.AreEqual(0.5, bayes.GetBadProbability(test));

                test = "A Z";
                Assert.AreEqual(0.5, bayes.GetBadProbability(test));

                test = "a b z";
                val = bayes.GetBadProbability(test);
                Assert.AreEqual(1.0 / 3.0, val, double.Epsilon);

                test = "a y z";
                val = bayes.GetBadProbability(test);
                Assert.AreEqual(2.0 / 3.0, val, double.Epsilon);

                bayes.AutoTrain = true; // Only happens if result passes threshold test.
                test = "a b c d e f g h i j k l m n o p q";
                val = bayes.GetBadProbability(test);
                Assert.IsTrue(val > 0.06 && val < 0.07); // 0.06...
                val = bayes.GetBadProbability(test);
                Assert.IsTrue(val > 0.05 && val < 0.06); // 0.05...
                val = bayes.GetBadProbability(test);
                Assert.IsTrue(val > 0.04 && val < 0.05); // 0.04...
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            using (var bayes = new BFEngine(new ConsolePlatformServices()))
            {
                // Train 5 times to reach MinTokenOccurrence.
                bayes.TrainAsGood($"a b c d e f g h i j k l m n o p");
                bayes.TrainAsGood($"a b c d e f g h i j k l m n o p");
                bayes.TrainAsGood($"a b c d e f g h i j k l m n o p");
                bayes.TrainAsGood($"a b c d e f g h i j k l m n o p");
                bayes.TrainAsGood($"a b c d e f g h i j k l m n o p");

                // Train 5 times to reach MinTokenOccurrence.
                bayes.TrainAsBad("q r s t u v w x y z");
                bayes.TrainAsBad("q r s t u v w x y z");
                bayes.TrainAsBad("q r s t u v w x y z");
                bayes.TrainAsBad("q r s t u v w x y z");
                bayes.TrainAsBad("q r s t u v w x y z");

                Console.WriteLine($"Good events: {bayes.GoodEventCount} | Bad events: {bayes.BadEventCount} | Token Count: {bayes.TokenCount}.");

                string test;
                test = "a b c d e f g h i j k l m n o p q r s t u v w x y z"; // 26
                //test = "a b c d e f g h i j p q r s"; // 14
                //test = "a b c d e f g h i j p q r s t"; // 15
                Console.WriteLine($"Evaluate \"{test}\": {bayes.GetBadProbability(test):0.00}");

                test = "a b c d e f g h i j k l m n o p q aa";
                bayes.AutoTrain = true; // Only happens if result passes threshold test.
                Console.WriteLine($"Evaluate \"{test}\": {bayes.GetBadProbability(test):0.00}");
                Console.WriteLine($"Evaluate \"{test}\": {bayes.GetBadProbability(test):0.00}");
                Console.WriteLine($"Evaluate \"{test}\": {bayes.GetBadProbability(test):0.00}");

                //SaveLoadTest(bayes);

                // Perf test.
                //RunPerfTest(bayes, test);

                Console.WriteLine("Press any key to continue . . .");
                Console.ReadKey(true);
            }
        }
示例#3
0
        private static void RunPerfTest(BFEngine bayes, string test)
        {
            int length = 10000;

            var watch = Stopwatch.StartNew();
            for (int i = 0; i < length; i++)
            {
                double prob = bayes.GetBadProbability(test);
            }
            watch.Stop();
            Console.WriteLine($"Ellapsed: {watch.ElapsedMilliseconds} ms.");
        }