Sha256: ed2acb49aeadd3f903e54abb1002bd2000cbff2a3c2f0276d582bd43b9832a36

Contents?: true

Size: 1.29 KB

Versions: 7

Compression:

Stored size: 1.29 KB

Contents

package bae;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class FrequencyTable {

    private Map<String, Map<String, Long>> frequencyTable;

    public FrequencyTable() {
        this.frequencyTable = new HashMap<>();
    }

    public void insertOrIgnore(String label) {
        // Add new hash to frequency table if it's not already there
        if(!this.frequencyTable.containsKey(label)) {
            this.frequencyTable.put(label, new HashMap<String, Long>());
        }
    }

    public void increaseFrequencyBy(String label, String word, long frequency) {
        // Add label if it doesn't exist
        insertOrIgnore(label);

        Map<String, Long> frequencyRow = this.frequencyTable.get(label);

        // Make sure we have a frequency for that position in the table
        if(!frequencyRow.containsKey(word)) {
            frequencyRow.put(word, 0L);
        }

        // Update frequency
        frequencyRow.put(word, frequencyRow.get(word) + frequency);
    }

    public Set<String> getLabels() {
        return this.frequencyTable.keySet();
    }

    public long get(String label, String word) {
        try {
            return this.frequencyTable.get(label).get(word);
        } catch (NullPointerException e) {
            return 0L;
        }
    }
}

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
bae-0.2.0 src/main/java/bae/FrequencyTable.java
bae-0.1.0 src/main/java/bae/FrequencyTable.java
bae-0.0.9 src/main/java/bae/FrequencyTable.java
bae-0.0.8-java src/main/java/bae/FrequencyTable.java
bae-0.0.7-java src/main/java/bae/FrequencyTable.java
bae-0.0.6-java src/main/java/bae/FrequencyTable.java
bae-0.0.5-java src/main/java/bae/FrequencyTable.java