File: BagOfSourcedTokens.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 (88 lines) | stat: -rw-r--r-- 2,430 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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 BagOfSourcedTokens extends BasicStringWrapper implements SourcedStringWrapper
{
    private Map weightMap = new TreeMap();
    private Set unsourcedTokens = new TreeSet();
    private double totalWeight = 0;
    private SourcedToken[] tokens;
	
    BagOfSourcedTokens(String s,SourcedToken[] tokens) 
    {
        super(s);
        this.tokens = tokens;
        for (int i=0; i<tokens.length; i++) {
            weightMap.put(tokens[i], new Double(getWeight(tokens[i])+1) );
            unsourcedTokens.add(tokens[i].getValue());
        }
        totalWeight = tokens.length;
    }

    public String getSource() {
        return tokens[0].getSource();
    }

    /** Iterates over all tokens in the bag. */
    Iterator tokenIterator() {
        return weightMap.keySet().iterator();
    }
	
    /** Test if this token appears at least once. */
    SourcedToken getEquivalentToken(Token tok) {
        if (unsourcedTokens.contains(tok.getValue())) {
            for (int i=0; i<tokens.length; i++) {
                if (tokens[i].getValue().equals(tok.getValue())) {
                    return tokens[i];
                }
            }
            System.out.println("This is a problem");
            return null;
        } else {
            return 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;
    }

    /** Return array of tokens */
    SourcedToken[] getSourcedTokens() {
        return tokens;
    }
}