Search in sources :

Example 11 with TailCharIterator

use of org.trie4j.tail.TailCharIterator in project trie4j by takawitter.

the class TailPatriciaTrieNode method getLetters.

public char[] getLetters(CharSequence tails) {
    List<Character> letters = new ArrayList<Character>();
    if (firstChar != (char) 0xffff) {
        letters.add(firstChar);
    }
    TailCharIterator it = new TailCharIterator(tails, tailIndex);
    while (it.hasNext()) {
        letters.add(it.next());
    }
    char[] ret = new char[letters.size()];
    for (int i = 0; i < ret.length; i++) {
        ret[i] = letters.get(i);
    }
    return ret;
}
Also used : ArrayList(java.util.ArrayList) TailCharIterator(org.trie4j.tail.TailCharIterator)

Example 12 with TailCharIterator

use of org.trie4j.tail.TailCharIterator in project trie4j by takawitter.

the class OptimizedTailDoubleArray method predictiveSearch.

@Override
public Iterable<String> predictiveSearch(String prefix) {
    List<String> ret = new ArrayList<String>();
    StringBuilder current = new StringBuilder();
    char[] chars = prefix.toCharArray();
    int nodeIndex = 0;
    TailCharIterator it = new TailCharIterator(tails, -1);
    for (int i = 0; i < chars.length; i++) {
        int ti = tail[nodeIndex];
        if (ti != -1) {
            int first = i;
            it.setIndex(ti);
            do {
                if (!it.hasNext())
                    break;
                if (it.next() != chars[i])
                    return ret;
                i++;
            } while (i < chars.length);
            if (i >= chars.length)
                break;
            current.append(chars, first, i - first);
        }
        int cid = findCharId(chars[i]);
        if (cid == -1)
            return ret;
        int next = base[nodeIndex] + cid;
        if (next < 0 || check.length <= next || (next + check[next]) != nodeIndex)
            return ret;
        nodeIndex = next;
        current.append(chars[i]);
    }
    Deque<Pair<Integer, char[]>> q = new LinkedList<Pair<Integer, char[]>>();
    q.add(Pair.create(nodeIndex, current.toString().toCharArray()));
    while (!q.isEmpty()) {
        Pair<Integer, char[]> p = q.pop();
        int ni = p.getFirst();
        StringBuilder buff = new StringBuilder().append(p.getSecond());
        int ti = tail[ni];
        if (ti != -1) {
            it.setIndex(ti);
            while (it.hasNext()) {
                buff.append(it.next());
            }
        }
        if (term.get(ni))
            ret.add(buff.toString());
        for (Map.Entry<Character, Integer> e : charCodes.entrySet()) {
            int b = base[ni];
            if (b == BASE_EMPTY)
                continue;
            if (b == (BASE_EMPTY - 1))
                continue;
            int next = b + e.getValue();
            if (check.length <= next)
                continue;
            if (next + check[next] == ni) {
                StringBuilder bu = new StringBuilder(buff);
                bu.append(e.getKey());
                q.push(Pair.create(next, bu.toString().toCharArray()));
            }
        }
    }
    return ret;
/*/
		List<String> ret = new ArrayList<String>();
		StringBuilder current = new StringBuilder();
		char[] chars = prefix.toCharArray();
		int nodeIndex = 0;
		for(int i = 0; i < chars.length; i++){
			int ti = tail[nodeIndex];
			if(ti != -1){
				int first = i;
				TailCharIterator it = new TailCharIterator(tails,  ti);
				do{
					if(!it.hasNext()) break;
					if(it.next() != chars[i]) return ret;
					i++;
				} while(i < chars.length);
				if(i >= chars.length) break;
				current.append(chars, i, i - first);
			}
			int cid = findCharId(chars[i]);
			if(cid == -1) return ret;
			int next = base[nodeIndex] + cid;
			if(next < 0 || check.length <= next || check[next] != cid) return ret;
			nodeIndex = next;
			current.append(chars[i]);
		}
		Deque<Pair<Integer, char[]>> q = new LinkedList<Pair<Integer,char[]>>();
		q.add(Pair.create(nodeIndex, current.toString().toCharArray()));
		while(!q.isEmpty()){
			Pair<Integer, char[]> p = q.pop();
			int ni = p.getFirst();
			StringBuilder buff = new StringBuilder().append(p.getSecond());
			int ti = tail[ni];
			if(ti != -1){
				TailCharIterator it = new TailCharIterator(tails, ti);
				while(it.hasNext()){
					buff.append(it.next());
				}
			}
			if(term.get(ni)) ret.add(buff.toString());
			for(Map.Entry<Character, Integer> e : charCodes.entrySet()){
				int b = base[ni];
				if(b == BASE_EMPTY) continue;
				int next = b + e.getValue();
				if(check.length <= next) continue;
				if(check[next] == e.getValue()){
					StringBuilder bu = new StringBuilder(buff);
					bu.append(e.getKey());
					q.push(Pair.create(next, bu.toString().toCharArray()));
				}
			}
		}
		return ret;
//*/
}
Also used : ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) TailCharIterator(org.trie4j.tail.TailCharIterator) Map(java.util.Map) TreeMap(java.util.TreeMap) Pair(org.trie4j.util.Pair)

