/// <summary> /// 判断键是否为有效关联键 /// </summary> /// <typeparam name="T">泛型,具体类型由调用者传入</typeparam> /// <param name="keyHead">相邻键中首项</param> /// <param name="keyTail">相邻键中尾项</param> /// <param name="objMemoryBondColl">相邻字典</param> /// <returns>返回是否判断的结果:true、相邻项有关;false、相邻项无关</returns> /// <remarks>判断标准:共享键概率 > 单字概率之积 </remarks> public static bool IsBondValid <T>(T keyHead, T keyTail, MemoryBondColl <T> objMemoryBondColl) { //如果相邻项任何一个不在相邻字典中,则返回false 。 if (!objMemoryBondColl.Contains(keyHead) || !objMemoryBondColl.Contains(keyTail)) { return(false); } //分别获得相邻单项的频次 double dHeadValidCount = CalcRemeberValue <T>(keyHead, objMemoryBondColl); double dTailValidCount = CalcRemeberValue <T>(keyTail, objMemoryBondColl); //获得相邻字典全库的总词频 double dTotalValidCount = objMemoryBondColl.MinuteOffsetSize; if (dTotalValidCount <= 0) { return(false); } //获得相邻项共现的频次 MemoryItemColl <T> objLinkColl = objMemoryBondColl[keyHead].LinkColl; if (!objLinkColl.Contains(keyTail)) { return(false); } double dShareValidCount = CalcRemeberValue <T>(keyTail, objLinkColl); //返回计算的结果 return(dShareValidCount / dHeadValidCount > dTailValidCount / dTotalValidCount); }
/// <summary> /// 将相邻两项(邻键)添加到集合中 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="keyHead"></param> /// <param name="keyTail"></param> /// <param name="objMemoryBondColl"></param> public static void UpdateMemoryBondColl <T>(T keyHead, T keyTail, MemoryBondColl <T> objMemoryBondColl) { if (!objMemoryBondColl.Contains(keyHead)) { MemoryBondMDL <T> bond = new MemoryBondMDL <T>(); bond.KeyItem.Key = keyHead; bond.LinkColl.OffsetTotalCount = 0; bond.LinkColl.MinuteOffsetSize = objMemoryBondColl.MinuteOffsetSize; objMemoryBondColl.Add(bond); } MemoryBondMDL <T> objBondMDL = objMemoryBondColl[keyHead]; MemoryItemMDL <T> mdl = objBondMDL.KeyItem; double dRemeberValue = MemoryDAL.CalcRemeberValue(objMemoryBondColl.OffsetTotalCount - mdl.UpdateOffsetCount, objMemoryBondColl.MinuteOffsetSize); mdl.TotalCount += 1; //累加总计数 mdl.ValidDegree = CalcValidDegree <T>(mdl, dRemeberValue); //计算成熟度 mdl.ValidCount = mdl.ValidCount * dRemeberValue + 1; // 遗忘累频=记忆保留量+1 mdl.UpdateOffsetCount = objMemoryBondColl.OffsetTotalCount; //更新时的偏移量 MemoryItemColl <T> objLinkColl = objBondMDL.LinkColl; objLinkColl.OffsetTotalCount = objMemoryBondColl.OffsetTotalCount; UpdateMemoryItemColl(keyTail, objLinkColl); objMemoryBondColl.OffsetTotalCount += 1; }
/// <summary> /// 计算邻键尾项记忆剩余量 /// </summary> /// <typeparam name="T">泛型,具体类别由调用者传入</typeparam> /// <param name="keyHead">相邻两项的首项</param> /// <param name="keyTail">相邻两项的尾项</param> /// <param name="objMemoryBondColl">邻键集合</param> /// <returns>返回记忆剩余量</returns> public static double CalcRemeberValue <T>(T keyHead, T keyTail, MemoryBondColl <T> objMemoryBondColl) { if (!objMemoryBondColl.Contains(keyHead)) { return(0); } MemoryBondMDL <T> objBondMDL = objMemoryBondColl[keyHead]; MemoryItemColl <T> objLinkColl = objBondMDL.LinkColl; return(CalcRemeberValue <T>(keyTail, objLinkColl)); }
/// <summary> /// 计算邻键首项记忆剩余量 /// </summary> /// <typeparam name="T">泛型,具体类别由调用者传入</typeparam> /// <param name="key">相邻两项的首项</param> /// <param name="objMemoryBondColl">邻键集合</param> /// <returns>返回记忆剩余量</returns> public static double CalcRemeberValue <T>(T key, MemoryBondColl <T> objMemoryBondColl) { if (!objMemoryBondColl.Contains(key)) { return(0); } MemoryBondMDL <T> objBondMDL = objMemoryBondColl[key]; MemoryItemMDL <T> mdl = objBondMDL.KeyItem; double dRemeberValue = MemoryDAL.CalcRemeberValue(objMemoryBondColl.OffsetTotalCount - mdl.UpdateOffsetCount, objMemoryBondColl.MinuteOffsetSize); return(mdl.ValidCount * dRemeberValue); }
/// <summary> /// 清理邻键集合,移除低于阈值的邻键 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="objMemoryBondColl"></param> /// <param name="dMinValidValue"></param> public static void ClearMemoryBondColl <T>(MemoryBondColl <T> objMemoryBondColl, double dMinValidValue = 1.28) { for (int k = objMemoryBondColl.Count - 1; k >= 0; k--) { if (objMemoryBondColl[k].KeyItem.ValidCount < dMinValidValue) { objMemoryBondColl.RemoveAt(k); } else { ClearMemoryItemColl <T>(objMemoryBondColl[k].LinkColl, dMinValidValue); } } }