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
TokenGroup.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 Lucene.Net.Analysis;
20 using Lucene.Net.Analysis.Tokenattributes;
21 
22 namespace Lucene.Net.Search.Highlight
23 {
24  /// <summary> One, or several overlapping tokens, along with the score(s) and the
25  /// scope of the original text
26  /// </summary>
27  public class TokenGroup
28  {
29  private static readonly int MAX_NUM_TOKENS_PER_GROUP = 50;
30 
31  private Token[] tokens = new Token[MAX_NUM_TOKENS_PER_GROUP];
32  private float[] scores = new float[MAX_NUM_TOKENS_PER_GROUP];
33  private int startOffset = 0;
34  private int endOffset = 0;
35  private float tot;
36 
37  public int MatchStartOffset { get; private set; }
38  public int MatchEndOffset { get; private set; }
39  public int NumTokens { get; private set; }
40 
41  private IOffsetAttribute offsetAtt;
42  private ITermAttribute termAtt;
43 
44  public TokenGroup(TokenStream tokenStream)
45  {
46  NumTokens = 0;
47  offsetAtt = tokenStream.AddAttribute<IOffsetAttribute>();
48  termAtt = tokenStream.AddAttribute<ITermAttribute>();
49  }
50 
51  protected internal void AddToken(float score)
52  {
53  if (NumTokens < MAX_NUM_TOKENS_PER_GROUP)
54  {
55  int termStartOffset = offsetAtt.StartOffset;
56  int termEndOffset = offsetAtt.EndOffset;
57  if (NumTokens == 0)
58  {
59  startOffset = MatchStartOffset = termStartOffset;
60  endOffset = MatchEndOffset = termEndOffset;
61  tot += score;
62  }
63  else
64  {
65  startOffset = Math.Min(startOffset, termStartOffset);
66  endOffset = Math.Max(endOffset, termEndOffset);
67  if (score > 0)
68  {
69  if (tot == 0)
70  {
71  MatchStartOffset = offsetAtt.StartOffset;
72  MatchEndOffset = offsetAtt.EndOffset;
73  }
74  else
75  {
76  MatchStartOffset = Math.Min(MatchStartOffset, termStartOffset);
77  MatchEndOffset = Math.Max(MatchEndOffset, termEndOffset);
78  }
79  tot += score;
80  }
81  }
82  Token token = new Token(termStartOffset, termEndOffset);
83  token.SetTermBuffer(termAtt.Term);
84  tokens[NumTokens] = token;
85  scores[NumTokens] = score;
86  NumTokens++;
87  }
88  }
89 
90  protected internal bool IsDistinct()
91  {
92  return offsetAtt.StartOffset >= endOffset;
93  }
94 
95  protected internal void Clear()
96  {
97  NumTokens = 0;
98  tot = 0;
99  }
100 
101 
102  /// <summary>
103  /// the "n"th token
104  /// </summary>
105  /// <param name="index">a value between 0 and numTokens -1</param>
106  public Token GetToken(int index)
107  {
108  return tokens[index];
109  }
110 
111  /// <summary>
112  /// the "n"th score
113  /// </summary>
114  /// <param name="index">a value between 0 and numTokens -1</param>
115  public float GetScore(int index)
116  {
117  return scores[index];
118  }
119 
120  /// <summary>
121  /// the end position in the original text
122  /// </summary>
123  public int EndOffset
124  {
125  get { return endOffset; }
126  }
127 
128  /// <summary>
129  /// The start position in the original text
130  /// </summary>
131  public int StartOffset
132  {
133  get { return startOffset; }
134  }
135 
136  /// <summary>
137  /// All tokens' scores summed up
138  /// </summary>
139  public float TotalScore
140  {
141  get { return tot; }
142  }
143  }
144 }