-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Script to extract doc lengths (#791)
Script that produces tsv file which contains the length of all documents, the unique term count and the lossy unique term count as used in the BM25similarity class.
- Loading branch information
1 parent
8638830
commit 61f6f20
Showing
3 changed files
with
84 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/main/java/io/anserini/util/ExtractDocumentLengths.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* Anserini: A Lucene toolkit for replicable information retrieval research | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.anserini.util; | ||
|
||
import org.apache.lucene.index.DirectoryReader; | ||
import org.apache.lucene.index.IndexReader; | ||
import org.apache.lucene.index.Terms; | ||
import org.apache.lucene.index.TermsEnum; | ||
import org.apache.lucene.store.Directory; | ||
import org.apache.lucene.store.FSDirectory; | ||
import org.apache.lucene.util.SmallFloat; | ||
import org.kohsuke.args4j.*; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.PrintStream; | ||
import java.nio.file.Paths; | ||
|
||
public class ExtractDocumentLengths { | ||
|
||
public static class Args { | ||
@Option(name = "-index", metaVar = "[path]", required = true, usage = "Lucene index") | ||
String index; | ||
|
||
@Option(name = "-output", metaVar = "[file]", required = true, usage = "output file") | ||
String output; | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
Args myArgs = new Args(); | ||
CmdLineParser parser = new CmdLineParser(myArgs, ParserProperties.defaults().withUsageWidth(90)); | ||
|
||
try { | ||
parser.parseArgument(args); | ||
} catch (CmdLineException e) { | ||
System.err.println(e.getMessage()); | ||
parser.printUsage(System.err); | ||
return; | ||
} | ||
|
||
Directory dir = FSDirectory.open(Paths.get(myArgs.index)); | ||
IndexReader reader = DirectoryReader.open(dir); | ||
|
||
PrintStream out = new PrintStream(new FileOutputStream(new File(myArgs.output))); | ||
|
||
int numDocs = reader.numDocs(); | ||
out.println("luceneID\tcount\tuniquecount\tlossycount"); | ||
for (int i = 0; i < numDocs; i++) { | ||
int total = 0; | ||
Terms terms = reader.getTermVector(i, "contents"); | ||
if(terms == null) { | ||
out.println(i + "\t" + 0 + "\t" + 0 + "\t" + 0); | ||
continue; | ||
} | ||
TermsEnum termsEnum = terms.iterator(); | ||
while ((termsEnum.next()) != null) { | ||
total += termsEnum.totalTermFreq(); | ||
} | ||
long length = SmallFloat.longToInt4(terms.size()); | ||
out.println(i + "\t" + total + "\t" + terms.size() + "\t" + length) ; | ||
} | ||
out.close(); | ||
} | ||
} |