001: /*
002: ***********************************************************************
003: * Copyright (C) 2005-2006, International Business Machines *
004: * Corporation and others. All Rights Reserved. *
005: ***********************************************************************
006: *
007: */
008:
009: package com.ibm.icu.dev.tool.charsetdet.sbcs;
010:
011: import java.util.Collection;
012: import java.util.TreeMap;
013:
014: /**
015: * @author emader
016: *
017: * TODO To change the template for this generated type comment go to
018: * Window - Preferences - Java - Code Style - Code Templates
019: */
020: public class NGramList {
021: public interface NGramKeyMapper {
022: Object mapKey(String key);
023: }
024:
025: public static final class NGram implements Comparable {
026: private String value;
027: private int refCount;
028:
029: public NGram(String theValue, int theRefCount) {
030: value = theValue;
031: refCount = theRefCount;
032: }
033:
034: public NGram(String theValue) {
035: this (theValue, 1);
036: }
037:
038: public NGram(NGram other) {
039: this (other.getValue(), other.getRefCount());
040: }
041:
042: public final String getValue() {
043: return value;
044: }
045:
046: public final int getRefCount() {
047: return refCount;
048: }
049:
050: public final void incrementRefCount() {
051: refCount += 1;
052: }
053:
054: // Note: This makes higher refCounts come *before* lower refCounts...
055: public int compareTo(Object o) {
056: NGram ng = (NGram) o;
057:
058: return ng.getRefCount() - refCount;
059: }
060: }
061:
062: protected TreeMap ngrams;
063: protected int totalNGrams;
064: protected int uniqueNGrams;
065:
066: protected final int N_GRAM_SIZE = 3;
067:
068: private NGramKeyMapper keyMapper;
069:
070: /**
071: *
072: */
073: public NGramList(NGramKeyMapper theMapper) {
074: keyMapper = theMapper;
075:
076: ngrams = new TreeMap();
077: totalNGrams = uniqueNGrams = 0;
078: }
079:
080: public void setMapper(NGramKeyMapper nGramKeyMapper) {
081: keyMapper = nGramKeyMapper;
082: }
083:
084: public NGram get(Object mappedKey) {
085: return (NGram) ngrams.get(mappedKey);
086: }
087:
088: public NGram get(String key) {
089: Object mappedKey = keyMapper.mapKey(key);
090:
091: return get(mappedKey);
092: }
093:
094: public void put(String key) {
095: Object mappedKey = keyMapper.mapKey(key);
096: NGram ngram = get(mappedKey);
097:
098: totalNGrams += 1;
099:
100: if (ngram == null) {
101: uniqueNGrams += 1;
102: ngrams.put(mappedKey, new NGram(key));
103: } else {
104: ngram.incrementRefCount();
105: }
106: }
107:
108: public Collection values() {
109: return ngrams.values();
110: }
111:
112: public Collection keys() {
113: return ngrams.keySet();
114: }
115:
116: public int getTotalNGrams() {
117: return totalNGrams;
118: }
119:
120: public int getUniqueNGrams() {
121: return uniqueNGrams;
122: }
123: }
|