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
WeightedSpanTerm.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 System.Linq;
21 using System.Text;
22 
23 namespace Lucene.Net.Search.Highlight
24 {
25  /// <summary>
26  /// Lightweight class to hold term, Weight, and positions used for scoring this term.
27  /// </summary>
29  {
30  private bool _positionSensitive;
31  private readonly List<PositionSpan> _positionSpans = new List<PositionSpan>();
32 
33  public WeightedSpanTerm(float weight, String term)
34  : base(weight, term)
35  {
36 
37  this._positionSpans = new List<PositionSpan>();
38  }
39 
40  public WeightedSpanTerm(float weight, String term, bool positionSensitive)
41  : base(weight, term)
42  {
43 
44  this._positionSensitive = positionSensitive;
45  }
46 
47  /// <summary>
48  /// Checks to see if this term is valid at <c>position</c>.
49  /// </summary>
50  /// <param name="position">to check against valid term postions</param>
51  /// <returns>true iff this term is a hit at this position</returns>
52  public bool CheckPosition(int position)
53  {
54  // There would probably be a slight speed improvement if PositionSpans
55  // where kept in some sort of priority queue - that way this method
56  // could
57  // bail early without checking each PositionSpan.
58 
59  foreach (var positionSpan in _positionSpans)
60  {
61  if (((position >= positionSpan.Start) && (position <= positionSpan.End)))
62  {
63  return true;
64  }
65  }
66 
67  return false;
68  }
69 
70  public void AddPositionSpans(List<PositionSpan> positionSpans)
71  {
72  this._positionSpans.AddRange(positionSpans);
73  }
74 
75  public bool IsPositionSensitive()
76  {
77  return _positionSensitive;
78  }
79 
80  public void SetPositionSensitive(bool positionSensitive)
81  {
82  this._positionSensitive = positionSensitive;
83  }
84 
85  public List<PositionSpan> GetPositionSpans()
86  {
87  return _positionSpans;
88  }
89  }
90 
91 
92  // Utility class to store a Span
93  public class PositionSpan
94  {
95  public int Start { get; private set; }
96  public int End { get; private set; }
97 
98  public PositionSpan(int start, int end)
99  {
100  this.Start = start;
101  this.End = end;
102  }
103  }
104 }