Beispiel #1
0
 public static Word checkEmotion(Word word,double threshold)
 {
     word.Emotional = Common.SeekEmotionDictionary(word.szTerm.Trim());
     if (word.Emotional!=Emotion.NA)
     {
         word.Confidential = 1d;
     }
     else
     {
         double orientation=Common.similarity.calOrientation(word.szTerm, word.szWordType);
         if (orientation > threshold)
         {
             word.Emotional = Emotion.Positive;
             word.Confidential = orientation;
         }
         else if (orientation < -threshold)
         {
             word.Emotional = Emotion.Negative;
             word.Confidential = orientation;
         }
         else
         {
             word.Emotional = Emotion.NA;
             word.Confidential = 0d;
         }
     }
     return word;
 }
Beispiel #2
0
        private void btnAnalyze_Click(object sender, EventArgs e)
        {
            this.txtContent.Text = "开始进行分析,这需要几分钟时间,请耐心等待";

            if (!ICTCLAS.initialize())
            {
                MessageBox.Show("Split Word Tools does not initialize!","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }

            System.Data.DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("emotion", typeof(string)));
            dt.Columns.Add(new DataColumn("confidence", typeof(double)));
            dt.Columns.Add(new DataColumn("word",typeof(string)));
            dt.Columns.Add(new DataColumn("wordtype",typeof(string)));
            dt.Columns.Add(new DataColumn("sentence",typeof(string)));

            foreach (var item in this.chkListFiles.CheckedItems)
            {
                string filename = item.ToString();
                StreamReader rd = new StreamReader(selectedDirectory+"\\"+filename, Encoding.GetEncoding("gb2312"), true);
                string filedata = rd.ReadToEnd().Trim().Replace("\r", "");
                filedata = filedata.Replace("\n", "");
                char[] delimiterChars = { '。', '!', '?' };
                string[] seArray = filedata.Split(delimiterChars,StringSplitOptions.RemoveEmptyEntries);

                for (int i = 0; i < seArray.Length; i++)
                {
                    seArray[i] = seArray[i].Trim();

                    if (!String.IsNullOrEmpty(seArray[i]))
                    {
                        ArrayList wordList = new ArrayList();
                        ICTCLAS.splitword(seArray[i], wordList);

                        foreach (SegWord term in wordList)
                        {
                            if (Common.isEmotionWordType(term.szWord.Trim(), term.wordType) && term.szWord.Trim().Length>1)
                            {
                                WordType wordtype = WordType.UNSET;
                                if (term.wordType[0] == 'n')
                                {
                                    wordtype = WordType.N;
                                }
                                else if (term.wordType[0] == 'v')
                                {
                                    wordtype = WordType.V;
                                }
                                else if (term.wordType[0] == 'a')
                                {
                                    wordtype = WordType.ADJ;
                                }
                                else if (term.wordType[0] == 'd')
                                {
                                    wordtype = WordType.ADV;
                                }

                                Word word = new Word(term.szWord.Trim(), wordtype);
                                word=Common.checkEmotion(word, double.Parse(this.txtThreshold.Text));

                                if(word.Emotional!=Emotion.NA)
                                {
                                    string sentence = this.outputEmotionSentence(Common.number, term.szWord.Trim(), term.Number, word.Emotional, wordList, item.ToString(), word.Confidential);
                                    DataRow row = dt.NewRow();
                                    row["emotion"] = word.Emotional.ToString();
                                    row["confidence"] = word.Confidential;
                                    row["word"] = word.szTerm;
                                    row["wordtype"] = word.szWordType.ToString();
                                    row["sentence"] = sentence;
                                    dt.Rows.Add(row);
                                }
                            }
                        }
                    }
                }
            }

            DataView dv = new DataView(dt);
            Hashtable ht = new Hashtable();
            dv.Sort = "confidence desc";
            int id = 0;
            StringBuilder sb = new StringBuilder();
            foreach (DataRowView row in dv)
            {
                if (!ht.ContainsKey((string)row["word"]))
                {
                    if (id < 100)
                    {
                        sb.AppendLine((id + 1).ToString() + "\t" + (string)row["sentence"]);
                        ht.Add((string)row["word"], (string)row["word"]);
                    }
                    id++;
                }
            }

            if (sb.Length > 0)
            {
                this.txtContent.Text = sb.ToString();
            }
            else
            {
                this.txtContent.Text = "分析结束,没有任何情感次";
            }
            ICTCLAS.uninitialize();
        }