/// <summary> /// 设置关键词高亮 /// </summary> /// <param name="content"></param> /// <param name="keyWordSet"></param> /// <returns></returns> protected virtual string SetHighKeyWord(string content, ISet <string> keyWordSet) { if (keyWordSet == null || keyWordSet.Count <= 0) { return(content); } const string preTag = "<span class='highlight'>", endTag = "</span>"; var newContent = new StringBuilder(content.Length); var seg = new JiebaSegmenter(); int index = 0; var contentWords = seg.TokenizeAll(content).Where(t => keyWordSet.Contains(t.Word)).ToArray(); var resultToken = contentWords.MergeTokenList(); foreach (var token in resultToken) { var length = token.StartIndex - index; if (length > 0) { newContent.Append(content.Substring(index, length)); } else if (length < 0 && index < token.EndIndex) { token.Word = index < token.EndIndex ? content.Substring(index, token.EndIndex - index) : ""; } newContent.Append(preTag); newContent.Append(token.Word); newContent.Append(endTag); index = Math.Max(index, token.EndIndex); } if (index <= content.Length - 1) { newContent.Append(content.Substring(index)); } return(newContent.ToString()); }
public void ResetTextReader(TextReader reader) { var text = reader.ReadToEnd(); _tokens = _segmenter.TokenizeAll(text).MergeTokenList(_keywords); _position = -1; }
public void ResetTextReader(TextReader reader) { var text = reader.ReadToEnd(); var buffer = segmenter.TokenizeAll(text); tokens = buffer is IList <Token> list ? list : buffer.ToArray(); position = -1; }