2 ***********************************************************************
3 * Copyright (C) 2005-2006, International Business Machines *
4 * Corporation and others. All Rights Reserved. *
5 ***********************************************************************
9 package com.ibm.icu.dev.tool.charsetdet.sbcs;
11 import java.util.Collection;
12 import java.util.TreeMap;
17 * TODO To change the template for this generated type comment go to
18 * Window - Preferences - Java - Code Style - Code Templates
20 public class NGramList
22 public interface NGramKeyMapper
24 Object mapKey(String key);
27 public static final class NGram implements Comparable
32 public NGram(String theValue, int theRefCount)
35 refCount = theRefCount;
38 public NGram(String theValue)
43 public NGram(NGram other)
45 this(other.getValue(), other.getRefCount());
48 public final String getValue()
53 public final int getRefCount()
58 public final void incrementRefCount()
63 // Note: This makes higher refCounts come *before* lower refCounts...
64 public int compareTo(Object o)
68 return ng.getRefCount() - refCount;
72 protected TreeMap ngrams;
73 protected int totalNGrams;
74 protected int uniqueNGrams;
76 protected final int N_GRAM_SIZE = 3;
78 private NGramKeyMapper keyMapper;
83 public NGramList(NGramKeyMapper theMapper)
85 keyMapper = theMapper;
87 ngrams = new TreeMap();
88 totalNGrams = uniqueNGrams = 0;
91 public void setMapper(NGramKeyMapper nGramKeyMapper)
93 keyMapper = nGramKeyMapper;
96 public NGram get(Object mappedKey)
98 return (NGram) ngrams.get(mappedKey);
101 public NGram get(String key)
103 Object mappedKey = keyMapper.mapKey(key);
105 return get(mappedKey);
108 public void put(String key)
110 Object mappedKey = keyMapper.mapKey(key);
111 NGram ngram = get(mappedKey);
117 ngrams.put(mappedKey, new NGram(key));
119 ngram.incrementRefCount();
123 public Collection values()
125 return ngrams.values();
128 public Collection keys()
130 return ngrams.keySet();
133 public int getTotalNGrams()
138 public int getUniqueNGrams()