예제 #1
0
        private static void TrainingRecordData()
        {
            var mongoClient = new MongoClient("mongodb://*****:*****@"[^a-zA-Z]", " ").Trim().ToLowerInvariant().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Where(d => d.Count() < 20).ToArray();
            var trainingDataCollection     = database.GetCollection <ProcessInfoLabeledItem>("training_data");
            var records    = trainingDataCollection.Find(Builders <ProcessInfoLabeledItem> .Filter.Empty).ToList();
            var vocabulary = records.Select(c => c.Title + " " + c.Process).SelectMany(filter).Distinct().OrderBy(str => str).ToList();

            List <string> x = records.Select(item => item.Title + " " + item.Process).ToList();

            double[] y = records.Select(item => (double)item.Category).ToArray();

            var problemBuilder = new TextClassificationProblemBuilder();

            problemBuilder.RefineText = filter;
            var problem = problemBuilder.CreateProblem(x, y, vocabulary.ToList());

            const int C     = 1;
            var       model = new C_SVC(problem, KernelHelper.LinearKernel(), C);
            var       _predictionDictionary = new Dictionary <Karma, string> {
                { Karma.Bad, "Bad" }, { Karma.Good, "Good" }, { Karma.Neutral, "Neutral" }
            };

            var newXs = database.GetCollection <AppUsageRecord>("daily_records").Find(Builders <AppUsageRecord> .Filter.Eq(f => f.Id, AppUsageRecord.GetGeneratedId(DateTime.Now))).FirstOrDefault().ActiveApps.Select(c => c.Value).Select(c => c.MainWindowTitle + " " + c.ProcessName);

            foreach (var _x in newXs)
            {
                var newX       = TextClassificationProblemBuilder.CreateNode(_x, vocabulary, problemBuilder.RefineText);
                var predictedY = model.Predict(newX);
                Console.WriteLine($"For title {_x}");
                Console.WriteLine($"The prediction is {_predictionDictionary[(Karma)predictedY]}");
            }
        }
예제 #2
0
        private void TrainingData()
        {
            string        dateFilePath = Path.Combine(Directory.GetCurrentDirectory(), $"sunnyData.csv");
            var           dataTable    = DataTable.New.ReadCsv(dateFilePath);
            List <string> x            = dataTable.Rows.Select(row => row["Text"]).ToList();

            double[] y = dataTable.Rows.Select(row => double.Parse(row["IsSunny"])).ToArray();

            var vocabulary = x.SelectMany(GetWords).Distinct().OrderBy(w => w).ToList();

            var problemBuilder = new TextClassificationProblemBuilder();
            var problem        = problemBuilder.CreateProblem(x, y, vocabulary.ToList());

            const int C     = 1;
            var       model = new C_SVC(problem, KernelHelper.LinearKernel(), C);

            string userInput;
            var    _predictionDictionary = new Dictionary <int, string> {
                { -1, "Rainy" }, { 1, "Sunny" }
            };

            do
            {
                userInput = Console.ReadLine();
                var newX = TextClassificationProblemBuilder.CreateNode(userInput, vocabulary);

                var predictedY = model.Predict(newX);
                Console.WriteLine("The prediction is {0}", _predictionDictionary[(int)predictedY]);
                Console.WriteLine(new string('=', 50));
            } while (userInput != "quit");

            Console.WriteLine("");
        }