Пример #1
0
 static public void RunPeTM(PorSForJointje PorS, Smoothing SmoothingBE, BnOrNot bnOrNot)
 {
     double[] accuracyOfPersonalityForEachGroup = new double[GroupVariable.AnnotatorGroups.Length];
     for (int groupIndex = 0; groupIndex < GroupVariable.AnnotatorGroups.Length; ++groupIndex)
     {
         IDictionary <Annotator, IDictionary <Character, IDictionary <Will, double> > > okcx = PersonalityFunction.CalculateOkcx(groupIndex);//模拟人对角色个性的标注,计算一次就不变了
         Mce  mce  = InitializeMce(groupIndex);
         Sije sije = null;
         if (bnOrNot == BnOrNot.Yes)
         {
             sije = InitializeSijeWithBN(mce, groupIndex);
         }
         else
         {
             sije = InitializeSije(mce, groupIndex); //old new
         }
         Pje            pje    = null;               //p(t|e)
         Pdata          pdata  = null;
         IList <double> Pdatas = new List <double>();
         for (int convergeTime = 1; convergeTime <= Variable.ConvergeTimeThreshold; ++convergeTime)
         {
             mce = CalculateMce(sije, groupIndex);
             PAkjl pakjl = CoreFunction.CalculatePAkjl(Variable.LabelArray, sije.ToSij, convergeTime, groupIndex);
             BEkef bekef = PersonalityFunction.CalculateBExy(mce, okcx, SmoothingBE, convergeTime, groupIndex);
             if (PorS == PorSForJointje.P)
             {
                 pje = CalculatePje(sije, convergeTime);
             }
             if (CalculatePdataAndSije(ref sije, pakjl, bekef, pje, mce, okcx, ref pdata, Pdatas, groupIndex))//old/new
             {
                 break;
             }
         }
         IDictionary <Sentence, IDictionary <Will, double> > sic = ObtainBinaryResult(sije, mce, groupIndex);
         WriteBinaryResultFile(sic, mce, groupIndex);
         PersonalityFunction.WriteMVResultFile(mce, groupIndex);
         accuracyOfPersonalityForEachGroup[groupIndex] = PersonalityPaperFunction.AccuracyOfPersonalityForEachGroup(PersonalityVariable.TruePersonality, mce.EstimatedPersonality);
     }
     Function.ConsoleWriteLine("Accuracy Of PeTM: " + PersonalityPaperFunction.AccuracyOfPersonality(accuracyOfPersonalityForEachGroup));
 }
