public static RangeSet Split(this IWordSplitter splitter, ByteBlock text) { RangeSet words = new RangeSet(); splitter.Split(text.Array, new Range(text.Index, text.Length), words); return(words); }
public ByteBlock Highlight(ByteBlock value, IWordSplitter splitter, List <HighlightTerm> terms) { if (terms == null) { throw new ArgumentNullException("terms"); } if (splitter == null) { throw new ArgumentNullException("splitter"); } ByteBlockAppender appender = new ByteBlockAppender(value); // Make a lowercase value copy to compare ByteBlock valueLower = value.Copy(); valueLower.ToLowerInvariant(); // Split the value and compare words to highlight terms RangeSet ranges = splitter.Split(valueLower); for (int rangeIndex = 0; rangeIndex < ranges.Count; ++rangeIndex) { Range r = ranges.Ranges[rangeIndex]; ByteBlock valueWord = new ByteBlock(valueLower.Array, r.Index, r.Length); foreach (HighlightTerm term in terms) { // If this word in the value starts with a search term... if (term.Matches(valueWord)) { // Append to the beginning of the word, if not already past this point if (appender.AppendTo(valueWord.Index)) { // Wrap and append the *prefix* of the term from the query appender.Append(_wrapPrefix); appender.AppendTo(valueWord.Index + term.Value.Length); appender.Append(_wrapSuffix); } // If we matched, do not check other HighlightTerms against this word break; } } } // Append the remaining content and return appender.AppendRemainder(); return(appender.Value()); }
public static List <HighlightTerm> WordsForColumn(IExpression where, string columnName, IWordSplitter splitter) { List <HighlightTerm> terms = new List <HighlightTerm>(); // For each term in the query expression... IList <TermExpression> matchingTerms = where.GetAllTerms(columnName); foreach (TermExpression term in matchingTerms) { ByteBlock termValue; if (term.Value.TryConvert <ByteBlock>(out termValue)) { ByteBlock termValueLower = termValue.Copy(); termValueLower.ToLowerInvariant(); if (term.Operator == Operator.MatchesExact) { terms.Add(new HighlightTerm(termValueLower, true)); } else if (term.Operator == Operator.Matches) { // Split the term value RangeSet set = splitter.Split(termValueLower); // Add each range as a word to the result [exact when under expand length limit] for (int i = 0; i < set.Count; ++i) { Range r = set.Ranges[i]; ByteBlock partBlock = new ByteBlock(termValueLower.Array, r.Index, r.Length); terms.Add(new HighlightTerm(partBlock, partBlock.Length < WordIndex.MinimumPrefixExpandLength)); } } } } return(terms); }