Search in sources :

Example 1 with TailCharIterator

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;
}
Also used : ArrayList(java.util.ArrayList) TailCharIterator(org.trie4j.tail.TailCharIterator) LinkedList(java.util.LinkedList) Pair(org.trie4j.util.Pair)

Example 2 with TailCharIterator

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;
}
Also used : ArrayList(java.util.ArrayList) TailCharIterator(org.trie4j.tail.TailCharIterator)

Example 3 with TailCharIterator

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);
}
Also used : TailCharIterator(org.trie4j.tail.TailCharIterator)

Example 4 with TailCharIterator

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;
}
Also used : ArrayList(java.util.ArrayList) TailCharIterator(org.trie4j.tail.TailCharIterator)

Example 5 with TailCharIterator

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;
}
Also used : ArrayList(java.util.ArrayList) Range(org.trie4j.util.Range) TailCharIterator(org.trie4j.tail.TailCharIterator) Pair(org.trie4j.util.Pair)

Aggregations

TailCharIterator (org.trie4j.tail.TailCharIterator)20 ArrayList (java.util.ArrayList)11 Pair (org.trie4j.util.Pair)7 LinkedList (java.util.LinkedList)5 Range (org.trie4j.util.Range)4 FastTailCharIterator (org.trie4j.tail.FastTailCharIterator)2 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 TreeSet (java.util.TreeSet)1