pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / misc / src / java / org / apache / lucene / misc / GetTermInfo.java
1 package org.apache.lucene.misc;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 import java.io.File;
20 import org.apache.lucene.store.Directory;
21 import org.apache.lucene.store.FSDirectory;
22
23 import org.apache.lucene.index.Term;
24 import org.apache.lucene.index.IndexReader;
25
26
27 /*
28  * Utility to get document frequency and total number of occurrences (sum of the tf for each doc)  of a term. 
29  */
30 public class GetTermInfo {
31   
32     public static void main(String[] args) throws Exception {
33     
34     FSDirectory dir = null;
35     String inputStr = null;
36     String field = null;
37     
38     if (args.length == 3) {
39       dir = FSDirectory.open(new File(args[0]));
40       field = args[1];
41       inputStr = args[2];
42     } else {
43       usage();
44       System.exit(1);
45     }
46     Term term = new Term(field, inputStr);
47     getTermInfo(dir,term);
48   }
49   
50     public static void getTermInfo(Directory dir, Term term) throws Exception {
51     IndexReader reader = IndexReader.open(dir);
52     
53     long totalTF = HighFreqTerms.getTotalTermFreq(reader, term);
54     System.out.printf("%s:%s \t total_tf = %,d \t doc freq = %,d \n",
55         term.field(), term.text(), totalTF, reader.docFreq(term)); 
56   }
57    
58   private static void usage() {
59     System.out
60         .println("\n\nusage:\n\t"
61             + "java " + GetTermInfo.class.getName() + " <index dir> field term \n\n");
62   }
63 }