use of org.apache.lucene.util.IntsRefBuilder in project lucene-solr by apache.
the class TestFSTs method testInternalFinalState.
public void testInternalFinalState() throws Exception {
final PositiveIntOutputs outputs = PositiveIntOutputs.getSingleton();
final Builder<Long> builder = new Builder<>(FST.INPUT_TYPE.BYTE1, 0, 0, true, true, Integer.MAX_VALUE, outputs, true, 15);
builder.add(Util.toIntsRef(new BytesRef("stat"), new IntsRefBuilder()), outputs.getNoOutput());
builder.add(Util.toIntsRef(new BytesRef("station"), new IntsRefBuilder()), outputs.getNoOutput());
final FST<Long> fst = builder.finish();
StringWriter w = new StringWriter();
//Writer w = new OutputStreamWriter(new FileOutputStream("/x/tmp/out.dot"));
Util.toDot(fst, w, false, false);
w.close();
//System.out.println(w.toString());
// check for accept state at label t
assertTrue(w.toString().indexOf("[label=\"t\" style=\"bold\"") != -1);
// check for accept state at label n
assertTrue(w.toString().indexOf("[label=\"n\" style=\"bold\"") != -1);
}
use of org.apache.lucene.util.IntsRefBuilder in project lucene-solr by apache.
the class TestFSTs method testShortestPathsWFST.
/** like testShortestPaths, but uses pairoutputs so we have both a weight and an output */
public void testShortestPathsWFST() throws Exception {
PairOutputs<Long, Long> outputs = new PairOutputs<>(// weight
PositiveIntOutputs.getSingleton(), // output
PositiveIntOutputs.getSingleton());
final Builder<Pair<Long, Long>> builder = new Builder<>(FST.INPUT_TYPE.BYTE1, outputs);
final IntsRefBuilder scratch = new IntsRefBuilder();
builder.add(Util.toIntsRef(new BytesRef("aab"), scratch), outputs.newPair(22L, 57L));
builder.add(Util.toIntsRef(new BytesRef("aac"), scratch), outputs.newPair(7L, 36L));
builder.add(Util.toIntsRef(new BytesRef("ax"), scratch), outputs.newPair(17L, 85L));
final FST<Pair<Long, Long>> fst = builder.finish();
//Writer w = new OutputStreamWriter(new FileOutputStream("out.dot"));
//Util.toDot(fst, w, false, false);
//w.close();
Util.TopResults<Pair<Long, Long>> res = Util.shortestPaths(fst, fst.getFirstArc(new FST.Arc<Pair<Long, Long>>()), outputs.getNoOutput(), minPairWeightComparator, 3, true);
assertTrue(res.isComplete);
assertEquals(3, res.topN.size());
assertEquals(Util.toIntsRef(new BytesRef("aac"), scratch), res.topN.get(0).input);
// weight
assertEquals(7L, res.topN.get(0).output.output1.longValue());
// output
assertEquals(36L, res.topN.get(0).output.output2.longValue());
assertEquals(Util.toIntsRef(new BytesRef("ax"), scratch), res.topN.get(1).input);
// weight
assertEquals(17L, res.topN.get(1).output.output1.longValue());
// output
assertEquals(85L, res.topN.get(1).output.output2.longValue());
assertEquals(Util.toIntsRef(new BytesRef("aab"), scratch), res.topN.get(2).input);
// weight
assertEquals(22L, res.topN.get(2).output.output1.longValue());
// output
assertEquals(57L, res.topN.get(2).output.output2.longValue());
}
use of org.apache.lucene.util.IntsRefBuilder in project lucene-solr by apache.
the class TestUTF32ToUTF8 method testSingleton.
public void testSingleton() throws Exception {
int iters = atLeast(100);
for (int iter = 0; iter < iters; iter++) {
String s = TestUtil.randomRealisticUnicodeString(random());
Automaton a = Automata.makeString(s);
Automaton utf8 = new UTF32ToUTF8().convert(a);
IntsRefBuilder ints = new IntsRefBuilder();
Util.toIntsRef(new BytesRef(s), ints);
Set<IntsRef> set = new HashSet<>();
set.add(ints.get());
assertEquals(set, TestOperations.getFiniteStrings(utf8));
}
}
use of org.apache.lucene.util.IntsRefBuilder in project lucene-solr by apache.
the class TestFSTs method testDuplicateFSAString.
public void testDuplicateFSAString() throws Exception {
String str = "foobar";
final Outputs<Object> outputs = NoOutputs.getSingleton();
final Builder<Object> b = new Builder<>(FST.INPUT_TYPE.BYTE1, outputs);
IntsRefBuilder ints = new IntsRefBuilder();
for (int i = 0; i < 10; i++) {
b.add(Util.toIntsRef(new BytesRef(str), ints), outputs.getNoOutput());
}
FST<Object> fst = b.finish();
// count the input paths
int count = 0;
final BytesRefFSTEnum<Object> fstEnum = new BytesRefFSTEnum<>(fst);
while (fstEnum.next() != null) {
count++;
}
assertEquals(1, count);
assertNotNull(Util.get(fst, new BytesRef(str)));
assertNull(Util.get(fst, new BytesRef("foobaz")));
}
use of org.apache.lucene.util.IntsRefBuilder in project lucene-solr by apache.
the class TestFSTs method testRejectNoLimits.
public void testRejectNoLimits() throws IOException {
final PositiveIntOutputs outputs = PositiveIntOutputs.getSingleton();
final Builder<Long> builder = new Builder<Long>(FST.INPUT_TYPE.BYTE1, outputs);
final IntsRefBuilder scratch = new IntsRefBuilder();
builder.add(Util.toIntsRef(new BytesRef("aab"), scratch), 22L);
builder.add(Util.toIntsRef(new BytesRef("aac"), scratch), 7L);
builder.add(Util.toIntsRef(new BytesRef("adcd"), scratch), 17L);
builder.add(Util.toIntsRef(new BytesRef("adcde"), scratch), 17L);
builder.add(Util.toIntsRef(new BytesRef("ax"), scratch), 17L);
final FST<Long> fst = builder.finish();
final AtomicInteger rejectCount = new AtomicInteger();
Util.TopNSearcher<Long> searcher = new Util.TopNSearcher<Long>(fst, 2, 6, minLongComparator) {
@Override
protected boolean acceptResult(IntsRef input, Long output) {
boolean accept = output.intValue() == 7;
if (!accept) {
rejectCount.incrementAndGet();
}
return accept;
}
};
searcher.addStartPaths(fst.getFirstArc(new FST.Arc<Long>()), outputs.getNoOutput(), true, new IntsRefBuilder());
Util.TopResults<Long> res = searcher.search();
assertEquals(rejectCount.get(), 4);
// rejected(4) + topN(2) <= maxQueueSize(6)
assertTrue(res.isComplete);
assertEquals(1, res.topN.size());
assertEquals(Util.toIntsRef(new BytesRef("aac"), scratch), res.topN.get(0).input);
assertEquals(7L, res.topN.get(0).output.longValue());
rejectCount.set(0);
searcher = new Util.TopNSearcher<Long>(fst, 2, 5, minLongComparator) {
@Override
protected boolean acceptResult(IntsRef input, Long output) {
boolean accept = output.intValue() == 7;
if (!accept) {
rejectCount.incrementAndGet();
}
return accept;
}
};
searcher.addStartPaths(fst.getFirstArc(new FST.Arc<Long>()), outputs.getNoOutput(), true, new IntsRefBuilder());
res = searcher.search();
assertEquals(rejectCount.get(), 4);
// rejected(4) + topN(2) > maxQueueSize(5)
assertFalse(res.isComplete);
}
Aggregations