public string[] GenerateSnippet() { var highlightedTerms = new HashSet <int>(); var highlighter = new TermHighlighter(query.TermsWithSynonyms, "<b>", "</b>"); var snippet = new List <string> { highlighter.Highlight(document.Title, highlightedTerms) }; string lastLineWithTerm = null; foreach (string line in document.Content) { int termCountBeforeHighlighting = highlightedTerms.Count; string highlightedLine = highlighter.Highlight(line, highlightedTerms); if (highlightedTerms.Count > termCountBeforeHighlighting && termCountBeforeHighlighting != query.TermsWithSynonyms.Length) { snippet.Add(highlightedLine); } if (line != highlightedLine && lastLineWithTerm == null) { lastLineWithTerm = highlightedLine; } } if (snippet.Count == 1) { snippet.Add(lastLineWithTerm ?? highlighter.Highlight(document.Content.First())); } return(snippet.ToArray()); }
protected void Page_Load(object sender, EventArgs e) { string documentId = Request.QueryString["id"]; if (documentId.Any(x => !char.IsDigit(x))) { GoAway(); } Document document; try { document = new Document(documentId); } catch { GoAway(); return; } string sourceQuery = Request.QueryString["sourceQuery"]; var highlighter = new TermHighlighter( RawQueryParser.Parse(sourceQuery, null, null, null).TermsWithSynonyms, "<span style='background-color: yellow'>", "</span>"); if (!string.IsNullOrEmpty(sourceQuery)) { document.Content = document.Content.Select(x => highlighter.Highlight(x)); } string finalText = string.Join("", document.Content.Select(x => string.Format("<div>{0}</div>", x)).ToArray()); lblOriginalText.Text = string.Format("<h1>{0}</h1><div>{1}</div>", highlighter.Highlight(document.Title), finalText); }