示例#1
0
        public int Predict(string input)
        {
            var newX       = TextClassificationProblemBuilder.CreateNode(input, vocabulary);
            var predictedY = model.Predict(newX);

            return((int)predictedY);
        }
示例#2
0
        private SvmMethod()
        {
            var           path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Files/SentimentAnalysisDataset.csv");
            List <string> x    = new List <string>();

            List <double> y = new List <double>();

            if (File.Exists(path))
            {
                var lines = File.ReadAllLines(path);
                for (int i = 0; i < 500; i++)//5146
                {
                    var lineArr = lines[i].Split(new string[] { ",Sentiment140,", ",Kaggle," }, StringSplitOptions.None);
                    y.Add(double.Parse(lineArr[0].Split(',')[1]));
                    x.Add(lineArr[1].Trim());
                }
            }

            //var dataTable = DataTable.New.ReadCsv(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Files/spamdata.csv"));
            //List<string> x = dataTable.Rows.Select(row => row["Text"]).ToList();
            //double[] y = dataTable.Rows.Select(row => double.Parse(row["IsSpam"])).ToArray();

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

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

            const int C = 1;

            model = new C_SVC(problem, KernelHelper.LinearKernel(), C);

            _predictionDictionary = new Dictionary <int, string> {
                { 0, "negative" }, { 1, "positive" }
            };
        }
示例#3
0
        public void Create_Train_SVMmodel(string path_dataCSV_trainning, double C)
        {
            var           dataTable = DataAccess.DataTable.New.ReadCsv(path_dataCSV_trainning);
            List <string> x         = dataTable.Rows.Select(row => row["text"]).ToList();

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

            vocabulary = x.SelectMany(GetWords).Distinct().OrderBy(word => word).ToList();
            var problemBuilder = new TextClassificationProblemBuilder();
            var problem        = problemBuilder.CreateProblem(x, y.ToArray(), vocabulary.ToList());

            model = new C_SVC(problem, KernelHelper.LinearKernel(), C);
        }
示例#4
0
        public int Analyze(string message)
        {
            //var accuracy = model.GetCrossValidationAccuracy(10);
            //var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, string.Format("Files/model_{0}_accuracy.model", accuracy));
            //model.Export(path);

            var newX = TextClassificationProblemBuilder.CreateNode(message, vocabulary);

            var predictedY    = model.Predict(newX);
            var predictedProb = model.PredictProbabilities(newX);

            //return _predictionDictionary[(int)predictedY];
            return((int)predictedY > 0 ? 1 : -1);
        }