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
SimpleFragListBuilder.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.Text;
21 
22 using WeightedPhraseInfo = Lucene.Net.Search.Vectorhighlight.FieldPhraseList.WeightedPhraseInfo;
23 
24 namespace Lucene.Net.Search.Vectorhighlight
25 {
26  /// <summary>
27  /// A simple implementation of FragListBuilder.
28  /// </summary>
30  {
31 
32  public static int MARGIN = 6;
33  public static int MIN_FRAG_CHAR_SIZE = MARGIN * 3;
34 
35  public FieldFragList CreateFieldFragList(FieldPhraseList fieldPhraseList, int fragCharSize)
36  {
37  if (fragCharSize < MIN_FRAG_CHAR_SIZE)
38  throw new ArgumentException("fragCharSize(" + fragCharSize + ") is too small. It must be " +
39  MIN_FRAG_CHAR_SIZE + " or higher.");
40 
41  FieldFragList ffl = new FieldFragList(fragCharSize);
42 
43  List<WeightedPhraseInfo> wpil = new List<WeightedPhraseInfo>();
44  LinkedList<WeightedPhraseInfo>.Enumerator ite = fieldPhraseList.phraseList.GetEnumerator();
45 
46  WeightedPhraseInfo phraseInfo = null;
47  int startOffset = 0;
48  bool taken = false;
49  while (true)
50  {
51  if (!taken)
52  {
53  if (!ite.MoveNext()) break;
54  phraseInfo = ite.Current;
55  }
56  taken = false;
57  if (phraseInfo == null) break;
58 
59  // if the phrase violates the border of previous fragment, discard it and try next phrase
60  if (phraseInfo.StartOffset < startOffset) continue;
61 
62  wpil.Clear();
63  wpil.Add(phraseInfo);
64  int st = phraseInfo.StartOffset - MARGIN < startOffset ?
65  startOffset : phraseInfo.StartOffset - MARGIN;
66  int en = st + fragCharSize;
67  if (phraseInfo.EndOffset > en)
68  en = phraseInfo.EndOffset;
69  startOffset = en;
70 
71  while (true)
72  {
73  if (ite.MoveNext())
74  {
75  phraseInfo = ite.Current;
76  taken = true;
77  if (phraseInfo == null) break;
78  }
79  else
80  break;
81  if (phraseInfo.EndOffset <= en)
82  wpil.Add(phraseInfo);
83  else
84  break;
85  }
86  ffl.Add(st, en, wpil);
87  }
88  return ffl;
89  }
90 
91  }
92 }