我在我的java应用程序中使用了一个Lucene算法来查找索引中的匹配字符串。 我从所有点击中获取了前5个顶级文档,但我想检查或计算原始字符串和匹配字符串的匹配项。 在Lucene有可能吗? Lucene有什么方法可以找到它吗? 例如: -
original string = I am good.
matching string = am good.
% of matching = 95
答案 0 :(得分:4)
当你说匹配百分比时,你的意思是什么?如果您想知道结果文档中包含多少原始文本的单词(例如,在您的情况下,3个中有2个) 然后你可以使用term vectors来完成工作,获得字段的术语向量并记录和迭代术语,看看你正在寻找的内容中是否有术语。或者甚至你可以存储字符串并获取整个内容并进行数学计算(如果存储不是问题)。 当前lucene使用vector space model(将从版本6x更改为BM25)来计算得分,并通过ScroeDoc为您提供匹配得分 但得分doc给出十进制值,如果足够则使用它。
如果这不能回答问题,那么请提供有关如何使用样本计算的更多详细信息。
希望这会有所帮助。
PS,我编写了简单的脚本,因此您可以根据需要查看并更正它:
package org.query;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.analysis.tokenattributes.TermToBytesRefAttribute;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.BytesRef;
import org.junit.Before;
import org.junit.Test;
import java.util.HashSet;
import java.util.Set;
/**
* Created by ekamolid on 11/2/2015.
*/
public class LevenshteinTest {
private RAMDirectory directory;
private IndexSearcher searcher;
private IndexReader reader;
private Analyzer analyzer;
@Before
public void setUp() throws Exception {
directory = new RAMDirectory();
analyzer = new WhitespaceAnalyzer();
IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(analyzer));
Document doc = new Document();
FieldType fieldType = new FieldType();
fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS);
fieldType.setStoreTermVectors(true);
doc.add(new Field("f", "the quick brown fox jumps over the lazy dog", fieldType));
writer.addDocument(doc);
doc = new Document();
doc.add(new Field("f", "the quick red fox jumps over the sleepy cat", fieldType));
writer.addDocument(doc);
doc = new Document();
doc.add(new Field("f", "quiick caar went xyztz dog", fieldType));
writer.addDocument(doc);
writer.close();
reader = DirectoryReader.open(directory);
searcher = new IndexSearcher(reader);
}
public static int distance(String a, String b) { //code is taken from http://rosettacode.org/wiki/Levenshtein_distance#Java
a = a.toLowerCase();
b = b.toLowerCase();
// i == 0
int[] costs = new int[b.length() + 1];
for (int j = 0; j < costs.length; j++)
costs[j] = j;
for (int i = 1; i <= a.length(); i++) {
// j == 0; nw = lev(i - 1, j)
costs[0] = i;
int nw = i - 1;
for (int j = 1; j <= b.length(); j++) {
int cj = Math.min(1 + Math.min(costs[j], costs[j - 1]), a.charAt(i - 1) == b.charAt(j - 1) ? nw : nw + 1);
nw = costs[j];
costs[j] = cj;
}
}
return costs[b.length()];
}
@Test
public void test1() throws Exception {
String s = "quick caar dog";
TokenStream tokenStream = analyzer.tokenStream("abc", s);
TermToBytesRefAttribute termAttribute = tokenStream.getAttribute(TermToBytesRefAttribute.class);
Set<String> stringSet = new HashSet<>();
tokenStream.reset();
BooleanQuery.Builder builder = new BooleanQuery.Builder();
while (tokenStream.incrementToken()) {
stringSet.add(termAttribute.getBytesRef().utf8ToString());
Query query = new FuzzyQuery(new Term("f", termAttribute.getBytesRef().utf8ToString()), 2); //search only 2 edits
builder.add(query, BooleanClause.Occur.SHOULD);
}
TopDocs hits = searcher.search(builder.build(), 10);
int exactMatch = 0;
int match1 = 0;
int match2 = 0;
for (ScoreDoc scoreDoc : hits.scoreDocs) {
exactMatch = match1 = match2 = 0;
Terms terms = reader.getTermVector(scoreDoc.doc, "f");
TermsEnum termsEnum = terms.iterator();
while (true) {
BytesRef bytesRef = termsEnum.next();
if (bytesRef == null) {
break;
}
String str = bytesRef.utf8ToString();
if (stringSet.contains(str)) {
exactMatch++;
continue;
}
for (String s1 : stringSet) {
int distance = distance(s1, str);
if (distance <= 1) {
match1++;
} else if (distance <= 2) {
match2++;
}
}
}
System.out.print(" doc=" + scoreDoc.doc);
System.out.print(" exactMatch=" + exactMatch);
System.out.print(" match1=" + match1);
System.out.println(" match2=" + match1);
}
}
}
我得到的输出是:
doc=2 exactMatch=2 match1=1 match2=1
doc=1 exactMatch=1 match1=0 match2=0
doc=0 exactMatch=2 match1=0 match2=0
这是工作代码并告诉我们有多少个字符完全匹配,其中有多少是1个字符差异和2个字符差异。因此,您可以将登录信息放在那里,根据您手上的数字计算%。由于您在迭代文档时,这可能会慢一点,但您应该将结果限制为特定数字(示例中为10
),因此它不会很慢。