我有索引文档。他们有内容:
文档 1:
Green table stood in the room. The room was small.
文件 2:
Green tables stood in the room. The room was large.
我正在寻找“绿表”。我会找到 Document1 和 Document2。我想显示找到了哪些短语。我在第一份文件中找到了 - “绿表”。我在第二份文件中找到 - “绿色表”。我将如何获得单词列表(“green table”和“greens table”)?我使用的是 Lucene.Net 版本 3.0.3。
最佳答案
您可以使用荧光笔标记“找到的单词”。 如果您出于其他原因想要找到它们,您仍然可以使用荧光笔,然后使用正则表达式(或简单的子字符串循环)来提取单词。
例如:
Query objQuery = new TermQuery(new Term("content", strQuery));
QueryScorer scorer = new QueryScorer(objQuery , "content");
SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<b>","</b>");
highlighter = new Highlighter(formatter, scorer);
highlighter.TextFragmenter = new SimpleFragmenter(9999);
for (int i = 0; i < topRealtedDocs.ScoreDocs.Length; i++)
{
TokenStream stream = TokenSources.GetAnyTokenStream(searcher.IndexReader, topRealtedDocs.ScoreDocs[i].Doc, "content", analyzer);
string strSnippet = highlighter.GetBestFragment(stream, doc.GetValue("content"));
// here you can do what you want with the snippet. add it to your result or for example extract the words (not with a regex - this is just an example from here! use what ever you need):
List<string> foundPhrases = new List<string>();
while (strSnippet.IndexOf("<b>") > -1)
{
int indexStart = strSnippet.IndexOf("<b>");
int indexEnd = strSnippet.IndexOf("</b>");
foundPhrases.Add(strSnippet.Substring(indexStart, indexEnd - indexStart));
strSnippet = strSnippet.Substring(indexEnd);
}
}
暗利
关于c# - 如何使用 Lucene.Net 获取找到的单词列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16517341/