public void read_test()
        {
            string basePath = NUnit.Framework.TestContext.CurrentContext.TestDirectory;

            MemoryStream file   = new MemoryStream(Encoding.Default.GetBytes(Resources.L1R_LR_a9a));
            TextReader   reader = new StreamReader(file);

            File.WriteAllText(Path.Combine(basePath, "svm.txt"), reader.ReadToEnd());

            #region doc_read
            // Let's say we have used LIBLINEAR to learn a linear SVM model that has
            // been stored in a text file named "svm.txt". We would like to load this
            // same model in .NET and use it to make predictions using C#.
            //
            // First, we use LibSvmModel.Load to load the LIBLINEAR model from disk:
            LibSvmModel model = LibSvmModel.Load(Path.Combine(basePath, "svm.txt"));

            // Now, we can use the model class to create the equivalent Accord.NET SVM:
            SupportVectorMachine svm = model.CreateMachine();

            // Now, we can use this machine normally, like as shown in the
            // examples in the Support Vector Machine documentation page.
            #endregion

            Assert.AreEqual(2, svm.NumberOfClasses);
            Assert.AreEqual(122, svm.NumberOfInputs);
            Assert.AreEqual(1, svm.Weights.Length);
            Assert.AreEqual(1, svm.SupportVectors.Length);
        }
Esempio n. 2
0
        public void ReadLinearMachineTest()
        {
            MemoryStream file = new MemoryStream(
                Encoding.Default.GetBytes(Resources.L1R_LR_a9a));

            LibSvmModel reader = LibSvmModel.Load(file);

            Assert.AreEqual(-1, reader.Bias);
            Assert.AreEqual(2, reader.Classes);
            Assert.AreEqual(123, reader.Dimension);
            Assert.AreEqual(2, reader.Labels.Length);
            Assert.AreEqual(+1, reader.Labels[0]);
            Assert.AreEqual(-1, reader.Labels[1]);
            Assert.AreEqual(LibSvmSolverType.L1RegularizedLogisticRegression, reader.Solver);
            Assert.AreEqual(null, reader.Vectors);
            Assert.AreEqual(123, reader.Weights.Length);



            for (int i = 0; i < a9a_weights.Length; i++)
            {
                Assert.AreEqual(a9a_weights[i], reader.Weights[i]);
            }


            var machine = reader.CreateMachine();

            Assert.IsNull(machine.SupportVectors);
            Assert.AreEqual(machine.Threshold, a9a_weights[0]);

            for (int i = 0; i < machine.Weights.Length; i++)
            {
                Assert.AreEqual(machine.Weights[i], a9a_weights[i + 1]);
            }
        }
        public void ReadWriteTest_a9a()
        {
            string basePath = NUnit.Framework.TestContext.CurrentContext.TestDirectory;

            MemoryStream file = new MemoryStream(Encoding.Default.GetBytes(Resources.L1R_LR_a9a));

            LibSvmModel model1   = LibSvmModel.Load(file);
            string      savePath = Path.Combine(basePath, "svm.txt");

            var svm1 = model1.CreateMachine();

            model1.Save(savePath);

            LibSvmModel model2 = LibSvmModel.Load(savePath);
            var         svm2   = model2.CreateMachine();

            LibSvmModel model3 = LibSvmModel.FromMachine(svm1);
            var         svm3   = model3.CreateMachine();

            model3.Solver = LibSvmSolverType.L1RegularizedLogisticRegression;

            string aPath = Path.Combine(basePath, "a.txt");
            string bPath = Path.Combine(basePath, "b.txt");
            string cPath = Path.Combine(basePath, "c.txt");

            model1.Save(aPath);
            model2.Save(bPath);
            model3.Save(cPath);

            string a = File.ReadAllText(aPath);
            string b = File.ReadAllText(bPath);
            string c = File.ReadAllText(cPath);

            Assert.AreEqual(a, b);
            Assert.AreEqual(a, c);


            Assert.AreEqual(svm1.Weights, svm2.Weights);
            Assert.AreEqual(svm1.SupportVectors, svm2.SupportVectors);
            Assert.AreEqual(svm1.Threshold, svm2.Threshold);

            Assert.AreEqual(svm1.Weights, svm3.Weights);
            Assert.AreEqual(svm1.SupportVectors, svm3.SupportVectors);
            Assert.AreEqual(svm1.Threshold, svm3.Threshold);
        }
        public void WriteLinearMachineTest_ExactCopy()
        {
            MemoryStream file = new MemoryStream(
                Encoding.Default.GetBytes(Resources.L1R_LR_a9a));

            LibSvmModel reader = LibSvmModel.Load(file);

            MemoryStream destination = new MemoryStream();

            reader.Save(destination);

            destination.Seek(0, SeekOrigin.Begin);

            TextReader textReader = new StreamReader(destination);

            string[] actual = textReader.ReadToEnd()
                              .Split(new[] { Environment.NewLine }, StringSplitOptions.None);

            string[] expected = Resources.L1R_LR_a9a
                                .Split(new[] { "\r\n" }, StringSplitOptions.None);

            Assert.AreEqual(expected.Length, actual.Length);

            for (int i = 0; i < 7; i++)
            {
                Assert.AreEqual(expected[i], actual[i]);
            }

            for (int i = 6; i < expected.Length; i++)
            {
                if (expected[i] == actual[i])
                {
                    continue;
                }

                double a = Double.Parse(expected[i], CultureInfo.InvariantCulture);
                double b = Double.Parse(actual[i], CultureInfo.InvariantCulture);

                Assert.AreEqual(a, b);
            }

            Assert.AreEqual(expected[expected.Length - 1], String.Empty);
        }
