コード例 #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");

                bayes.GoodEventCount.Should().Be(5);
                bayes.BadEventCount.Should().Be(5);
                bayes.TokenCount.Should().Be(26);

                string test;
                double val;

                test = "a";
                bayes.GetBadProbability(test).Should().Be(0);

                test = "A";
                bayes.GetBadProbability(test).Should().Be(0);

                test = "z";
                bayes.GetBadProbability(test).Should().Be(1);

                test = "a z";
                bayes.GetBadProbability(test).Should().Be(0.5);

                test = "A Z";
                bayes.GetBadProbability(test).Should().Be(0.5);

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

                test = "a y z";
                val  = bayes.GetBadProbability(test);
                val.Should().BeApproximately(2.0 / 3.0, 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);
                val.Should().BeInRange(0.06, 0.07); // 0.06...
                val = bayes.GetBadProbability(test);
                val.Should().BeInRange(0.05, 0.06); // 0.05...
                val = bayes.GetBadProbability(test);
                val.Should().BeInRange(0.04, 0.05); // 0.04...
            }
        }
コード例 #2
0
        static async Task 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}");

                await SaveLoadTestAsync(bayes).ConfigureAwait(false);

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

                Console.WriteLine("Press any key to continue . . .");
                Console.ReadKey();
            }
        }
コード例 #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.");
        }