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