use of org.trie4j.tail.TailCharIterator in project trie4j by takawitter.
the class InlinedTailLOUDSPPTrie method contains.
@Override
public boolean contains(String text) {
// root
int nodeId = 0;
TailCharIterator it = tailArray.newIterator();
int n = text.length();
for (int i = 0; i < n; i++) {
nodeId = getChildNode(nodeId, text.charAt(i));
if (nodeId == -1)
return false;
it.setOffset(tailArray.getIteratorOffset(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 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 = new TailCharIterator(tails, -1);
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.setIndex(tail[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.setIndex(tail[nid]);
while (tci.hasNext()) b.append(tci.next());
String letter = b.toString();
if (term.get(nid))
ret.add(letter);
int s = bv.select0(nid) + 1;
int e = bv.next0(s);
int lastNodeId = bv.rank1(s) + e - s - 1;
for (int i = (e - 1); i >= s; i--) {
queue.offerFirst(Pair.create(lastNodeId--, letter));
}
}
return ret;
}
use of org.trie4j.tail.TailCharIterator in project trie4j by takawitter.
the class TailLOUDSTrie method getNodeId.
public int getNodeId(String text) {
// root
int nodeId = 0;
Range r = new Range();
TailCharIterator it = tailArray.newIterator();
int n = text.length();
for (int i = 0; i < n; i++) {
nodeId = getChildNode(nodeId, text.charAt(i), r);
if (nodeId == -1)
return -1;
it.setOffset(tailArray.getIteratorOffset(nodeId));
while (it.hasNext()) {
i++;
if (i == n)
return -1;
if (text.charAt(i) != it.next())
return -1;
}
}
return nodeId;
}
use of org.trie4j.tail.TailCharIterator in project trie4j by takawitter.
the class TailPatriciaTrie method insert.
protected TailPatriciaTrieNode insert(TailPatriciaTrieNode node, String letters, int offset) {
TailCharIterator it = new TailCharIterator(tails, node.getTailIndex());
int count = 0;
boolean matchComplete = true;
int lettersLength = letters.length();
while (it.hasNext() && offset < lettersLength) {
if (letters.charAt(offset) != it.next()) {
matchComplete = false;
break;
}
offset++;
count++;
}
if (offset == lettersLength) {
if (it.hasNext()) {
// n: abcde
// l: abc
char c = it.next();
int idx = it.getNextIndex();
if (!it.hasNext()) {
idx = -1;
}
TailPatriciaTrieNode newChild = newNode(c, idx, node);
node.setTailIndex((count > 0) ? tailBuilder.insert(letters, offset - count, count) : -1);
node.setChildren(newNodeArray(newChild));
node.setTerminate(true);
size++;
nodeSize++;
return node;
} else {
// l: abc
if (!node.isTerminate()) {
node.setTerminate(true);
size++;
}
return node;
}
} else {
if (!matchComplete) {
// n: abcwz
// l: abcde
int firstOffset = offset - count;
char n1Fc = it.current();
int n1Idx = it.getNextIndex();
if (!it.hasNext()) {
n1Idx = -1;
}
TailPatriciaTrieNode n1 = newNode(n1Fc, n1Idx, node);
char n2Fc = letters.charAt(offset++);
int n2Idx = (offset < lettersLength) ? tailBuilder.insert(letters, offset, lettersLength - offset) : -1;
TailPatriciaTrieNode n2 = newNode(n2Fc, n2Idx, true);
if (count > 0) {
node.setTailIndex(tailBuilder.insert(letters, firstOffset, count));
} else {
node.setTailIndex(-1);
}
node.setTerminate(false);
node.setChildren((n1.getFirstLetter() < n2.getFirstLetter()) ? newNodeArray(n1, n2) : newNodeArray(n2, n1));
size++;
nodeSize += 2;
return n2;
} else {
// n: abc
// l: abcde
char fc = letters.charAt(offset++);
// find node
Pair<TailPatriciaTrieNode, Integer> ret = node.findNode(fc);
TailPatriciaTrieNode child = ret.getFirst();
if (child != null) {
return insert(child, letters, offset);
} else {
int idx = (offset < lettersLength) ? tailBuilder.insert(letters, offset, lettersLength - offset) : -1;
TailPatriciaTrieNode newNode = newNode(fc, idx, true);
node.addChild(ret.getSecond(), newNode);
size++;
nodeSize++;
return newNode;
}
}
}
}
use of org.trie4j.tail.TailCharIterator in project trie4j by takawitter.
the class TailPatriciaTrie method findWord.
@Override
public int findWord(CharSequence chars, int start, int end, StringBuilder word) {
TailCharIterator it = new TailCharIterator(tails, -1);
for (int i = start; i < end; i++) {
TailPatriciaTrieNode node = root;
for (int j = i; j < end; j++) {
node = node.getChild(chars.charAt(j));
if (node == null)
break;
boolean matched = true;
it.setIndex(node.getTailIndex());
while (it.hasNext()) {
j++;
if (j == end || chars.charAt(j) != it.next()) {
matched = false;
break;
}
}
if (matched) {
if (node.isTerminate()) {
if (word != null)
word.append(chars, i, j + 1);
return i;
}
} else {
break;
}
}
}
return -1;
}
Aggregations