Пример #2
0
        //废弃
        static private bool CalculatePdataAndSijeOld(ref Sije sije, PAkjl pakjl, BEkef bekef, Pje pje, Mce mce, IDictionary <Annotator, IDictionary <Character, IDictionary <Will, double> > > okxc, ref Pdata pdata, IList <double> pdatas, int groupIndex)
        {
            bool isFinished = false;
            //sije的分子
            IDictionary <Sentence, IDictionary <Labelset, IDictionary <Will, double> > > numerator = new Dictionary <Sentence, IDictionary <Labelset, IDictionary <Will, double> > >();
            //sij的分母(P(data on i))
            IDictionary <Sentence, double> denominator = new Dictionary <Sentence, double>();

            //计算分子
            foreach (Sentence sentence in sije.Value.Keys)
            {
                numerator.Add(sentence, new Dictionary <Labelset, IDictionary <Will, double> >());
                //开始计算
                foreach (Labelset labelsetj in sije.Value[sentence].Keys)//j: true label
                {
                    double valueOfNumeratorForStrong = 1;
                    double valueOfNumeratorForWeak   = 1;
                    foreach (Annotator annotator in sentence.AnnotaitonGroups[groupIndex].AnnotatorAnnotationDic.Keys)
                    {
                        Labelset labelsetl = sentence.AnnotaitonGroups[groupIndex].AnnotatorAnnotationDic[annotator].ToLabelset(Variable.LabelArray);
                        if (pakjl.Value[annotator].ContainsKey(labelsetj))
                        {
                            if (pakjl.Value[annotator][labelsetj].ContainsKey(labelsetl))
                            {
                                valueOfNumeratorForStrong *= pakjl.Value[annotator][labelsetj][labelsetl];
                                valueOfNumeratorForWeak   *= pakjl.Value[annotator][labelsetj][labelsetl];
                            }
                            else
                            {
                                valueOfNumeratorForStrong = 0;
                                valueOfNumeratorForWeak   = 0;
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                        //β部分的值(Personality新增)
                        valueOfNumeratorForStrong *= Math.Pow(bekef.Value[annotator][Tuple.Create(Will.strong, Will.strong)], okxc[annotator][sentence.Character][Will.strong])
                                                     * Math.Pow(bekef.Value[annotator][Tuple.Create(Will.strong, Will.weak)], okxc[annotator][sentence.Character][Will.weak]);
                        valueOfNumeratorForWeak *= Math.Pow(bekef.Value[annotator][Tuple.Create(Will.weak, Will.strong)], okxc[annotator][sentence.Character][Will.strong])
                                                   * Math.Pow(bekef.Value[annotator][Tuple.Create(Will.weak, Will.weak)], okxc[annotator][sentence.Character][Will.weak]);
                    }
                    //if (valueOfNumerator == 0) continue;
                    //乘以P(t|c)*P(c)
                    valueOfNumeratorForStrong *= sije.Value[sentence][labelsetj][Will.strong] * mce.Value[sentence.Character][Will.strong] * Math.Pow(labelsetj.HowStrong, 1);
                    valueOfNumeratorForWeak   *= sije.Value[sentence][labelsetj][Will.weak] * mce.Value[sentence.Character][Will.weak] * Math.Pow(labelsetj.HowWeak, 1);
                    if (valueOfNumeratorForStrong != 0 || valueOfNumeratorForWeak != 0)
                    {
                        numerator[sentence].Add(labelsetj, new Dictionary <Will, double>());
                        numerator[sentence][labelsetj].Add(Will.strong, valueOfNumeratorForStrong);
                        numerator[sentence][labelsetj].Add(Will.weak, valueOfNumeratorForWeak);
                    }
                }
                double valueOfDenominator = 0;
                foreach (Labelset Labelsetq in numerator[sentence].Keys)//因为是加,故只需遍历numerator里有的标注,不需遍历所有标注
                {
                    valueOfDenominator += numerator[sentence][Labelsetq][Will.strong];
                    valueOfDenominator += numerator[sentence][Labelsetq][Will.weak];
                }
                denominator.Add(sentence, valueOfDenominator);
            }

            //计算Pdata和Sij
            pdata = pdata != null ? new Pdata(++pdata.Time, pdata.Value) : new Pdata(1, 0);
            sije  = new Sije(++sije.Time);
            foreach (Sentence sentence in Variable.Sentences)
            {
                sije.Value.Add(sentence, new Dictionary <Labelset, IDictionary <Will, double> >());
                foreach (Labelset labelset in numerator[sentence].Keys)
                {
                    sije.Value[sentence].Add(labelset, new Dictionary <Will, double>());
                    sije.Value[sentence][labelset][Will.strong] = numerator[sentence][labelset][Will.strong] / denominator[sentence];//Dic赋值时没有的元素会自动加
                    sije.Value[sentence][labelset][Will.weak]   = numerator[sentence][labelset][Will.weak] / denominator[sentence];
                }
                pdata.Value += -Math.Log(denominator[sentence]);
            }
            if (pdatas.Contains(pdata.Value) || (Math.Abs(pdata.MondifiedValue) <= Variable.ConvergeValueThreshold))
            {
                isFinished = true;
            }
            else
            {
                pdatas.Add(pdata.Value);
            }
            if (Variable.OutputPdata)
            {
                Variable.OutputFile.WriteLine(pdata.ToString());
            }
            return(isFinished);
        }
Пример #3
0
        static private bool CalculatePdataAndSije(ref Sije sije, PAkjl pakjl, BEkef bekef, Pje pje, Mce mce, IDictionary <Annotator, IDictionary <Character, IDictionary <Will, double> > > okxc, ref Pdata pdata, IList <double> pdatas, int groupIndex)
        {
            bool isFinished = false;
            //sije的分子
            IDictionary <Sentence, IDictionary <Labelset, IDictionary <Will, double> > > numerator = new Dictionary <Sentence, IDictionary <Labelset, IDictionary <Will, double> > >();
            //sij的分母(P(data on i))
            IDictionary <Sentence, double> denominator = new Dictionary <Sentence, double>();

            //计算分子
            foreach (Sentence sentence in sije.Value.Keys)
            {
                numerator.Add(sentence, new Dictionary <Labelset, IDictionary <Will, double> >());

                #region 联合概率P(t,m)
                IDictionary <Labelset, IDictionary <Will, double> > jointje;
                if (pje == null)//PorS == PorSForJointje.S
                {
                    //求后验概率P(t|e),新增
                    jointje = new Dictionary <Labelset, IDictionary <Will, double> >();
                    IDictionary <Will, double> willDenominator = new Dictionary <Will, double>();
                    willDenominator.Add(Will.strong, 0);
                    willDenominator.Add(Will.weak, 0);
                    foreach (Labelset labelset in sije.Value[sentence].Keys)
                    {
                        //double valueOfStrong = sije.Value[sentence][labelset][Will.strong] * labelset.HowStrong;//这两个不科学
                        //double valueOfWeak = sije.Value[sentence][labelset][Will.weak] * labelset.HowWeak;
                        double valueOfStrong = sije.Value[sentence][labelset][Will.strong];
                        double valueOfWeak   = sije.Value[sentence][labelset][Will.weak];
                        if (jointje.ContainsKey(labelset))
                        {
                            jointje[labelset][Will.strong] += valueOfStrong;
                            jointje[labelset][Will.weak]   += valueOfWeak;
                        }
                        else
                        {
                            jointje.Add(labelset, new Dictionary <Will, double>());
                            jointje[labelset].Add(Will.strong, valueOfStrong);
                            jointje[labelset].Add(Will.weak, valueOfWeak);
                        }
                        willDenominator[Will.strong] += valueOfStrong;
                        willDenominator[Will.weak]   += valueOfWeak;
                    }
                    //p(t|e)
                    foreach (Labelset labelset in jointje.Keys.ToArray())
                    {
                        if (willDenominator[Will.strong] != 0)
                        {
                            jointje[labelset][Will.strong] /= willDenominator[Will.strong];
                        }
                        if (willDenominator[Will.weak] != 0)
                        {
                            jointje[labelset][Will.weak] /= willDenominator[Will.weak];
                        }
                    }
                    //p(t|e)*p(x)
                    if (willDenominator[Will.strong] != 0 && willDenominator[Will.weak] != 0)//有一个等于0就不用再算了
                    {
                        foreach (Labelset labelset in jointje.Keys.ToArray())
                        {
                            jointje[labelset][Will.strong] *= mce.Value[sentence.Character][Will.strong];
                            jointje[labelset][Will.weak]   *= mce.Value[sentence.Character][Will.weak];
                        }
                    }
                }
                else//PorS == PorSForJointje.P
                {
                    jointje = pje.Value;
                    double valueOfStrong = 0;
                    double valueOfWeak   = 0;
                    foreach (Labelset labelset in jointje.Keys.ToArray())
                    {
                        valueOfStrong += jointje[labelset][Will.strong];
                        valueOfWeak   += jointje[labelset][Will.weak];
                    }
                    if (valueOfStrong != 0 && valueOfWeak != 0)
                    {
                        foreach (Labelset labelset in jointje.Keys.ToArray())
                        {
                            jointje[labelset][Will.strong] *= mce.Value[sentence.Character][Will.strong];
                            jointje[labelset][Will.weak]   *= mce.Value[sentence.Character][Will.weak];
                        }
                    }
                }
                #endregion

                foreach (Labelset labelsetj in jointje.Keys)//j: true label
                {
                    #region P({n}|t,e)
                    double valueOfNumeratorForStrong = 1;
                    double valueOfNumeratorForWeak   = 1;
                    foreach (Annotator annotator in sentence.AnnotaitonGroups[groupIndex].AnnotatorAnnotationDic.Keys)
                    {
                        Labelset labelsetl = sentence.AnnotaitonGroups[groupIndex].AnnotatorAnnotationDic[annotator].ToLabelset(Variable.LabelArray);
                        if (pakjl.Value[annotator].ContainsKey(labelsetj))
                        {
                            if (pakjl.Value[annotator][labelsetj].ContainsKey(labelsetl))
                            {
                                valueOfNumeratorForStrong *= pakjl.Value[annotator][labelsetj][labelsetl];
                                valueOfNumeratorForWeak   *= pakjl.Value[annotator][labelsetj][labelsetl];
                            }
                            else
                            {
                                valueOfNumeratorForStrong = 0;
                                valueOfNumeratorForWeak   = 0;
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                        //β部分的值(Personality新增)
                        valueOfNumeratorForStrong *= Math.Pow(bekef.Value[annotator][Tuple.Create(Will.strong, Will.strong)], okxc[annotator][sentence.Character][Will.strong])
                                                     * Math.Pow(bekef.Value[annotator][Tuple.Create(Will.strong, Will.weak)], okxc[annotator][sentence.Character][Will.weak]);
                        valueOfNumeratorForWeak *= Math.Pow(bekef.Value[annotator][Tuple.Create(Will.weak, Will.strong)], okxc[annotator][sentence.Character][Will.strong])
                                                   * Math.Pow(bekef.Value[annotator][Tuple.Create(Will.weak, Will.weak)], okxc[annotator][sentence.Character][Will.weak]);
                    }
                    #endregion
                    //乘以(P(t|e)*P(e))
                    valueOfNumeratorForStrong *= jointje[labelsetj][Will.strong];
                    valueOfNumeratorForWeak   *= jointje[labelsetj][Will.weak];
                    if (valueOfNumeratorForStrong != 0 || valueOfNumeratorForWeak != 0)
                    {
                        numerator[sentence].Add(labelsetj, new Dictionary <Will, double>());
                        numerator[sentence][labelsetj].Add(Will.strong, valueOfNumeratorForStrong);
                        numerator[sentence][labelsetj].Add(Will.weak, valueOfNumeratorForWeak);
                    }
                }

                double valueOfDenominator = 0;
                foreach (Labelset Labelsetq in numerator[sentence].Keys)//因为是加,故只需遍历numerator里有的标注,不需遍历所有标注
                {
                    valueOfDenominator += numerator[sentence][Labelsetq][Will.strong];
                    valueOfDenominator += numerator[sentence][Labelsetq][Will.weak];
                }
                denominator.Add(sentence, valueOfDenominator);
            }

            //计算Pdata和Sij
            pdata = pdata != null ? new Pdata(++pdata.Time, pdata.Value) : new Pdata(1, 0);
            sije  = new Sije(++sije.Time);
            foreach (Sentence sentence in Variable.Sentences)
            {
                sije.Value.Add(sentence, new Dictionary <Labelset, IDictionary <Will, double> >());
                foreach (Labelset labelset in numerator[sentence].Keys)
                {
                    sije.Value[sentence].Add(labelset, new Dictionary <Will, double>());
                    sije.Value[sentence][labelset][Will.strong] = numerator[sentence][labelset][Will.strong] / denominator[sentence];//Dic赋值时没有的元素会自动加
                    sije.Value[sentence][labelset][Will.weak]   = numerator[sentence][labelset][Will.weak] / denominator[sentence];
                }
                pdata.Value += -Math.Log(denominator[sentence]);
            }
            if (pdatas.Contains(pdata.Value) || (Math.Abs(pdata.MondifiedValue) <= Variable.ConvergeValueThreshold))
            {
                isFinished = true;
            }
            else
            {
                pdatas.Add(pdata.Value);
            }
            if (Variable.OutputPdata)
            {
                Variable.OutputFile.WriteLine(pdata.ToString());
            }
            return(isFinished);
        }