Example 13 with TailCharIterator

use of org.trie4j.tail.TailCharIterator in project trie4j by takawitter.

the class OptimizedTailDoubleArray method commonPrefixSearch.

@Override
public Iterable<String> commonPrefixSearch(String query) {
    List<String> ret = new ArrayList<String>();
    char[] chars = query.toCharArray();
    int ci = 0;
    int ni = 0;
    if (tail[0] != -1) {
        TailCharIterator it = new TailCharIterator(tails, tail[0]);
        while (it.hasNext()) {
            ci++;
            if (ci >= chars.length)
                return ret;
            if (it.next() != chars[ci])
                return ret;
        }
        if (term.get(0))
            ret.add(new String(chars, 0, ci + 1));
    }
    TailCharIterator it = new TailCharIterator(tails, -1);
    for (; ci < chars.length; ci++) {
        int cid = findCharId(chars[ci]);
        if (cid == -1)
            return ret;
        int b = base[ni];
        if (b == BASE_EMPTY)
            return ret;
        if (b == (BASE_EMPTY - 1))
            return ret;
        int next = b + cid;
        if (check.length <= next || (next + check[next]) != ni)
            return ret;
        ni = next;
        if (tail[ni] != -1) {
            it.setIndex(tail[ni]);
            while (it.hasNext()) {
                ci++;
                if (ci >= chars.length)
                    return ret;
                if (it.next() != chars[ci])
                    return ret;
            }
        }
        if (term.get(ni))
            ret.add(new String(chars, 0, ci + 1));
    }
    return ret;
}
Also used : ArrayList(java.util.ArrayList) TailCharIterator(org.trie4j.tail.TailCharIterator)

Example 14 with TailCharIterator

use of org.trie4j.tail.TailCharIterator in project trie4j by takawitter.

the class OptimizedTailDoubleArray method contains.

public boolean contains(String text) {
    char[] chars = text.toCharArray();
    int charsIndex = 0;
    int nodeIndex = 0;
    TailCharIterator it = new TailCharIterator(tails, -1);
    while (charsIndex < chars.length) {
        int tailIndex = tail[nodeIndex];
        if (tailIndex != -1) {
            it.setIndex(tailIndex);
            while (it.hasNext()) {
                if (chars.length <= charsIndex)
                    return false;
                if (chars[charsIndex] != it.next())
                    return false;
                charsIndex++;
            }
            if (chars.length == charsIndex) {
                if (!it.hasNext())
                    return term.get(nodeIndex);
                else
                    return false;
            }
        }
        int cid = findCharId(chars[charsIndex]);
        if (cid == -1)
            return false;
        int i = cid + base[nodeIndex];
        if (i < 0 || check.length <= i || (i + check[i]) != nodeIndex)
            return false;
        charsIndex++;
        nodeIndex = i;
    }
    return term.get(nodeIndex);
}
Also used : TailCharIterator(org.trie4j.tail.TailCharIterator)

Example 15 with TailCharIterator

use of org.trie4j.tail.TailCharIterator in project trie4j by takawitter.

the class TailDoubleArray method commonPrefixSearchWithTermId.

@Override
public Iterable<Pair<String, Integer>> commonPrefixSearchWithTermId(String query) {
    List<Pair<String, Integer>> ret = new ArrayList<Pair<String, Integer>>();
    int charsLen = query.length();
    int ci = 0;
    int ni = 0;
    TailCharIterator it = tailArray.newIterator();
    for (; ci < charsLen; ci++) {
        int cid = findCharId(query.charAt(ci));
        if (cid == -1)
            return ret;
        int b = base[ni];
        if (b == BASE_EMPTY)
            return ret;
        int next = b + cid;
        if (check.length <= next || check[next] != ni)
            return ret;
        ni = next;
        int ti = tailArray.getIteratorOffset(ni);
        if (ti != -1) {
            it.setIndex(ti);
            while (it.hasNext()) {
                char c = it.next();
                ci++;
                if (ci >= charsLen)
                    return ret;
                if (c != query.charAt(ci))
                    return ret;
            }
        }
        if (term.get(ni))
            ret.add(Pair.create(query.substring(0, ci + 1), term.rank1(ni) - 1));
    }
    return ret;
}
Also used : ArrayList(java.util.ArrayList) 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