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
SortedTermVectorMapper.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> Store a sorted collection of <see cref="Lucene.Net.Index.TermVectorEntry" />s. Collects all term information
26  /// into a single, SortedSet.
27  /// <br/>
28  /// NOTE: This Mapper ignores all Field information for the Document. This means that if you are using offset/positions you will not
29  /// know what Fields they correlate with.
30  /// <br/>
31  /// This is not thread-safe
32  /// </summary>
34  {
35  private SortedSet<TermVectorEntry> currentSet;
36  private IDictionary<string, TermVectorEntry> termToTVE = new HashMap<string, TermVectorEntry>();
37  private bool storeOffsets;
38  private bool storePositions;
39  /// <summary> Stand-in name for the field in <see cref="TermVectorEntry" />.</summary>
40  public const System.String ALL = "_ALL_";
41 
42  /// <summary> </summary>
43  /// <param name="comparator">A Comparator for sorting <see cref="TermVectorEntry" />s
44  /// </param>
45  public SortedTermVectorMapper(IComparer<TermVectorEntry> comparator)
46  : this(false, false, comparator)
47  {
48  }
49 
50 
51  public SortedTermVectorMapper(bool ignoringPositions, bool ignoringOffsets, IComparer<TermVectorEntry> comparator)
52  : base(ignoringPositions, ignoringOffsets)
53  {
54  currentSet = new SortedSet<TermVectorEntry>(comparator);
55  }
56 
57  /// <summary> </summary>
58  /// <param name="term">The term to map
59  /// </param>
60  /// <param name="frequency">The frequency of the term
61  /// </param>
62  /// <param name="offsets">Offset information, may be null
63  /// </param>
64  /// <param name="positions">Position information, may be null
65  /// </param>
66  //We need to combine any previous mentions of the term
67  public override void Map(System.String term, int frequency, TermVectorOffsetInfo[] offsets, int[] positions)
68  {
69  TermVectorEntry entry = termToTVE[term];
70  if (entry == null)
71  {
72  entry = new TermVectorEntry(ALL, term, frequency, storeOffsets == true?offsets:null, storePositions == true?positions:null);
73  termToTVE[term] = entry;
74  currentSet.Add(entry);
75  }
76  else
77  {
78  entry.Frequency = entry.Frequency + frequency;
79  if (storeOffsets)
80  {
81  TermVectorOffsetInfo[] existingOffsets = entry.GetOffsets();
82  //A few diff. cases here: offsets is null, existing offsets is null, both are null, same for positions
83  if (existingOffsets != null && offsets != null && offsets.Length > 0)
84  {
85  //copy over the existing offsets
86  TermVectorOffsetInfo[] newOffsets = new TermVectorOffsetInfo[existingOffsets.Length + offsets.Length];
87  Array.Copy(existingOffsets, 0, newOffsets, 0, existingOffsets.Length);
88  Array.Copy(offsets, 0, newOffsets, existingOffsets.Length, offsets.Length);
89  entry.SetOffsets(newOffsets);
90  }
91  else if (existingOffsets == null && offsets != null && offsets.Length > 0)
92  {
93  entry.SetOffsets(offsets);
94  }
95  //else leave it alone
96  }
97  if (storePositions)
98  {
99  int[] existingPositions = entry.GetPositions();
100  if (existingPositions != null && positions != null && positions.Length > 0)
101  {
102  int[] newPositions = new int[existingPositions.Length + positions.Length];
103  Array.Copy(existingPositions, 0, newPositions, 0, existingPositions.Length);
104  Array.Copy(positions, 0, newPositions, existingPositions.Length, positions.Length);
105  entry.SetPositions(newPositions);
106  }
107  else if (existingPositions == null && positions != null && positions.Length > 0)
108  {
109  entry.SetPositions(positions);
110  }
111  }
112  }
113  }
114 
115  public override void SetExpectations(System.String field, int numTerms, bool storeOffsets, bool storePositions)
116  {
117 
118  this.storeOffsets = storeOffsets;
119  this.storePositions = storePositions;
120  }
121 
122  /// <summary> The TermVectorEntrySet. A SortedSet of <see cref="TermVectorEntry" /> objects. Sort is by the comparator passed into the constructor.
123  /// <br/>
124  /// This set will be empty until after the mapping process takes place.
125  ///
126  /// </summary>
127  /// <value> The SortedSet of &lt;see cref=&quot;TermVectorEntry&quot; /&gt;. </value>
128  public virtual SortedSet<TermVectorEntry> TermVectorEntrySet
129  {
130  get { return currentSet; }
131  }
132  }
133 }