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
VectorHighlightMapper.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 #if LUCENENET_350 //Lucene.Net specific code. See https://issues.apache.org/jira/browse/LUCENENET-350
19 
20 using System;
21 using System.Collections.Generic;
22 using Lucene.Net.Index;
23 
24 namespace Lucene.Net.Search.Vectorhighlight
25 {
26  public class VectorHighlightMapper : TermVectorMapper, ITermFreqVector, TermPositionVector
27  {
28  private readonly List<string> _terms;
29  private Dictionary<string, TermVectorOffsetInfo[]> _tvoi;
30  private Dictionary<string, int[]> _positions;
31  private Dictionary<string, int> _frequency;
32  private List<string> _indexMap;
33  private string _field;
34  private bool _storeOffsets;
35  private bool _storePositions;
36 
37  public VectorHighlightMapper(List<string> terms)
38  {
39  _terms = terms;
40  _tvoi = new Dictionary<string, TermVectorOffsetInfo[]>();
41  _positions = new Dictionary<string, int[]>();
42  _frequency = new Dictionary<string, int>();
43  _indexMap = new List<string>();
44  }
45 
46  public override void SetExpectations(string field, int numTerms, bool storeOffsets, bool storePositions)
47  {
48  _field = field;
49  _storeOffsets = storeOffsets;
50  _storePositions = storePositions;
51  if (_storeOffsets)
52  _tvoi = new Dictionary<string, TermVectorOffsetInfo[]>(numTerms);
53  if (_storePositions)
54  _positions = new Dictionary<string, int[]>(numTerms);
55  _frequency = new Dictionary<string, int>(numTerms);
56  _indexMap = new List<string>(numTerms);
57  }
58 
59  public override void Map(string term, int frequency, TermVectorOffsetInfo[] offsets, int[] positions)
60  {
61  if (StringUtils.AnyTermMatch(_terms, term))
62  {
63  _indexMap.Add(term);
64  if (_storeOffsets)
65  _tvoi.Add(term, offsets);
66  if (_storePositions)
67  _positions.Add(term, positions);
68  _frequency.Add(term,frequency);
69  }
70  }
71 
72  public string Field
73  {
74  get { return _field; }
75  }
76 
77  public int Size
78  {
79  get { return _tvoi.Count; }
80  }
81 
82  public string[] GetTerms()
83  {
84  string[] result = new string[_tvoi.Count];
85  _tvoi.Keys.CopyTo(result,0);
86  return result;
87  }
88 
89  public int[] GetTermFrequencies()
90  {
91  int[] result = new int[_frequency.Count];
92  _frequency.Values.CopyTo(result,0);
93  return result;
94  }
95 
96  public int IndexOf(string term)
97  {
98  return _indexMap.IndexOf(term);
99  }
100 
101  public int[] IndexesOf(string[] terms, int start, int len)
102  {
103  int[] result = new int[terms.Length];
104  for (int i = 0; i < terms.Length; i++)
105  {
106  string term = terms[i];
107  result[i] = _indexMap.IndexOf(term, start, len);
108  }
109  return result;
110  }
111 
112  public int[] GetTermPositions(int index)
113  {
114  if (index<_positions.Count)
115  {
116  string key = _indexMap[index];
117  return _positions[key];
118  }
119  return new int[0];
120  }
121 
122  public TermVectorOffsetInfo[] GetOffsets(int index)
123  {
124  if (index < _tvoi.Count)
125  {
126  string key = _indexMap[index];
127  return _tvoi[key];
128  }
129  return new TermVectorOffsetInfo[0];
130  }
131  }
132 }
133 
134 #endif