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
FieldFragList.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 Lucene.Net.Documents;
23 using Lucene.Net.Search;
24 using Lucene.Net.Index;
25 
26 using Toffs = Lucene.Net.Search.Vectorhighlight.FieldPhraseList.WeightedPhraseInfo.Toffs;
27 using WeightedPhraseInfo = Lucene.Net.Search.Vectorhighlight.FieldPhraseList.WeightedPhraseInfo;
28 
29 
30 namespace Lucene.Net.Search.Vectorhighlight
31 {
32  ///<summary>
33  /// FieldFragList has a list of "frag info" that is used by FragmentsBuilder class
34  /// to create fragments (snippets).
35  ///</summary>
36  public class FieldFragList
37  {
38  private int fragCharSize;
39  public List<WeightedFragInfo> fragInfos = new List<WeightedFragInfo>();
40 
41 
42  /// <summary>
43  /// a constructor.
44  /// </summary>
45  /// <param name="fragCharSize">the length (number of chars) of a fragment</param>
46  public FieldFragList(int fragCharSize)
47  {
48  this.fragCharSize = fragCharSize;
49  }
50 
51  /// <summary>
52  /// convert the list of WeightedPhraseInfo to WeightedFragInfo, then add it to the fragInfos
53  /// </summary>
54  /// <param name="startOffset">start offset of the fragment</param>
55  /// <param name="endOffset">end offset of the fragment</param>
56  /// <param name="phraseInfoList">list of WeightedPhraseInfo objects</param>
57  public void Add(int startOffset, int endOffset, List<WeightedPhraseInfo> phraseInfoList)
58  {
59  fragInfos.Add(new WeightedFragInfo(startOffset, endOffset, phraseInfoList));
60  }
61 
62  public class WeightedFragInfo
63  {
64 
65  internal List<SubInfo> subInfos;
66  internal float totalBoost;
67  internal int startOffset;
68  internal int endOffset;
69 
70  public WeightedFragInfo(int startOffset, int endOffset, List<WeightedPhraseInfo> phraseInfoList)
71  {
72  this.startOffset = startOffset;
73  this.endOffset = endOffset;
74  subInfos = new List<SubInfo>();
75  foreach (WeightedPhraseInfo phraseInfo in phraseInfoList)
76  {
77  SubInfo subInfo = new SubInfo(phraseInfo.text, phraseInfo.termsOffsets, phraseInfo.seqnum);
78  subInfos.Add(subInfo);
79  totalBoost += phraseInfo.boost;
80  }
81  }
82 
83  public override string ToString()
84  {
85  StringBuilder sb = new StringBuilder();
86  sb.Append("subInfos=(");
87  foreach (SubInfo si in subInfos)
88  sb.Append(si.ToString());
89  sb.Append(")/").Append(totalBoost.ToString(".0").Replace(System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator,".")).Append('(').Append(startOffset).Append(',').Append(endOffset).Append(')');
90  return sb.ToString();
91  }
92 
93  internal class SubInfo
94  {
95  internal String text; // unnecessary member, just exists for debugging purpose
96  internal List<Toffs> termsOffsets; // usually termsOffsets.size() == 1,
97  // but if position-gap > 1 and slop > 0 then size() could be greater than 1
98  internal int seqnum;
99  internal SubInfo(String text, List<Toffs> termsOffsets, int seqnum)
100  {
101  this.text = text;
102  this.termsOffsets = termsOffsets;
103  this.seqnum = seqnum;
104  }
105 
106  public override string ToString()
107  {
108  StringBuilder sb = new StringBuilder();
109  sb.Append(text).Append('(');
110  foreach (Toffs to in termsOffsets)
111  sb.Append(to.ToString());
112  sb.Append(')');
113  return sb.ToString();
114  }
115  }
116  }
117  }
118 }