//private KeyWordANFStatus CheckChar(char chr, ref KeyWordDic lastDic) //{ // if (lastDic != null) // { // bool isacc = lastDic.HasEndChar; // if (lastDic.TryGetValue(chr, out lastDic)) // { // if (lastDic.HasEndChar) // { // lastDic = null; // if (isacc) // return KeyWordANFStatus.accept2; // return KeyWordANFStatus.accept; // } // else // { // if (isacc) // return KeyWordANFStatus.acceptwating; // return KeyWordANFStatus.wating; // } // } // if (isacc) // return KeyWordANFStatus.accept1; // } // if (dics[chr] != null) // { // lastDic = dics[chr]; // } // return KeyWordANFStatus.abort; //} private KeyWordANFStatus CheckChar(char chr, ref KeyWordDic lastDic, KeyWordANFStatus lastStatu) { if (lastDic != null) { bool isacc = lastStatu != KeyWordANFStatus.accept && lastDic.HasEndChar; if (lastDic.TryGetValue(chr, out lastDic)) { if (lastDic.HasEndChar) { //lastDic = null; if (isacc) { //lastDic = null; return(KeyWordANFStatus.accept2); } return(KeyWordANFStatus.accept); } else { if (isacc) { return(KeyWordANFStatus.acceptwating); } return(KeyWordANFStatus.wating); } } if (isacc) { lastDic = null; return(KeyWordANFStatus.accept1); } } if (dics[chr] != null) { lastDic = dics[chr]; } return(KeyWordANFStatus.abort); }
public IEnumerable <KeyWordMatchResult> MatchKeyWord(string text) { if (text == null) { yield break; } KeyWordDic innerDic = null; int iStart = 0; KeyWordANFStatus statu = KeyWordANFStatus.abort; string substr; try { dicLock.EnterReadLock(); var tlen = text.Length; for (int i = 0; i < tlen; i++) { if (innerDic == null) { innerDic = dics[text[i]]; iStart = i; } else { statu = CheckChar(text[i], ref innerDic, statu); if (statu == KeyWordANFStatus.abort) { //if (innerDic == null) // iStart = i + 1; //else { int j = 1; for (; j < i - iStart; j++) { if (dics[text[iStart + j]] != null) { break; } } iStart += j; i = iStart; innerDic = dics[text[i]]; } } else if (statu == KeyWordANFStatus.accept1) { var wm = text[i - 1].ToString(); object tag = null; if (DicTag.ContainsKey(wm)) { tag = DicTag[wm]; } yield return(new KeyWordMatchResult { KeyWordMatched = wm, PostionStart = iStart, PostionEnd = i, Tag = tag, }); i--; iStart = i; } else if (statu == KeyWordANFStatus.accept2) { var wm = text[i - 1].ToString(); object tag = null; if (DicTag.ContainsKey(wm)) { tag = DicTag[wm]; } yield return(new KeyWordMatchResult { KeyWordMatched = text[i - 1].ToString(), PostionStart = iStart, PostionEnd = i, Tag = tag, }); wm = text.Substring(iStart, 2); tag = null; if (DicTag.ContainsKey(wm)) { tag = DicTag[wm]; } yield return(new KeyWordMatchResult { KeyWordMatched = text.Substring(iStart, 2), PostionStart = iStart, PostionEnd = i, Tag = tag, }); iStart = i; } else if (statu == KeyWordANFStatus.acceptwating) { var wm = text[i - 1].ToString(); object tag = null; if (DicTag.ContainsKey(wm)) { tag = DicTag[wm]; } yield return(new KeyWordMatchResult { KeyWordMatched = text[i - 1].ToString(), PostionStart = iStart, PostionEnd = i, Tag = tag, }); iStart = i - 1; } else if (statu == KeyWordANFStatus.accept) { substr = text.Substring(iStart, i - iStart + 1); object tag = null; if (DicTag.ContainsKey(substr)) { tag = DicTag[substr]; } yield return(new KeyWordMatchResult { KeyWordMatched = substr, PostionStart = iStart, PostionEnd = i, Tag = tag, }); //for (int j = 1; j < substr.Length; j++) //{ // innerDic = dics[substr[j]]; // if (innerDic != null) // { // i = iStart + j; // iStart = i; // break; // } //} } } } } finally { dicLock.ExitReadLock(); } }