org.rx.util.pinyin.ForwardLongestSelector Maven / Gradle / Ivy
package org.rx.util.pinyin;
import org.ahocorasick.trie.Emit;
import java.util.*;
/**
* 正向最大匹配选择器
*
* Created by guyacong on 2016/12/28.
*/
final class ForwardLongestSelector implements SegmentationSelector {
static final Engine.EmitComparator HIT_COMPARATOR = new Engine.EmitComparator();
@Override
public List select(final Collection emits) {
if (emits == null) {
return null;
}
List results = new ArrayList(emits);
Collections.sort(results, HIT_COMPARATOR);
int endValueToRemove = -1;
Set emitToRemove = new TreeSet();
for (Emit emit : results) {
if (emit.getStart() > endValueToRemove && emit.getEnd() > endValueToRemove) {
endValueToRemove = emit.getEnd();
} else {
emitToRemove.add(emit);
}
}
results.removeAll(emitToRemove);
return results;
}
}