@Test public void testBasicFound() { ArrayList<String> strs = new ArrayList<String>(); strs.add("part"); strs.add("par"); strs.add("partition"); strs.add("party"); strs.add("parties"); strs.add("paint"); Collections.sort(strs); int baseId = 0; TrieDictionaryForestBuilder<String> builder = newDictBuilder(strs, baseId); TrieDictionaryForest<String> dict = builder.build(); dict.dump(System.out); int expectId = baseId; for (String s : strs) { assertEquals(expectId, dict.getIdFromValue(s)); expectId++; } assertSameBehaviorAsTrie(dict, strs, baseId); }
@Test //one string one tree public void testMultiTree() { ArrayList<String> strs = new ArrayList<String>(); strs.add("part"); strs.add("par"); strs.add("partition"); strs.add("party"); strs.add("parties"); strs.add("paint"); strs.add("一二三"); //Chinese test strs.add("四五六"); strs.add(""); Collections.sort(strs, new ByteComparator<String>(new StringBytesConverter())); int baseId = 5; int maxTreeSize = 0; TrieDictionaryForestBuilder<String> builder = newDictBuilder(strs, baseId, maxTreeSize); TrieDictionaryForest<String> dict = builder.build(); dict.dump(System.out); assertEquals(strs.size(), dict.getTrees().size()); int expectId = baseId; for (String s : strs) { assertEquals(expectId, dict.getIdFromValue(s)); expectId++; } assertSameBehaviorAsTrie(dict, strs, baseId); }
@Test public void testNullValue() { //encounter null value when building dictionary ArrayList<String> strs = new ArrayList<String>(); strs.add(null); strs.add("abc"); System.out.println(strs); int maxTreeSize = 0; TrieDictionaryForestBuilder<String> builder = newDictBuilder(strs, 0, maxTreeSize); TrieDictionaryForest<String> dict = builder.build(); dict.dump(System.out); //null value query int id = dict.getIdFromValue(null, 0); assertEquals(255, id); id = dict.getIdFromValue(null, 1); assertEquals(255, id); id = dict.getIdFromValue(null, -1); assertEquals(255, id); assertSameBehaviorAsTrie(dict, strs, 0); }
@Test public void serializeTest() { ArrayList<String> testData = getTestData(10); TrieDictionaryForestBuilder<String> b = newDictBuilder(testData, 10, 0); TrieDictionaryForest<String> dict = b.build(); dict = testSerialize(dict); dict.dump(System.out); for (String str : testData) { assertEquals(str, dict.getValueFromId(dict.getIdFromValue(str))); } }
System.out.println("tree size:" + dict.getTrees().size()); System.out.println("--------------dict-----------------"); dict.dump(System.out); System.out.println("--------------set-------------------"); System.out.println(set);