public static Strategy createDemoSingleBayesStrategy()
        {
            Console.WriteLine("Naive Bayes Classifier> Building up ....");

            List<Transmitter> tempT = new List<Transmitter>();
            Strategy tempS = new Strategy(0, tempT);

            tempT.Add(new ClassifierMLBayes(ClassifierLearnableBayesType.Naive));
            tempT.Add(new Speaker("normal", tempS)); // 1
            tempT.Add(new Speaker("spam", tempS));  // 2

            tempS.connectClassifier(0, 1, "GOOD");
            tempS.connectClassifier(0, 2, "BAD");

            // 训练过程
            ClassifierMLBayes c0 = (ClassifierMLBayes)tempT[0];
            c0._filter = new FilterToLowcase();
            c0._detector = new DetectorSpace();

            c0.train("Nobody owns the water", "GOOD");
            c0.train("the quick rabbit jumps fences", "GOOD");
            c0.train("the quick brown fox jumps", "GOOD");
            c0.train("buy pharmaceuticals now!", "BAD");
            c0.train("make quick money at the online casino.", "BAD");
            c0.showModelInfo(null, true);
            //teachWithFile(c0, "../Data/DP/TrainData.txt");

            return tempS;
        }
        public static Strategy createDemoNormalStrategy()
        {
            Console.WriteLine("Normal Classifier > Building up ....");

            List<Transmitter> tempT = new List<Transmitter>();
            Strategy tempS = new Strategy(0, tempT);

            tempT.Add(new ClassifierHaveNumber());
            tempT.Add(new ClassifierLength10());
            tempT.Add(new ClassifierUppercase());
            tempT.Add(new Speaker("Category01", tempS));
            tempT.Add(new Speaker("Category02", tempS));
            tempT.Add(new Speaker("Category03", tempS));

            tempS.connectClassifier(0, 1, "HaveNumber");
            tempS.connectClassifier(0, 2, "NoNumber");
            tempS.connectClassifier(1, 3, "Length>10");
            tempS.connectClassifier(1, 4, "Length<=10");
            tempS.connectClassifier(2, 4, "HaveUppercase");
            tempS.connectClassifier(2, 5, "NoUppercase");

            return tempS;
        }
Example #3
0
 public void add(Strategy strategy, float weight)
 {
     _strategies.Add(strategy);
     _weights.Add(weight);
 }
Example #4
0
        protected Strategy _strategy; // 自己汇报的strategy

        #endregion Fields

        #region Constructors

        public Speaker(string finalCategory, Strategy strategy)
        {
            _finalCategory = finalCategory;
            _strategy = strategy;
        }
        public static void testBinary(Strategy strategy, string testFileName)
        {
            DateTime dt = DateTime.Now;
            Console.WriteLine("start testing ...");

            StreamReader sr = new StreamReader(testFileName);

            int[] whiteReal = { 0, 0 };
            int[] blackReal = { 0, 0 };

            string line = "";
            while (!string.IsNullOrEmpty(line = sr.ReadLine()))
            {
                if (line.Equals("1") || line.Equals("GOOD")) // is White
                {
                    line = sr.ReadLine();

                    if (strategy.judgeItem(line).Equals("GOOD"))
                    {
                        whiteReal[0]++;
                    }
                    else // SPAM
                    {
                        whiteReal[1]++;
                    }
                }
                else // is Black
                {
                    line = sr.ReadLine();
                    if (strategy.judgeItem(line).Equals("GOOD"))
                    {
                        blackReal[0]++;
                    }
                    else // SPAM
                    {
                        blackReal[1]++;
                    }
                }
            }
            sr.Close();

            Console.WriteLine("stop testing ...");
            Console.WriteLine(DateTime.Now - dt);

            StringBuilder sb = new StringBuilder(strategy._entrance);

            sb.Append("\r\n");
            sb.Append(testFileName);
            sb.Append("\r\n");
            sb.Append("----------------------------------------\r\n");
            sb.Append("Man\\Com\tWhite\tBlack\tTotal\tRecall\r\n");

            sb.Append(string.Format("White\t{0}\t{1}\t{2}\t{3:P}\r\n",
                whiteReal[0],
                whiteReal[1],
                whiteReal[0] + whiteReal[1],
                (float)(whiteReal[0]) / (whiteReal[0] + whiteReal[1])));

            sb.Append(string.Format("Black\t{0}\t{1}\t{2}\t{3:P}\r\n",
                blackReal[0],
                blackReal[1],
                blackReal[0] + blackReal[1],
                (float)(blackReal[1]) / (blackReal[0] + blackReal[1])));

            sb.Append(string.Format("Total\t{0}\t{1}\t{2}\r\n",
                whiteReal[0] + blackReal[0],
                whiteReal[1] + blackReal[1],
                whiteReal[0] + blackReal[0] + whiteReal[1] + blackReal[1]));

            sb.Append(string.Format("Prcison\t{0:P}\t{1:P}\r\n",
                (float)whiteReal[0] / (whiteReal[0] + blackReal[0]),
                (float)blackReal[1] / (whiteReal[1] + blackReal[1])));

            sb.Append("\n");
            sb.Append(string.Format("[Precision:{0:P}]\r\n",
                (float)blackReal[1] / (whiteReal[1] + blackReal[1])));

            sb.Append(string.Format("[Recall:{0:P}]\r\n",
                (float)(blackReal[1]) / (blackReal[0] + blackReal[1])));

            sb.Append("----------------------------------------\r\n");

            Console.WriteLine(sb);
            StreamWriter sw = new StreamWriter("../Data/Report.txt", true);
            sw.Write(sb);
            sw.Close();
        }