//指定will public KeyValuePair <Labelset, double> CalculateJointBestLabelset(Sentence sentence, Will will) { Labelset bestResult = new Labelset(Variable.LabelArray, 0); double bestResultValue = 0; foreach (Labelset subAnnotaton in this.Value[sentence].Keys)//此处决定了Sij.Value的Key不可以是Tuple<Sentence, Labelset>类型 { if (this.Value[sentence][subAnnotaton][will] > bestResultValue) { bestResult = new Labelset(subAnnotaton); bestResultValue = this.Value[sentence][subAnnotaton][will]; } else if (this.Value[sentence][subAnnotaton][will] == bestResultValue) { foreach (Label label in subAnnotaton.Labels.Keys) { if (subAnnotaton.Labels[label]) { bestResult.Labels[label] = true; } } } } return(new KeyValuePair <Labelset, double>(bestResult, bestResultValue)); }
private void labelsetLabelChanged(Labelset labelset, RouterInput routerInput, string oldText, string newText) { if ((labelset == this.labelset) && (currentInput == routerInput)) { CurrentValue = newText; } }
//不指定will,废弃 public KeyValuePair <Labelset, double> CalculateJointBestLabelset(Sentence sentence, ref IDictionary <Will, double> willForResult)//同是最大值的j们取并 { Labelset bestResult = null; double bestResultValue = 0; willForResult.Add(Will.strong, 0); willForResult.Add(Will.weak, 0); foreach (Labelset subAnnotaton in this.Value[sentence].Keys)//此处决定了Sij.Value的Key不可以是Tuple<Sentence, Labelset>类型 { if (this.Value[sentence][subAnnotaton][Will.strong] != 0) { if (this.Value[sentence][subAnnotaton][Will.strong] > bestResultValue) { bestResult = new Labelset(subAnnotaton); bestResultValue = this.Value[sentence][subAnnotaton][Will.strong]; willForResult[Will.strong] = this.Value[sentence][subAnnotaton][Will.strong]; willForResult[Will.weak] = this.Value[sentence][subAnnotaton][Will.weak]; } else if (this.Value[sentence][subAnnotaton][Will.strong] == bestResultValue) { foreach (Label label in subAnnotaton.Labels.Keys) { if (subAnnotaton.Labels[label]) { bestResult.Labels[label] = true; } } willForResult[Will.strong] = this.Value[sentence][subAnnotaton][Will.strong]; willForResult[Will.weak] = this.Value[sentence][subAnnotaton][Will.weak]; } } if (this.Value[sentence][subAnnotaton][Will.weak] != 0) { if (this.Value[sentence][subAnnotaton][Will.weak] > bestResultValue) { bestResult = new Labelset(subAnnotaton); bestResultValue = this.Value[sentence][subAnnotaton][Will.weak]; willForResult[Will.strong] = this.Value[sentence][subAnnotaton][Will.strong]; willForResult[Will.weak] = this.Value[sentence][subAnnotaton][Will.weak]; } else if (this.Value[sentence][subAnnotaton][Will.weak] == bestResultValue) { foreach (Label label in subAnnotaton.Labels.Keys) { if (subAnnotaton.Labels[label]) { bestResult.Labels[label] = true; } } willForResult[Will.strong] = this.Value[sentence][subAnnotaton][Will.strong]; willForResult[Will.weak] = this.Value[sentence][subAnnotaton][Will.weak]; } } } double will = willForResult[Will.strong] + willForResult[Will.weak]; willForResult[Will.strong] /= will; willForResult[Will.weak] /= will; return(new KeyValuePair <Labelset, double>(bestResult, bestResultValue)); }
public Substitute(Router router, int outputIndex, Labelset labelset) { if (router == null) { CurrentValue = "?"; return; } this.router = router; if (router.Outputs.Count < outputIndex) { CurrentValue = "?"; return; } output = router.Outputs[outputIndex - 1]; if (labelset == null) { CurrentValue = "?"; return; } this.labelset = labelset; currentInput = output.Crosspoint; output.CrosspointChanged += crosspointChangedHandler; CurrentValue = labelset.GetText(output.Crosspoint); labelset.LabelTextChanged += labelsetLabelChanged; }
public override void Init(object[] argumentObjects) { Router router = argumentObjects[0] as Router; if (router == null) { CurrentValue = "?"; return; } output = router.GetOutput((int)argumentObjects[1]); if (output == null) { CurrentValue = "?"; return; } output.CurrentSourceChanged += currentSourceChangedHandler; labelset = argumentObjects[2] as Labelset; if (labelset == null) { CurrentValue = "?"; return; } labelset.LabelTextChanged += labelsetTextChangedHandler; currentSource = output.CurrentSource; if (currentSource == null) { CurrentValue = "?"; return; } CurrentValue = labelset.GetLabel(currentSource).Text ?? "?"; }
//计算角色个性 static public IDictionary <Character, Tuple <Will, string> > GetGoldOfPersonality() { IDictionary <Character, Tuple <Will, string> > characterPersonalities = new Dictionary <Character, Tuple <Will, string> >(); foreach (Character character in ConsistencyVariable.Characters) { double numberOfTrueStrongAffects = 0; double numberOfTrueWeakAffects = 0; foreach (Sentence sentence in character.Sentences) { foreach (Annotator annotator in Variable.Annotators) { if (Variable.Data[annotator].ContainsKey(sentence)) { foreach (Annotation annotation in Variable.Data[annotator][sentence]) { Labelset labelset = annotation.ToLabelset(Variable.LabelArray); numberOfTrueStrongAffects += labelset.NumberOfTrueStrongAffects; numberOfTrueWeakAffects += labelset.NumberOfTrueWeakAffects; } } } } characterPersonalities.Add(character, Tuple.Create(numberOfTrueStrongAffects >= numberOfTrueWeakAffects ? Will.strong : Will.weak, "s:" + numberOfTrueStrongAffects + "; w:" + numberOfTrueWeakAffects)); } return(characterPersonalities); }
//废弃 static private Sije InitializeSijeOld(Mce mce, int groupIndex) { Sije sije = new Sije(1); foreach (Sentence sentence in Variable.Sentences) { double will = 0; sije.Value.Add(sentence, new Dictionary <Labelset, IDictionary <Will, double> >()); foreach (Annotation annotation in sentence.AnnotaitonGroups[groupIndex].AnnotatorAnnotationDic.Values) { Labelset labelset = annotation.ToLabelset(Variable.LabelArray); double strong = mce.Value[sentence.Character][Will.strong] * Math.Pow(labelset.HowStrong, 1); double weak = mce.Value[sentence.Character][Will.weak] * Math.Pow(labelset.HowWeak, 1); will += strong + weak; if (sije.Value[sentence].ContainsKey(labelset)) { sije.Value[sentence][labelset][Will.strong] += strong; sije.Value[sentence][labelset][Will.weak] += weak; } else { sije.Value[sentence].Add(labelset, new Dictionary <Will, double>()); sije.Value[sentence][labelset].Add(Will.strong, strong); sije.Value[sentence][labelset].Add(Will.weak, weak); } } foreach (Labelset labelset in sije.Value[sentence].Keys.ToArray()) { sije.Value[sentence][labelset][Will.strong] /= will; sije.Value[sentence][labelset][Will.weak] /= will; } } return(sije); }
static private Mce InitializeMce(int groupIndex)//初始化Sije时使用 { Mce mce = new Mce(0); foreach (Character character in ConsistencyVariable.Characters) { double numberOfTrueStrongAffects = 0; double numberOfTrueWeakAffects = 0; foreach (Sentence sentence in character.Sentences) { foreach (Annotation annotation in sentence.AnnotaitonGroups[groupIndex].AnnotatorAnnotationDic.Values) { Labelset labelset = annotation.ToLabelset(Variable.LabelArray); numberOfTrueStrongAffects += labelset.NumberOfTrueStrongAffects; numberOfTrueWeakAffects += labelset.NumberOfTrueWeakAffects; } } IDictionary <Will, double> willAndValue = new Dictionary <Will, double>(); double will = numberOfTrueStrongAffects + numberOfTrueWeakAffects; willAndValue.Add(Will.strong, will == 0 ? 0.5 : numberOfTrueStrongAffects / will);//此处不是平滑 willAndValue.Add(Will.weak, will == 0 ? 0.5 : numberOfTrueWeakAffects / will); mce.Value.Add(character, willAndValue); } return(mce); }
public IDynamicTextFunctionSubstitute GetSubstitute(object[] arguments) { Router router = RouterDatabase.Instance.GetTById((int)arguments[0]); Labelset labelset = LabelsetDatabase.Instance.GetTById((int)arguments[2]); return(new Substitute(router, (int)arguments[1], labelset)); }
private void labelTextChanged(Labelset labelset, RouterInput routerInput, string oldText, string newText) { foreach (LabelProxy labelProxy in this) { if (labelProxy.RouterInput == routerInput) { labelProxy.TextChanged(); } } }
public LabelsetProxy(Labelset labelset) { this.Labelset = labelset; this.Labelset.LabelTextChanged += labelTextChanged; foreach (Router router in RouterDatabase.Instance) { foreach (RouterInput routerInput in router.Inputs) { Add(new LabelProxy(Labelset, routerInput)); } } }
//相当于P(t,x) static private Sije InitializeSije(Mce mce, int groupIndex) { Sije sije = new Sije(1); foreach (Sentence sentence in Variable.Sentences) { sije.Value.Add(sentence, 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 (Annotation annotation in sentence.AnnotaitonGroups[groupIndex].AnnotatorAnnotationDic.Values) { Labelset labelset = annotation.ToLabelset(Variable.LabelArray); if (sije.Value[sentence].ContainsKey(labelset)) { sije.Value[sentence][labelset][Will.strong] += labelset.HowStrong; sije.Value[sentence][labelset][Will.weak] += labelset.HowWeak; } else { sije.Value[sentence].Add(labelset, new Dictionary <Will, double>()); sije.Value[sentence][labelset].Add(Will.strong, labelset.HowStrong); sije.Value[sentence][labelset].Add(Will.weak, labelset.HowWeak); } willDenominator[Will.strong] += labelset.HowStrong; willDenominator[Will.weak] += labelset.HowWeak; } //p(t|e) foreach (Labelset labelset in sije.Value[sentence].Keys.ToArray()) { if (willDenominator[Will.strong] != 0) { sije.Value[sentence][labelset][Will.strong] /= willDenominator[Will.strong]; } if (willDenominator[Will.weak] != 0) { sije.Value[sentence][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 sije.Value[sentence].Keys.ToArray()) { sije.Value[sentence][labelset][Will.strong] *= mce.Value[sentence.Character][Will.strong]; sije.Value[sentence][labelset][Will.weak] *= mce.Value[sentence.Character][Will.weak]; } } } return(sije); }
public override void Init(object[] argumentObjects) { ISystemObject systemObject = argumentObjects[0] as ISystemObject; if (systemObject == null) { CurrentValue = "?"; return; } Labelset labelset = argumentObjects[1] as Labelset; if (labelset == null) { CurrentValue = "?"; return; } this.labelset = labelset; labelset.LabelTextChanged += labelsetLabelChanged; CurrentValue = labelset.GetText(systemObject); }
//废弃 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); }
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); }
public LabelProxy(Labelset labelset, ISystemObject systemObject) : base(systemObject) => this.labelset = labelset;
static private Sij Initialize(int groupIndex) { Sij sij = new Sij(1); IDictionary <Label, double> labelFloatDic = new Dictionary <Label, double>(); foreach (Label label in Variable.LabelArray) { labelFloatDic.Add(label, 0); } IDictionary <Sentence, IDictionary <Label, double> > ProbabilityOfLabelTrue = new Dictionary <Sentence, IDictionary <Label, double> >(); IDictionary <Sentence, IDictionary <Label, double> > ProbabilityOfLabelFalse = new Dictionary <Sentence, IDictionary <Label, double> >(); foreach (Sentence sentence in Variable.Sentences) { ProbabilityOfLabelTrue.Add(sentence, new Dictionary <Label, double>(labelFloatDic)); ProbabilityOfLabelFalse.Add(sentence, new Dictionary <Label, double>(labelFloatDic)); } foreach (Sentence sentence in Variable.Sentences) { foreach (Annotation annotation in sentence.AnnotaitonGroups[groupIndex].AnnotatorAnnotationDic.Values) { foreach (Label label in Variable.LabelArray) { if (annotation.Labels[label]) { ++ProbabilityOfLabelTrue[sentence][label]; } else { ++ProbabilityOfLabelFalse[sentence][label]; } } } } foreach (Sentence sentence in Variable.Sentences) { foreach (Label label in Variable.LabelArray) { ProbabilityOfLabelTrue[sentence][label] /= Variable.NumberOfAnnotationsPerSentenceAfterGrouping; ProbabilityOfLabelFalse[sentence][label] /= Variable.NumberOfAnnotationsPerSentenceAfterGrouping; } } foreach (Sentence sentence in Variable.Sentences) { sij.Value.Add(sentence, new Dictionary <Labelset, double>()); for (int l = 0; l < Math.Pow(2, Variable.LabelArray.Length); ++l) { Labelset Labelset = new Labelset(Variable.LabelArray, l); double value = 1; foreach (Label label in Labelset.Labels.Keys) { if (Labelset.Labels[label]) { value *= ProbabilityOfLabelTrue[sentence][label]; } else { value *= ProbabilityOfLabelFalse[sentence][label]; } } if (value != 0) { sij.Value[sentence].Add(Labelset, value); } } } return(sij); }
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]; // } //} }
public LabelsetEditorForm(Labelset labelset) { InitializeComponent(); this.labelset = (labelset != null) ? labelset : new Labelset(); AddingNew = (this.labelset == null); }
public LabelProxy(Labelset labelset, RouterInput routerInput) { this.labelset = labelset; this.RouterInput = routerInput; }
public LabelAndWitness(Labelset singleAnnotation, Labelset labelset) { this.SingleAnnotation = singleAnnotation; this.Labelset = labelset; }
static public Sij Initialize(int groupIndex, double threshold, IndependenceEstimation independentEstimation) { #region 初始化 Sij sij = new Sij(1); #endregion Label[] labelArray = GroupFunction.DescendLabelsByNumber(groupIndex); Graph BN = NDDSFunction.BuildBN(groupIndex, labelArray, independentEstimation, threshold); #region 从BN中求每个情感(事件)的父节点(条件) IDictionary <Label, IList <Label> > LabelsAndPas = new Dictionary <Label, IList <Label> >(); foreach (Label label in labelArray) { LabelsAndPas.Add(label, new List <Label>()); } foreach (KeyValuePair <LabelPair, bool> hasRelationship in BN.AdjMatrix) { if (hasRelationship.Value) { LabelsAndPas[hasRelationship.Key.Second].Add(hasRelationship.Key.First); } } #endregion #region 求计算联合概率的参数 IDictionary <Sentence, IDictionary <LabelAndWitness, double> > Probability = new Dictionary <Sentence, IDictionary <LabelAndWitness, double> >(); foreach (Sentence sentence in Variable.Sentences) { Probability.Add(sentence, new Dictionary <LabelAndWitness, double>()); } IDictionary <Smoothing, double[]> smoothingNumber = Function.SmoothingNumber(2); foreach (KeyValuePair <Label, IList <Label> > labelAndPas in LabelsAndPas) { if (labelAndPas.Value.Count == 0) { foreach (Sentence sentence in Variable.Sentences) { double numberOfLabelTrue = 0; double numberOfLabelFalse = 0; foreach (Annotation annotation in sentence.AnnotaitonGroups[groupIndex].AnnotatorAnnotationDic.Values) { if (annotation.Labels[labelAndPas.Key]) { ++numberOfLabelTrue; } else { ++numberOfLabelFalse; } } if (NDDSVariable.SmoothBN != Smoothing.None) { Probability[sentence].Add(new LabelAndWitness(new Labelset(labelAndPas.Key, false), new Labelset()), (numberOfLabelFalse + smoothingNumber[NDDSVariable.SmoothBN][0]) / (Variable.NumberOfAnnotationsPerSentenceAfterGrouping + smoothingNumber[NDDSVariable.SmoothBN][1])); Probability[sentence].Add(new LabelAndWitness(new Labelset(labelAndPas.Key, true), new Labelset()), (numberOfLabelTrue + (numberOfLabelTrue + smoothingNumber[NDDSVariable.SmoothBN][0]) / (Variable.NumberOfAnnotationsPerSentenceAfterGrouping + smoothingNumber[NDDSVariable.SmoothBN][1]))); } else { Probability[sentence].Add(new LabelAndWitness(new Labelset(labelAndPas.Key, false), new Labelset()), numberOfLabelFalse / Variable.NumberOfAnnotationsPerSentenceAfterGrouping); Probability[sentence].Add(new LabelAndWitness(new Labelset(labelAndPas.Key, true), new Labelset()), numberOfLabelTrue / Variable.NumberOfAnnotationsPerSentenceAfterGrouping); } } } else { for (int i = 0; i < Math.Pow(2, labelAndPas.Value.Count); ++i) { Labelset Labelset = new Labelset(labelAndPas.Value, i); foreach (Sentence sentence in Variable.Sentences) { double numberOfLabelTrue = 0; double numberOfLabelFalse = 0; foreach (Annotation annotation in sentence.AnnotaitonGroups[groupIndex].AnnotatorAnnotationDic.Values) { if (annotation.IsAccordingToLabelset(Labelset)) { if (annotation.Labels[labelAndPas.Key]) { ++numberOfLabelTrue; } else { ++numberOfLabelFalse; } } } if (NDDSVariable.SmoothBN != Smoothing.None) { Probability[sentence].Add(new LabelAndWitness(new Labelset(labelAndPas.Key, false), Labelset), (numberOfLabelFalse + smoothingNumber[NDDSVariable.SmoothBN][0]) / (Variable.NumberOfAnnotationsPerSentenceAfterGrouping + smoothingNumber[NDDSVariable.SmoothBN][1])); Probability[sentence].Add(new LabelAndWitness(new Labelset(labelAndPas.Key, true), Labelset), (numberOfLabelTrue + smoothingNumber[NDDSVariable.SmoothBN][0]) / (Variable.NumberOfAnnotationsPerSentenceAfterGrouping + smoothingNumber[NDDSVariable.SmoothBN][1])); } else { Probability[sentence].Add(new LabelAndWitness(new Labelset(labelAndPas.Key, false), Labelset), numberOfLabelFalse / Variable.NumberOfAnnotationsPerSentenceAfterGrouping); Probability[sentence].Add(new LabelAndWitness(new Labelset(labelAndPas.Key, true), Labelset), numberOfLabelTrue / Variable.NumberOfAnnotationsPerSentenceAfterGrouping); } } } } } #endregion #region 计算Sij IDictionary <Sentence, double> denominator = new Dictionary <Sentence, double>();//归一化参数 foreach (Sentence sentence in Variable.Sentences) { sij.Value.Add(sentence, new Dictionary <Labelset, double>()); denominator.Add(sentence, 0); for (int l = 0; l < Math.Pow(2, Variable.LabelArray.Length); ++l) { Labelset Labelset = new Labelset(Variable.LabelArray, l); double value = 1; foreach (Label label in labelArray) { Labelset singleLabelAnnotation = new Labelset(label, Labelset.Labels[label]); Labelset subLabelset = new Labelset(LabelsAndPas[label], Labelset); value *= Probability[sentence][new LabelAndWitness(singleLabelAnnotation, subLabelset)]; } if (value != 0) { sij.Value[sentence].Add(Labelset, value); denominator[sentence] += value; } } } #endregion #region 归一化 foreach (Sentence sentence in Variable.Sentences.ToArray()) { foreach (Labelset labelset in sij.Value[sentence].Keys.ToArray()) { sij.Value[sentence][labelset] /= denominator[sentence]; } } #endregion return(sij); }