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