コード例 #1
0
        private static void CreateList()
        {
            StreamReader sr = new StreamReader("corpus.txt"); /** Change textfile name from here. **/
            string       line;

            while ((line = sr.ReadLine()) != null)
            {
                CorpusWord    cw     = new CorpusWord();
                List <string> tokens = line.Split(' ').ToList();
                if (tokens.Count > 1)
                {
                    while (tokens.Count > 2)
                    {
                        tokens[0] = tokens[0] + " " + tokens[1];
                        tokens[1] = tokens[2];
                        if (tokens.Count > 3)
                        {
                            tokens[2] = tokens[3];
                        }
                        tokens.RemoveAt(tokens.Count - 1);
                    }
                    cw.Attribute = CorpusWord.ToAttribute(tokens[1]);
                }
                else
                {
                    cw.Attribute = Attribute.NULL;
                }

                cw.Word = tokens[0];
                corpusList.Add(cw);
            }
            count = corpusList.Count;
            sr.Close();
        }
コード例 #2
0
        public static void Train()
        {
            SVMProblem svmproblem = new SVMProblem();
            List <Tuple <Sentence, string> > sentences = new List <Tuple <Sentence, string> >();
            StreamReader sr = new StreamReader("training_data.txt");
            string       line;

            while ((line = sr.ReadLine()) != null)
            {
                string[] lines = line.Split(',');
                lines[0] = lines[0].Substring(1, lines[0].Length - 2);
                string sentence = lines[0];
                sentences.Add(Tuple.Create(Sentence.ParseIntoSentence(sentence, 0, false), lines[1]));
            }

            List <SVMNode[]> modelNodes = new List <SVMNode[]>();

            for (int i = 0; i < sentences.Count; i++)
            {
                List <SVMNode> nodes = new List <SVMNode>();
                for (int j = 0; j < Corpus.count; j++)
                {
                    SVMNode sentenceNode = new SVMNode(j, 0);
                    nodes.Add(sentenceNode);
                }
                for (int j = 0; j < sentences[i].Item1.SentenceWords.Count; j++)
                {
                    CorpusWord cw                   = Corpus.CorpusList.Find(c => c.Word.Equals(sentences[i].Item1.SentenceWords[j].Stem.Word) && c.Attribute.Equals(sentences[i].Item1.SentenceWords[j].Stem.Attribute));
                    SVMNode    sentenceNode         = new SVMNode(Corpus.CorpusList.IndexOf(cw), 1);
                    SVMNode    sentenceNodeToRemove = new SVMNode(Corpus.CorpusList.IndexOf(cw), 0);
                    nodes.Remove(sentenceNodeToRemove);
                    nodes.Add(sentenceNode);
                }
                SVMNode[] sentenceNodes = nodes.ToArray();
                sentenceNodes = sentenceNodes.OrderBy(x => x.Index).ToArray();

                svmproblem.Add(sentenceNodes, Corpus.CorpusList.IndexOf(Corpus.CorpusList.Find(c => c.Word.Equals(sentences[i].Item2))));
            }

            model = SVM.Train(svmproblem, parameter);

            sr.Close();
        }
コード例 #3
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (field1.Text != "" && field2.Text != "")
            {
                sentence1 = Sentence.ParseIntoSentence(field1.Text, 1, true);
                sentence2 = Sentence.ParseIntoSentence(field2.Text, 2, true);
                if (sentence1 != null && sentence2 != null)
                {
                    List <SVMNode> nodes = new List <SVMNode>();
                    for (int j = 0; j < Corpus.count; j++)
                    {
                        SVMNode sentenceNode = new SVMNode(j, 0);
                        nodes.Add(sentenceNode);
                    }
                    for (int j = 0; j < sentence1.SentenceWords.Count; j++)
                    {
                        CorpusWord cw                   = Corpus.CorpusList.Find(c => c.Word.Equals(sentence1.SentenceWords[j].Stem.Word) && c.Attribute.Equals(sentence1.SentenceWords[j].Stem.Attribute));
                        SVMNode    sentenceNode         = new SVMNode(Corpus.CorpusList.IndexOf(cw), 1);
                        SVMNode    sentenceNodeToRemove = new SVMNode(Corpus.CorpusList.IndexOf(cw), 0);
                        nodes.Remove(sentenceNodeToRemove);
                        nodes.Add(sentenceNode);
                    }
                    SVMNode[] sentenceNodes = nodes.ToArray();
                    sentenceNodes = sentenceNodes.OrderBy(x => x.Index).ToArray();

                    double s1_prediction = SVM.Predict(SVMInterface.model, sentenceNodes);

                    nodes = new List <SVMNode>();
                    for (int j = 0; j < Corpus.count; j++)
                    {
                        SVMNode sentenceNode = new SVMNode(j, 0);
                        nodes.Add(sentenceNode);
                    }
                    for (int j = 0; j < sentence2.SentenceWords.Count; j++)
                    {
                        CorpusWord cw                   = Corpus.CorpusList.Find(c => c.Word.Equals(sentence2.SentenceWords[j].Stem.Word) && c.Attribute.Equals(sentence2.SentenceWords[j].Stem.Attribute));
                        SVMNode    sentenceNode         = new SVMNode(Corpus.CorpusList.IndexOf(cw), 1);
                        SVMNode    sentenceNodeToRemove = new SVMNode(Corpus.CorpusList.IndexOf(cw), 0);
                        nodes.Remove(sentenceNodeToRemove);
                        nodes.Add(sentenceNode);
                    }
                    sentenceNodes = nodes.ToArray();
                    sentenceNodes = sentenceNodes.OrderBy(x => x.Index).ToArray();

                    double s2_prediction = SVM.Predict(SVMInterface.model, sentenceNodes);

                    Console.WriteLine(s1_prediction + " " + s2_prediction);

                    if (s1_prediction != s2_prediction)
                    {
                        result.Text = "UNRELATED";
                    }
                    else if (sentence1 != null && sentence2 != null)
                    {
                        if (sentence1.Polarity == sentence2.Polarity)
                        {
                            result.Text = "NO CONTRADICTION";
                        }
                        if (sentence1.Polarity != sentence2.Polarity)
                        {
                            result.Text = "CONTRADICTION";
                        }
                    }
                }
            }
        }