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