forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
IDFValueSource.cs
77 lines (72 loc) · 2.88 KB
/
IDFValueSource.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Lucene version compatibility level 4.8.1
using Lucene.Net.Index;
using Lucene.Net.Search;
using Lucene.Net.Search.Similarities;
using Lucene.Net.Support;
using Lucene.Net.Util;
using System;
using System.Collections;
namespace Lucene.Net.Queries.Function.ValueSources
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/// <summary>
/// Function that returns <see cref="TFIDFSimilarity.Idf(long, long)"/>
/// for every document.
/// <para/>
/// Note that the configured Similarity for the field must be
/// a subclass of <see cref="TFIDFSimilarity"/>
/// @lucene.internal
/// </summary>
[ExceptionToClassNameConvention]
public class IDFValueSource : DocFreqValueSource
{
public IDFValueSource(string field, string val, string indexedField, BytesRef indexedBytes)
: base(field, val, indexedField, indexedBytes)
{
}
public override string Name => "idf";
public override FunctionValues GetValues(IDictionary context, AtomicReaderContext readerContext)
{
var searcher = (IndexSearcher)context["searcher"];
TFIDFSimilarity sim = AsTFIDF(searcher.Similarity, m_field);
if (sim is null)
{
throw UnsupportedOperationException.Create("requires a TFIDFSimilarity (such as DefaultSimilarity)");
}
int docfreq = searcher.IndexReader.DocFreq(new Term(m_indexedField, m_indexedBytes));
float idf = sim.Idf(docfreq, searcher.IndexReader.MaxDoc);
return new ConstDoubleDocValues(idf, this);
}
// tries extra hard to cast the sim to TFIDFSimilarity
internal static TFIDFSimilarity AsTFIDF(Similarity sim, string field)
{
while (sim is PerFieldSimilarityWrapper perFieldSimilarityWrapper)
{
sim = perFieldSimilarityWrapper.Get(field);
}
if (sim is TFIDFSimilarity similarity)
{
return similarity;
}
else
{
return null;
}
}
}
}