001: package it.unimi.dsi.mg4j.search.score;
002:
003: /*
004: * MG4J: Managing Gigabytes for Java
005: *
006: * Copyright (C) 2004-2007 Paolo Boldi
007: *
008: * This library is free software; you can redistribute it and/or modify it
009: * under the terms of the GNU Lesser General Public License as published by the Free
010: * Software Foundation; either version 2.1 of the License, or (at your option)
011: * any later version.
012: *
013: * This library is distributed in the hope that it will be useful, but
014: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
015: * or FITfNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
016: * for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: *
022: */
023:
024: import java.io.IOException;
025:
026: import it.unimi.dsi.mg4j.index.Index;
027: import it.unimi.dsi.util.Interval;
028: import it.unimi.dsi.mg4j.search.IntervalIterator;
029: import it.unimi.dsi.mg4j.search.IntervalIterators;
030:
031: /** Computes the Clarke–Cormack score of all interval iterators of a document.
032: * This score function is defined in Charles L.A. Clarke and Gordon V. Cormack, “Shortest-Substring
033: * Retrieval and Ranking”, <i>ACM Transactions on Information Systems</i>, 18(1):44−78, 2000,
034: * at page 65.
035: *
036: * <p>The score for each index depends on two parameters: an integer <var>h</var> and a double α.
037: * The score is obtained summing up a certain score assigned to all intervals in the interval iterator
038: * under examination. The score assigned to an interval is 1 if the interval
039: * has length smaller than <var>h</var>; otherwise, it is obtained by dividing <var>h</var> by
040: * the interval length, and raising the result to the power of α.
041: *
042: * <p>Note that the score assigned to each interval is between 0 and 1 (highest scores corresponding
043: * to best intervals). The score assigned to an interval iterator is thus bounded from above by the
044: * number of intervals; an alternative version allows one to have normalized scores (in this case, the resulting
045: * value is an average instead of a sum). A scorer with similar relative ranks, but inherently (almost) normalised
046: * is provided by {@link it.unimi.dsi.mg4j.search.score.VignaScorer}.
047: *
048: * <p>Typically, one sets <var>h</var>=16 (or a bit larger) and α=1 (or a bit smaller),
049: * but the authors say that the method is rather stable w.r.t. changes in the values of parameters.
050: *
051: */
052: public class ClarkeCormackScorer extends AbstractWeightedScorer
053: implements DelegatingScorer {
054: /** The default value for <var>h</var>. */
055: public static final int DEFAULT_H = 16;
056: /** The parameter h. */
057: public final int h;
058: /** The parameter alpha. */
059: public final double alpha;
060: /** Whether the result should be normalized (i.e., between 0 and 1). */
061: public final boolean normalize;
062:
063: /** Creates a Clarke–Cormack scorer.
064: *
065: * @param h the parameter <var>h</var>.
066: * @param alpha the parameter α.
067: * @param normalize whether the result should be normalized.
068: */
069: public ClarkeCormackScorer(final int h, final double alpha,
070: final boolean normalize) {
071: this .h = h;
072: this .alpha = alpha;
073: this .normalize = normalize;
074: }
075:
076: /** Creates a Clarke–Cormack scorer.
077: *
078: * @param h the parameter <var>h</var>.
079: * @param alpha the parameter α.
080: * @param normalize whether the result should be normalized.
081: */
082: public ClarkeCormackScorer(final String h, final String alpha,
083: final String normalize) {
084: this (Integer.parseInt(h), Double.parseDouble(alpha), Boolean
085: .parseBoolean(normalize));
086: }
087:
088: /** Default constructor, assigning the default values (<var>h</var>={@link #DEFAULT_H}, α=1) to the
089: * parameters; the resulting scorer is normalized.
090: */
091: public ClarkeCormackScorer() {
092: this (DEFAULT_H, 1, true);
093: }
094:
095: public synchronized ClarkeCormackScorer copy() {
096: final ClarkeCormackScorer scorer = new ClarkeCormackScorer(h,
097: alpha, normalize);
098: scorer.setWeights(index2Weight);
099: return scorer;
100: }
101:
102: public double score(final Index index) throws IOException {
103: final IntervalIterator it = documentIterator
104: .intervalIterator(index);
105: if (it == IntervalIterators.TRUE
106: || it == IntervalIterators.FALSE)
107: return 0;
108: double result = 0;
109: int lt, count = 0;
110: Interval interval;
111: while ((interval = it.nextInterval()) != null) {
112: count++;
113: lt = interval.length();
114: if (lt < h)
115: result += 1;
116: else
117: result += Math.pow(h / (double) lt, alpha);
118: }
119: return normalize ? result / count : result;
120: }
121:
122: public String toString() {
123: return "Clarke-Cormack(" + h + ", " + alpha + ", " + normalize
124: + ')';
125: }
126:
127: /** Returns true.
128: * @return true.
129: */
130: public boolean usesIntervals() {
131: return true;
132: }
133: }
|