/// <summary> /// 在文本中查找第一个关键字 /// </summary> /// <param name="text">文本</param> /// <returns></returns> public WordsSearchResult FindFirst(string text) { TrieNode2 ptr = null; for (int i = 0; i < text.Length; i++) { TrieNode2 tn; if (ptr == null) { tn = _first[text[i]]; } else { if (ptr.TryGetValue(text[i], out tn) == false) { tn = _first[text[i]]; } } if (tn != null) { if (tn.End) { var item = tn.Results[0]; return(new WordsSearchResult(_keywords[item], i + 1 - _keywords[item].Length, i, _indexs[item])); } } ptr = tn; } return(WordsSearchResult.Empty); }
/// <summary> /// 在文本中查找所有的关键字 /// </summary> /// <param name="text">文本</param> /// <returns></returns> public List <WordsSearchResult> FindAll(string text) { TrieNode2 ptr = null; List <WordsSearchResult> list = new List <WordsSearchResult>(); for (int i = 0; i < text.Length; i++) { TrieNode2 tn; if (ptr == null) { tn = _first[text[i]]; } else { if (ptr.TryGetValue(text[i], out tn) == false) { tn = _first[text[i]]; } } if (tn != null) { if (tn.End) { foreach (var item in tn.Results) { list.Add(new WordsSearchResult(_keywords[item], i + 1 - _keywords[item].Length, i, _indexs[item])); } } } ptr = tn; } return(list); }
/// <summary> /// 判断文本是否包含关键字 /// </summary> /// <param name="text">文本</param> /// <returns></returns> public bool ContainsAny(string text) { TrieNode2 ptr = null; foreach (char t in text) { TrieNode2 tn; if (ptr == null) { tn = _first[t]; } else { if (ptr.TryGetValue(t, out tn) == false) { tn = _first[t]; } } if (tn != null) { if (tn.End) { return(true); } } ptr = tn; } return(false); }
/// <summary> /// 在文本中查找所有的关键字 /// </summary> /// <param name="text">文本</param> /// <returns></returns> public List <string> FindAll(string text) { TrieNode2 ptr = null; List <string> list = new List <string>(); foreach (char t in text) { TrieNode2 tn; if (ptr == null) { tn = _first[t]; } else { if (ptr.TryGetValue(t, out tn) == false) { tn = _first[t]; } } if (tn != null) { if (tn.End) { foreach (var item in tn.Results) { list.Add(_keywords[item]); } } } ptr = tn; } return(list); }
/// <summary> /// 在文本中查找第一个关键字 /// </summary> /// <param name="text">文本</param> /// <returns></returns> public string FindFirst(string text) { TrieNode2 ptr = null; foreach (char t in text) { TrieNode2 tn; if (ptr == null) { tn = _first[t]; } else { if (ptr.TryGetValue(t, out tn) == false) { tn = _first[t]; } } if (tn != null) { if (tn.End) { return(_keywords[tn.Results[0]]); } } ptr = tn; } return(null); }
private void TryLinks(TrieNode2 node, TrieNode2 node2, Dictionary <TrieNode2, TrieNode2> links) { foreach (var item in node.m_values) { TrieNode2 tn; if (node2 == null) { tn = _first[item.Key]; if (tn != null) { links[item.Value] = tn; } } else { if (node2.TryGetValue(item.Key, out tn)) { links[item.Value] = tn; } } TryLinks(item.Value, tn, links); } }
/// <summary> /// 在文本中替换所有的关键字 /// </summary> /// <param name="text">文本</param> /// <param name="replaceChar">替换符</param> /// <returns></returns> public string Replace(string text, char replaceChar = '*') { StringBuilder result = new StringBuilder(text); TrieNode2 ptr = null; for (int i = 0; i < text.Length; i++) { TrieNode2 tn; if (ptr == null) { tn = _first[text[i]]; } else { if (ptr.TryGetValue(text[i], out tn) == false) { tn = _first[text[i]]; } } if (tn != null) { if (tn.End) { var maxLength = _keywords[tn.Results[0]].Length; var start = i + 1 - maxLength; for (int j = start; j <= i; j++) { result[j] = replaceChar; } } } ptr = tn; } return(result.ToString()); }
public bool Find(string text, string hz, string[] pinyins) { TrieNode2 ptr = null; for (int i = 0; i < text.Length; i++) { TrieNode2 tn; if (ptr == null) { tn = _first[text[i]]; } else { if (ptr.TryGetValue(text[i], out tn) == false) { tn = _first[text[i]]; } } if (tn != null) { if (tn.End) { foreach (var result in tn.Results) { var keyword = _keywords[result]; var start = i + 1 - keyword.Length; var end = i; bool isok = true; var keywordPinyins = _keywordPinyins[result]; for (int j = 0; j < keyword.Length; j++) { var idx = start + j; var py = keywordPinyins[j]; if (py.Length == 1 && py[0] >= 0x3400 && py[0] <= 0x9fd5) { if (hz[idx] != py[0]) { isok = false; break; } } else { if (pinyins[idx].StartsWith(py) == false) { isok = false; break; } } } if (isok) { return(true); } } } } ptr = tn; } return(false); }