@Override public List<LookupResult> lookup(CharSequence key, Set<BytesRef> contexts, boolean higherWeightsFirst, int num) { if (contexts != null) { throw new IllegalArgumentException("this suggester doesn't support contexts"); } final List<Completion> completions; if (higherWeightsFirst) { completions = higherWeightsCompletion.lookup(key, num); } else { completions = normalCompletion.lookup(key, num); } final ArrayList<LookupResult> results = new ArrayList<>(completions.size()); CharsRefBuilder spare = new CharsRefBuilder(); for (Completion c : completions) { spare.copyUTF8Bytes(c.utf8); results.add(new LookupResult(spare.toString(), c.bucket)); } return results; }
private void assertMatchEquals(List<Completion> res, String... expected) { String [] result = new String [res.size()]; for (int i = 0; i < res.size(); i++) { result[i] = res.get(i).toString(); } if (!Arrays.equals(stripScore(expected), stripScore(result))) { int colLen = Math.max(maxLen(expected), maxLen(result)); StringBuilder b = new StringBuilder(); String format = "%" + colLen + "s " + "%" + colLen + "s\n"; b.append(String.format(Locale.ROOT, format, "Expected", "Result")); for (int i = 0; i < Math.max(result.length, expected.length); i++) { b.append(String.format(Locale.ROOT, format, i < expected.length ? expected[i] : "--", i < result.length ? result[i] : "--")); } System.err.println(b.toString()); fail("Expected different output:\n" + b.toString()); } }
@Override public List<LookupResult> lookup(CharSequence key, boolean higherWeightsFirst, int num) { final List<Completion> completions; if (higherWeightsFirst) { completions = higherWeightsCompletion.lookup(key, num); } else { completions = normalCompletion.lookup(key, num); } final ArrayList<LookupResult> results = new ArrayList<LookupResult>(completions.size()); CharsRef spare = new CharsRef(); for (Completion c : completions) { spare.grow(c.utf8.length); UnicodeUtil.UTF8toUTF16(c.utf8, spare); results.add(new LookupResult(spare.toString(), c.bucket)); } return results; }
public void testThreeByte() throws Exception { String key = new String(new byte[] { (byte) 0xF0, (byte) 0xA4, (byte) 0xAD, (byte) 0xA2}, StandardCharsets.UTF_8); FSTCompletionBuilder builder = new FSTCompletionBuilder(); builder.add(new BytesRef(key), 0); FSTCompletion lookup = builder.build(); List<Completion> result = lookup.lookup(stringToCharSequence(key), 1); assertEquals(1, result.size()); }
public void testThreeByte() throws Exception { String key = new String(new byte[] { (byte) 0xF0, (byte) 0xA4, (byte) 0xAD, (byte) 0xA2}, "UTF-8"); FSTCompletionBuilder builder = new FSTCompletionBuilder(); builder.add(new BytesRef(key), 0); FSTCompletion lookup = builder.build(); List<Completion> result = lookup.lookup(stringToCharSequence(key), 1); assertEquals(1, result.size()); }