Example #1
0
 static private void CalculatePij(LabelPair bilabel, int group)
 {
     double[,] Nil = new double[Variable.Sentences.Count, 4];
     //句子i被标的总次数,无论标签为何,用于计算Sij
     double[] Ni = new double[Variable.Sentences.Count];
     foreach (Sentence sentence in Variable.Sentences)
     {
         if (sentence.ID < SupervisedVariable.NumberOfTraningSentences)
         {
             continue;
         }
         foreach (Annotation j in sentence.AnnotaitonGroups[group].AnnotatorAnnotationDic.Values)
         {
             ++Nil[sentence.ID, j.IntLabel];
             ++Ni[sentence.ID];
         }
     }
     SPDSVariable.Sij = new Sij(0);
     foreach (Sentence sentence in Variable.Sentences)
     {
         if (sentence.ID < SupervisedVariable.NumberOfTraningSentences)
         {
             continue;
         }
         for (int j = 0; j < 4; ++j)
         {
             SPDSVariable.Sij.Value[sentence][new Labelset(bilabel.ToArray(), j)] = Nil[sentence.ID, j] / Ni[sentence.ID];
         }
     }
 }
Example #2
0
        static private void ObtainLabelResult(LabelPair bilabel, int group)
        {
            foreach (Sentence sentence in Variable.Sentences)
            {
                if (sentence.ID < SupervisedVariable.NumberOfTraningSentences)
                {
                    continue;
                }
                sentence.AnnotaitonGroups[group].SPDSNumResult.Labels[bilabel.First]  += SPDSVariable.Sij.Value[sentence][new Labelset(bilabel.ToList(), 1)] + SPDSVariable.Sij.Value[sentence][new Labelset(bilabel.ToList(), 3)];  //得到numeric结果
                sentence.AnnotaitonGroups[group].SPDSNumResult.Labels[bilabel.Second] += SPDSVariable.Sij.Value[sentence][new Labelset(bilabel.ToList(), 2)] + SPDSVariable.Sij.Value[sentence][new Labelset(bilabel.ToList(), 3)];; //得到numeric结果
                switch (SPDSVariable.Sij.CalculateJointBestLabelset(sentence).Key.IntLabel)
                {
                case 1:
                    sentence.AnnotaitonGroups[group].SPDSResult.Labels[bilabel.Second] = true;
                    break;

                case 2:
                    sentence.AnnotaitonGroups[group].SPDSResult.Labels[bilabel.First] = true;
                    break;

                case 3:
                    sentence.AnnotaitonGroups[group].SPDSResult.Labels[bilabel.Second] = true;
                    sentence.AnnotaitonGroups[group].SPDSResult.Labels[bilabel.First]  = true;
                    break;
                }
            }
        }
Example #3
0
 static private void CalculatePj(LabelPair bilabel)
 {
     SPDSVariable.Pj = new Pj(0);
     foreach (Sentence sentence in Variable.Sentences)
     {
         if (sentence.ID < SupervisedVariable.NumberOfTraningSentences)
         {
             continue;
         }
         for (int j = 0; j < 4; ++j)
         {
             SPDSVariable.Pj.Value[new Labelset(bilabel.ToArray(), j)] += SPDSVariable.Sij.Value[sentence][new Labelset(bilabel.ToArray(), j)];
         }
     }
     for (int j = 0; j < 4; ++j)
     {
         SPDSVariable.Pj.Value[new Labelset(bilabel.ToArray(), j)] += SPDSVariable.TrainingPj.Value[new Labelset(bilabel.ToArray(), j)];
     }
     if (Variable.PjDividSentenceCount)
     {
         for (int j = 0; j < 4; ++j)
         {
             SPDSVariable.Pj.Value[new Labelset(bilabel.ToArray(), j)] /= Variable.Sentences.Count;
         }
     }
 }
Example #4
0
 public LabelPairMatching(LabelPair bilabel0, LabelPair bilabel1, LabelPair bilabel2, LabelPair bilabel3, LabelPair bilabel4)
 {
     bilabels = new List <LabelPair>();
     bilabels.Add(bilabel0);
     bilabels.Add(bilabel1);
     bilabels.Add(bilabel2);
     bilabels.Add(bilabel3);
     bilabels.Add(bilabel4);
 }
Example #5
0
 private static void list_add(LabelPair node)
 {
     if (_current == _nodes.Length)
     {
         LabelPair[] tmp = new LabelPair[_current * 2];
         Array.Copy(_nodes, tmp, _current);
         _nodes = tmp;
     }
     _nodes[_current++] = node;
 }
Example #6
0
        public LBL1(ref FileBase file)
        {
            int start = file.ReadInt32();

            file.Skip(0x8);

            int  baseOffs = file.Position();
            uint count    = file.ReadUInt32();

            mEntries = new List <LabelEntry>();

            for (int i = 0; i < count; i++)
            {
                LabelEntry e = new LabelEntry();
                e.PairCount = file.ReadUInt32();

                int offs = file.ReadInt32();

                List <LabelPair> pairs = new List <LabelPair>();

                int pos = file.Position();
                file.Seek(baseOffs + offs);

                for (int j = 0; j < e.PairCount; j++)
                {
                    LabelPair p = new LabelPair();
                    p.Label      = file.ReadStringLenPrefix();
                    p.TextOffset = file.ReadUInt32();

                    pairs.Add(p);
                }

                file.Seek(pos);

                e.Pairs = pairs;
                mEntries.Add(e);
            }

            file.Seek(start + baseOffs);

            while (file.Position() % 0x10 != 0)
            {
                file.Skip(0x1);
            }
        }
Example #7
0
        static private void CalculatePAkjl(LabelPair bilabel, int group)
        {
            SPDSVariable.PAkjl = new PAkjl(0);
            IDictionary <string, double[, ]> numerator   = new Dictionary <string, double[, ]>(); //分子
            IDictionary <string, double[]>   denominator = new Dictionary <string, double[]>();   //分母

            //计算分子分母
            foreach (Sentence sentence in Variable.Sentences)
            {
                foreach (KeyValuePair <Annotator, Annotation> kAndl in sentence.AnnotaitonGroups[group].AnnotatorAnnotationDic)
                {
                    if (!numerator.ContainsKey(kAndl.Key.ID))
                    {
                        numerator.Add(kAndl.Key.ID, new double[4, 4]);
                        denominator.Add(kAndl.Key.ID, new double[4]);
                    }
                    if (sentence.ID >= SupervisedVariable.NumberOfTraningSentences)
                    {
                        continue;
                    }
                    for (int j = 0; j < 4; ++j)//正确标签
                    {
                        numerator[kAndl.Key.ID][j, kAndl.Value.IntLabel] += SPDSVariable.TrainingSij.Value[sentence][new Labelset(bilabel.ToArray(), j)];
                        denominator[kAndl.Key.ID][j] += SPDSVariable.TrainingSij.Value[sentence][new Labelset(bilabel.ToArray(), j)];
                    }
                }
            }
            //计算π
            foreach (Annotator annotator in GroupVariable.AnnotatorGroups[group]) //人
            {
                for (int j = 0; j < 4; ++j)                                       //正确标签
                {
                    if (denominator[annotator.ID][j] != 0)                        //某些结果就是在所有句子中都没出现过
                    {
                        for (int l = 0; l < 4; ++l)                               //人标的标签
                        {
                            SPDSVariable.PAkjl.Value[annotator][new Labelset(bilabel.ToArray(), j)][new Labelset(bilabel.ToArray(), j)] = numerator[annotator.ID][j, l] / denominator[annotator.ID][j];
                        }
                    }
                }
            }
        }
Example #8
0
 static private void InitializeTrainingSijAndPj(LabelPair bilabel)
 {
     double[,] Nil = new double[SupervisedVariable.NumberOfTraningSentences, 4];
     //句子i被标的总次数,无论标签为何,用于计算Sij
     double[] Ni = new double[SupervisedVariable.NumberOfTraningSentences];
     foreach (Sentence sentence in Variable.Sentences)
     {
         if (sentence.ID >= SupervisedVariable.NumberOfTraningSentences)
         {
             break;
         }
         foreach (Annotator annotator in Variable.Annotators)
         {
             foreach (Annotation annotation in Variable.Data[annotator][sentence])
             {
                 ++Nil[sentence.ID, annotation.IntLabel];
                 ++Ni[sentence.ID];
             }
         }
     }
     foreach (Sentence sentence in Variable.Sentences)
     {
         if (sentence.ID < SupervisedVariable.NumberOfTraningSentences)
         {
             for (int j = 0; j < 4; ++j)
             {
                 SPDSVariable.TrainingSij.Value[sentence][new Labelset(bilabel.ToArray(), j)] = Nil[sentence.ID, j] / Ni[sentence.ID];
                 SPDSVariable.TrainingPj.Value[new Labelset(bilabel.ToArray(), j)]           += SPDSVariable.TrainingSij.Value[sentence][new Labelset(bilabel.ToArray(), j)];
             }
         }
         else
         {
             break;
         }
     }
 }
Example #9
0
 static private void ObtainLabelResult(LabelPair bilabel, int group, Sij sij)
 {
     foreach (Sentence sentence in Variable.Sentences)
     {
         //得到numeric结果
         foreach (Labelset labelset in sij.Value[sentence].Keys)
         {
             foreach (Label label in labelset.Labels.Keys)//其实就一个Label
             {
                 if (labelset.Labels[label])
                 {
                     sentence.AnnotaitonGroups[group].PDSNumResult.Labels[label] = sij.Value[sentence][labelset];
                 }
             }
         }
         //得到binary结果
         KeyValuePair <Labelset, double> resultAndProbability = sij.CalculateJointBestLabelset(sentence);
         foreach (Label label in resultAndProbability.Key.Labels.Keys)
         {
             sentence.AnnotaitonGroups[group].PDSResult.Labels[label] = resultAndProbability.Key.Labels[label];
         }
         sentence.AnnotaitonGroups[group].PDSResult.Probability *= resultAndProbability.Value;
     }
 }
