com.github.liblevenshtein.collection.dictionary.DawgIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of liblevenshtein-lite Show documentation
Show all versions of liblevenshtein-lite Show documentation
A library for spelling-correction based on Levenshtein Automata.
package com.github.liblevenshtein.collection.dictionary;
import com.github.liblevenshtein.collection.AbstractIterator;
import java.util.ArrayDeque;
import java.util.Iterator;
import java.util.Queue;
/**
* Iterates over the terms within an {@link Dawg}.
* @author Dylon Edwards
* @since 2.1.0
*/
public class DawgIterator extends AbstractIterator {
private static final long serialVersionUID = 1L;
/**
* Queue for traversing the terms in the {@link Dawg} in a
* depth-first search manner.
*/
private final Queue prefixes = new ArrayDeque<>();
/**
* Returns whether the current {@link DawgNode} represents the last character
* in some term.
*/
private final IFinalFunction isFinal;
/**
* Initializes a new {@link DawgIterator}.
* @param root Root of the DAWG structure to traverse
* @param isFinal Returns whether some {@link DawgNode} represents the last
* character in some term.
*/
public DawgIterator(
final DawgNode root,
final IFinalFunction isFinal) {
this.isFinal = isFinal;
prefixes.offer(new Prefix(root));
}
/**
* {@inheritDoc}
*/
@Override
protected void advance() {
if (null == next && !prefixes.isEmpty()) {
DawgNode node;
String value;
do {
final Prefix prefix = prefixes.poll();
node = prefix.node();
value = prefix.value();
final Iterator iter = node.labels();
while (iter.hasNext()) {
final char label = iter.next();
final DawgNode nextNode = node.transition(label);
prefixes.add(new Prefix(nextNode, prefix, label));
}
}
while (!isFinal.at(node));
this.next = value;
}
}
}