Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
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 
30  {
31  private IDictionary<string, IDictionary<int, TVPositionInfo>> fieldToTerms;
32 
33  private System.String currentField;
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 
48  public override bool IsIgnoringPositions
49  {
50  get { return false; }
51  }
52 
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 
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 
107  public virtual IDictionary<string, IDictionary<int, TVPositionInfo>> FieldToTerms
108  {
109  get { return fieldToTerms; }
110  }
111 
113  public class TVPositionInfo
114  {
118  virtual public int Position
119  {
120  get
121  {
122  return position;
123  }
124 
125  }
129  virtual public IList<String> Terms
130  {
131  get
132  {
133  return terms;
134  }
135 
136  }
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 }