Example #10
0
 static public LabelPair[] GenerateBilabels(int group)
 {
     Label[] labelArray = GroupFunction.DescendLabelsByNumber(group);
     #region 初始化,用于构造互信息参数
     IDictionary <Label, double> labelFloatDic = new Dictionary <Label, double>();
     IList <LabelPair>           labelPairList = new List <LabelPair>();//前后无序,45个,用于初始化
     Function.InitializeEmptyLabelDic(ref labelFloatDic, ref labelPairList, labelArray);
     IDictionary <Label, double> numberOfLabelTrue  = new Dictionary <Label, double>(labelFloatDic);
     IDictionary <Label, double> numberOfLabelFalse = new Dictionary <Label, double>(labelFloatDic);
     #endregion
     #region 求互信息的参数
     IList <Label> traversedLabels = new List <Label>();
     int           N = 0;
     foreach (Sentence sentence in Variable.Sentences)
     {
         foreach (Annotation annotation in sentence.AnnotaitonGroups[group].AnnotatorAnnotationDic.Values)
         {
             ++N;
             traversedLabels.Clear();
             foreach (Label label1 in labelArray)
             {
                 traversedLabels.Add(label1);
                 if (annotation.Labels[label1])
                 {
                     ++numberOfLabelTrue[label1];
                     foreach (Label label2 in labelArray)
                     {
                         if (!traversedLabels.Contains(label2))
                         {
                             LabelPair labelPair = labelPairList.First(lp => lp.First == label1 && lp.Second == label2);
                             if (annotation.Labels[label2])
                             {
                                 if (labelPair.Label1TrueLabel2TrueSentenceAndFreq.ContainsKey(sentence))
                                 {
                                     ++labelPair.Label1TrueLabel2TrueSentenceAndFreq[sentence];
                                 }
                                 else
                                 {
                                     labelPair.Label1TrueLabel2TrueSentenceAndFreq.Add(sentence, 1);
                                 }
                             }
                             else
                             {
                                 if (labelPair.Label1TrueLabel2FalseSentenceAndFreq.ContainsKey(sentence))
                                 {
                                     ++labelPair.Label1TrueLabel2FalseSentenceAndFreq[sentence];
                                 }
                                 else
                                 {
                                     labelPair.Label1TrueLabel2FalseSentenceAndFreq.Add(sentence, 1);
                                 }
                             }
                         }
                     }
                 }
                 else
                 {
                     ++numberOfLabelFalse[label1];
                     foreach (Label label2 in labelArray)
                     {
                         if (!traversedLabels.Contains(label2))
                         {
                             LabelPair labelPair = labelPairList.First(lp => lp.First == label1 && lp.Second == label2);
                             if (annotation.Labels[label2])
                             {
                                 if (labelPair.Label1FalseLabel2TrueSentenceAndFreq.ContainsKey(sentence))
                                 {
                                     ++labelPair.Label1FalseLabel2TrueSentenceAndFreq[sentence];
                                 }
                                 else
                                 {
                                     labelPair.Label1FalseLabel2TrueSentenceAndFreq.Add(sentence, 1);
                                 }
                             }
                             else
                             {
                                 if (labelPair.Label1FalseLabel2FalseSentenceAndFreq.ContainsKey(sentence))
                                 {
                                     ++labelPair.Label1FalseLabel2FalseSentenceAndFreq[sentence];
                                 }
                                 else
                                 {
                                     labelPair.Label1FalseLabel2FalseSentenceAndFreq.Add(sentence, 1);
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     #endregion
     #region 情感对排序,论文用
     //List<LabelPair> sortednumberOfLabel1TrueLabel2True = new List<LabelPair>(labelPairList);//变量名为MI,其实值为joint entropy
     //sortednumberOfLabel1TrueLabel2True.Sort(delegate(LabelPair s1, LabelPair s2)
     //{
     //    return s2.Label1TrueLabel2TrueFrequency.CompareTo(s1.Label1TrueLabel2TrueFrequency);
     //});
     //List<LabelPair> sortednumberOfLabel1TrueLabel2False = new List<LabelPair>(labelPairList);//变量名为MI,其实值为joint entropy
     //sortednumberOfLabel1TrueLabel2False.Sort(delegate(LabelPair s1, LabelPair s2)
     //{
     //    return s2.Label1TrueLabel2FalseFrequency.CompareTo(s1.Label1TrueLabel2FalseFrequency);
     //});
     //List<LabelPair> sortednumberOfLabel1FalseLabel2True = new List<LabelPair>(labelPairList);//变量名为MI,其实值为joint entropy
     //sortednumberOfLabel1FalseLabel2True.Sort(delegate(LabelPair s1, LabelPair s2)
     //{
     //    return s2.Label1FalseLabel2TrueFrequency.CompareTo(s1.Label1FalseLabel2TrueFrequency);
     //});
     //List<LabelPair> sortednumberOfLabel1FalseLabel2False = new List<LabelPair>(labelPairList);//变量名为MI,其实值为joint entropy
     //sortednumberOfLabel1FalseLabel2False.Sort(delegate(LabelPair s1, LabelPair s2)
     //{
     //    return s2.Label1FalseLabel2FalseFrequency.CompareTo(s1.Label1FalseLabel2FalseFrequency);
     //});
     #endregion
     #region 求联合熵
     for (int i = 0; i < labelPairList.Count; ++i)
     {
         if (labelPairList[i].Label1TrueLabel2TrueFrequency != 0)
         {
             labelPairList[i].Weight += -(labelPairList[i].Label1TrueLabel2TrueFrequency / N) * Math.Log((labelPairList[i].Label1TrueLabel2TrueFrequency / N), 2);
         }
         if (labelPairList[i].Label1TrueLabel2FalseFrequency != 0)
         {
             labelPairList[i].Weight += -(labelPairList[i].Label1TrueLabel2FalseFrequency / N) * Math.Log((labelPairList[i].Label1TrueLabel2FalseFrequency / N), 2);
         }
         if (labelPairList[i].Label1FalseLabel2TrueFrequency != 0)
         {
             labelPairList[i].Weight += -(labelPairList[i].Label1FalseLabel2TrueFrequency / N) * Math.Log((labelPairList[i].Label1FalseLabel2TrueFrequency / N), 2);
         }
         if (labelPairList[i].Label1FalseLabel2FalseFrequency != 0)
         {
             labelPairList[i].Weight += -(labelPairList[i].Label1FalseLabel2FalseFrequency / N) * Math.Log((labelPairList[i].Label1FalseLabel2FalseFrequency / N), 2);
         }
     }
     List <LabelPair> sortedMutualInformation = new List <LabelPair>(labelPairList);//变量名为MI,其实值为joint entropy;排序仅为观察用
     sortedMutualInformation.Sort(delegate(LabelPair s1, LabelPair s2)
     {
         return(s1.Weight.CompareTo(s2.Weight));
     });
     #endregion
     return(MinimumWeightedPerfectMatching(labelPairList, true));
 }
Example #11
0
 static private void CalculateSij(LabelPair bilabel, int group)
 {
     double[,] numerator = new double[Variable.Sentences.Count, 4];//sij的分子
     for (int i = 0; i < Variable.Sentences.Count; ++i)
     {
         if (i < SupervisedVariable.NumberOfTraningSentences)
         {
             continue;
         }
         for (int j = 0; j < 4; ++j)
         {
             numerator[i, j] = 1;
         }
     }
     foreach (Sentence sentence in Variable.Sentences)
     {
         if (sentence.ID < SupervisedVariable.NumberOfTraningSentences)
         {
             continue;
         }
         for (int j = 0; j < 4; ++j)                                                                                         //正确标签
         {
             foreach (KeyValuePair <Annotator, Annotation> kAndl in sentence.AnnotaitonGroups[group].AnnotatorAnnotationDic) //这里面的l肯定只包括k标过的
             {
                 numerator[sentence.ID, j] *= SPDSVariable.PAkjl.Value[kAndl.Key][new Labelset(bilabel.ToArray(), j)][new Labelset(bilabel.ToArray(), kAndl.Value.IntLabel)];
             }
             numerator[sentence.ID, j] *= SPDSVariable.Pj.Value[new Labelset(bilabel.ToArray(), j)];
         }
     }
     double[] denominator = new double[Variable.Sentences.Count];
     for (int i = 0; i < Variable.Sentences.Count; ++i)
     {
         if (i < SupervisedVariable.NumberOfTraningSentences)
         {
             continue;
         }
         for (int q = 0; q < 4; ++q)
         {
             denominator[i] += numerator[i, q];
         }
     }
     SPDSVariable.Sij = new Sij(++SPDSVariable.Sij.Time);
     foreach (Sentence sentence in Variable.Sentences)
     {
         if (sentence.ID < SupervisedVariable.NumberOfTraningSentences)
         {
             continue;
         }
         for (int j = 0; j < 4; ++j)
         {
             if (Variable.SijDividPDataOnI)
             {
                 SPDSVariable.Sij.Value[sentence][new Labelset(bilabel.ToArray(), j)] = numerator[sentence.ID, j] / denominator[sentence.ID];
             }
             else
             {
                 SPDSVariable.Sij.Value[sentence][new Labelset(bilabel.ToArray(), j)] = numerator[sentence.ID, j];
             }
         }
     }
 }
Example #12
0
        //生成树
        static public IList <KeyValuePair <LabelPair, double> > GenerateIMTree(IDictionary <Label, double> numberOfLabelTrue, IDictionary <Label, double> numberOfLabelFalse,
                                                                               IDictionary <LabelPair, double> numberOfLabel1TrueLabel2True, IDictionary <LabelPair, double> numberOfLabel1TrueLabel2False, IDictionary <LabelPair, double> numberOfLabel1FalseLabel2True,
                                                                               IDictionary <LabelPair, double> numberOfLabel1FalseLabel2False, int n, Label[] labelArray)//Dictinary的顺序不可控,所以返回值的类型必须是List<KeyValuePair>
        {
            #region 求互信息
            IDictionary <LabelPair, double> labelPairFloat = new Dictionary <LabelPair, double>();
            IList <Label> traversedLabels = new List <Label>();
            foreach (Label label1 in labelArray)
            {
                traversedLabels.Add(label1);
                foreach (Label label2 in labelArray)
                {
                    if (!traversedLabels.Contains(label2))
                    {
                        labelPairFloat.Add(new LabelPair(label1, label2), 0);
                    }
                }
            }
            IDictionary <LabelPair, double> mutualInformation = new Dictionary <LabelPair, double>(labelPairFloat);
            traversedLabels.Clear();
            foreach (Label label1 in labelArray)
            {
                traversedLabels.Add(label1);
                foreach (Label label2 in labelArray)
                {
                    if (!traversedLabels.Contains(label2))
                    {
                        LabelPair labelPair12 = new LabelPair(label1, label2);
                        LabelPair labelPair21 = new LabelPair(label2, label1);//这么算对,因为labelPair12或labelPair21在同一时间段内只能存在一个
                        if (numberOfLabel1TrueLabel2True.ContainsKey(labelPair12))
                        {
                            if (numberOfLabel1TrueLabel2True[labelPair12] != 0)
                            {
                                mutualInformation[labelPair12] += (numberOfLabel1TrueLabel2True[labelPair12] / n) * Math.Log((numberOfLabel1TrueLabel2True[labelPair12] * n) / (numberOfLabelTrue[label1] * numberOfLabelTrue[label2]));
                            }
                        }
                        else if (numberOfLabel1TrueLabel2True[labelPair21] != 0)
                        {
                            mutualInformation[labelPair12] += (numberOfLabel1TrueLabel2True[labelPair21] / n) * Math.Log((numberOfLabel1TrueLabel2True[labelPair21] * n) / (numberOfLabelTrue[label1] * numberOfLabelTrue[label2]));
                        }
                        if (numberOfLabel1TrueLabel2False.ContainsKey(labelPair12))
                        {
                            if (numberOfLabel1TrueLabel2False[labelPair12] != 0)
                            {
                                mutualInformation[labelPair12] += (numberOfLabel1TrueLabel2False[labelPair12] / n) * Math.Log((numberOfLabel1TrueLabel2False[labelPair12] * n) / (numberOfLabelTrue[label1] * numberOfLabelFalse[label2]));
                            }
                        }
                        else if (numberOfLabel1FalseLabel2True[labelPair21] != 0)
                        {
                            mutualInformation[labelPair12] += (numberOfLabel1FalseLabel2True[labelPair21] / n) * Math.Log((numberOfLabel1FalseLabel2True[labelPair21] * n) / (numberOfLabelTrue[label1] * numberOfLabelFalse[label2]));
                        }
                        if (numberOfLabel1FalseLabel2True.ContainsKey(labelPair12))
                        {
                            if (numberOfLabel1FalseLabel2True[labelPair12] != 0)
                            {
                                mutualInformation[labelPair12] += (numberOfLabel1FalseLabel2True[labelPair12] / n) * Math.Log((numberOfLabel1FalseLabel2True[labelPair12] * n) / (numberOfLabelFalse[label1] * numberOfLabelTrue[label2]));
                            }
                        }
                        else if (numberOfLabel1TrueLabel2False[labelPair21] != 0)
                        {
                            mutualInformation[labelPair12] += (numberOfLabel1TrueLabel2False[labelPair21] / n) * Math.Log((numberOfLabel1TrueLabel2False[labelPair21] * n) / (numberOfLabelFalse[label1] * numberOfLabelTrue[label2]));
                        }
                        if (numberOfLabel1FalseLabel2False.ContainsKey(labelPair12))
                        {
                            if (numberOfLabel1FalseLabel2False[labelPair12] != 0)
                            {
                                mutualInformation[labelPair12] += (numberOfLabel1FalseLabel2False[labelPair12] / n) * Math.Log((numberOfLabel1FalseLabel2False[labelPair12] * n) / (numberOfLabelFalse[label1] * numberOfLabelFalse[label2]));
                            }
                        }
                        else if (numberOfLabel1FalseLabel2False[labelPair21] != 0)
                        {
                            mutualInformation[labelPair12] += (numberOfLabel1FalseLabel2False[labelPair21] / n) * Math.Log((numberOfLabel1FalseLabel2False[labelPair21] * n) / (numberOfLabelFalse[label1] * numberOfLabelFalse[label2]));
                        }
                    }
                }
            }
            List <KeyValuePair <LabelPair, double> > sortedMutualInformation = new List <KeyValuePair <LabelPair, double> >(mutualInformation);
            sortedMutualInformation.Sort(delegate(KeyValuePair <LabelPair, double> s1, KeyValuePair <LabelPair, double> s2)
            {
                return(s2.Value.CompareTo(s1.Value));
            });
            #endregion
            #region 求最小生成树(无方向)
            IList <KeyValuePair <LabelPair, double> > minTree = new List <KeyValuePair <LabelPair, double> >();
            IList <IList <Label> > LabelsCuts = new List <IList <Label> >();
            minTree.Add(sortedMutualInformation.First());
            LabelsCuts.Add(new List <Label>());
            LabelsCuts.First().Add(sortedMutualInformation.First().Key.First);
            LabelsCuts.First().Add(sortedMutualInformation.First().Key.Second);
            sortedMutualInformation.Remove(sortedMutualInformation.First());
            //for (int i = 0; i < sortedMutualInformation.Count && tree.Count != labelArray.Length - 1 && sortedMutualInformation[i].Value != 0; ++i)
            for (int i = 0; i < sortedMutualInformation.Count && minTree.Count != labelArray.Length - 1; ++i)
            {
                if (!sortedMutualInformation[i].Key.AreTwoLabelsInOneCut(LabelsCuts))
                {
                    minTree.Add(sortedMutualInformation[i]);
                    if (sortedMutualInformation[i].Key.AreTwoLabelsInNoCut(LabelsCuts))
                    {
                        LabelsCuts.Add(new List <Label>());
                        LabelsCuts.Last().Add(sortedMutualInformation.First().Key.First);
                        LabelsCuts.Last().Add(sortedMutualInformation.First().Key.Second);
                    }
                    else
                    {
                        int cutIndex = sortedMutualInformation[i].Key.IsOnlyOneLabelInOneCut(LabelsCuts);
                        if (cutIndex != -1)
                        {
                            if ((LabelsCuts[cutIndex].Contains(sortedMutualInformation[i].Key.First) && !LabelsCuts[cutIndex].Contains(sortedMutualInformation[i].Key.Second)))
                            {
                                LabelsCuts[cutIndex].Add(sortedMutualInformation[i].Key.Second);
                            }
                            else
                            {
                                LabelsCuts[cutIndex].Add(sortedMutualInformation[i].Key.First);
                            }
                        }
                        else
                        {
                            for (int ci = 0; ci < LabelsCuts.Count; ++ci)
                            {
                                if (LabelsCuts[ci].Contains(sortedMutualInformation[i].Key.First) || LabelsCuts[ci].Contains(sortedMutualInformation[i].Key.Second))
                                {
                                    for (int cj = ci + 1; cj < LabelsCuts.Count; ++cj)
                                    {
                                        if (LabelsCuts[cj].Contains(sortedMutualInformation[i].Key.First) || LabelsCuts[cj].Contains(sortedMutualInformation[i].Key.Second))
                                        {
                                            foreach (Label label in LabelsCuts[cj])
                                            {
                                                LabelsCuts[ci].Add(label);
                                            }
                                            LabelsCuts.RemoveAt(cj);
                                            goto EndMerge;
                                        }
                                    }
                                }
                            }
                            EndMerge :;
                        }
                    }
                }
                sortedMutualInformation.Remove(sortedMutualInformation[i]);
                --i;
            }
            #endregion
            #region 生成树(有方向,前节点是求概率的条件(父节点),后节点是求概率的对象(子节点))
            IList <KeyValuePair <LabelPair, double> > tree = new List <KeyValuePair <LabelPair, double> >();
            Label         currentLabel = labelArray[0];
            IList <Label> waitedLabels = new List <Label>();
            for (int i = 0; i < minTree.Count; ++i)
            {
                if (minTree[i].Key.Contains(currentLabel))
                {
                    if (minTree[i].Key.First == currentLabel)
                    {
                        tree.Add(new KeyValuePair <LabelPair, double>(minTree[i].Key, minTree[i].Value));
                        waitedLabels.Add(minTree[i].Key.Second);
                    }
                    else
                    {
                        tree.Add(new KeyValuePair <LabelPair, double>(minTree[i].Key.Reverse, minTree[i].Value));
                        waitedLabels.Add(minTree[i].Key.First);
                    }
                    minTree.RemoveAt(i);
                    --i;
                }
                if (i == minTree.Count - 1 && waitedLabels.Count != 0)
                {
                    currentLabel = waitedLabels.First();
                    waitedLabels.RemoveAt(0);
                    i = -1;
                }
            }
            return(tree);

            #endregion
        }
Example #13
0
 public LabelPairMatching(LabelPair bilabel0, LabelPair bilabel1)
 {
     bilabels = new List <LabelPair>();
     bilabels.Add(bilabel0);
     bilabels.Add(bilabel1);
 }
Example #14
0
        static private void Initialize()//运行过TreeForAll,就不用再计算每句的树了
        {
            #region 初始化
            DTDDSVariable.Sij = new Sij(1);
            //每句的信息,用于树的具体值(每个句子对应的树的值不同
            IDictionary <Label, double>     labelFloatDic  = new Dictionary <Label, double>();
            IDictionary <LabelPair, double> labelPairFloat = new Dictionary <LabelPair, double>();//前后无序,45个,用于初始化
            //Function.InitializeEmptyLabelDic(ref labelFloatDic, ref labelPairFloat, Variable.LabelArray);

            IDictionary <Sentence, IDictionary <Label, double> >     NumberOfLabelTrue              = new Dictionary <Sentence, IDictionary <Label, double> >();
            IDictionary <Sentence, IDictionary <Label, double> >     NumberOfLabelFalse             = new Dictionary <Sentence, IDictionary <Label, double> >();
            IDictionary <Sentence, IDictionary <LabelPair, double> > NumberOfLabel1TrueLabel2True   = new Dictionary <Sentence, IDictionary <LabelPair, double> >();
            IDictionary <Sentence, IDictionary <LabelPair, double> > NumberOfLabel1TrueLabel2False  = new Dictionary <Sentence, IDictionary <LabelPair, double> >();
            IDictionary <Sentence, IDictionary <LabelPair, double> > NumberOfLabel1FalseLabel2True  = new Dictionary <Sentence, IDictionary <LabelPair, double> >();
            IDictionary <Sentence, IDictionary <LabelPair, double> > NumberOfLabel1FalseLabel2False = new Dictionary <Sentence, IDictionary <LabelPair, double> >();
            foreach (Sentence sentence in Variable.Sentences)
            {
                NumberOfLabelTrue.Add(sentence, new Dictionary <Label, double>(labelFloatDic));
                NumberOfLabelFalse.Add(sentence, new Dictionary <Label, double>(labelFloatDic));
                NumberOfLabel1TrueLabel2True.Add(sentence, new Dictionary <LabelPair, double>(labelPairFloat));
                NumberOfLabel1TrueLabel2False.Add(sentence, new Dictionary <LabelPair, double>(labelPairFloat));
                NumberOfLabel1FalseLabel2True.Add(sentence, new Dictionary <LabelPair, double>(labelPairFloat));
                NumberOfLabel1FalseLabel2False.Add(sentence, new Dictionary <LabelPair, double>(labelPairFloat));
            }
            #endregion
            #region 求互信息的参数
            foreach (Annotator annotator in Variable.Annotators)
            {
                foreach (Sentence sentence in Variable.Sentences)
                {
                    foreach (Annotation annotation in Variable.Data[annotator][sentence])
                    {
                        IList <Label> traversedLabels = new List <Label>();
                        foreach (Label label1 in Variable.LabelArray)
                        {
                            traversedLabels.Add(label1);
                            if (annotation.Labels[label1])
                            {
                                ++NumberOfLabelTrue[sentence][label1];
                                foreach (Label label2 in Variable.LabelArray)
                                {
                                    if (!traversedLabels.Contains(label2))
                                    {
                                        if (annotation.Labels[label2])
                                        {
                                            ++NumberOfLabel1TrueLabel2True[sentence][new LabelPair(label1, label2)];
                                        }
                                        else
                                        {
                                            ++NumberOfLabel1TrueLabel2False[sentence][new LabelPair(label1, label2)];
                                        }
                                    }
                                }
                            }
                            else
                            {
                                ++NumberOfLabelFalse[sentence][label1];
                                foreach (Label label2 in Variable.LabelArray)
                                {
                                    if (!traversedLabels.Contains(label2))
                                    {
                                        if (annotation.Labels[label2])
                                        {
                                            ++NumberOfLabel1FalseLabel2True[sentence][new LabelPair(label1, label2)];
                                        }
                                        else
                                        {
                                            ++NumberOfLabel1FalseLabel2False[sentence][new LabelPair(label1, label2)];
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            #endregion
            #region 为每个句子生成树
            foreach (Sentence sentence in Variable.Sentences)
            {
                IDictionary <Label, int> numberOfEachLabel = new Dictionary <Label, int>();
                foreach (Label label in Variable.LabelArray)
                {
                    numberOfEachLabel.Add(label, 0);
                }
                foreach (Annotator annotator in Variable.Annotators)
                {
                    foreach (Annotation annotation in Variable.Data[annotator][sentence])
                    {
                        foreach (Label label in Variable.LabelArray)
                        {
                            if (annotation.Labels[label])
                            {
                                ++numberOfEachLabel[label];
                            }
                        }
                    }
                }
                List <KeyValuePair <Label, int> > sortedLabel = new List <KeyValuePair <Label, int> >(numberOfEachLabel);
                sortedLabel.Sort(delegate(KeyValuePair <Label, int> s1, KeyValuePair <Label, int> s2)
                {
                    return(s2.Value.CompareTo(s1.Value));
                });
                sentence.LabelArray = new Label[Variable.LabelArray.Length];
                for (int a = 0; a < Variable.LabelArray.Length; ++a)
                {
                    sentence.LabelArray[a] = sortedLabel[a].Key;
                }
                sentence.Tree = TDDSFunction.GenerateIMTree(NumberOfLabelTrue[sentence], NumberOfLabelFalse[sentence],
                                                            NumberOfLabel1TrueLabel2True[sentence], NumberOfLabel1TrueLabel2False[sentence],
                                                            NumberOfLabel1FalseLabel2True[sentence], NumberOfLabel1FalseLabel2False[sentence], Variable.NumberOfAnnotationsPerSentenceAfterGrouping, sentence.LabelArray);
            }
            #endregion
            //NumberOfIncompletedTreeSentence();
            #region 初始化Sij
            foreach (Sentence sentence in Variable.Sentences)
            {
                for (int l = 0; l < Math.Pow(2, Variable.LabelArray.Length); ++l)
                {
                    Annotation annotation = new Annotation(l);
                    if (annotation.Labels[sentence.Tree[0].Key.First])
                    {
                        DTDDSVariable.Sij.Value[sentence][new Labelset(Variable.LabelArray, l)] = NumberOfLabelTrue[sentence][sentence.Tree[0].Key.First] / Variable.NumberOfAnnotationsPerSentenceAfterGrouping;
                    }
                    else
                    {
                        DTDDSVariable.Sij.Value[sentence][new Labelset(Variable.LabelArray, l)] = NumberOfLabelFalse[sentence][sentence.Tree[0].Key.First] / Variable.NumberOfAnnotationsPerSentenceAfterGrouping;
                    }
                    foreach (KeyValuePair <LabelPair, double> ap in sentence.Tree)
                    {
                        LabelPair reverse = new LabelPair(ap.Key.Second, ap.Key.First);
                        if (annotation.Labels[ap.Key.First])
                        {
                            if (NumberOfLabelTrue[sentence][ap.Key.First] != 0)//考虑分母为0的情况
                            {
                                if (annotation.Labels[ap.Key.Second])
                                {
                                    if (NumberOfLabel1TrueLabel2True[sentence].ContainsKey(ap.Key))
                                    {
                                        DTDDSVariable.Sij.Value[sentence][new Labelset(Variable.LabelArray, l)] *= NumberOfLabel1TrueLabel2True[sentence][ap.Key] / NumberOfLabelTrue[sentence][ap.Key.First];
                                    }
                                    else
                                    {
                                        DTDDSVariable.Sij.Value[sentence][new Labelset(Variable.LabelArray, l)] *= NumberOfLabel1TrueLabel2True[sentence][reverse] / NumberOfLabelTrue[sentence][ap.Key.First];
                                    }
                                }
                                else if (NumberOfLabel1TrueLabel2False[sentence].ContainsKey(ap.Key))
                                {
                                    DTDDSVariable.Sij.Value[sentence][new Labelset(Variable.LabelArray, l)] *= NumberOfLabel1TrueLabel2False[sentence][ap.Key] / NumberOfLabelTrue[sentence][ap.Key.First];
                                }
                                else
                                {
                                    DTDDSVariable.Sij.Value[sentence][new Labelset(Variable.LabelArray, l)] *= NumberOfLabel1FalseLabel2True[sentence][reverse] / NumberOfLabelTrue[sentence][ap.Key.First];
                                }
                            }
                            else
                            {
                                DTDDSVariable.Sij.Value[sentence][new Labelset(Variable.LabelArray, l)] *= 0;
                                break;
                            }
                        }
                        else
                        {
                            if (NumberOfLabelFalse[sentence][ap.Key.First] != 0)//考虑分母为0的情况
                            {
                                if (annotation.Labels[ap.Key.Second])
                                {
                                    if (NumberOfLabel1FalseLabel2True[sentence].ContainsKey(ap.Key))
                                    {
                                        DTDDSVariable.Sij.Value[sentence][new Labelset(Variable.LabelArray, l)] *= NumberOfLabel1FalseLabel2True[sentence][ap.Key] / NumberOfLabelFalse[sentence][ap.Key.First];
                                    }
                                    else
                                    {
                                        DTDDSVariable.Sij.Value[sentence][new Labelset(Variable.LabelArray, l)] *= NumberOfLabel1TrueLabel2False[sentence][reverse] / NumberOfLabelFalse[sentence][ap.Key.First];
                                    }
                                }
                                else if (NumberOfLabel1FalseLabel2False[sentence].ContainsKey(ap.Key))
                                {
                                    DTDDSVariable.Sij.Value[sentence][new Labelset(Variable.LabelArray, l)] *= NumberOfLabel1FalseLabel2False[sentence][ap.Key] / NumberOfLabelFalse[sentence][ap.Key.First];
                                }
                                else
                                {
                                    DTDDSVariable.Sij.Value[sentence][new Labelset(Variable.LabelArray, l)] *= NumberOfLabel1FalseLabel2False[sentence][reverse] / NumberOfLabelFalse[sentence][ap.Key.First];
                                }
                            }
                            else
                            {
                                DTDDSVariable.Sij.Value[sentence][new Labelset(Variable.LabelArray, l)] *= 0;
                                break;
                            }
                        }
                    }
                }
            }
            #endregion
        }
Example #15
0
        public IList <IList <Label> > GetWitnesses(LabelPair labelpair)
        {
            IList <IList <Label> > witnesses = new List <IList <Label> >();
            //既是未包含参数两个label的列表,也是包含8个元素的子集
            IList <Label> labelArrayWithoutTheTwoLabels = new List <Label>();

            foreach (Label label in Variable.LabelArray)
            {
                if (label != labelpair.First && label != labelpair.Second)
                {
                    labelArrayWithoutTheTwoLabels.Add(label);
                }
            }
            //0个元素的子集(空集)
            witnesses.Add(new List <Label>());
            //1个元素的子集
            foreach (Label label in labelArrayWithoutTheTwoLabels)
            {
                IList <Label> witness = new List <Label>();
                witness.Add(label);
                witnesses.Add(witness);
            }
            //2个元素的子集
            for (int a = 0; a < labelArrayWithoutTheTwoLabels.Count; ++a)
            {
                for (int b = a + 1; b < labelArrayWithoutTheTwoLabels.Count; ++b)
                {
                    IList <Label> witness = new List <Label>();
                    witness.Add(labelArrayWithoutTheTwoLabels[a]);
                    witness.Add(labelArrayWithoutTheTwoLabels[b]);
                    witnesses.Add(witness);
                }
            }
            //3个元素的子集
            for (int a = 0; a < labelArrayWithoutTheTwoLabels.Count; ++a)
            {
                for (int b = a + 1; b < labelArrayWithoutTheTwoLabels.Count; ++b)
                {
                    for (int c = b + 1; c < labelArrayWithoutTheTwoLabels.Count; ++c)
                    {
                        IList <Label> witness = new List <Label>();
                        witness.Add(labelArrayWithoutTheTwoLabels[a]);
                        witness.Add(labelArrayWithoutTheTwoLabels[b]);
                        witness.Add(labelArrayWithoutTheTwoLabels[c]);
                        witnesses.Add(witness);
                    }
                }
            }
            //4个元素的子集
            for (int a = 0; a < labelArrayWithoutTheTwoLabels.Count; ++a)
            {
                for (int b = a + 1; b < labelArrayWithoutTheTwoLabels.Count; ++b)
                {
                    for (int c = b + 1; c < labelArrayWithoutTheTwoLabels.Count; ++c)
                    {
                        for (int d = c + 1; d < labelArrayWithoutTheTwoLabels.Count; ++d)
                        {
                            IList <Label> witness = new List <Label>();
                            witness.Add(labelArrayWithoutTheTwoLabels[a]);
                            witness.Add(labelArrayWithoutTheTwoLabels[b]);
                            witness.Add(labelArrayWithoutTheTwoLabels[c]);
                            witness.Add(labelArrayWithoutTheTwoLabels[d]);
                            witnesses.Add(witness);
                        }
                    }
                }
            }
            //5个元素的子集
            for (int a = 0; a < labelArrayWithoutTheTwoLabels.Count; ++a)
            {
                for (int b = a + 1; b < labelArrayWithoutTheTwoLabels.Count; ++b)
                {
                    for (int c = b + 1; c < labelArrayWithoutTheTwoLabels.Count; ++c)
                    {
                        for (int d = c + 1; d < labelArrayWithoutTheTwoLabels.Count; ++d)
                        {
                            for (int e = d + 1; e < labelArrayWithoutTheTwoLabels.Count; ++e)
                            {
                                IList <Label> witness = new List <Label>();
                                witness.Add(labelArrayWithoutTheTwoLabels[a]);
                                witness.Add(labelArrayWithoutTheTwoLabels[b]);
                                witness.Add(labelArrayWithoutTheTwoLabels[c]);
                                witness.Add(labelArrayWithoutTheTwoLabels[d]);
                                witness.Add(labelArrayWithoutTheTwoLabels[e]);
                                witnesses.Add(witness);
                            }
                        }
                    }
                }
            }
            //6个元素的子集
            for (int a = 0; a < labelArrayWithoutTheTwoLabels.Count; ++a)
            {
                for (int b = a + 1; b < labelArrayWithoutTheTwoLabels.Count; ++b)
                {
                    for (int c = b + 1; c < labelArrayWithoutTheTwoLabels.Count; ++c)
                    {
                        for (int d = c + 1; d < labelArrayWithoutTheTwoLabels.Count; ++d)
                        {
                            for (int e = d + 1; e < labelArrayWithoutTheTwoLabels.Count; ++e)
                            {
                                for (int f = e + 1; f < labelArrayWithoutTheTwoLabels.Count; ++f)
                                {
                                    IList <Label> witness = new List <Label>();
                                    witness.Add(labelArrayWithoutTheTwoLabels[a]);
                                    witness.Add(labelArrayWithoutTheTwoLabels[b]);
                                    witness.Add(labelArrayWithoutTheTwoLabels[c]);
                                    witness.Add(labelArrayWithoutTheTwoLabels[d]);
                                    witness.Add(labelArrayWithoutTheTwoLabels[e]);
                                    witness.Add(labelArrayWithoutTheTwoLabels[f]);
                                    witnesses.Add(witness);
                                }
                            }
                        }
                    }
                }
            }
            //7个元素的子集
            for (int a = 0; a < labelArrayWithoutTheTwoLabels.Count; ++a)
            {
                for (int b = a + 1; b < labelArrayWithoutTheTwoLabels.Count; ++b)
                {
                    for (int c = b + 1; c < labelArrayWithoutTheTwoLabels.Count; ++c)
                    {
                        for (int d = c + 1; d < labelArrayWithoutTheTwoLabels.Count; ++d)
                        {
                            for (int e = d + 1; e < labelArrayWithoutTheTwoLabels.Count; ++e)
                            {
                                for (int f = e + 1; f < labelArrayWithoutTheTwoLabels.Count; ++f)
                                {
                                    for (int g = f + 1; g < labelArrayWithoutTheTwoLabels.Count; ++g)
                                    {
                                        IList <Label> witness = new List <Label>();
                                        witness.Add(labelArrayWithoutTheTwoLabels[a]);
                                        witness.Add(labelArrayWithoutTheTwoLabels[b]);
                                        witness.Add(labelArrayWithoutTheTwoLabels[c]);
                                        witness.Add(labelArrayWithoutTheTwoLabels[d]);
                                        witness.Add(labelArrayWithoutTheTwoLabels[e]);
                                        witness.Add(labelArrayWithoutTheTwoLabels[f]);
                                        witness.Add(labelArrayWithoutTheTwoLabels[g]);
                                        witnesses.Add(witness);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            //8个元素的子集
            witnesses.Add(labelArrayWithoutTheTwoLabels);
            return(witnesses);
        }//有错误,只留在Probability的代码里,不用
Example #16
0
        }//有错误,只留在Probability的代码里,不用

        public IList <IList <Label> > GetLatentWitnesses(LabelPair labelPair, int i)
        {
            IList <IList <Label> > latentWitnesses = new List <IList <Label> >();
            //既是未包含参数两个label的列表,也是包含8个元素的子集
            IList <Label> adjLabelsWithoutTheTwoLabels = new List <Label>();

            foreach (Label label in Variable.LabelArray)
            {
                if (!labelPair.Contains(label) && (AdjMatrix[new LabelPair(labelPair.First, label)] || AdjMatrix[new LabelPair(labelPair.Second, label)]))//此处跟书上不一样,书上错了
                {
                    adjLabelsWithoutTheTwoLabels.Add(label);
                }
            }
            switch (i)
            {
            case 0:
                latentWitnesses.Add(new List <Label>());
                break;

            case 1:
                for (int a = 0; a < adjLabelsWithoutTheTwoLabels.Count; ++a)
                {
                    IList <Label> witness = new List <Label>();
                    witness.Add(adjLabelsWithoutTheTwoLabels[a]);
                    latentWitnesses.Add(witness);
                }
                break;

            case 2:
                for (int a = 0; a < adjLabelsWithoutTheTwoLabels.Count; ++a)
                {
                    for (int b = a + 1; b < adjLabelsWithoutTheTwoLabels.Count; ++b)
                    {
                        IList <Label> witness = new List <Label>();
                        witness.Add(adjLabelsWithoutTheTwoLabels[a]);
                        witness.Add(adjLabelsWithoutTheTwoLabels[b]);
                        latentWitnesses.Add(witness);
                    }
                }
                break;

            case 3:
                for (int a = 0; a < adjLabelsWithoutTheTwoLabels.Count; ++a)
                {
                    for (int b = a + 1; b < adjLabelsWithoutTheTwoLabels.Count; ++b)
                    {
                        for (int c = b + 1; c < adjLabelsWithoutTheTwoLabels.Count; ++c)
                        {
                            IList <Label> witness = new List <Label>();
                            witness.Add(adjLabelsWithoutTheTwoLabels[a]);
                            witness.Add(adjLabelsWithoutTheTwoLabels[b]);
                            witness.Add(adjLabelsWithoutTheTwoLabels[c]);
                            latentWitnesses.Add(witness);
                        }
                    }
                }
                break;

            case 4:
                for (int a = 0; a < adjLabelsWithoutTheTwoLabels.Count; ++a)
                {
                    for (int b = a + 1; b < adjLabelsWithoutTheTwoLabels.Count; ++b)
                    {
                        for (int c = b + 1; c < adjLabelsWithoutTheTwoLabels.Count; ++c)
                        {
                            for (int d = c + 1; d < adjLabelsWithoutTheTwoLabels.Count; ++d)
                            {
                                IList <Label> witness = new List <Label>();
                                witness.Add(adjLabelsWithoutTheTwoLabels[a]);
                                witness.Add(adjLabelsWithoutTheTwoLabels[b]);
                                witness.Add(adjLabelsWithoutTheTwoLabels[c]);
                                witness.Add(adjLabelsWithoutTheTwoLabels[d]);
                                latentWitnesses.Add(witness);
                            }
                        }
                    }
                }
                break;

            case 5:
                for (int a = 0; a < adjLabelsWithoutTheTwoLabels.Count; ++a)
                {
                    for (int b = a + 1; b < adjLabelsWithoutTheTwoLabels.Count; ++b)
                    {
                        for (int c = b + 1; c < adjLabelsWithoutTheTwoLabels.Count; ++c)
                        {
                            for (int d = c + 1; d < adjLabelsWithoutTheTwoLabels.Count; ++d)
                            {
                                for (int e = d + 1; e < adjLabelsWithoutTheTwoLabels.Count; ++e)
                                {
                                    IList <Label> witness = new List <Label>();
                                    witness.Add(adjLabelsWithoutTheTwoLabels[a]);
                                    witness.Add(adjLabelsWithoutTheTwoLabels[b]);
                                    witness.Add(adjLabelsWithoutTheTwoLabels[c]);
                                    witness.Add(adjLabelsWithoutTheTwoLabels[d]);
                                    witness.Add(adjLabelsWithoutTheTwoLabels[e]);
                                    latentWitnesses.Add(witness);
                                }
                            }
                        }
                    }
                }
                break;

            case 6:
                for (int a = 0; a < adjLabelsWithoutTheTwoLabels.Count; ++a)
                {
                    for (int b = a + 1; b < adjLabelsWithoutTheTwoLabels.Count; ++b)
                    {
                        for (int c = b + 1; c < adjLabelsWithoutTheTwoLabels.Count; ++c)
                        {
                            for (int d = c + 1; d < adjLabelsWithoutTheTwoLabels.Count; ++d)
                            {
                                for (int e = d + 1; e < adjLabelsWithoutTheTwoLabels.Count; ++e)
                                {
                                    for (int f = e + 1; f < adjLabelsWithoutTheTwoLabels.Count; ++f)
                                    {
                                        IList <Label> witness = new List <Label>();
                                        witness.Add(adjLabelsWithoutTheTwoLabels[a]);
                                        witness.Add(adjLabelsWithoutTheTwoLabels[b]);
                                        witness.Add(adjLabelsWithoutTheTwoLabels[c]);
                                        witness.Add(adjLabelsWithoutTheTwoLabels[d]);
                                        witness.Add(adjLabelsWithoutTheTwoLabels[e]);
                                        witness.Add(adjLabelsWithoutTheTwoLabels[f]);
                                        latentWitnesses.Add(witness);
                                    }
                                }
                            }
                        }
                    }
                }
                break;

            case 7:
                for (int a = 0; a < adjLabelsWithoutTheTwoLabels.Count; ++a)
                {
                    for (int b = a + 1; b < adjLabelsWithoutTheTwoLabels.Count; ++b)
                    {
                        for (int c = b + 1; c < adjLabelsWithoutTheTwoLabels.Count; ++c)
                        {
                            for (int d = c + 1; d < adjLabelsWithoutTheTwoLabels.Count; ++d)
                            {
                                for (int e = d + 1; e < adjLabelsWithoutTheTwoLabels.Count; ++e)
                                {
                                    for (int f = e + 1; f < adjLabelsWithoutTheTwoLabels.Count; ++f)
                                    {
                                        for (int g = f + 1; g < adjLabelsWithoutTheTwoLabels.Count; ++g)
                                        {
                                            IList <Label> witness = new List <Label>();
                                            witness.Add(adjLabelsWithoutTheTwoLabels[a]);
                                            witness.Add(adjLabelsWithoutTheTwoLabels[b]);
                                            witness.Add(adjLabelsWithoutTheTwoLabels[c]);
                                            witness.Add(adjLabelsWithoutTheTwoLabels[d]);
                                            witness.Add(adjLabelsWithoutTheTwoLabels[e]);
                                            witness.Add(adjLabelsWithoutTheTwoLabels[f]);
                                            witness.Add(adjLabelsWithoutTheTwoLabels[g]);
                                            latentWitnesses.Add(witness);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                break;

            case 8:
                for (int a = 0; a < adjLabelsWithoutTheTwoLabels.Count; ++a)
                {
                    for (int b = a + 1; b < adjLabelsWithoutTheTwoLabels.Count; ++b)
                    {
                        for (int c = b + 1; c < adjLabelsWithoutTheTwoLabels.Count; ++c)
                        {
                            for (int d = c + 1; d < adjLabelsWithoutTheTwoLabels.Count; ++d)
                            {
                                for (int e = d + 1; e < adjLabelsWithoutTheTwoLabels.Count; ++e)
                                {
                                    for (int f = e + 1; f < adjLabelsWithoutTheTwoLabels.Count; ++f)
                                    {
                                        for (int g = f + 1; g < adjLabelsWithoutTheTwoLabels.Count; ++g)
                                        {
                                            for (int h = g + 1; h < adjLabelsWithoutTheTwoLabels.Count; ++h)
                                            {
                                                IList <Label> witness = new List <Label>();
                                                witness.Add(adjLabelsWithoutTheTwoLabels[a]);
                                                witness.Add(adjLabelsWithoutTheTwoLabels[b]);
                                                witness.Add(adjLabelsWithoutTheTwoLabels[c]);
                                                witness.Add(adjLabelsWithoutTheTwoLabels[d]);
                                                witness.Add(adjLabelsWithoutTheTwoLabels[e]);
                                                witness.Add(adjLabelsWithoutTheTwoLabels[f]);
                                                witness.Add(adjLabelsWithoutTheTwoLabels[g]);
                                                witness.Add(adjLabelsWithoutTheTwoLabels[h]);
                                                latentWitnesses.Add(witness);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                break;
            }
            return(latentWitnesses);
        }
Example #17
0
        static private Graph BuildBN(int group, Label[] labelArray, IndependenceEstimation independentEstimation, double thresholdOfIndependentForNetwork)
        {
            #region Build-PMap-Skeleton
            Graph H = new Graph();
            int   i = 0;
            IDictionary <LabelPair, IList <Label> > labelPairAndWitness = new Dictionary <LabelPair, IList <Label> >();
            while (i <= H.MaxDegree())
            {
                foreach (Label X in labelArray)
                {
                    foreach (Label Y in labelArray)
                    {
                        LabelPair labelPair = new LabelPair(X, Y);
                        if (X != Y && H.AdjMatrix[labelPair] && H.AdjMatrix[labelPair.Reverse])//相接的点
                        {
                            foreach (List <Label> witness in H.GetLatentWitnesses(labelPair, i))
                            {
                                bool isAWitness = false;
                                switch (independentEstimation)
                                {
                                case IndependenceEstimation.Probability:
                                    isAWitness = labelPair.IsAWitnessByProbability(witness, group, thresholdOfIndependentForNetwork);
                                    break;

                                case IndependenceEstimation.MutualInformation:
                                    isAWitness = labelPair.IsAWitnessByMI(witness, group, thresholdOfIndependentForNetwork);
                                    break;
                                }
                                if (isAWitness)
                                {
                                    labelPairAndWitness.Add(labelPair, witness);
                                    H.AdjMatrix[labelPair]         = false;
                                    H.AdjMatrix[labelPair.Reverse] = false;
                                    break;
                                }
                            }
                        }
                    }
                }
                ++i;
            }
            #endregion
            #region Mark-Immoralities
            bool isChanged = true;
            while (isChanged)
            {
                isChanged = false;
                foreach (Label Xi in labelArray)
                {
                    foreach (Label Xj in labelArray)
                    {
                        foreach (Label Xk in labelArray)
                        {
                            if (Xi != Xj && Xj != Xk && Xi != Xk)//三个情感两两不等
                            {
                                if (H.AdjMatrix[new LabelPair(Xi, Xj)] && H.AdjMatrix[new LabelPair(Xj, Xi)] &&
                                    H.AdjMatrix[new LabelPair(Xj, Xk)] && H.AdjMatrix[new LabelPair(Xk, Xj)] &&
                                    !H.AdjMatrix[new LabelPair(Xi, Xk)] && !H.AdjMatrix[new LabelPair(Xk, Xi)])
                                {
                                    if ((labelPairAndWitness.ContainsKey(new LabelPair(Xi, Xk)) && !labelPairAndWitness[new LabelPair(Xi, Xk)].Contains(Xj)))
                                    {
                                        H.AdjMatrix[new LabelPair(Xj, Xi)] = false;
                                        H.AdjMatrix[new LabelPair(Xj, Xk)] = false;
                                        isChanged = true;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            #endregion
            #region Build-PDAG
            bool isConverged = false;
            while (!isConverged)
            {
                //R1
                bool foundR1 = false;
                foreach (Label X in labelArray)
                {
                    foreach (Label Y in labelArray)
                    {
                        foreach (Label Z in labelArray)
                        {
                            if (X != Y && X != Z && Y != Z)
                            {
                                if (H.AdjMatrix[new LabelPair(X, Y)] && !H.AdjMatrix[new LabelPair(Y, X)] &&
                                    H.AdjMatrix[new LabelPair(Y, Z)] && H.AdjMatrix[new LabelPair(Z, Y)] &&
                                    !H.AdjMatrix[new LabelPair(X, Z)] && !H.AdjMatrix[new LabelPair(Z, X)])
                                {
                                    H.AdjMatrix[new LabelPair(Z, Y)] = false;
                                    foundR1 = true;
                                }
                            }
                        }
                    }
                }
                //R2
                bool foundR2 = false;
                foreach (Label X in labelArray)
                {
                    foreach (Label Y in labelArray)
                    {
                        foreach (Label Z in labelArray)
                        {
                            if (X != Y && X != Z && Y != Z)
                            {
                                if (H.AdjMatrix[new LabelPair(X, Y)] && !H.AdjMatrix[new LabelPair(Y, X)] &&
                                    H.AdjMatrix[new LabelPair(Y, Z)] && !H.AdjMatrix[new LabelPair(Z, Y)] &&
                                    H.AdjMatrix[new LabelPair(X, Z)] && H.AdjMatrix[new LabelPair(Z, X)])
                                {
                                    H.AdjMatrix[new LabelPair(Z, X)] = false;
                                    foundR2 = true;
                                }
                            }
                        }
                    }
                }
                //R3
                bool foundR3 = false;
                foreach (Label X in labelArray)
                {
                    foreach (Label Y1 in labelArray)
                    {
                        foreach (Label Y2 in labelArray)
                        {
                            foreach (Label Z in labelArray)
                            {
                                if (X != Y1 && X != Y2 && X != Z && Y1 != Y2 && Y1 != Z && Y2 != Z)
                                {
                                    if (H.AdjMatrix[new LabelPair(X, Y1)] && H.AdjMatrix[new LabelPair(Y1, X)] &&
                                        H.AdjMatrix[new LabelPair(X, Y2)] && H.AdjMatrix[new LabelPair(Y2, X)] &&
                                        H.AdjMatrix[new LabelPair(X, Z)] && H.AdjMatrix[new LabelPair(Z, X)] &&
                                        H.AdjMatrix[new LabelPair(Y1, Z)] && !H.AdjMatrix[new LabelPair(Z, Y1)] &&
                                        H.AdjMatrix[new LabelPair(Y2, Z)] && !H.AdjMatrix[new LabelPair(Z, Y2)])
                                    {
                                        H.AdjMatrix[new LabelPair(Z, X)] = false;
                                        foundR3 = true;
                                    }
                                }
                            }
                        }
                    }
                }
                //是否Converged
                if (!foundR1 && !foundR2 && !foundR3)
                {
                    isConverged = true;
                }
            }
            #endregion
            #region Complete-BN
            foreach (Label X in labelArray)
            {
                foreach (Label Y in labelArray)
                {
                    if (X != Y)
                    {
                        if (H.AdjMatrix[new LabelPair(X, Y)] && H.AdjMatrix[new LabelPair(Y, X)])
                        {
                            H.AdjMatrix[new LabelPair(Y, X)] = false;
                        }
                    }
                }
            }
            #endregion
            return(H);
        }
Example #18
0
        /// <summary>
        /// Performs connected components analysis.
        /// </summary>
        /// <param name="image">The image to analyze</param>
        /// <returns>The connected components image</returns>
        public static unsafe LabelImage Compute(BinaryImage image)
        {
            _current = 0;
            int   r, c;
            short currentLabel, currentSet;
            short label0, label1, label2, label3, set0, set1;
            short tl, t, tr, l;
            int   rows = image.Rows;
            int   columns = image.Columns;
            int   count, labelCount;

            short[]    equiv  = new short[rows * columns];
            LabelImage labels = new LabelImage(rows, columns);

            fixed(bool *imageBuf = image.RawArray)
            {
                fixed(short *labelsBuf = labels.RawArray, equivBuf = equiv)
                {
                    bool * imagePtr = imageBuf + columns + 1;
                    short *tlPtr = labelsBuf;
                    short *tPtr = tlPtr + 1;
                    short *trPtr = tPtr + 1;
                    short *lPtr = tlPtr + columns;
                    short *labelsPtr = lPtr + 1;
                    short *equivPtr = equivBuf;
                    short *set0Ptr, set1Ptr;

                    label0       = label1 = label2 = label3 = 0;
                    currentLabel = 1;
                    r            = rows - 2;
                    while (r-- != 0)
                    //for (r = 1; r < rows - 1; r++)
                    {
                        c = columns - 2;
                        // we are already at c+1
                        while (c-- != 0)
                        //for (c = 1; c < columns - 1; c++)
                        {
                            if (*imagePtr)
                            {
                                tl = *tlPtr;
                                t  = *tPtr;
                                tr = *trPtr;
                                l  = *lPtr;

                                labelCount = 0;

                                if (tl != 0)
                                {
                                    label0 = tl;
                                    labelCount++;
                                }

                                if (t != 0)
                                {
                                    if (labelCount == 0)
                                    {
                                        labelCount++;
                                        label0 = t;
                                    }
                                    else if (labelCount == 1 && t != label0)
                                    {
                                        labelCount++;
                                        label1 = t;
                                    }
                                }

                                if (tr != 0)
                                {
                                    if (labelCount == 0)
                                    {
                                        labelCount++;
                                        label0 = tr;
                                    }

                                    else if (labelCount == 1 && tr != label0)
                                    {
                                        labelCount++;
                                        label1 = tr;
                                    }
                                    else if (labelCount == 2 && tr != label0 && tr != label1)
                                    {
                                        labelCount++;
                                        label2 = tr;
                                    }
                                }

                                if (l != 0)
                                {
                                    if (labelCount == 0)
                                    {
                                        labelCount++;
                                        label0 = l;
                                    }
                                    else if (labelCount == 1 && l != label0)
                                    {
                                        labelCount++;
                                        label1 = l;
                                    }
                                    else if (labelCount == 2 && l != label0 && l != label1)
                                    {
                                        labelCount++;
                                        label2 = l;
                                    }
                                    else if (labelCount == 3 && l != label0 && l != label1 && l != label2)
                                    {
                                        labelCount++;
                                        label3 = l;
                                    }
                                }

                                if (labelCount == 0)
                                {
                                    *labelsPtr = currentLabel++;
                                }
                                else if (labelCount == 1)
                                {
                                    *labelsPtr = label0;
                                }
                                else if (labelCount == 2)
                                {
                                    *labelsPtr = label0;
                                    list_add(new LabelPair {
                                        Label1 = label0, Label2 = label1
                                    });
                                }
                                else if (labelCount == 3)
                                {
                                    *labelsPtr = label0;
                                    list_add(new LabelPair {
                                        Label1 = label0, Label2 = label1
                                    });
                                    list_add(new LabelPair {
                                        Label1 = label0, Label2 = label2
                                    });
                                    list_add(new LabelPair {
                                        Label1 = label1, Label2 = label2
                                    });
                                }
                                else if (labelCount == 4)
                                {
                                    *labelsPtr = label0;
                                    list_add(new LabelPair {
                                        Label1 = label0, Label2 = label1
                                    });
                                    list_add(new LabelPair {
                                        Label1 = label0, Label2 = label2
                                    });
                                    list_add(new LabelPair {
                                        Label1 = label0, Label2 = label3
                                    });
                                    list_add(new LabelPair {
                                        Label1 = label1, Label2 = label2
                                    });
                                    list_add(new LabelPair {
                                        Label1 = label1, Label2 = label3
                                    });
                                    list_add(new LabelPair {
                                        Label1 = label2, Label2 = label3
                                    });
                                }
                            }
                            imagePtr++;
                            tlPtr++;
                            tPtr++;
                            trPtr++;
                            lPtr++;
                            labelsPtr++;
                        }
                        // get to c+1
                        imagePtr  += 2;
                        tlPtr     += 2;
                        tPtr      += 2;
                        trPtr     += 2;
                        lPtr      += 2;
                        labelsPtr += 2;
                    }

                    // resolve equivalencies
                    count = currentLabel;
                    while (count-- != 0)
                    {
                        *equivPtr++ = 0;
                    }
                    currentSet = 1;
                    fixed(LabelPair *nodeBuf = _nodes)
                    {
                        LabelPair *nodePtr = nodeBuf;

                        while (_current-- != 0)
                        {
                            LabelPair current = *nodePtr++;

                            set0Ptr = equivBuf + current.Label1;
                            set1Ptr = equivBuf + current.Label2;
                            bool contain1 = *set0Ptr != 0;
                            bool contain2 = *set1Ptr != 0;
                            if (contain1 && contain2)
                            {
                                set0 = *set0Ptr;
                                set1 = *set1Ptr;
                                if (set0 != set1)
                                {
                                    equivPtr = equivBuf;
                                    count    = currentLabel;
                                    while (count-- != 0)
                                    {
                                        if (*equivPtr == set1)
                                        {
                                            *equivPtr = set0;
                                        }
                                        equivPtr++;
                                    }
                                }
                            }
                            else if (contain1)
                            {
                                *set1Ptr = *set0Ptr;
                            }
                            else if (contain2)
                            {
                                *set0Ptr = *set1Ptr;
                            }
                            else
                            {
                                *set0Ptr = currentSet;
                                *set1Ptr = currentSet;
                                currentSet++;
                            }
                        }
                    }

                    labelsPtr = labelsBuf;
                    count     = rows * columns;
                    while (count-- != 0)
                    {
                        currentLabel = *labelsPtr;

                        if (currentLabel == 0)
                        {
                            labelsPtr++;
                            continue;
                        }

                        equivPtr = equivBuf + currentLabel;
                        if (*equivPtr == 0)
                        {
                            *equivPtr = currentSet++;
                        }
                        *labelsPtr++ = *equivPtr;
                    }
                }
            }

            return(labels);
        }
Example #19
0
        static private void Initialize(int group)
        {
            Label[] labelArray = GroupFunction.DescendLabelsByNumber(group);
            #region 初始化
            TDDSVariable.Sij = new Sij(1);
            //整体的信息,用于构造互信息参数,求树(所有句子一棵树)
            IDictionary <Label, double>     labelFloatDic  = new Dictionary <Label, double>();
            IDictionary <LabelPair, double> labelPairFloat = new Dictionary <LabelPair, double>();//前后无序,45个,用于初始化
            //Function.InitializeEmptyLabelDic(ref labelFloatDic, ref labelPairFloat, labelArray);
            IDictionary <Label, double>     numberOfLabelTrue              = new Dictionary <Label, double>(labelFloatDic);
            IDictionary <Label, double>     numberOfLabelFalse             = new Dictionary <Label, double>(labelFloatDic);
            IDictionary <LabelPair, double> numberOfLabel1TrueLabel2True   = new Dictionary <LabelPair, double>(labelPairFloat);
            IDictionary <LabelPair, double> numberOfLabel1TrueLabel2False  = new Dictionary <LabelPair, double>(labelPairFloat);
            IDictionary <LabelPair, double> numberOfLabel1FalseLabel2True  = new Dictionary <LabelPair, double>(labelPairFloat);
            IDictionary <LabelPair, double> numberOfLabel1FalseLabel2False = new Dictionary <LabelPair, double>(labelPairFloat);

            IDictionary <Sentence, IDictionary <Label, double> >     ProbabilityOfLabelTrue              = new Dictionary <Sentence, IDictionary <Label, double> >();
            IDictionary <Sentence, IDictionary <Label, double> >     ProbabilityOfLabelFalse             = new Dictionary <Sentence, IDictionary <Label, double> >();
            IDictionary <Sentence, IDictionary <LabelPair, double> > ProbabilityOfLabel1TrueLabel2True   = new Dictionary <Sentence, IDictionary <LabelPair, double> >();
            IDictionary <Sentence, IDictionary <LabelPair, double> > ProbabilityOfLabel1TrueLabel2False  = new Dictionary <Sentence, IDictionary <LabelPair, double> >();
            IDictionary <Sentence, IDictionary <LabelPair, double> > ProbabilityOfLabel1FalseLabel2True  = new Dictionary <Sentence, IDictionary <LabelPair, double> >();
            IDictionary <Sentence, IDictionary <LabelPair, double> > ProbabilityOfLabel1FalseLabel2False = new Dictionary <Sentence, IDictionary <LabelPair, double> >();

            //每句的信息,用于树的具体值(每个句子对应的树的值不同)
            foreach (Sentence sentence in Variable.Sentences)
            {
                ProbabilityOfLabelTrue.Add(sentence, new Dictionary <Label, double>(labelFloatDic));
                ProbabilityOfLabelFalse.Add(sentence, new Dictionary <Label, double>(labelFloatDic));
                ProbabilityOfLabel1TrueLabel2True.Add(sentence, new Dictionary <LabelPair, double>(labelPairFloat));
                ProbabilityOfLabel1TrueLabel2False.Add(sentence, new Dictionary <LabelPair, double>(labelPairFloat));
                ProbabilityOfLabel1FalseLabel2True.Add(sentence, new Dictionary <LabelPair, double>(labelPairFloat));
                ProbabilityOfLabel1FalseLabel2False.Add(sentence, new Dictionary <LabelPair, double>(labelPairFloat));
            }
            #endregion
            #region 求互信息的参数
            int N = 0;
            foreach (Sentence sentence in Variable.Sentences)
            {
                foreach (Annotation annotation in sentence.AnnotaitonGroups[group].AnnotatorAnnotationDic.Values)
                {
                    ++N;
                    IList <Label> traversedLabels = new List <Label>();
                    foreach (Label label1 in labelArray)
                    {
                        traversedLabels.Add(label1);
                        if (annotation.Labels[label1])
                        {
                            ++numberOfLabelTrue[label1];
                            ++ProbabilityOfLabelTrue[sentence][label1];
                            foreach (Label label2 in labelArray)
                            {
                                if (!traversedLabels.Contains(label2))
                                {
                                    if (annotation.Labels[label2])
                                    {
                                        ++numberOfLabel1TrueLabel2True[new LabelPair(label1, label2)];
                                        ++ProbabilityOfLabel1TrueLabel2True[sentence][new LabelPair(label1, label2)];
                                    }
                                    else
                                    {
                                        ++numberOfLabel1TrueLabel2False[new LabelPair(label1, label2)];
                                        ++ProbabilityOfLabel1TrueLabel2False[sentence][new LabelPair(label1, label2)];
                                    }
                                }
                            }
                        }
                        else
                        {
                            ++numberOfLabelFalse[label1];
                            ++ProbabilityOfLabelFalse[sentence][label1];
                            foreach (Label label2 in labelArray)
                            {
                                if (!traversedLabels.Contains(label2))
                                {
                                    if (annotation.Labels[label2])
                                    {
                                        ++numberOfLabel1FalseLabel2True[new LabelPair(label1, label2)];
                                        ++ProbabilityOfLabel1FalseLabel2True[sentence][new LabelPair(label1, label2)];
                                    }
                                    else
                                    {
                                        ++numberOfLabel1FalseLabel2False[new LabelPair(label1, label2)];
                                        ++ProbabilityOfLabel1FalseLabel2False[sentence][new LabelPair(label1, label2)];
                                    }
                                }
                            }
                        }
                    }
                }
            }
            #endregion
            #region 求树(全部一棵树)
            IList <KeyValuePair <LabelPair, double> > tree = TDDSFunction.GenerateIMTree(numberOfLabelTrue, numberOfLabelFalse,
                                                                                         numberOfLabel1TrueLabel2True, numberOfLabel1TrueLabel2False,
                                                                                         numberOfLabel1FalseLabel2True, numberOfLabel1FalseLabel2False, N, labelArray);//此处是导致多线程结果不同的原因:虽然换组时Variable.LabelArray不会变化,但原先sentence中用于CommonTree和DistinctTree计算的成员属性没有做区分。
            #endregion
            #region 初始化Sij
            if (TDDSVariable.SmoothTree == Smoothing.None)
            {
                foreach (Sentence sentence in Variable.Sentences)
                {
                    foreach (Label label in labelArray)
                    {
                        ProbabilityOfLabelTrue[sentence][label]  /= Variable.NumberOfAnnotationsPerSentenceAfterGrouping;
                        ProbabilityOfLabelFalse[sentence][label] /= Variable.NumberOfAnnotationsPerSentenceAfterGrouping;
                    }
                    foreach (LabelPair labelPair in labelPairFloat.Keys)
                    {
                        ProbabilityOfLabel1TrueLabel2True[sentence][labelPair]   /= Variable.NumberOfAnnotationsPerSentenceAfterGrouping;
                        ProbabilityOfLabel1TrueLabel2False[sentence][labelPair]  /= Variable.NumberOfAnnotationsPerSentenceAfterGrouping;
                        ProbabilityOfLabel1FalseLabel2True[sentence][labelPair]  /= Variable.NumberOfAnnotationsPerSentenceAfterGrouping;
                        ProbabilityOfLabel1FalseLabel2False[sentence][labelPair] /= Variable.NumberOfAnnotationsPerSentenceAfterGrouping;
                    }
                }
            }
            else
            {
                IDictionary <Smoothing, double[]> smoothingNumber = Function.SmoothingNumber(Variable.NumberOfAnnotationsPerSentenceAfterGrouping);
                foreach (Sentence sentence in Variable.Sentences)
                {
                    foreach (Label label in labelArray)
                    {
                        ProbabilityOfLabelTrue[sentence][label]  = (ProbabilityOfLabelTrue[sentence][label] + smoothingNumber[TDDSVariable.SmoothTree][0]) / (Variable.NumberOfAnnotationsPerSentenceAfterGrouping + smoothingNumber[TDDSVariable.SmoothTree][1]);
                        ProbabilityOfLabelFalse[sentence][label] = (ProbabilityOfLabelFalse[sentence][label] + smoothingNumber[TDDSVariable.SmoothTree][0]) / (Variable.NumberOfAnnotationsPerSentenceAfterGrouping + smoothingNumber[TDDSVariable.SmoothTree][1]);;
                    }
                    foreach (LabelPair labelPair in labelPairFloat.Keys)
                    {
                        ProbabilityOfLabel1TrueLabel2True[sentence][labelPair]   = (ProbabilityOfLabel1TrueLabel2True[sentence][labelPair] + smoothingNumber[TDDSVariable.SmoothTree][0]) / (Variable.NumberOfAnnotationsPerSentenceAfterGrouping + smoothingNumber[TDDSVariable.SmoothTree][1]);
                        ProbabilityOfLabel1TrueLabel2False[sentence][labelPair]  = (ProbabilityOfLabel1TrueLabel2False[sentence][labelPair] + smoothingNumber[TDDSVariable.SmoothTree][0]) / (Variable.NumberOfAnnotationsPerSentenceAfterGrouping + smoothingNumber[TDDSVariable.SmoothTree][1]);
                        ProbabilityOfLabel1FalseLabel2True[sentence][labelPair]  = (ProbabilityOfLabel1FalseLabel2True[sentence][labelPair] + smoothingNumber[TDDSVariable.SmoothTree][0]) / (Variable.NumberOfAnnotationsPerSentenceAfterGrouping + smoothingNumber[TDDSVariable.SmoothTree][1]);
                        ProbabilityOfLabel1FalseLabel2False[sentence][labelPair] = (ProbabilityOfLabel1FalseLabel2False[sentence][labelPair] + smoothingNumber[TDDSVariable.SmoothTree][0]) / (Variable.NumberOfAnnotationsPerSentenceAfterGrouping + smoothingNumber[TDDSVariable.SmoothTree][1]);
                    }
                }
            }
            foreach (Sentence sentence in Variable.Sentences)
            {
                for (int l = 0; l < Math.Pow(2, Variable.LabelArray.Length); ++l)
                {
                    Labelset Labelset = new Labelset(Variable.LabelArray, l);
                    TDDSVariable.Sij.Value[sentence].Add(Labelset, 1);
                    if (TDDSVariable.SmoothTree == Smoothing.None)
                    {
                        if (Labelset.Labels[tree[0].Key.First])
                        {
                            if (ProbabilityOfLabelTrue[sentence][tree[0].Key.First] != 0)
                            {
                                TDDSVariable.Sij.Value[sentence][Labelset] *= ProbabilityOfLabelTrue[sentence][tree[0].Key.First];//应该除,除后准确率更高,原因未知
                            }
                        }
                        else
                        {
                            if (ProbabilityOfLabelFalse[sentence][tree[0].Key.First] != 0)
                            {
                                TDDSVariable.Sij.Value[sentence][Labelset] *= ProbabilityOfLabelFalse[sentence][tree[0].Key.First];
                            }
                        }
                    }
                    else
                    {
                        if (Labelset.Labels[tree[0].Key.First])
                        {
                            TDDSVariable.Sij.Value[sentence][Labelset] *= ProbabilityOfLabelTrue[sentence][tree[0].Key.First];
                        }
                        else
                        {
                            TDDSVariable.Sij.Value[sentence][Labelset] *= ProbabilityOfLabelFalse[sentence][tree[0].Key.First];
                        }
                    }
                    foreach (KeyValuePair <LabelPair, double> labelPairAndValue in tree)
                    {
                        LabelPair labelPair = labelPairAndValue.Key;
                        if (TDDSVariable.SmoothTree == Smoothing.None)
                        {
                            if (Labelset.Labels[labelPair.First])
                            {
                                if (ProbabilityOfLabelTrue[sentence][labelPair.First] != 0)//考虑分母为0的情况
                                {
                                    if (Labelset.Labels[labelPair.Second])
                                    {
                                        if (ProbabilityOfLabel1TrueLabel2True[sentence].ContainsKey(labelPair))
                                        {
                                            TDDSVariable.Sij.Value[sentence][Labelset] *= ProbabilityOfLabel1TrueLabel2True[sentence][labelPair] / ProbabilityOfLabelTrue[sentence][labelPair.First];
                                        }
                                        else
                                        {
                                            TDDSVariable.Sij.Value[sentence][Labelset] *= ProbabilityOfLabel1TrueLabel2True[sentence][labelPair.Reverse] / ProbabilityOfLabelTrue[sentence][labelPair.First];
                                        }
                                    }
                                    else
                                    {
                                        if (ProbabilityOfLabel1TrueLabel2False[sentence].ContainsKey(labelPair))
                                        {
                                            TDDSVariable.Sij.Value[sentence][Labelset] *= ProbabilityOfLabel1TrueLabel2False[sentence][labelPair] / ProbabilityOfLabelTrue[sentence][labelPair.First];
                                        }
                                        else
                                        {
                                            TDDSVariable.Sij.Value[sentence][Labelset] *= ProbabilityOfLabel1FalseLabel2True[sentence][labelPair.Reverse] / ProbabilityOfLabelTrue[sentence][labelPair.First];
                                        }
                                    }
                                }
                                else
                                {
                                    TDDSVariable.Sij.Value[sentence][new Labelset(Variable.LabelArray, l)] *= 0;//此处应该是0,不是1
                                    break;
                                }
                            }
                            else
                            {
                                if (ProbabilityOfLabelFalse[sentence][labelPair.First] != 0)//考虑分母为0的情况
                                {
                                    if (Labelset.Labels[labelPair.Second])
                                    {
                                        if (ProbabilityOfLabel1FalseLabel2True[sentence].ContainsKey(labelPair))
                                        {
                                            TDDSVariable.Sij.Value[sentence][Labelset] *= ProbabilityOfLabel1FalseLabel2True[sentence][labelPair] / ProbabilityOfLabelFalse[sentence][labelPair.First];
                                        }
                                        else
                                        {
                                            TDDSVariable.Sij.Value[sentence][Labelset] *= ProbabilityOfLabel1TrueLabel2False[sentence][labelPair.Reverse] / ProbabilityOfLabelFalse[sentence][labelPair.First];
                                        }
                                    }
                                    else
                                    {
                                        if (ProbabilityOfLabel1FalseLabel2False[sentence].ContainsKey(labelPair))
                                        {
                                            TDDSVariable.Sij.Value[sentence][Labelset] *= ProbabilityOfLabel1FalseLabel2False[sentence][labelPair] / ProbabilityOfLabelFalse[sentence][labelPair.First];
                                        }
                                        else
                                        {
                                            TDDSVariable.Sij.Value[sentence][Labelset] *= ProbabilityOfLabel1FalseLabel2False[sentence][labelPair.Reverse] / ProbabilityOfLabelFalse[sentence][labelPair.First];
                                        }
                                    }
                                }
                                else
                                {
                                    TDDSVariable.Sij.Value[sentence][new Labelset(Variable.LabelArray, l)] *= 0;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            if (Labelset.Labels[labelPair.First])
                            {
                                if (Labelset.Labels[labelPair.Second])
                                {
                                    if (ProbabilityOfLabel1TrueLabel2True[sentence].ContainsKey(labelPair))
                                    {
                                        TDDSVariable.Sij.Value[sentence][Labelset] *= ProbabilityOfLabel1TrueLabel2True[sentence][labelPair] / ProbabilityOfLabelTrue[sentence][labelPair.First];
                                    }
                                    else
                                    {
                                        TDDSVariable.Sij.Value[sentence][Labelset] *= ProbabilityOfLabel1TrueLabel2True[sentence][labelPair.Reverse] / ProbabilityOfLabelTrue[sentence][labelPair.First];
                                    }
                                }
                                else
                                {
                                    if (ProbabilityOfLabel1TrueLabel2False[sentence].ContainsKey(labelPair))
                                    {
                                        TDDSVariable.Sij.Value[sentence][Labelset] *= ProbabilityOfLabel1TrueLabel2False[sentence][labelPair] / ProbabilityOfLabelTrue[sentence][labelPair.First];
                                    }
                                    else
                                    {
                                        TDDSVariable.Sij.Value[sentence][Labelset] *= ProbabilityOfLabel1FalseLabel2True[sentence][labelPair.Reverse] / ProbabilityOfLabelTrue[sentence][labelPair.First];
                                    }
                                }
                            }
                            else
                            {
                                if (Labelset.Labels[labelPair.Second])
                                {
                                    if (ProbabilityOfLabel1FalseLabel2True[sentence].ContainsKey(labelPair))
                                    {
                                        TDDSVariable.Sij.Value[sentence][Labelset] *= ProbabilityOfLabel1FalseLabel2True[sentence][labelPair] / ProbabilityOfLabelFalse[sentence][labelPair.First];
                                    }
                                    else
                                    {
                                        TDDSVariable.Sij.Value[sentence][Labelset] *= ProbabilityOfLabel1TrueLabel2False[sentence][labelPair.Reverse] / ProbabilityOfLabelFalse[sentence][labelPair.First];
                                    }
                                }
                                else
                                {
                                    if (ProbabilityOfLabel1FalseLabel2False[sentence].ContainsKey(labelPair))
                                    {
                                        TDDSVariable.Sij.Value[sentence][Labelset] *= ProbabilityOfLabel1FalseLabel2False[sentence][labelPair] / ProbabilityOfLabelFalse[sentence][labelPair.First];
                                    }
                                    else
                                    {
                                        TDDSVariable.Sij.Value[sentence][Labelset] *= ProbabilityOfLabel1FalseLabel2False[sentence][labelPair.Reverse] / ProbabilityOfLabelFalse[sentence][labelPair.First];
                                    }
                                }
                            }
                        }
                    }
                }
            }
            //for (int i = 0; i < Variable.Sentences.Count; ++i)
            //{
            //    double all = 0;
            //    for (int j = 0; j < DependentVariable.NumberOfIntlabel; ++j)
            //    {
            //        all += TDDSVariable.Sij.Value[sentence][new Labelset(bilabel.ToArray(), j)];
            //    }
            //    for (int j = 0; j < DependentVariable.NumberOfIntlabel; ++j)
            //    {
            //        TDDSVariable.Sij.Value[sentence][new Labelset(bilabel.ToArray(), j)] /= all;
            //    }
            //}
            #endregion
            //Variable.OutputFile.WriteLine(TDDSVariable.Sij.ToString(DependentVariable.NumberOfIntlabel));
            //Variable.OutputFile.Close();
            //double[] ii = new double[Variable.Sentences.Count];
            //foreach (Sentence Sentence in Variable.Sentences)
            //{
            //    for (int l = 0; l < DependentVariable.NumberOfIntlabel; ++l)
            //    {
            //        ii[Sentence.ID] += TDDSVariable.Sij.Value[Sentence.ID, l];
            //    }
            //}
        }
Example #20
0
        //将label分成两组(strong-willed, weak-willed)
        static public void GroupLabels()
        {
            #region 注释(5个最大为真)
            //IDictionary<string, Label[]> characterConsistencies = new Dictionary<string, Label[]>();
            //characterConsistencies.Add("勇", new Label[] { Label.excitement, Label.sadness, Label.anger, Label.disgust, Label.surprise });
            //characterConsistencies.Add("政", new Label[] { Label.excitement, Label.happiness, Label.sadness, Label.anger, Label.disgust });
            //characterConsistencies.Add("お母さん", new Label[] { Label.relief, Label.happiness, Label.sadness, Label.anger, Label.disgust });
            //characterConsistencies.Add("小野", new Label[] { Label.excitement, Label.fondness, Label.happiness, Label.anger, Label.surprise });
            //characterConsistencies.Add("山田", new Label[] { Label.excitement, Label.fondness, Label.happiness, Label.anger, Label.surprise });
            //characterConsistencies.Add("他の子", new Label[] { Label.excitement, Label.fondness, Label.happiness, Label.sadness, Label.fear });
            //characterConsistencies.Add("先生", new Label[] { Label.excitement, Label.relief, Label.fondness, Label.anger, Label.disgust });
            //characterConsistencies.Add("尾沢先生", new Label[] { Label.relief, Label.fondness, Label.sadness, Label.disgust, Label.fear });
            //characterConsistencies.Add("二郎", new Label[] { Label.excitement, Label.relief, Label.sadness, Label.anger, Label.disgust });
            //characterConsistencies.Add("誠", new Label[] { Label.excitement, Label.relief, Label.fondness, Label.happiness, Label.sadness });
            //characterConsistencies.Add("正", new Label[] { Label.excitement, Label.sadness, Label.anger, Label.disgust, Label.fear });
            //characterConsistencies.Add("新", new Label[] { Label.relief, Label.fondness, Label.happiness, Label.sadness, Label.disgust });
            //characterConsistencies.Add("年", new Label[] { Label.excitement, Label.relief, Label.fondness, Label.happiness, Label.sadness });
            //characterConsistencies.Add("声", new Label[] { Label.excitement, Label.relief, Label.fondness, Label.happiness, Label.surprise });
            //characterConsistencies.Add("きみ子", new Label[] { Label.excitement, Label.relief, Label.fondness, Label.happiness, Label.sadness });
            //characterConsistencies.Add("二人", new Label[] { Label.excitement, Label.relief, Label.fondness, Label.happiness, Label.sadness });
            //characterConsistencies.Add("おばさん", new Label[] { Label.excitement, Label.relief, Label.fondness, Label.happiness, Label.surprise });
            //characterConsistencies.Add("お姉さん", new Label[] { Label.excitement, Label.relief, Label.fondness, Label.happiness, Label.sadness });
            //characterConsistencies.Add("お母さん", new Label[] { Label.excitement, Label.relief, Label.anger, Label.disgust, Label.fear });
            //characterConsistencies.Add("お父さん", new Label[] { Label.excitement, Label.relief, Label.sadness, Label.anger, Label.disgust });
            //characterConsistencies.Add("米屋さん", new Label[] { Label.excitement, Label.relief, Label.fondness, Label.happiness, Label.surprise });
            //if (((IList)characterConsistencies["勇"]).Contains(Label.excitement)) { }
            #endregion

            IDictionary <Label, int[]> labelDistribution = new Dictionary <Label, int[]>();
            labelDistribution.Add(Label.sadness, new int[] { 76, 166, 93, 0, 5, 25, 4, 16, 97, 213, 16, 32, 22, 0, 56, 5, 0, 7, 1, 72, 1 });
            labelDistribution.Add(Label.relief, new int[] { 50, 84, 245, 8, 8, 12, 61, 14, 78, 211, 0, 29, 25, 1, 29, 42, 34, 10, 8, 32, 17 });
            labelDistribution.Add(Label.fondness, new int[] { 28, 59, 101, 23, 43, 31, 5, 2, 32, 132, 0, 51, 27, 8, 66, 14, 74, 18, 2, 18, 25 });
            labelDistribution.Add(Label.happiness, new int[] { 53, 141, 116, 15, 30, 41, 3, 0, 33, 127, 0, 30, 6, 17, 76, 46, 79, 8, 1, 16, 19 });
            labelDistribution.Add(Label.excitement, new int[] { 118, 134, 13, 13, 24, 14, 12, 1, 91, 141, 9, 6, 7, 16, 30, 20, 13, 3, 4, 30, 9 });
            labelDistribution.Add(Label.disgust, new int[] { 131, 123, 21, 9, 7, 12, 7, 2, 74, 58, 16, 8, 0, 0, 16, 0, 0, 0, 9, 97, 1 });
            labelDistribution.Add(Label.anger, new int[] { 184, 180, 114, 24, 30, 5, 126, 0, 91, 76, 23, 3, 0, 0, 7, 0, 0, 0, 3, 39, 0 });
            labelDistribution.Add(Label.surprise, new int[] { 122, 55, 74, 10, 17, 11, 2, 0, 32, 86, 3, 2, 0, 22, 10, 3, 9, 0, 1, 19, 3 });
            labelDistribution.Add(Label.fear, new int[] { 55, 58, 13, 0, 1, 17, 2, 2, 30, 77, 6, 1, 2, 0, 22, 1, 0, 2, 3, 19, 1 });
            labelDistribution.Add(Label.shame, new int[] { 33, 38, 16, 0, 1, 2, 1, 0, 26, 13, 0, 7, 0, 0, 25, 0, 0, 0, 0, 5, 8 });

            //简单差
            IDictionary <Label, int> varianceFromRelief = new Dictionary <Label, int>();
            foreach (Label label in Variable.LabelArray)
            {
                int variance = 0;
                for (int i = 0; i < 21; ++i)
                {
                    variance += Math.Abs(labelDistribution[Label.excitement][i] - labelDistribution[label][i]);
                }
                varianceFromRelief.Add(label, variance);
            }
            List <KeyValuePair <Label, int> > sortedVariances = new List <KeyValuePair <Label, int> >(varianceFromRelief);
            sortedVariances.Sort(delegate(KeyValuePair <Label, int> s1, KeyValuePair <Label, int> s2)
            {
                return(s1.Value.CompareTo(s2.Value));
            });

            //欧几里得距离
            IDictionary <Label, double> distanceFromRelief = new Dictionary <Label, double>();
            foreach (Label label in Variable.LabelArray)
            {
                double distance = 0;
                for (int i = 0; i < 21; ++i)
                {
                    distance += Math.Pow(labelDistribution[Label.excitement][i] - labelDistribution[label][i], 2);
                }
                distanceFromRelief.Add(label, Math.Pow(distance, 0.5));
            }
            List <KeyValuePair <Label, double> > sortedDistance = new List <KeyValuePair <Label, double> >(distanceFromRelief);
            sortedDistance.Sort(delegate(KeyValuePair <Label, double> s1, KeyValuePair <Label, double> s2)
            {
                return(s1.Value.CompareTo(s2.Value));
            });

            //先匹配,人工分组
            IList <LabelPair> labelPairList   = new List <LabelPair>();//前后无序,45个,用于初始化
            IList <Label>     traversedLabels = new List <Label>();
            foreach (Label label1 in Variable.LabelArray)
            {
                traversedLabels.Add(label1);
                foreach (Label label2 in Variable.LabelArray)
                {
                    if (!traversedLabels.Contains(label2))
                    {
                        LabelPair labelPair = new LabelPair(label1, label2);
                        double    distance  = 0;
                        for (int i = 0; i < 21; ++i)//两个故事,21个角色
                        {
                            //distance += Math.Abs(labelDistribution[label1][i] - labelDistribution[label2][i]);
                            distance += Math.Pow(labelDistribution[label1][i] - labelDistribution[label2][i], 2);
                        }
                        //labelPair.Weight = distance;
                        labelPair.Weight = Math.Pow(distance, 0.5);
                        labelPairList.Add(labelPair);
                    }
                }
            }
            LabelPair[] labelPairs = MultilabelEstimation.Algorithm.PDS.PDSFunction.MinimumWeightedPerfectMatching(labelPairList, true);
        }