pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / index / SortedTermVectorMapper.java
1 package org.apache.lucene.index;
2 /**
3  * Copyright 2007 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 import java.util.*;
19
20 /**
21  * Store a sorted collection of {@link org.apache.lucene.index.TermVectorEntry}s.  Collects all term information
22  * into a single, SortedSet.
23  * <br/>
24  * NOTE: This Mapper ignores all Field information for the Document.  This means that if you are using offset/positions you will not
25  * know what Fields they correlate with.
26  *  <br/>
27  * This is not thread-safe  
28  */
29 public class SortedTermVectorMapper extends TermVectorMapper{
30
31
32   private SortedSet<TermVectorEntry> currentSet;
33   private Map<String,TermVectorEntry> termToTVE = new HashMap<String,TermVectorEntry>();
34   private boolean storeOffsets;
35   private boolean storePositions;
36   /**
37    * Stand-in name for the field in {@link TermVectorEntry}.
38    */
39   public static final String ALL = "_ALL_";
40
41   /**
42    *
43    * @param comparator A Comparator for sorting {@link TermVectorEntry}s
44    */
45   public SortedTermVectorMapper(Comparator<TermVectorEntry> comparator) {
46     this(false, false, comparator);
47   }
48
49
50   public SortedTermVectorMapper(boolean ignoringPositions, boolean ignoringOffsets, Comparator<TermVectorEntry> comparator) {
51     super(ignoringPositions, ignoringOffsets);
52     currentSet = new TreeSet<TermVectorEntry>(comparator);
53   }
54
55   /**
56    *
57    * @param term The term to map
58    * @param frequency The frequency of the term
59    * @param offsets Offset information, may be null
60    * @param positions Position information, may be null
61    */
62   //We need to combine any previous mentions of the term
63   @Override
64   public void map(String term, int frequency, TermVectorOffsetInfo[] offsets, int[] positions) {
65     TermVectorEntry entry =  termToTVE.get(term);
66     if (entry == null) {
67       entry = new TermVectorEntry(ALL, term, frequency, 
68               storeOffsets == true ? offsets : null,
69               storePositions == true ? positions : null);
70       termToTVE.put(term, entry);
71       currentSet.add(entry);
72     } else {
73       entry.setFrequency(entry.getFrequency() + frequency);
74       if (storeOffsets)
75       {
76         TermVectorOffsetInfo [] existingOffsets = entry.getOffsets();
77         //A few diff. cases here:  offsets is null, existing offsets is null, both are null, same for positions
78         if (existingOffsets != null && offsets != null && offsets.length > 0)
79         {
80           //copy over the existing offsets
81           TermVectorOffsetInfo [] newOffsets = new TermVectorOffsetInfo[existingOffsets.length + offsets.length];
82           System.arraycopy(existingOffsets, 0, newOffsets, 0, existingOffsets.length);
83           System.arraycopy(offsets, 0, newOffsets, existingOffsets.length, offsets.length);
84           entry.setOffsets(newOffsets);
85         }
86         else if (existingOffsets == null && offsets != null && offsets.length > 0)
87         {
88           entry.setOffsets(offsets);
89         }
90         //else leave it alone
91       }
92       if (storePositions)
93       {
94         int [] existingPositions = entry.getPositions();
95         if (existingPositions != null && positions != null && positions.length > 0)
96         {
97           int [] newPositions = new int[existingPositions.length + positions.length];
98           System.arraycopy(existingPositions, 0, newPositions, 0, existingPositions.length);
99           System.arraycopy(positions, 0, newPositions, existingPositions.length, positions.length);
100           entry.setPositions(newPositions);
101         }
102         else if (existingPositions == null && positions != null && positions.length > 0)
103         {
104           entry.setPositions(positions);
105         }
106       }
107     }
108
109
110   }
111
112   @Override
113   public void setExpectations(String field, int numTerms, boolean storeOffsets, boolean storePositions) {
114
115     this.storeOffsets = storeOffsets;
116     this.storePositions = storePositions;
117   }
118
119   /**
120    * The TermVectorEntrySet.  A SortedSet of {@link TermVectorEntry} objects.  Sort is by the comparator passed into the constructor.
121    *<br/>
122    * This set will be empty until after the mapping process takes place.
123    *
124    * @return The SortedSet of {@link TermVectorEntry}.
125    */
126   public SortedSet<TermVectorEntry> getTermVectorEntrySet()
127   {
128     return currentSet;
129   }
130
131 }