Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / tools / monkeydoc / Lucene.Net / Lucene.Net / Index / PositionBasedTermVectorMapper.cs
1 /* 
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  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 using System;
19
20 namespace Mono.Lucene.Net.Index
21 {
22         
23         /// <summary> For each Field, store position by position information.  It ignores frequency information
24         /// <p/>
25         /// This is not thread-safe.
26         /// </summary>
27         public class PositionBasedTermVectorMapper:TermVectorMapper
28         {
29                 private System.Collections.IDictionary fieldToTerms;
30                 
31                 private System.String currentField;
32                 /// <summary> A Map of Integer and TVPositionInfo</summary>
33                 private System.Collections.IDictionary currentPositions;
34                 private bool storeOffsets;
35                 
36                 
37                 
38                 
39                 /// <summary> 
40                 /// 
41                 /// </summary>
42                 public PositionBasedTermVectorMapper():base(false, false)
43                 {
44                 }
45                 
46                 public PositionBasedTermVectorMapper(bool ignoringOffsets):base(false, ignoringOffsets)
47                 {
48                 }
49                 
50                 /// <summary> Never ignores positions.  This mapper doesn't make much sense unless there are positions</summary>
51                 /// <returns> false
52                 /// </returns>
53                 public override bool IsIgnoringPositions()
54                 {
55                         return false;
56                 }
57                 
58                 /// <summary> Callback for the TermVectorReader. </summary>
59                 /// <param name="term">
60                 /// </param>
61                 /// <param name="frequency">
62                 /// </param>
63                 /// <param name="offsets">
64                 /// </param>
65                 /// <param name="positions">
66                 /// </param>
67                 public override void  Map(System.String term, int frequency, TermVectorOffsetInfo[] offsets, int[] positions)
68                 {
69                         for (int i = 0; i < positions.Length; i++)
70                         {
71                                 System.Int32 posVal = (System.Int32) positions[i];
72                                 TVPositionInfo pos = (TVPositionInfo) currentPositions[posVal];
73                                 if (pos == null)
74                                 {
75                                         pos = new TVPositionInfo(positions[i], storeOffsets);
76                                         currentPositions[posVal] = pos;
77                                 }
78                                 pos.addTerm(term, offsets != null?offsets[i]:null);
79                         }
80                 }
81                 
82                 /// <summary> Callback mechanism used by the TermVectorReader</summary>
83                 /// <param name="field"> The field being read
84                 /// </param>
85                 /// <param name="numTerms">The number of terms in the vector
86                 /// </param>
87                 /// <param name="storeOffsets">Whether offsets are available
88                 /// </param>
89                 /// <param name="storePositions">Whether positions are available
90                 /// </param>
91                 public override void  SetExpectations(System.String field, int numTerms, bool storeOffsets, bool storePositions)
92                 {
93                         if (storePositions == false)
94                         {
95                                 throw new System.SystemException("You must store positions in order to use this Mapper");
96                         }
97                         if (storeOffsets == true)
98                         {
99                                 //ignoring offsets
100                         }
101                         fieldToTerms = new System.Collections.Hashtable(numTerms);
102                         this.storeOffsets = storeOffsets;
103                         currentField = field;
104                         currentPositions = new System.Collections.Hashtable();
105                         fieldToTerms[currentField] = currentPositions;
106                 }
107                 
108                 /// <summary> Get the mapping between fields and terms, sorted by the comparator
109                 /// 
110                 /// </summary>
111                 /// <returns> A map between field names and a Map.  The sub-Map key is the position as the integer, the value is {@link Mono.Lucene.Net.Index.PositionBasedTermVectorMapper.TVPositionInfo}.
112                 /// </returns>
113                 public virtual System.Collections.IDictionary GetFieldToTerms()
114                 {
115                         return fieldToTerms;
116                 }
117                 
118                 /// <summary> Container for a term at a position</summary>
119                 public class TVPositionInfo
120                 {
121                         /// <summary> </summary>
122                         /// <returns> The position of the term
123                         /// </returns>
124                         virtual public int Position
125                         {
126                                 get
127                                 {
128                                         return position;
129                                 }
130                                 
131                         }
132                         /// <summary> Note, there may be multiple terms at the same position</summary>
133                         /// <returns> A List of Strings
134                         /// </returns>
135                         virtual public System.Collections.IList Terms
136                         {
137                                 get
138                                 {
139                                         return terms;
140                                 }
141                                 
142                         }
143                         /// <summary> Parallel list (to {@link #getTerms()}) of TermVectorOffsetInfo objects.  There may be multiple entries since there may be multiple terms at a position</summary>
144                         /// <returns> A List of TermVectorOffsetInfo objects, if offsets are store.
145                         /// </returns>
146                         virtual public System.Collections.IList Offsets
147                         {
148                                 get
149                                 {
150                                         return offsets;
151                                 }
152                                 
153                         }
154                         private int position;
155                         //a list of Strings
156                         private System.Collections.IList terms;
157                         //A list of TermVectorOffsetInfo
158                         private System.Collections.IList offsets;
159                         
160                         
161                         public TVPositionInfo(int position, bool storeOffsets)
162                         {
163                                 this.position = position;
164                                 terms = new System.Collections.ArrayList();
165                                 if (storeOffsets)
166                                 {
167                                         offsets = new System.Collections.ArrayList();
168                                 }
169                         }
170                         
171                         internal virtual void  addTerm(System.String term, TermVectorOffsetInfo info)
172                         {
173                                 terms.Add(term);
174                                 if (offsets != null)
175                                 {
176                                         offsets.Add(info);
177                                 }
178                         }
179                 }
180         }
181 }