mirror of https://github.com/apache/lucene.git
LUCENE-7466 - added axiomatic similarity, patch from Peilin Yang
This commit is contained in:
parent
f42cc2a8c3
commit
4236da27d1
|
@ -0,0 +1,159 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
package org.apache.lucene.search.similarities;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.lucene.search.Explanation;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Axiomatic approaches for IR. From Hui Fang and Chengxiang Zhai
|
||||||
|
* 2005. An Exploration of Axiomatic Approaches to Information Retrieval.
|
||||||
|
* In Proceedings of the 28th annual international ACM SIGIR
|
||||||
|
* conference on Research and development in information retrieval
|
||||||
|
* (SIGIR '05). ACM, New York, NY, USA, 480-487.
|
||||||
|
* <p>
|
||||||
|
* There are a family of models. All of them are based on BM25,
|
||||||
|
* Pivoted Document Length Normalization and Language model with
|
||||||
|
* Dirichlet prior. Some components (e.g. Term Frequency,
|
||||||
|
* Inverted Document Frequency) in the original models are modified
|
||||||
|
* so that they follow some axiomatic constraints.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @lucene.experimental
|
||||||
|
*/
|
||||||
|
public abstract class Axiomatic extends SimilarityBase {
|
||||||
|
/**
|
||||||
|
* hyperparam for the growth function
|
||||||
|
*/
|
||||||
|
protected final float s;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* hyperparam for the primitive weighthing function
|
||||||
|
*/
|
||||||
|
protected final float k;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* the query length
|
||||||
|
*/
|
||||||
|
protected final int queryLen;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor setting all Axiomatic hyperparameters
|
||||||
|
* @param s hyperparam for the growth function
|
||||||
|
* @param queryLen the query length
|
||||||
|
* @param k hyperparam for the primitive weighting function
|
||||||
|
*/
|
||||||
|
public Axiomatic(float s, int queryLen, float k) {
|
||||||
|
if (Float.isFinite(s) == false || Float.isNaN(s) || s < 0 || s > 1) {
|
||||||
|
throw new IllegalArgumentException("illegal s value: " + s + ", must be between 0 and 1");
|
||||||
|
}
|
||||||
|
if (Float.isFinite(k) == false || Float.isNaN(k) || k < 0 || k > 1) {
|
||||||
|
throw new IllegalArgumentException("illegal k value: " + k + ", must be between 0 and 1");
|
||||||
|
}
|
||||||
|
if (queryLen < 0 || queryLen > Integer.MAX_VALUE) {
|
||||||
|
throw new IllegalArgumentException("illegal query length value: "
|
||||||
|
+ queryLen + ", must be larger 0 and smaller than MAX_INT");
|
||||||
|
}
|
||||||
|
this.s = s;
|
||||||
|
this.queryLen = queryLen;
|
||||||
|
this.k = k;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor setting only s, letting k and queryLen to default
|
||||||
|
* @param s hyperparam for the growth function
|
||||||
|
*/
|
||||||
|
public Axiomatic(float s) {
|
||||||
|
this(s, 1, 0.35f);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor setting s and queryLen, letting k to default
|
||||||
|
* @param s hyperparam for the growth function
|
||||||
|
* @param queryLen the query length
|
||||||
|
*/
|
||||||
|
public Axiomatic(float s, int queryLen) {
|
||||||
|
this(s, queryLen, 0.35f);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default constructor
|
||||||
|
*/
|
||||||
|
public Axiomatic() {
|
||||||
|
this(0.25f, 1, 0.35f);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public float score(BasicStats stats, float freq, float docLen) {
|
||||||
|
return tf(stats, freq, docLen)
|
||||||
|
* ln(stats, freq, docLen)
|
||||||
|
* tfln(stats, freq, docLen)
|
||||||
|
* idf(stats, freq, docLen)
|
||||||
|
- gamma(stats, freq, docLen);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void explain(List<Explanation> subs, BasicStats stats, int doc,
|
||||||
|
float freq, float docLen) {
|
||||||
|
if (stats.getBoost() != 1.0f) {
|
||||||
|
subs.add(Explanation.match(stats.getBoost(), "boost"));
|
||||||
|
}
|
||||||
|
|
||||||
|
subs.add(Explanation.match(this.k, "k"));
|
||||||
|
subs.add(Explanation.match(this.s, "s"));
|
||||||
|
subs.add(Explanation.match(this.queryLen, "queryLen"));
|
||||||
|
subs.add(Explanation.match(tf(stats, freq, docLen), "tf"));
|
||||||
|
subs.add(Explanation.match(ln(stats, freq, docLen), "ln"));
|
||||||
|
subs.add(Explanation.match(tfln(stats, freq, docLen), "tfln"));
|
||||||
|
subs.add(Explanation.match(idf(stats, freq, docLen), "idf"));
|
||||||
|
subs.add(Explanation.match(gamma(stats, freq, docLen), "gamma"));
|
||||||
|
super.explain(subs, stats, doc, freq, docLen);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Name of the axiomatic method.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public abstract String toString();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the term frequency component
|
||||||
|
*/
|
||||||
|
protected abstract float tf(BasicStats stats, float freq, float docLen);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the document length component
|
||||||
|
*/
|
||||||
|
protected abstract float ln(BasicStats stats, float freq, float docLen);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the mixed term frequency and document length component
|
||||||
|
*/
|
||||||
|
protected abstract float tfln(BasicStats stats, float freq, float docLen);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the inverted document frequency component
|
||||||
|
*/
|
||||||
|
protected abstract float idf(BasicStats stats, float freq, float docLen);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the gamma component (only for F3EXp and F3LOG)
|
||||||
|
*/
|
||||||
|
protected abstract float gamma(BasicStats stats, float freq, float docLen);
|
||||||
|
}
|
|
@ -0,0 +1,95 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
package org.apache.lucene.search.similarities;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* F1EXP is defined as Sum(tf(term_doc_freq)*ln(docLen)*IDF(term))
|
||||||
|
* where IDF(t) = pow((N+1)/df(t), k) N=total num of docs, df=doc freq
|
||||||
|
*
|
||||||
|
* @lucene.experimental
|
||||||
|
*/
|
||||||
|
public class AxiomaticF1EXP extends Axiomatic {
|
||||||
|
/**
|
||||||
|
* Constructor setting s and k, letting queryLen to default
|
||||||
|
* @param s hyperparam for the growth function
|
||||||
|
* @param k hyperparam for the primitive weighting function
|
||||||
|
*/
|
||||||
|
public AxiomaticF1EXP(float s, float k) {
|
||||||
|
super(s, 1, k);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor setting s only, letting k and queryLen to default
|
||||||
|
* @param s hyperparam for the growth function
|
||||||
|
*/
|
||||||
|
public AxiomaticF1EXP(float s) {
|
||||||
|
this(s, 0.35f);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default constructor
|
||||||
|
*/
|
||||||
|
public AxiomaticF1EXP() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "F1EXP";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the term frequency component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float tf(BasicStats stats, float freq, float docLen) {
|
||||||
|
if (freq <= 0.0) return 0f;
|
||||||
|
return (float) (1 + Math.log(1 + Math.log(freq)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the document length component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float ln(BasicStats stats, float freq, float docLen) {
|
||||||
|
return (stats.getAvgFieldLength() + this.s) / (stats.getAvgFieldLength() + docLen * this.s);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the mixed term frequency and document length component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float tfln(BasicStats stats, float freq, float docLen) {
|
||||||
|
return 1f;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the inverted document frequency component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float idf(BasicStats stats, float freq, float docLen) {
|
||||||
|
return (float) Math.pow((stats.getNumberOfDocuments() + 1.0) / stats.getDocFreq(), this.k);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the gamma component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float gamma(BasicStats stats, float freq, float docLen) {
|
||||||
|
return 0f;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,88 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
package org.apache.lucene.search.similarities;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* F1LOG is defined as Sum(tf(term_doc_freq)*ln(docLen)*IDF(term))
|
||||||
|
* where IDF(t) = ln((N+1)/df(t)) N=total num of docs, df=doc freq
|
||||||
|
*
|
||||||
|
* @lucene.experimental
|
||||||
|
*/
|
||||||
|
public class AxiomaticF1LOG extends Axiomatic {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor setting s only, letting k and queryLen to default
|
||||||
|
*
|
||||||
|
* @param s hyperparam for the growth function
|
||||||
|
*/
|
||||||
|
public AxiomaticF1LOG(float s) {
|
||||||
|
super(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default constructor
|
||||||
|
*/
|
||||||
|
public AxiomaticF1LOG() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "F1LOG";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the term frequency component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float tf(BasicStats stats, float freq, float docLen) {
|
||||||
|
if (freq <= 0.0) return 0f;
|
||||||
|
return (float) (1 + Math.log(1 + Math.log(freq)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the document length component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float ln(BasicStats stats, float freq, float docLen) {
|
||||||
|
return (stats.getAvgFieldLength() + this.s) / (stats.getAvgFieldLength() + docLen * this.s);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the mixed term frequency and document length component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float tfln(BasicStats stats, float freq, float docLen) {
|
||||||
|
return 1f;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the inverted document frequency component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float idf(BasicStats stats, float freq, float docLen) {
|
||||||
|
return (float) Math.log((stats.getNumberOfDocuments() + 1.0) / stats.getDocFreq());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the gamma component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float gamma(BasicStats stats, float freq, float docLen) {
|
||||||
|
return 0f;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,94 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
package org.apache.lucene.search.similarities;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* F2EXP is defined as Sum(tfln(term_doc_freq, docLen)*IDF(term))
|
||||||
|
* where IDF(t) = pow((N+1)/df(t), k) N=total num of docs, df=doc freq
|
||||||
|
*
|
||||||
|
* @lucene.experimental
|
||||||
|
*/
|
||||||
|
public class AxiomaticF2EXP extends Axiomatic {
|
||||||
|
/**
|
||||||
|
* Constructor setting s and k, letting queryLen to default
|
||||||
|
* @param s hyperparam for the growth function
|
||||||
|
* @param k hyperparam for the primitive weighting function
|
||||||
|
*/
|
||||||
|
public AxiomaticF2EXP(float s, float k) {
|
||||||
|
super(s, 1, k);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor setting s only, letting k and queryLen to default
|
||||||
|
* @param s hyperparam for the growth function
|
||||||
|
*/
|
||||||
|
public AxiomaticF2EXP(float s) {
|
||||||
|
this(s, 0.35f);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default constructor
|
||||||
|
*/
|
||||||
|
public AxiomaticF2EXP() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "F2EXP";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the term frequency component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float tf(BasicStats stats, float freq, float docLen) {
|
||||||
|
return 1f;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the document length component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float ln(BasicStats stats, float freq, float docLen) {
|
||||||
|
return 1f;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the mixed term frequency and document length component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float tfln(BasicStats stats, float freq, float docLen) {
|
||||||
|
return freq / (freq + this.s + this.s * docLen / stats.getAvgFieldLength());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the inverted document frequency component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float idf(BasicStats stats, float freq, float docLen) {
|
||||||
|
return (float) Math.pow((stats.getNumberOfDocuments() + 1.0) / stats.getDocFreq(), this.k);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the gamma component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float gamma(BasicStats stats, float freq, float docLen) {
|
||||||
|
return 0f;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,86 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
package org.apache.lucene.search.similarities;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* F2EXP is defined as Sum(tfln(term_doc_freq, docLen)*IDF(term))
|
||||||
|
* where IDF(t) = ln((N+1)/df(t)) N=total num of docs, df=doc freq
|
||||||
|
*
|
||||||
|
* @lucene.experimental
|
||||||
|
*/
|
||||||
|
public class AxiomaticF2LOG extends Axiomatic {
|
||||||
|
/**
|
||||||
|
* Constructor setting s only, letting k and queryLen to default
|
||||||
|
*
|
||||||
|
* @param s hyperparam for the growth function
|
||||||
|
*/
|
||||||
|
public AxiomaticF2LOG(float s) {
|
||||||
|
super(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default constructor
|
||||||
|
*/
|
||||||
|
public AxiomaticF2LOG() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "F2LOG";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the term frequency component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float tf(BasicStats stats, float freq, float docLen) {
|
||||||
|
return 1f;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the document length component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float ln(BasicStats stats, float freq, float docLen) {
|
||||||
|
return 1f;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the mixed term frequency and document length component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float tfln(BasicStats stats, float freq, float docLen) {
|
||||||
|
return freq / (freq + this.s + this.s * docLen / stats.getAvgFieldLength());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the inverted document frequency component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float idf(BasicStats stats, float freq, float docLen) {
|
||||||
|
return (float) Math.log((stats.getNumberOfDocuments() + 1.0) / stats.getDocFreq());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the gamma component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float gamma(BasicStats stats, float freq, float docLen) {
|
||||||
|
return 0f;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,94 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
package org.apache.lucene.search.similarities;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* F2EXP is defined as Sum(tf(term_doc_freq)*IDF(term)-gamma(docLen, queryLen))
|
||||||
|
* where IDF(t) = pow((N+1)/df(t), k) N=total num of docs, df=doc freq
|
||||||
|
* gamma(docLen, queryLen) = (docLen-queryLen)*queryLen*s/avdl
|
||||||
|
*
|
||||||
|
* @lucene.experimental
|
||||||
|
*/
|
||||||
|
public class AxiomaticF3EXP extends Axiomatic {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor setting all Axiomatic hyperparameters
|
||||||
|
*
|
||||||
|
* @param s hyperparam for the growth function
|
||||||
|
* @param queryLen the query length
|
||||||
|
* @param k hyperparam for the primitive weighting function
|
||||||
|
*/
|
||||||
|
public AxiomaticF3EXP(float s, int queryLen, float k) {
|
||||||
|
super(s, queryLen, k);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor setting s and queryLen, letting k to default
|
||||||
|
*
|
||||||
|
* @param s hyperparam for the growth function
|
||||||
|
* @param queryLen the query length
|
||||||
|
*/
|
||||||
|
public AxiomaticF3EXP(float s, int queryLen) {
|
||||||
|
this(s, queryLen, 0.35f);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "F3EXP";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the term frequency component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float tf(BasicStats stats, float freq, float docLen) {
|
||||||
|
if (freq <= 0.0) return 0f;
|
||||||
|
return (float) (1 + Math.log(1 + Math.log(freq)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the document length component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float ln(BasicStats stats, float freq, float docLen) {
|
||||||
|
return 1f;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the mixed term frequency and document length component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float tfln(BasicStats stats, float freq, float docLen) {
|
||||||
|
return 1f;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the inverted document frequency component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float idf(BasicStats stats, float freq, float docLen) {
|
||||||
|
return (float) Math.pow((stats.getNumberOfDocuments() + 1.0) / stats.getDocFreq(), this.k);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the gamma component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float gamma(BasicStats stats, float freq, float docLen) {
|
||||||
|
return (docLen - this.queryLen) * this.s * this.queryLen / stats.getAvgFieldLength();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,83 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
package org.apache.lucene.search.similarities;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* F2EXP is defined as Sum(tf(term_doc_freq)*IDF(term)-gamma(docLen, queryLen))
|
||||||
|
* where IDF(t) = ln((N+1)/df(t)) N=total num of docs, df=doc freq
|
||||||
|
* gamma(docLen, queryLen) = (docLen-queryLen)*queryLen*s/avdl
|
||||||
|
*
|
||||||
|
* @lucene.experimental
|
||||||
|
*/
|
||||||
|
public class AxiomaticF3LOG extends Axiomatic {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor setting s and queryLen, letting k to default
|
||||||
|
*
|
||||||
|
* @param s hyperparam for the growth function
|
||||||
|
* @param queryLen the query length
|
||||||
|
*/
|
||||||
|
public AxiomaticF3LOG(float s, int queryLen) {
|
||||||
|
super(s, queryLen);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "F3LOG";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the term frequency component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float tf(BasicStats stats, float freq, float docLen) {
|
||||||
|
if (freq <= 0.0) return 0f;
|
||||||
|
return (float) (1 + Math.log(1 + Math.log(freq)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the document length component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float ln(BasicStats stats, float freq, float docLen) {
|
||||||
|
return 1f;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the mixed term frequency and document length component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float tfln(BasicStats stats, float freq, float docLen) {
|
||||||
|
return 1f;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the inverted document frequency component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float idf(BasicStats stats, float freq, float docLen) {
|
||||||
|
return (float) Math.log((stats.getNumberOfDocuments() + 1.0) / stats.getDocFreq());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compute the gamma component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected float gamma(BasicStats stats, float freq, float docLen) {
|
||||||
|
return (docLen - this.queryLen) * this.s * this.queryLen / stats.getAvgFieldLength();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,86 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
package org.apache.lucene.search.similarities;
|
||||||
|
|
||||||
|
import org.apache.lucene.util.LuceneTestCase;
|
||||||
|
|
||||||
|
public class TestAxiomaticSimilarity extends LuceneTestCase {
|
||||||
|
|
||||||
|
public void testSaneNormValues() {
|
||||||
|
Axiomatic sim = new AxiomaticF2EXP();
|
||||||
|
for (int i = 0; i < 256; i++) {
|
||||||
|
float len = sim.decodeNormValue((byte) i);
|
||||||
|
assertFalse("negative len: " + len + ", byte=" + i, len < 0.0f);
|
||||||
|
assertFalse("inf len: " + len + ", byte=" + i, Float.isInfinite(len));
|
||||||
|
assertFalse("nan len for byte=" + i, Float.isNaN(len));
|
||||||
|
if (i > 0) {
|
||||||
|
assertTrue("len is not decreasing: " + len + ",byte=" + i, len < sim.decodeNormValue((byte) (i - 1)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testIllegalS() {
|
||||||
|
IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> {
|
||||||
|
new AxiomaticF2EXP(Float.POSITIVE_INFINITY, 0.1f);
|
||||||
|
});
|
||||||
|
assertTrue(expected.getMessage().contains("illegal s value"));
|
||||||
|
|
||||||
|
expected = expectThrows(IllegalArgumentException.class, () -> {
|
||||||
|
new AxiomaticF2EXP(-1, 0.1f);
|
||||||
|
});
|
||||||
|
assertTrue(expected.getMessage().contains("illegal s value"));
|
||||||
|
|
||||||
|
expected = expectThrows(IllegalArgumentException.class, () -> {
|
||||||
|
new AxiomaticF2EXP(Float.NaN, 0.1f);
|
||||||
|
});
|
||||||
|
assertTrue(expected.getMessage().contains("illegal s value"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testIllegalK() {
|
||||||
|
IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> {
|
||||||
|
new AxiomaticF2EXP(0.35f, 2f);
|
||||||
|
});
|
||||||
|
assertTrue(expected.getMessage().contains("illegal k value"));
|
||||||
|
|
||||||
|
expected = expectThrows(IllegalArgumentException.class, () -> {
|
||||||
|
new AxiomaticF2EXP(0.35f, -1f);
|
||||||
|
});
|
||||||
|
assertTrue(expected.getMessage().contains("illegal k value"));
|
||||||
|
|
||||||
|
expected = expectThrows(IllegalArgumentException.class, () -> {
|
||||||
|
new AxiomaticF2EXP(0.35f, Float.POSITIVE_INFINITY);
|
||||||
|
});
|
||||||
|
assertTrue(expected.getMessage().contains("illegal k value"));
|
||||||
|
|
||||||
|
expected = expectThrows(IllegalArgumentException.class, () -> {
|
||||||
|
new AxiomaticF2EXP(0.35f, Float.NaN);
|
||||||
|
});
|
||||||
|
assertTrue(expected.getMessage().contains("illegal k value"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testIllegalQL() {
|
||||||
|
IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> {
|
||||||
|
new AxiomaticF3EXP(0.35f, -1);
|
||||||
|
});
|
||||||
|
assertTrue(expected.getMessage().contains("illegal query length value"));
|
||||||
|
|
||||||
|
expected = expectThrows(IllegalArgumentException.class, () -> {
|
||||||
|
new AxiomaticF2EXP(0.35f, Integer.MAX_VALUE + 1);
|
||||||
|
});
|
||||||
|
assertTrue(expected.getMessage().contains("illegal k value"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue