我已经解决了这个问题,我想分享我的答案以获得任何更正或评论..
问题是如何计算 avgLengthPath 参数。当我查看采用此参数的方法时:load()
可以看出它需要一个字符串,它是包含平均长度的文件的路径。所以avgLengthPath会是这样的:
/Users/admib/Study/avgLength
load()
方法如下:
public static void load(String path) throws NumberFormatException,
IOException {
BufferedReader in = new BufferedReader(new FileReader(path));
String line;
while (null != (line = in.readLine())) {
String field = line;
Float avg = new Float(in.readLine());
BM25Parameters.setAverageLength(field, avg);
}
in.close();
}
现在,免得看看如何创建这样的文件。我们可以看到上述方法逐行读取文件,并将每两行发送到另一个名为BM25Parameters.setAverageLength()
. avgLengthPath 文件的格式应该是这样的:
CONTENT
459.2903f
ANCHOR
84.55523f
其中第一行是文件名,第二行是该字段的平均长度。此外,第三行是另一个字段,第四行是该字段的平均长度。
此类文件的问题是我们无法从 Lucene 的默认位置获取文档长度。为了克服这个问题,我重新索引了我的集合并将文档长度添加为 Lucene 要索引的字段之一。
首先,我创建了一个获取文件并将文档长度作为字符串返回的方法。我称之为getDocLength(File f)
:
public static String getDocLength(File f) throws IOException {
FileInputStream stream = new FileInputStream(f);
try {
FileChannel fc = stream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
String doc = Charset.defaultCharset().decode(bb).toString();
int length = doc.length();
return Integer.toString(length);
} finally {
stream.close();
}
}
在索引过程中调用此方法来添加文档长度字段,如下所示:
protected Document getDocument(File f) throws Exception {
Document doc = new Document();
String docLength = Integer.toString(io.getDocLength(f));
doc.add(new Field("contents", new FileReader(f), Field.TermVector.YES));
doc.add(new Field("docLength", i, Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("filename", f.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("fullpath", f.getCanonicalPath(), Field.Store.YES, Field.Index.NOT_ANALYZED));
return doc;
}
最后,我创建了一个方法,循环遍历索引中的所有文档并计算平均文档长度,最后将结果以正确的格式保存到avgLengthPath文件中。我调用了这个方法generateAvgLengthPathFile()
:
public static void generateAvgLengthPathFile(String luceneIndexPath, String outputFilePath) {
try {
Directory dir = FSDirectory.open(new File(luceneIndexPath));
IndexReader reader = IndexReader.open(dir);
int totalLength = 0;
//here we loop through all the docs in the index
for (int i = 0; i < reader.maxDoc(); i++) {
if (reader.isDeleted(i)) {
continue;
}
Document doc = reader.document(i);
totalLength += Integer.parseInt(doc.get("docLength"));
}
//calculate the avarage length
float avarageLength = totalLength * 1.0f / reader.maxDoc() * 1.0f;
//create the a String varibale with the correct formate
String avgLengthPathFile = "contents" + "\n" + avarageLength;
//finally, save the file
Writer output = null;
String text = "contents" + "\n" + avarageLength;
File file = new File(outputFilePath);
output = new BufferedWriter(new FileWriter(file));
output.write(text);
output.close();
} catch (Exception e) {
System.err.println(e);
}
}