How to extract Document Term Vector in Lucene 3.5.0

后端 未结 2 488
走了就别回头了
走了就别回头了 2021-01-01 03:57

I am using Lucene 3.5.0 and I want to output term vectors of each document. For example I want to know the frequency of a term in all documents and in each specific document

相关标签:
2条回答
  • 2021-01-01 04:30

    First of all, you don't need to store term vectors in order to know solely the frequency of term in documents. Lucene stores these numbers nevertheless to use in TF-IDF calculation. You can access this information by calling IndexReader.termDocs(term) and iterating over the result.

    If you have some other purpose in mind and you actually need to access the term vectors, then you need to tell Lucene to store them, by passing Field.TermVector.YES as the last argument of Field constructor. Then, you can retrieve the vectors e.g. with IndexReader.getTermFreqVector().

    0 讨论(0)
  • 2021-01-01 04:37

    I am on Lucene core 3.0.3, but I expect the API will be very similar. This method will total up a term frequency map for a given set of Document numbers and a list of fields of interest, ignoring stop words.

        /**
     * Sums the term frequency vector of each document into a single term frequency map
     * @param indexReader the index reader, the document numbers are specific to this reader
     * @param docNumbers document numbers to retrieve frequency vectors from
     * @param fieldNames field names to retrieve frequency vectors from
     * @param stopWords terms to ignore
     * @return a map of each term to its frequency
     * @throws IOException
     */
    private Map<String,Integer> getTermFrequencyMap(IndexReader indexReader, List<Integer> docNumbers, String[] fieldNames, Set<String> stopWords)
    throws IOException {
        Map<String,Integer> totalTfv = new HashMap<String,Integer>(1024);
    
        for (Integer docNum : docNumbers) {
            for (String fieldName : fieldNames) {
                TermFreqVector tfv = indexReader.getTermFreqVector(docNum, fieldName);
                if (tfv == null) {
                    // ignore empty fields
                    continue;
                }
    
                String terms[] = tfv.getTerms();
                int termCount = terms.length;
                int freqs[] = tfv.getTermFrequencies();
    
                for (int t=0; t < termCount; t++) {
                    String term = terms[t];
                    int freq = freqs[t];
    
                    // filter out single-letter words and stop words
                    if (StringUtils.length(term) < 2 ||
                        stopWords.contains(term)) {
                        continue; // stop
                    }
    
                    Integer totalFreq = totalTfv.get(term);
                    totalFreq = (totalFreq == null) ? freq : freq + totalFreq;
                    totalTfv.put(term, totalFreq);
                }
            }
        }
    
        return totalTfv;
    }
    
    0 讨论(0)
提交回复
热议问题