Encoder { public Encoder(List<Set<String>> symbols) throws SequenceException { alphabets = new ArrayList<Set<String>>(symbols.size()); int encodingTableSize = 1; for (int i = 0; i < symbols.size(); i++) { Set<String> symbolSet = symbols.get(i); Set<String> alphabet = new HashSet<String>(symbolSet.size() + 1); alphabet.add(UNK); for (String token : symbolSet) { if (!token.equals(UNK)) { alphabet.add(token); } else { String msg = String.format( "Cannot create encoding table: symbol set %s contains " + "the \"unknown\" symbol %s", i, UNK); throw new SequenceException(msg); } } alphabets.add(alphabet); encodingTableSize *= alphabet.size(); } if (encodingTableSize > MAX_SIZE) { throw new SequenceException("Maximum size exceeded"); } encodingTable = new HashMap<StringArrayWrapper, Integer>( encodingTableSize); int i = 0; for (List<String> tupleAr : Sets.cartesianProduct(alphabets)) { StringArrayWrapper tuple = new StringArrayWrapper( tupleAr.toArray(new String[0])); encodingTable.put(tuple, i); i++; } } Encoder(List<Set<String>> symbols); int size(); int tableSize(); char encode(String[] tuple); char[] encodeClass(int index, String value); static final int MAX_SIZE; static final String UNK; }
@Test public void testEncoder() throws SequenceException { Encoder encoder = new Encoder(sets); assertEquals(3, encoder.size()); assertEquals(36, encoder.tableSize()); }