File: BagOfTokens.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 (67 lines) | stat: -rw-r--r-- 1,564 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
65
66
67
package com.wcohen.ss;

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

/**
 * A string, with an associated bag of tokens.  Each token has an
 * associated weight.
 * 
 */

class BagOfTokens extends BasicStringWrapper
{
	private Map weightMap = new TreeMap();
	private double totalWeight = 0;
	private Token[] tokens;
	
	BagOfTokens(String s,Token[] tokens) 
	{
		super(s);
		this.tokens = tokens;
		for (int i=0; i<tokens.length; i++) {
			weightMap.put(tokens[i], new Double(getWeight(tokens[i])+1) );
		}
		totalWeight = tokens.length;
	}

	/** Iterates over all tokens in the bag. */
    Iterator tokenIterator() {
		return weightMap.keySet().iterator();
	}
	
	/** Test if this token appears at least once. */
    boolean contains(Token tok) {
		return weightMap.get(tok)!=null;
	}
	
	/** Weight associated with a token: by default, the number of times
	 * the token appears in the bag. */
    double getWeight(Token tok) {
		Double f = (Double)weightMap.get(tok);
		return f==null ? 0 : f.doubleValue();
	}
	
	/** Change the weight of a token in the bag */
	void setWeight(Token tok, double d) {
		Double oldWeight = (Double)weightMap.get(tok);
		totalWeight += oldWeight==null ? d : (d - oldWeight.doubleValue());
		weightMap.put(tok,new Double(d));
	}
	
	/** Number of distinct tokens in the bag. */
	int size() {
		return weightMap.keySet().size();
	}

	/** Total weight of all tokens in bag */
	double getTotalWeight() {
		return totalWeight;
	}

	/** Return array of tokens */
	Token[] getTokens() {
		return tokens;
	}
}