]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/tools/misc/src/com/ibm/icu/dev/tool/charsetdet/sbcs/NGramList.java
Clean up imports.
[Dictionary.git] / jars / icu4j-52_1 / tools / misc / src / com / ibm / icu / dev / tool / charsetdet / sbcs / NGramList.java
1 /*
2  ***********************************************************************
3  * Copyright (C) 2005-2006, International Business Machines            *
4  * Corporation and others. All Rights Reserved.                        *
5  ***********************************************************************
6  *
7  */
8
9 package com.ibm.icu.dev.tool.charsetdet.sbcs;
10
11 import java.util.Collection;
12 import java.util.TreeMap;
13
14 /**
15  * @author emader
16  *
17  * TODO To change the template for this generated type comment go to
18  * Window - Preferences - Java - Code Style - Code Templates
19  */
20 public class NGramList
21 {
22     public interface NGramKeyMapper
23     {
24         Object mapKey(String key);
25     }
26     
27     public static final class NGram implements Comparable
28     {
29         private String value;
30         private int refCount;
31         
32         public NGram(String theValue, int theRefCount)
33         {
34             value    = theValue;
35             refCount = theRefCount;
36         }
37         
38         public NGram(String theValue)
39         {
40             this(theValue, 1);
41         }
42         
43         public NGram(NGram other)
44         {
45             this(other.getValue(), other.getRefCount());
46         }
47         
48         public final String getValue()
49         {
50             return value;
51         }
52         
53         public final int getRefCount()
54         {
55             return refCount;
56         }
57         
58         public final void incrementRefCount()
59         {
60             refCount += 1;
61         }
62         
63         // Note: This makes higher refCounts come *before* lower refCounts...
64         public int compareTo(Object o)
65         {
66             NGram ng = (NGram) o;
67             
68             return ng.getRefCount() - refCount;
69         }
70     }
71     
72     protected TreeMap ngrams;
73     protected int totalNGrams;
74     protected int uniqueNGrams;
75
76     protected final int N_GRAM_SIZE = 3;
77     
78     private NGramKeyMapper keyMapper;
79
80     /**
81      * 
82      */
83     public NGramList(NGramKeyMapper theMapper)
84     {
85         keyMapper = theMapper;
86         
87         ngrams = new TreeMap();
88         totalNGrams = uniqueNGrams = 0;
89     }
90     
91     public void setMapper(NGramKeyMapper nGramKeyMapper)
92     {
93         keyMapper = nGramKeyMapper;
94     }
95     
96     public NGram get(Object mappedKey)
97     {
98         return (NGram) ngrams.get(mappedKey);
99     }
100     
101     public NGram get(String key)
102     {
103         Object mappedKey = keyMapper.mapKey(key);
104         
105         return get(mappedKey);
106     }
107     
108     public void put(String key)
109     {
110         Object mappedKey = keyMapper.mapKey(key);
111         NGram ngram = get(mappedKey);
112         
113         totalNGrams += 1;
114         
115         if (ngram == null) {
116             uniqueNGrams += 1;
117             ngrams.put(mappedKey, new NGram(key));
118         } else {
119             ngram.incrementRefCount();
120         }
121     }
122     
123     public Collection values()
124     {
125         return ngrams.values();
126     }
127     
128     public Collection keys()
129     {
130         return ngrams.keySet();
131     }
132     
133     public int getTotalNGrams()
134     {
135         return totalNGrams;
136     }
137     
138     public int getUniqueNGrams()
139     {
140         return uniqueNGrams;
141     }
142 }