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
SegmentTermVector.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 
20 namespace Lucene.Net.Index
21 {
22 
23 
25  {
26  private System.String field;
27  private System.String[] terms;
28  private int[] termFreqs;
29 
30  internal SegmentTermVector(System.String field, System.String[] terms, int[] termFreqs)
31  {
32  this.field = field;
33  this.terms = terms;
34  this.termFreqs = termFreqs;
35  }
36 
37  /// <summary> </summary>
38  /// <value> The number of the field this vector is associated with </value>
39  public virtual string Field
40  {
41  get { return field; }
42  }
43 
44  public override System.String ToString()
45  {
46  System.Text.StringBuilder sb = new System.Text.StringBuilder();
47  sb.Append('{');
48  sb.Append(field).Append(": ");
49  if (terms != null)
50  {
51  for (int i = 0; i < terms.Length; i++)
52  {
53  if (i > 0)
54  sb.Append(", ");
55  sb.Append(terms[i]).Append('/').Append(termFreqs[i]);
56  }
57  }
58  sb.Append('}');
59 
60  return sb.ToString();
61  }
62 
63  public virtual int Size
64  {
65  get { return terms == null ? 0 : terms.Length; }
66  }
67 
68  public virtual System.String[] GetTerms()
69  {
70  return terms;
71  }
72 
73  public virtual int[] GetTermFrequencies()
74  {
75  return termFreqs;
76  }
77 
78  public virtual int IndexOf(System.String termText)
79  {
80  if (terms == null)
81  return - 1;
82  int res = System.Array.BinarySearch(terms, termText, System.StringComparer.Ordinal);
83  return res >= 0?res:- 1;
84  }
85 
86  public virtual int[] IndexesOf(System.String[] termNumbers, int start, int len)
87  {
88  // TODO: there must be a more efficient way of doing this.
89  // At least, we could advance the lower bound of the terms array
90  // as we find valid indexes. Also, it might be possible to leverage
91  // this even more by starting in the middle of the termNumbers array
92  // and thus dividing the terms array maybe in half with each found index.
93  int[] res = new int[len];
94 
95  for (int i = 0; i < len; i++)
96  {
97  res[i] = IndexOf(termNumbers[start + i]);
98  }
99  return res;
100  }
101  }
102 }