public IWoodInterface Add(IWoodInterface branch) { var temp = chars[branch.GetChar()]; if (temp == null) chars[branch.GetChar()] = branch; else { switch (branch.Status) { case 1: if (temp.Status == 3) { temp.Status = 2; } break; case 3: if (temp.Status == 1) { temp.Status = 2; } temp.Param = branch.Param; break; } } return chars[branch.GetChar()]; }
/// <summary> /// 用户自己定义的词典 /// </summary> /// <param name="forests"></param> public ToAnalysis(params IWoodInterface[] forests) { if (forests == null) { forests = new IWoodInterface[] {UserDefineLibrary.Forest}; } Forests = forests; }
/// <summary> /// 插入一个词 /// </summary> /// <param name="forest"></param> /// <param name="temp"></param> public static void InsertWord(IWoodInterface forest, string temp) { var param = temp.Split('\t'); temp = param[0]; var resultParams = new string[param.Length - 1]; for (var j = 1; j < param.Length; j++) { resultParams[j - 1] = param[j]; } InsertWord(forest, temp, resultParams); }
public IWoodInterface Add(IWoodInterface branch) { if (_branches == null) { _branches = new IWoodInterface[0]; } var bs = AnsjArrays.BinarySearch(_branches, branch.GetChar()); if (bs >= 0) { this._branch = _branches[bs]; switch (branch.Status) { case 0: _branch.Status = 1; break; case 1: if (_branch.Status == 3) { _branch.Status = 2; } break; case 3: if (_branch.Status != 3) { _branch.Status = 2; } _branch.Param = branch.Param; break; } return _branch; } var newBranches = new IWoodInterface[_branches.Length + 1]; var insert = -(bs + 1); Array.Copy(_branches, 0, newBranches, 0, insert); Array.Copy(_branches, insert, newBranches, insert + 1, _branches.Length - insert); newBranches[insert] = branch; _branches = newBranches; return branch; }
private string AllWords() { if ((!_isBack) || (_i == _chars.Length - 1)) { _i = (_root - 1); } for (_i += 1; _i < _chars.Length; _i = (_i + 1)) { _branch = _branch.Get(_chars[_i]); if (_branch == null) { _root += 1; _branch = _forest; _i = (_root - 1); _isBack = false; } else { switch (_branch.Status) { case 2: _isBack = true; Offe = (_tempOffe + _root); _param = _branch.Param; return new string(_chars, _root, _i - _root + 1); case 3: Offe = (_tempOffe + _root); _str = new string(_chars, _root, _i - _root + 1); _param = _branch.Param; _branch = _forest; _isBack = false; _root += 1; return _str; } } } _tempOffe += _chars.Length; return null; }
public GetWord(Forest forest, char[] chars) { _chars = chars; _forest = forest; _branch = forest; }
public void Reset(string content) { Offe = 0; _status = 0; _root = 0; _i = _root; _isBack = false; _tempOffe = 0; _chars = content.ToCharArray(); _branch = _forest; }
public GetWord(Forest forest, string content) { _chars = content.ToCharArray(); _forest = forest; _branch = forest; }
private string FrontWords() { for (; _i < _chars.Length + 1; _i++) { if (_i == _chars.Length) { _branch = null; } else { _branch = _branch.Get(_chars[_i]); } if (_branch == null) { _branch = _forest; if (_isBack) { Offe = _root; _str = new string(_chars, _root, _tempOffe); if ((_root > 0) && (IsE(_chars[(_root - 1)])) && (IsE(_str[0]))) { _str = Emptystring; } if ((_str.Length != 0) && (_root + _tempOffe < _chars.Length) && (IsE(_str[_str.Length - 1])) && (IsE(_chars[(_root + _tempOffe)]))) { _str = Emptystring; } if (_str.Length == 0) { _root += 1; _i = _root; } else { _i = (_root + _tempOffe); _root = _i; } _isBack = false; if (Emptystring.Equals(_str)) { return Emptystring; } return _str; } _i = _root; _root += 1; } else { switch (_branch.Status) { case 2: _isBack = true; _tempOffe = (_i - _root + 1); _param = _branch.Param; break; case 3: Offe = _root; _str = new string(_chars, _root, _i - _root + 1); var temp = _str; if ((_root > 0) && (IsE(_chars[(_root - 1)])) && (IsE(_str[0]))) { _str = Emptystring; } if ((_str.Length != 0) && (_i + 1 < _chars.Length) && (IsE(_str[(_str.Length - 1)])) && (IsE(_chars[(_i + 1)]))) { _str = Emptystring; } _param = _branch.Param; _branch = _forest; _isBack = false; if (temp.Length > 0) { _i += 1; _root = _i; } else { _i = (_root + 1); } if (Emptystring.Equals(_str)) { return Emptystring; } return _str; } } } _tempOffe += _chars.Length; return null; }
private static void InsertWord(IWoodInterface forest, string temp, string[] param) { var branch = forest; var chars = temp.ToCharArray(); for (var i = 0; i < chars.Length; i++) { if (chars.Length == i + 1) { branch.Add(new Branch(chars[i], 3, param)); } else { branch.Add(new Branch(chars[i], 1, null)); } branch = branch.Get(chars[i]); } }
public void Recognition() { foreach (var forest in _forests) { if (forest == null) { continue; } Reset(); _forest = forest; _branch = forest; var length = _terms.Length - 1; for (var i = 0; i < length; i++) { if (_terms[i] == null) continue; bool flag; if (Equals(_branch, forest)) { flag = false; } else { flag = true; } _branch = termStatus(_branch, _terms[i]); if (_branch == null) { if (_offe != -1) { i = _offe; } Reset(); } else if (_branch.Status == 3) { _endOffe = i; _tempNature = _branch.Param[0]; _tempFreq = getInt(_branch.Param[1], 50); if (_offe != -1 && _offe < _endOffe) { i = _offe; MakeNewTerm(); Reset(); } else { Reset(); } } else if (_branch.Status == 2) { _endOffe = i; if (_offe == -1) { _offe = i; } else { _tempNature = _branch.Param[0]; _tempFreq = getInt(_branch.Param[1], 50); if (flag) { MakeNewTerm(); } } } else if (_branch.Status == 1) { if (_offe == -1) { _offe = i; } } } if (_offe != -1 && _offe < _endOffe) { MakeNewTerm(); } } }
/// <summary> /// 传入一个term 返回这个term的状态 /// </summary> /// <param name="branch"></param> /// <param name="term"></param> /// <returns></returns> private IWoodInterface termStatus(IWoodInterface branch, Term term) { var name = term.Name; for (var j = 0; j < name.Length; j++) { branch = branch.Get(name.charAt(j)); if (branch == null) { return null; } } return branch; }
/// <summary> /// 重置 /// </summary> private void Reset() { _offe = -1; _endOffe = -1; _tempFreq = 50; _tempNature = null; _branch = _forest; }