File: MultiStringAvgDistance.java

package info (click to toggle)
libsecondstring-java 0.1~dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 764 kB
  • sloc: java: 9,592; xml: 114; makefile: 6
file content (50 lines) | stat: -rw-r--r-- 1,414 bytes parent folder | download
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
package com.wcohen.ss;

import com.wcohen.ss.api.*;
import java.util.*;

/**
 * StringDistance defined over Strings that are broken into fields,
 * with distance defined as the average distance between any field.
 */

public class MultiStringAvgDistance extends MultiStringDistance
{
	private StringDistance innerDistance;

	public MultiStringAvgDistance(StringDistance distance, String delim) { 
		super(delim);
		this.innerDistance = distance; 
	}

	/** Combine the scores for each primitive distance function on each field. */
	protected double scoreCombination(double[] multiScore) {
		double sum = 0.0;
		for (int i=0; i<multiScore.length; i++) {
			sum += multiScore[i];
		}
		return sum/multiScore.length;
	}

	/** Explain how to combine the scores for each primitive distance
	 * function on each field. */
	protected String explainScoreCombination(double[] multiScore) {
		StringBuffer buf = new StringBuffer("");
		PrintfFormat fmt = new PrintfFormat(" %.3f");
		buf.append("field-level scores [");
		for (int i=0; i<multiScore.length; i++) {
			buf.append(fmt.sprintf(multiScore[i]));
		}
		buf.append("] Average score:");
		buf.append(fmt.sprintf( scoreCombination(multiScore) ));
		return buf.toString();
	}

	protected StringDistance getDistance(int i) {
		return innerDistance;
	}

	static public void main(String[] argv) {
		doMain(new MultiStringAvgDistance( new JaroWinkler(), ":" ), argv);
	}
}