我正在寻找用于确定和选择某些文本区域的算法,这些算法与某些用户查询相关。也许选择与文本中的用户查询相关的片段。
任何人都可以推荐任何适合此任务的算法吗?
P.S。我看到了这个问题:Is there an algorithm for determining the relevance of a text to a theme? 但它不是我的问题的解决方案,因为我需要在文本中选择相关区域,并且使用机器学习算法不适合这项任务。
答案 0 :(得分:2)
您可以使用Lucene Highlighter。 Lucene的高亮包包含用于在上下文中提供"关键字的类#34;功能通常用于突出显示结果页面文本中的搜索词。
Highlighter类是中心组件,可以在Fragmenter,片段Scorer和Formatter类的帮助下,提取一段文本中最有趣的部分并突出显示它们。 Highlighter类的方法 getBestTextFragments 从文档中选择最可能相关的文本。
示例代码段:
Highlighter highlighter = new Highlighter(htmlFormatter, new QueryScorer(query));
for (int i = 0; i < 10; i++) {
int id = hits.scoreDocs[i].doc;
Document doc = searcher.doc(id);
TokenStream tokenStream = TokenSources.getAnyTokenStream(searcher.getIndexReader(), id, "body", analyzer);
TextFragment[] frag = highlighter.getBestTextFragments(tokenStream, text, false, 10);
...
...