/// <summary> /// 获得两个词性之间的频率 /// </summary> /// <param name="from"></param> /// <param name="to"></param> /// <returns></returns> public static int GetTwoNatureFreq(Nature from, Nature to) { if (from.index < 0 || to.index < 0) { return 0; } return _naturetable[from.index][to.index]; }
public TermNature(string natureStr, int frequency) { nature = NatureLibrary.GetNature(natureStr); this.frequency = frequency; }
/// <summary> /// 根据字符串得到词性.没有就创建一个 /// </summary> /// <param name="input"></param> /// <returns></returns> public static Nature GetNature(string input) { var nature = Naturemap[input]; if (nature == null) { nature = new Nature(input, MinusOne, MinusOne, One); Naturemap.Add(input, nature); return nature; } return nature; }
/// <summary> /// </summary> /// <param name="smartForest"></param> /// <param name="hm"></param> /// <param name="nature"></param> private void ValueResult(SmartForest<NewWord> smartForest, HashMap<string, double> hm, Nature nature) { // TODO Auto-generated method stub if (smartForest == null || smartForest.Branches == null) { return; } for (var i = 0; i < smartForest.Branches.Length; i++) { var param = smartForest.Branches[i].Param; if (smartForest.Branches[i].Status == 3) { if (param.IsActive && (nature == null || param.Nature.Equals(nature))) { hm.Add(param.Name, param.Score); } } else if (smartForest.Branches[i].Status == 2) { if (param.IsActive && (nature == null || param.Nature.Equals(nature))) { hm.Add(param.Name, param.Score); } ValueResult(smartForest.Branches[i], hm, nature); } else { ValueResult(smartForest.Branches[i], hm, nature); } } }
/// <summary> /// </summary> /// <param name="num"></param> /// <param name="nature"></param> /// <returns></returns> public List<KeyValuePair<string, double>> GetTopTree(int num, Nature nature) { if (_smartForest.Branches == null) { return null; } var hm = new HashMap<string, double>(); for (var i = 0; i < _smartForest.Branches.Length; i++) { ValueResult(_smartForest.Branches[i], hm, nature); } var sortMapByValue = CollectionUtil.SortMapByValue(hm, -1); if (num == 0) { return sortMapByValue; } num = Math.Min(num, sortMapByValue.Count); return sortMapByValue.Take(num).ToList(); }
public void Recognition() { if (_branch == null) { return; } var length = _terms.Length - 1; for (var i = 0; i < length; i++) { if (_terms[i] == null) { continue; } _from = _terms[i].From; _terms[i].Score = 0; _terms[i].SelfScore = 0; if (_branch != null) { _branch = _branch.GetBranch(_terms[i].Name); if (_branch == null || _branch.Status == 3) { Reset(); continue; } _offe = i; // 循环查找添加 var term = _terms[i]; _stringBuilder.Append(term.Name); if (_branch.Status == 2) { term.SelfScore = _branch.Param.Score; } var flag = true; while (flag) { term = term.To; _branch = _branch.GetBranch(term.Name); // 如果没有找到跳出 if (_branch == null) { break; } switch (_branch.Status) { case 1: _stringBuilder.Append(term.Name); continue; case 2: _stringBuilder.Append(term.Name); _score = _branch.Param.Score; _tempNature = _branch.Param.Nature; _to = term.To; MakeNewTerm(); continue; case 3: _stringBuilder.Append(term.Name); _score = _branch.Param.Score; _tempNature = _branch.Param.Nature; _to = term.To; MakeNewTerm(); flag = false; break; default: Trace.WriteLine("怎么能出现0呢?"); break; } } } Reset(); } }
/// <summary> /// 重置 /// </summary> private void Reset() { _offe = -1; _tempNature = null; _branch = _forest; _score = 0; _stringBuilder = new StringBuilder(); }