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
SimpleSpanFragmenter.cs
Go to the documentation of this file.
1 /*
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements. See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership. The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License. You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied. See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20 */
21 
22 using System;
23 using System.Collections.Generic;
24 using System.Linq;
25 using System.Text;
26 using Lucene.Net.Analysis;
27 using Lucene.Net.Analysis.Tokenattributes;
28 
29 namespace Lucene.Net.Search.Highlight
30 {
32  {
33  private static int DEFAULT_FRAGMENT_SIZE = 100;
34  private int fragmentSize;
35  private int currentNumFrags;
36  private int position = -1;
37  private QueryScorer queryScorer;
38  private int waitForPos = -1;
39  private int textSize;
40  private ITermAttribute termAtt;
41  private IPositionIncrementAttribute posIncAtt;
42  private IOffsetAttribute offsetAtt;
43 
45  public SimpleSpanFragmenter(QueryScorer queryScorer)
46  : this(queryScorer, DEFAULT_FRAGMENT_SIZE)
47  {
48 
49  }
50 
53  public SimpleSpanFragmenter(QueryScorer queryScorer, int fragmentSize)
54  {
55  this.fragmentSize = fragmentSize;
56  this.queryScorer = queryScorer;
57  }
58 
60  public bool IsNewFragment()
61  {
62  position += posIncAtt.PositionIncrement;
63 
64  if (waitForPos == position)
65  {
66  waitForPos = -1;
67  }
68  else if (waitForPos != -1)
69  {
70  return false;
71  }
72 
73  WeightedSpanTerm wSpanTerm = queryScorer.GetWeightedSpanTerm(termAtt.Term);
74 
75  if (wSpanTerm != null)
76  {
77  List<PositionSpan> positionSpans = wSpanTerm.GetPositionSpans();
78 
79  for (int i = 0; i < positionSpans.Count; i++)
80  {
81  if (positionSpans[i].Start == position)
82  {
83  waitForPos = positionSpans[i].End + 1;
84  break;
85  }
86  }
87  }
88 
89  bool isNewFrag = offsetAtt.EndOffset >= (fragmentSize*currentNumFrags)
90  && (textSize - offsetAtt.EndOffset) >= ((uint) fragmentSize >> 1);
91 
92 
93  if (isNewFrag)
94  {
95  currentNumFrags++;
96  }
97 
98  return isNewFrag;
99  }
100 
102  public void Start(String originalText, TokenStream tokenStream)
103  {
104  position = -1;
105  currentNumFrags = 1;
106  textSize = originalText.Length;
107  termAtt = tokenStream.AddAttribute<ITermAttribute>();
108  posIncAtt = tokenStream.AddAttribute<IPositionIncrementAttribute>();
109  offsetAtt = tokenStream.AddAttribute<IOffsetAttribute>();
110  }
111  }
112 }