File: AveragedStringDistanceLearner.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 (64 lines) | stat: -rwxr-xr-x 1,950 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.wcohen.ss;

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

/**
 * Abstract StringDistanceLearner class which averages results of a number of
 * inner distance metrics, learned by a number of inner distance learners.
 */

public class AveragedStringDistanceLearner extends CombinedStringDistanceLearner
{
	MultiStringWrapper prototype = null;
	
	public AveragedStringDistanceLearner() { super(); }

	public AveragedStringDistanceLearner(StringDistanceLearner[] innerLearners, String delim) {	super(innerLearners,delim); }

	protected void comboSetStringWrapperPool(Iterator it) {
		if (it.hasNext()) prototype = asMultiStringWrapper( (StringWrapper) it.next() );
	}
	protected boolean comboHasNextQuery() { return false; }
	protected DistanceInstance comboNextQuery() { return null; }
	protected void comboAddExample(DistanceInstance di) { ; }
	protected void comboSetDistanceInstancePool(Iterator i) {
		// use the first example to build a prototype
	}

	public StringDistance getDistance() {
		if (prototype==null) throw new IllegalStateException("need to be trained first");
		return new AveragedStringDistance(getInnerDistances(), prototype);
	}

	//
	// average of some string distances
	//

	private class AveragedStringDistance extends CombinedStringDistance
	{
		public AveragedStringDistance(StringDistance[] innerDistances, MultiStringWrapper prototype) 
		{
			super(innerDistances,prototype);
		}
		protected double doScore(MultiStringWrapper ms,MultiStringWrapper mt)
		{
			double totScore = 0.0;
			for (int i=0; i<ms.size(); i++) {
				StringDistance d = innerDistances[ ms.getDistanceLearnerIndex(i) ];
				totScore += d.score( ms.get(i), mt.get(i) );
			}
			return totScore/ms.size();
		}
		protected String explainCombination(MultiStringWrapper ms,MultiStringWrapper mt)
		{ 
			return "Final score is the average\n";
		}

		public String toString() 
		{
			return "[Average of:"+innerDistanceString()+"]";
		}
	}

}