use of org.trie4j.tail.TailCharIterator in project trie4j by takawitter.
the class InlinedTailLOUDSPPTrie method predictiveSearch.
@Override
public Iterable<String> predictiveSearch(String query) {
List<String> ret = new ArrayList<String>();
char[] chars = query.toCharArray();
int charsLen = chars.length;
// root
int nodeId = 0;
TailCharIterator tci = tailArray.newIterator();
String pfx = null;
int charsIndexBack = 0;
for (int charsIndex = 0; charsIndex < charsLen; charsIndex++) {
charsIndexBack = charsIndex;
int child = getChildNode(nodeId, chars[charsIndex]);
if (child == -1)
return ret;
tci.setOffset(tailArray.getIteratorOffset(child));
while (tci.hasNext()) {
charsIndex++;
if (charsIndex >= charsLen)
break;
if (chars[charsIndex] != tci.next())
return ret;
}
nodeId = child;
}
pfx = new String(chars, 0, charsIndexBack);
Deque<Pair<Integer, String>> queue = new LinkedList<Pair<Integer, String>>();
queue.offerLast(Pair.create(nodeId, pfx));
while (queue.size() > 0) {
Pair<Integer, String> element = queue.pollFirst();
int nid = element.getFirst();
StringBuilder b = new StringBuilder(element.getSecond());
b.append(labels[nid]);
tci.setOffset(tailArray.getIteratorOffset(nid));
while (tci.hasNext()) b.append(tci.next());
String letter = b.toString();
if (term.get(nid))
ret.add(letter);
if (r0.isZero(nid)) {
int s = r1.select0(r0.rank0(nid)) + 1;
int e = r1.next0(s) + 1;
for (int i = (e - 1); i >= s; i--) {
queue.offerFirst(Pair.create(i, letter));
}
}
}
return ret;
}
use of org.trie4j.tail.TailCharIterator in project trie4j by takawitter.
the class InlinedTailLOUDSPPTrie method commonPrefixSearch.
@Override
public Iterable<String> commonPrefixSearch(String query) {
List<String> ret = new ArrayList<String>();
char[] chars = query.toCharArray();
int charsLen = chars.length;
// root
int nodeId = 0;
TailCharIterator tci = tailArray.newIterator();
for (int charsIndex = 0; charsIndex < charsLen; charsIndex++) {
int child = getChildNode(nodeId, chars[charsIndex]);
if (child == -1)
return ret;
tci.setOffset(tailArray.getIteratorOffset(child));
while (tci.hasNext()) {
charsIndex++;
if (charsLen <= charsIndex)
return ret;
if (chars[charsIndex] != tci.next())
return ret;
}
if (term.get(child)) {
ret.add(new String(chars, 0, charsIndex + 1));
}
nodeId = child;
}
return ret;
}
use of org.trie4j.tail.TailCharIterator in project trie4j by takawitter.
the class InlinedTailLOUDSTrie method contains.
@Override
public boolean contains(String text) {
// root
int nodeId = 0;
TailCharIterator it = new TailCharIterator(tails, -1);
int n = text.length();
for (int i = 0; i < n; i++) {
nodeId = getChildNode(nodeId, text.charAt(i));
if (nodeId == -1)
return false;
it.setIndex(tail[nodeId]);
while (it.hasNext()) {
i++;
if (i == n)
return false;
if (text.charAt(i) != it.next())
return false;
}
}
return term.get(nodeId);
}
use of org.trie4j.tail.TailCharIterator in project trie4j by takawitter.
the class InlinedTailLOUDSTrie method commonPrefixSearch.
@Override
public Iterable<String> commonPrefixSearch(String query) {
List<String> ret = new ArrayList<String>();
char[] chars = query.toCharArray();
int charsLen = chars.length;
// root
int nodeId = 0;
TailCharIterator tci = new TailCharIterator(tails, -1);
for (int charsIndex = 0; charsIndex < charsLen; charsIndex++) {
int child = getChildNode(nodeId, chars[charsIndex]);
if (child == -1)
return ret;
tci.setIndex(tail[child]);
while (tci.hasNext()) {
charsIndex++;
if (charsLen <= charsIndex)
return ret;
if (chars[charsIndex] != tci.next())
return ret;
}
if (term.get(child)) {
ret.add(new String(chars, 0, charsIndex + 1));
}
nodeId = child;
}
return ret;
}
use of org.trie4j.tail.TailCharIterator in project trie4j by takawitter.
the class TailLOUDSTrie method commonPrefixSearchWithTermId.
@Override
public Iterable<Pair<String, Integer>> commonPrefixSearchWithTermId(String query) {
List<Pair<String, Integer>> ret = new ArrayList<Pair<String, Integer>>();
char[] chars = query.toCharArray();
int charsLen = chars.length;
// root
int nodeId = 0;
TailCharIterator tci = tailArray.newIterator();
Range r = new Range();
for (int charsIndex = 0; charsIndex < charsLen; charsIndex++) {
int child = getChildNode(nodeId, chars[charsIndex], r);
if (child == -1)
return ret;
tci.setOffset(tailArray.getIteratorOffset(child));
while (tci.hasNext()) {
charsIndex++;
if (charsLen <= charsIndex)
return ret;
if (chars[charsIndex] != tci.next())
return ret;
}
if (term.get(child)) {
ret.add(Pair.create(new String(chars, 0, charsIndex + 1), term.rank1(child) - 1));
}
nodeId = child;
}
return ret;
}
Aggregations