Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
PositionBasedTermVectorMapper.cs
Go to the documentation of this file.
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 using System.Collections.Generic;
20 using Lucene.Net.Support;
21 
22 namespace Lucene.Net.Index
23 {
24 
25  /// <summary> For each Field, store position by position information. It ignores frequency information
26  /// <p/>
27  /// This is not thread-safe.
28  /// </summary>
30  {
31  private IDictionary<string, IDictionary<int, TVPositionInfo>> fieldToTerms;
32 
33  private System.String currentField;
34  /// <summary> A Map of Integer and TVPositionInfo</summary>
35  private IDictionary<int, TVPositionInfo> currentPositions;
36  private bool storeOffsets;
37 
38  public PositionBasedTermVectorMapper():base(false, false)
39  {
40  }
41 
42  public PositionBasedTermVectorMapper(bool ignoringOffsets):base(false, ignoringOffsets)
43  {
44  }
45 
46  /// <summary> Never ignores positions. This mapper doesn't make much sense unless there are positions</summary>
47  /// <value> false </value>
48  public override bool IsIgnoringPositions
49  {
50  get { return false; }
51  }
52 
53  /// <summary> Callback for the TermVectorReader. </summary>
54  /// <param name="term">
55  /// </param>
56  /// <param name="frequency">
57  /// </param>
58  /// <param name="offsets">
59  /// </param>
60  /// <param name="positions">
61  /// </param>
62  public override void Map(System.String term, int frequency, TermVectorOffsetInfo[] offsets, int[] positions)
63  {
64  for (int i = 0; i < positions.Length; i++)
65  {
66  System.Int32 posVal = positions[i];
67  TVPositionInfo pos = currentPositions[posVal];
68  if (pos == null)
69  {
70  pos = new TVPositionInfo(positions[i], storeOffsets);
71  currentPositions[posVal] = pos;
72  }
73  pos.addTerm(term, offsets != null ? offsets[i] : TermVectorOffsetInfo.Null);
74  }
75  }
76 
77  /// <summary> Callback mechanism used by the TermVectorReader</summary>
78  /// <param name="field"> The field being read
79  /// </param>
80  /// <param name="numTerms">The number of terms in the vector
81  /// </param>
82  /// <param name="storeOffsets">Whether offsets are available
83  /// </param>
84  /// <param name="storePositions">Whether positions are available
85  /// </param>
86  public override void SetExpectations(System.String field, int numTerms, bool storeOffsets, bool storePositions)
87  {
88  if (storePositions == false)
89  {
90  throw new System.SystemException("You must store positions in order to use this Mapper");
91  }
92  if (storeOffsets == true)
93  {
94  //ignoring offsets
95  }
96  fieldToTerms = new HashMap<string, IDictionary<int, TVPositionInfo>>(numTerms);
97  this.storeOffsets = storeOffsets;
98  currentField = field;
99  currentPositions = new HashMap<int, TVPositionInfo>();
100  fieldToTerms[currentField] = currentPositions;
101  }
102 
103  /// <summary> Get the mapping between fields and terms, sorted by the comparator
104  ///
105  /// </summary>
106  /// <value> A map between field names and a Map. The sub-Map key is the position as the integer, the value is &lt;see cref=&quot;Lucene.Net.Index.PositionBasedTermVectorMapper.TVPositionInfo&quot; /&gt;. </value>
107  public virtual IDictionary<string, IDictionary<int, TVPositionInfo>> FieldToTerms
108  {
109  get { return fieldToTerms; }
110  }
111 
112  /// <summary> Container for a term at a position</summary>
113  public class TVPositionInfo
114  {
115  /// <summary> </summary>
116  /// <returns> The position of the term
117  /// </returns>
118  virtual public int Position
119  {
120  get
121  {
122  return position;
123  }
124 
125  }
126  /// <summary> Note, there may be multiple terms at the same position</summary>
127  /// <returns> A List of Strings
128  /// </returns>
129  virtual public IList<String> Terms
130  {
131  get
132  {
133  return terms;
134  }
135 
136  }
137  /// <summary> Parallel list (to <see cref="Terms" />) of TermVectorOffsetInfo objects.
138  /// There may be multiple entries since there may be multiple terms at a position</summary>
139  /// <returns> A List of TermVectorOffsetInfo objects, if offsets are store.
140  /// </returns>
141  virtual public IList<TermVectorOffsetInfo> Offsets
142  {
143  get
144  {
145  return offsets;
146  }
147 
148  }
149  private int position;
150  //a list of Strings
151  private IList<string> terms;
152  //A list of TermVectorOffsetInfo
153  private IList<TermVectorOffsetInfo> offsets;
154 
155 
156  public TVPositionInfo(int position, bool storeOffsets)
157  {
158  this.position = position;
159  terms = new List<string>();
160  if (storeOffsets)
161  {
162  offsets = new List<TermVectorOffsetInfo>();
163  }
164  }
165 
166  internal virtual void addTerm(System.String term, TermVectorOffsetInfo info)
167  {
168  terms.Add(term);
169  if (offsets != null)
170  {
171  offsets.Add(info);
172  }
173  }
174  }
175  }
176 }