@Override public boolean load(DataInput input) throws IOException { count = input.readVLong(); TSTNode root = trie.new TSTNode('\0', null); readRecursively(input, root); trie.setRoot(root); return true; }
@Override public boolean store(DataOutput output) throws IOException { output.writeVLong(count); TSTNode root = trie.getRoot(); if (root == null) { // empty tree return false; } writeRecursively(output, root); return true; }
@Override public List<LookupResult> lookup( CharSequence key, Set<BytesRef> contexts, boolean onlyMorePopular, int num) { if (contexts != null) { throw new IllegalArgumentException("this suggester doesn't support contexts"); } List<LookupResult> res = new ArrayList<>(); List<String> list; int count = onlyMorePopular ? num * 2 : num; if (usePrefix) { list = trie.matchPrefix(key, count); } else { list = trie.matchAlmost(key, count); } if (list == null || list.size() == 0) { return res; } int maxCnt = Math.min(num, list.size()); if (onlyMorePopular) { LookupPriorityQueue queue = new LookupPriorityQueue(num); for (String s : list) { long freq = ((Number) trie.get(s)).longValue(); queue.insertWithOverflow(new LookupResult(new CharsRef(s), freq)); } for (LookupResult lr : queue.getResults()) { res.add(lr); } } else { for (int i = 0; i < maxCnt; i++) { String s = list.get(i); long freq = ((Number) trie.get(s)).longValue(); res.add(new LookupResult(new CharsRef(s), freq)); } } return res; }
@Override public void build(InputIterator iterator) throws IOException { if (iterator.hasPayloads()) { throw new IllegalArgumentException("this suggester doesn't support payloads"); } if (iterator.hasContexts()) { throw new IllegalArgumentException("this suggester doesn't support contexts"); } count = 0; trie = new JaspellTernarySearchTrie(); trie.setMatchAlmostDiff(editDistance); BytesRef spare; final CharsRefBuilder charsSpare = new CharsRefBuilder(); while ((spare = iterator.next()) != null) { final long weight = iterator.weight(); if (spare.length == 0) { continue; } charsSpare.copyUTF8Bytes(spare); trie.put(charsSpare.toString(), Long.valueOf(weight)); count++; } }
/** Returns the value for the specified key, or null if the key does not exist. */ public Object get(CharSequence key) { return trie.get(key); }
/** * Adds a new node if <code>key</code> already exists, otherwise replaces its value. * * <p>This method always returns false. */ public boolean add(CharSequence key, Object value) { trie.put(key, value); // XXX return false; }
@Override public long ramBytesUsed() { return trie.ramBytesUsed(); }