Esempio n. 5
0
        static void Main()
        {
            // Path to the folder with classifiers models
            var jarRoot = @"\Users\devir\OneDrive\Documents\Visual Studio 2015\Projects\ner";
            var classifiersDirecrory = jarRoot + @"\classifiers";

            // Loading 3 class classifier model
            var classifier = CRFClassifier.getClassifierNoExceptions(
                classifiersDirecrory + @"\english.muc.7class.distsim.crf.ser.gz");

            var s1 = " She got up this morning at 9:00 am and went to a shop to spend five dollars to buy a 50% off toothbrush.";


            var s2 = "Tell the latest on olympics from the New York.";

            Console.WriteLine("{0}\n", classifier.classifyToCharacterOffsets(s1));
            Console.WriteLine("{0}\n", classifier.classifyWithInlineXML(s1));

            //MUNCULIN NER SATU SATU
            string result = classifier.classifyWithInlineXML(s1);
            String substr1 = "TIME";
            String substr2 = "LOCATION";
            String substr3 = "PERSON";
            String substr4 = "ORGANIZATION";
            String substr5 = "MONEY";
            String substr6 = "Percent";
            String substr7 = "Date";
            string total1, total2, total3, total4, total5, total6, total7;

            //if (result.Contains(substr1))
            //{
            //    string[] hasiltime = GetStringInBetween("<TIME>", "</TIME>", result, false, false);
            //    string output_time = hasiltime[0];
            //    string next_time = hasiltime[1];
            //    total1 = output_time;
            //   // Console.WriteLine(output_time);
            //}
            //if (result.Contains(substr2))
            //{
            //    string[] hasillocation = GetStringInBetween("<LOCATION>", "</LOCATION>", result, false, false);
            //    string output_location = hasillocation[0];
            //    string next_loc = hasillocation[1];
            //    //Console.WriteLine(output_location);
            //    total2 = output_location;
            //}
            //if (result.Contains(substr3))
            //{
            //    string[] hasilperson = GetStringInBetween("<PERSON>", "</PERSON>", result, false, false);
            //    string output_person = hasilperson[0];
            //    string next_person = hasilperson[1];
            //    //Console.WriteLine(hasilperson);
            //    total3 = output_person;
            //}
            //if (result.Contains(substr4))
            //{
            //    string[] hasilORGANIZATION = GetStringInBetween("<ORGANIZATION>", "</ORGANIZATION>", result, false, false);
            //    string output_ORGANIZATION = hasilORGANIZATION[0];
            //    string next_ORGANIZATION = hasilORGANIZATION[1];
            //    //Console.WriteLine(output_ORGANIZATION);
            //    total4 = output_ORGANIZATION;
            //}
            //if (result.Contains(substr5))
            //{
            //    string[] hasilMONEY = GetStringInBetween("<MONEY>", "</MONEY>", result, false, false);
            //    string output_MONEY = hasilMONEY[0];
            //    string next_MONEY = hasilMONEY[1];
            //    // Console.WriteLine(output_MONEY);
            //    total5 = output_MONEY;
            //}
            //if (result.Contains(substr6))
            //{
            //    string[] hasilPercent = GetStringInBetween("<Percent>", "</Percent>", result, false, false);
            //    string output_Percent = hasilPercent[0];
            //    string next_Percent = hasilPercent[1];
            //    //Console.WriteLine(output_Percent);
            //    total6 = output_Percent;
            //}
            //if (result.Contains(substr7))
            //{
            //    string[] hasilDate = GetStringInBetween("<Date>", "</Date>", result, false, false);
            //    string output_Date = hasilDate[0];
            //    string next_Date = hasilDate[1];
            //    //Console.WriteLine(output_Date);
            //    total7 = output_Date;

            //}

            string[] hasiltime   = GetStringInBetween("<TIME>", "</TIME>", result, false, false);
            string   output_time = hasiltime[0];
            string   next_time   = hasiltime[1];

            total1 = output_time;
            //Console.WriteLine(output_time);

            string[] hasillocation   = GetStringInBetween("<LOCATION>", "</LOCATION>", result, false, false);
            string   output_location = hasillocation[0];
            string   next_loc        = hasillocation[1];

            //Console.WriteLine(output_location);
            total2 = output_location;

            string[] hasilperson   = GetStringInBetween("<PERSON>", "</PERSON>", result, false, false);
            string   output_person = hasilperson[0];
            string   next_person   = hasilperson[1];

            //Console.WriteLine(hasilperson);
            total3 = output_person;

            string[] hasilORGANIZATION   = GetStringInBetween("<ORGANIZATION>", "</ORGANIZATION>", result, false, false);
            string   output_ORGANIZATION = hasilORGANIZATION[0];
            string   next_ORGANIZATION   = hasilORGANIZATION[1];

            //Console.WriteLine(output_ORGANIZATION);
            total4 = output_ORGANIZATION;

            string[] hasilMONEY   = GetStringInBetween("<MONEY>", "</MONEY>", result, false, false);
            string   output_MONEY = hasilMONEY[0];
            string   next_MONEY   = hasilMONEY[1];

            // Console.WriteLine(output_MONEY);
            total5 = output_MONEY;

            string[] hasilPercent   = GetStringInBetween("<Percent>", "</Percent>", result, false, false);
            string   output_Percent = hasilPercent[0];
            string   next_Percent   = hasilPercent[1];

            //Console.WriteLine(output_Percent);
            total6 = output_Percent;

            string[] hasilDate   = GetStringInBetween("<Date>", "</Date>", result, false, false);
            string   output_Date = hasilDate[0];
            string   next_Date   = hasilDate[1];

            //Console.WriteLine(output_Date);
            total7 = output_Date;


            //BOW
            string semua = total1 + ";" + total2 + ";" + total3 + ";" + total4 + ";" + total5 + ";" + total6 + ";" + total7 + ";";

            Console.WriteLine(semua);
            string[] gabungan = { total1, total2, total3, total4, total5, total6, total7 };

            foreach (var a in gabungan)
            {
                Console.WriteLine(a);
            }
            string[][] words = gabungan.Tokenize();
            //var codebook = new TFIDF()
            //{
            //    Tf = TermFrequency.Log,
            //    Idf = InverseDocumentFrequency.Default
            //};
            var codebook = new BagOfWords()
            {
                MaximumOccurance = 1 // the resulting vector will have only 0's and 1's
            };

            codebook.Learn(words);
            double[]   bow1            = codebook.Transform(words[0]);
            double[]   bow2            = codebook.Transform(words[1]);
            double[]   bow3            = codebook.Transform(words[2]);
            double[]   bow4            = codebook.Transform(words[3]);
            double[]   bow5            = codebook.Transform(words[4]);
            double[]   bow6            = codebook.Transform(words[5]);
            double[]   bow7            = codebook.Transform(words[6]);
            double[][] keseluruhanBOW1 = { bow1, bow2, bow3, bow4, bow5, bow6, bow7 };

            //coba
            bool quitNow = false;

            while (!quitNow)
            {
                string val;
                Console.Write("Enter question: ");
                val = Console.ReadLine();
                string[] textss =
                {
                    val,
                };



                string[][] wordss = textss.Tokenize();
                //var codebook2 = new TFIDF()
                //{
                //    Tf = TermFrequency.Log,
                //    Idf = InverseDocumentFrequency.Default
                //};
                var codebook2 = new BagOfWords()
                {
                    MaximumOccurance = 1 // the resulting vector will have only 0's and 1's
                };
                codebook2.Learn(wordss);
                double[] c1   = codebook2.Transform(wordss[0]);
                string   path = @"C:\Users\devir\OneDrive\Documents\Visual Studio 2015\Projects\ner";
                //var load_svm_model = Serializer.Load<MulticlassClassifierBase>(Path.Combine(path, "pelatihanSVMbayardanpergi.bin"));


                //LibSvmModel modela = LibSvmModel.Load(Path.Combine(path, "pelatihanSVMbayardanpergi.bint"));
                //int jawaban = load_svm_model.Decide( c1); // answer will be 2.
                // Now, we can use the model class to create the equivalent Accord.NET SVM:

                //Console.WriteLine(jawaban);
                LibSvmModel model = LibSvmModel.Load(Path.Combine(path, "pelatihanSVMbayardanpergi.txt"));

                // Now, we can use the model class to create the equivalent Accord.NET SVM:
                SupportVectorMachine svm = model.CreateMachine();

                // Compute classification error
                bool predicted = svm.Decide(c1);

                // var machine = teacher.Learn(inputs, outputs);

                if (predicted == false)
                {
                    Console.WriteLine("BAYAR");
                }
                ;
                if (predicted == true)
                {
                    Console.WriteLine("PERGI");
                }
                ;
                Console.ReadLine();
            }

            // In order to convert any 2d array to jagged one
            // let's use a generic implementation
        }