本文整理汇总了C#中Builder.Finish方法的典型用法代码示例。如果您正苦于以下问题:C# Builder.Finish方法的具体用法?C# Builder.Finish怎么用?C# Builder.Finish使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Builder
的用法示例。
在下文中一共展示了Builder.Finish方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Test
public virtual void Test()
{
int[] ints = new int[7];
IntsRef input = new IntsRef(ints, 0, ints.Length);
int seed = Random().Next();
Directory dir = new MMapDirectory(CreateTempDir("2BFST"));
for (int doPackIter = 0; doPackIter < 2; doPackIter++)
{
bool doPack = doPackIter == 1;
// Build FST w/ NoOutputs and stop when nodeCount > 2.2B
if (!doPack)
{
Console.WriteLine("\nTEST: 3B nodes; doPack=false output=NO_OUTPUTS");
Outputs<object> outputs = NoOutputs.Singleton;
object NO_OUTPUT = outputs.NoOutput;
Builder<object> b = new Builder<object>(FST.INPUT_TYPE.BYTE1, 0, 0, true, true, int.MaxValue, outputs, null, doPack, PackedInts.COMPACT, true, 15);
int count = 0;
Random r = new Random(seed);
int[] ints2 = new int[200];
IntsRef input2 = new IntsRef(ints2, 0, ints2.Length);
while (true)
{
//System.out.println("add: " + input + " -> " + output);
for (int i = 10; i < ints2.Length; i++)
{
ints2[i] = r.Next(256);
}
b.Add(input2, NO_OUTPUT);
count++;
if (count % 100000 == 0)
{
Console.WriteLine(count + ": " + b.FstSizeInBytes() + " bytes; " + b.TotStateCount + " nodes");
}
if (b.TotStateCount > int.MaxValue + 100L * 1024 * 1024)
{
break;
}
NextInput(r, ints2);
}
FST<object> fst = b.Finish();
for (int verify = 0; verify < 2; verify++)
{
Console.WriteLine("\nTEST: now verify [fst size=" + fst.SizeInBytes() + "; nodeCount=" + fst.NodeCount + "; arcCount=" + fst.ArcCount + "]");
Arrays.Fill(ints2, 0);
r = new Random(seed);
for (int i = 0; i < count; i++)
{
if (i % 1000000 == 0)
{
Console.WriteLine(i + "...: ");
}
for (int j = 10; j < ints2.Length; j++)
{
ints2[j] = r.Next(256);
}
Assert.AreEqual(NO_OUTPUT, Util.Get(fst, input2));
NextInput(r, ints2);
}
Console.WriteLine("\nTEST: enum all input/outputs");
IntsRefFSTEnum<object> fstEnum = new IntsRefFSTEnum<object>(fst);
Arrays.Fill(ints2, 0);
r = new Random(seed);
int upto = 0;
while (true)
{
IntsRefFSTEnum<object>.InputOutput<object> pair = fstEnum.Next();
if (pair == null)
{
break;
}
for (int j = 10; j < ints2.Length; j++)
{
ints2[j] = r.Next(256);
}
Assert.AreEqual(input2, pair.Input);
Assert.AreEqual(NO_OUTPUT, pair.Output);
upto++;
NextInput(r, ints2);
}
Assert.AreEqual(count, upto);
if (verify == 0)
{
Console.WriteLine("\nTEST: save/load FST and re-verify");
IndexOutput @out = dir.CreateOutput("fst", IOContext.DEFAULT);
fst.Save(@out);
@out.Dispose();
IndexInput @in = dir.OpenInput("fst", IOContext.DEFAULT);
fst = new FST<object>(@in, outputs);
@in.Dispose();
//.........这里部分代码省略.........
示例2: LoadTerms
private void LoadTerms()
{
var posIntOutputs = PositiveIntOutputs.Singleton;
var outputsInner = new PairOutputs<long?, long?>(posIntOutputs, posIntOutputs);
var outputs = new PairOutputs<long?, PairOutputs<long?,long?>.Pair>(posIntOutputs, outputsInner);
// honestly, wtf kind of generic mess is this.
var b = new Builder<PairOutputs<long?, PairOutputs<long?,long?>.Pair>.Pair>(FST.INPUT_TYPE.BYTE1, outputs);
var input = (IndexInput) _outerInstance._input.Clone();
input.Seek(_termsStart);
var lastTerm = new BytesRef(10);
long lastDocsStart = -1;
int docFreq = 0;
long totalTermFreq = 0;
var visitedDocs = new FixedBitSet(_maxDoc);
var scratchIntsRef = new IntsRef();
while (true)
{
SimpleTextUtil.ReadLine(input, _scratch);
if (_scratch.Equals(SimpleTextFieldsWriter.END) || StringHelper.StartsWith(_scratch, SimpleTextFieldsWriter.FIELD))
{
if (lastDocsStart != -1)
{
b.Add(Util.ToIntsRef(lastTerm, scratchIntsRef),
outputs.NewPair(lastDocsStart, outputsInner.NewPair(docFreq, totalTermFreq)));
_sumTotalTermFreq += totalTermFreq;
}
break;
}
if (StringHelper.StartsWith(_scratch, SimpleTextFieldsWriter.DOC))
{
docFreq++;
_sumDocFreq++;
UnicodeUtil.UTF8toUTF16(_scratch.Bytes, _scratch.Offset + SimpleTextFieldsWriter.DOC.Length, _scratch.Length - SimpleTextFieldsWriter.DOC.Length,
_scratchUtf16);
int docId = ArrayUtil.ParseInt(_scratchUtf16.Chars, 0, _scratchUtf16.Length);
visitedDocs.Set(docId);
}
else if (StringHelper.StartsWith(_scratch, SimpleTextFieldsWriter.FREQ))
{
UnicodeUtil.UTF8toUTF16(_scratch.Bytes, _scratch.Offset + SimpleTextFieldsWriter.FREQ.Length,
_scratch.Length - SimpleTextFieldsWriter.FREQ.Length, _scratchUtf16);
totalTermFreq += ArrayUtil.ParseInt(_scratchUtf16.Chars, 0, _scratchUtf16.Length);
}
else if (StringHelper.StartsWith(_scratch, SimpleTextFieldsWriter.TERM))
{
if (lastDocsStart != -1)
{
b.Add(Util.ToIntsRef(lastTerm, scratchIntsRef),
outputs.NewPair(lastDocsStart, outputsInner.NewPair(docFreq, totalTermFreq)));
}
lastDocsStart = input.FilePointer;
int len = _scratch.Length - SimpleTextFieldsWriter.TERM.Length;
if (len > lastTerm.Length)
{
lastTerm.Grow(len);
}
Array.Copy(_scratch.Bytes, SimpleTextFieldsWriter.TERM.Length, lastTerm.Bytes, 0, len);
lastTerm.Length = len;
docFreq = 0;
_sumTotalTermFreq += totalTermFreq;
totalTermFreq = 0;
_termCount++;
}
}
_docCount = visitedDocs.Cardinality();
_fst = b.Finish();
}
示例3: Build
/// <summary>
/// Builds the NormalizeCharMap; call this once you
/// are done calling <seealso cref="#add"/>.
/// </summary>
public virtual NormalizeCharMap Build()
{
FST<CharsRef> map;
try
{
Outputs<CharsRef> outputs = CharSequenceOutputs.Singleton;
Builder<CharsRef> builder = new Builder<CharsRef>(FST.INPUT_TYPE.BYTE2, outputs);
IntsRef scratch = new IntsRef();
foreach (var ent in pendingPairs)
{
builder.Add(Lucene.Net.Util.Fst.Util.ToUTF16(ent.Key, scratch), new CharsRef(ent.Value));
}
map = builder.Finish();
pendingPairs.Clear();
}
catch (IOException ioe)
{
// Bogus FST IOExceptions!! (will never happen)
throw new Exception("Should never happen", ioe);
}
return new NormalizeCharMap(map);
}
示例4: BuildAutomaton
/// <summary>
/// Builds the final automaton from a list of entries.
/// </summary>
private FST<object> BuildAutomaton(BytesRefSorter sorter)
{
// Build the automaton.
Outputs<object> outputs = NoOutputs.Singleton;
object empty = outputs.NoOutput;
Builder<object> builder = new Builder<object>(FST.INPUT_TYPE.BYTE1, 0, 0, true, true, shareMaxTailLength, outputs, null, false, PackedInts.DEFAULT, true, 15);
BytesRef scratch = new BytesRef();
BytesRef entry;
IntsRef scratchIntsRef = new IntsRef();
int count = 0;
BytesRefIterator iter = sorter.GetEnumerator();
while ((entry = iter.Next()) != null)
{
count++;
if (scratch.CompareTo(entry) != 0)
{
builder.Add(Util.Fst.Util.ToIntsRef(entry, scratchIntsRef), empty);
scratch.CopyBytes(entry);
}
}
return count == 0 ? null : builder.Finish();
}
示例5: LoadTermsIndex
private void LoadTermsIndex()
{
if (Fst != null) return;
var clone = (IndexInput) _vgtir._input.Clone();
clone.Seek(_indexStart);
Fst = new FST<long?>(clone, _vgtir._fstOutputs);
clone.Dispose();
/*
final String dotFileName = segment + "_" + fieldInfo.name + ".dot";
Writer w = new OutputStreamWriter(new FileOutputStream(dotFileName));
Util.toDot(fst, w, false, false);
System.out.println("FST INDEX: SAVED to " + dotFileName);
w.close();
*/
if (_vgtir._indexDivisor > 1)
{
// subsample
var scratchIntsRef = new IntsRef();
var outputs = PositiveIntOutputs.Singleton;
var builder = new Builder<long?>(FST.INPUT_TYPE.BYTE1, outputs);
var fstEnum = new BytesRefFSTEnum<long?>(Fst);
var count = _vgtir._indexDivisor;
BytesRefFSTEnum<long?>.InputOutput<long?> result;
while ((result = fstEnum.Next()) != null)
{
if (count == _vgtir._indexDivisor)
{
builder.Add(Util.ToIntsRef(result.Input, scratchIntsRef), result.Output);
count = 0;
}
count++;
}
Fst = builder.Finish();
}
}
示例6: Build
/// <summary>
/// Builds an <seealso cref="SynonymMap"/> and returns it.
/// </summary>
public virtual SynonymMap Build()
{
ByteSequenceOutputs outputs = ByteSequenceOutputs.Singleton;
// TODO: are we using the best sharing options?
var builder = new Builder<BytesRef>(FST.INPUT_TYPE.BYTE4, outputs);
BytesRef scratch = new BytesRef(64);
ByteArrayDataOutput scratchOutput = new ByteArrayDataOutput();
HashSet<int?> dedupSet;
if (dedup)
{
dedupSet = new HashSet<int?>();
}
else
{
dedupSet = null;
}
var spare = new sbyte[5];
Dictionary<CharsRef, MapEntry>.KeyCollection keys = workingSet.Keys;
CharsRef[] sortedKeys = keys.ToArray();
Arrays.Sort(sortedKeys, CharsRef.UTF16SortedAsUTF8Comparator);
IntsRef scratchIntsRef = new IntsRef();
//System.out.println("fmap.build");
for (int keyIdx = 0; keyIdx < sortedKeys.Length; keyIdx++)
{
CharsRef input = sortedKeys[keyIdx];
MapEntry output = workingSet[input];
int numEntries = output.ords.Count;
// output size, assume the worst case
int estimatedSize = 5 + numEntries * 5; // numEntries + one ord for each entry
scratch.Grow(estimatedSize);
scratchOutput.Reset(scratch.Bytes, scratch.Offset, scratch.Bytes.Length);
Debug.Assert(scratch.Offset == 0);
// now write our output data:
int count = 0;
for (int i = 0; i < numEntries; i++)
{
if (dedupSet != null)
{
// box once
int? ent = output.ords[i];
if (dedupSet.Contains(ent))
{
continue;
}
dedupSet.Add(ent);
}
scratchOutput.WriteVInt(output.ords[i]);
count++;
}
int pos = scratchOutput.Position;
scratchOutput.WriteVInt(count << 1 | (output.includeOrig ? 0 : 1));
int pos2 = scratchOutput.Position;
int vIntLen = pos2 - pos;
// Move the count + includeOrig to the front of the byte[]:
Array.Copy(scratch.Bytes, pos, spare, 0, vIntLen);
Array.Copy(scratch.Bytes, 0, scratch.Bytes, vIntLen, pos);
Array.Copy(spare, 0, scratch.Bytes, 0, vIntLen);
if (dedupSet != null)
{
dedupSet.Clear();
}
scratch.Length = scratchOutput.Position - scratch.Offset;
//System.out.println(" add input=" + input + " output=" + scratch + " offset=" + scratch.offset + " length=" + scratch.length + " count=" + count);
builder.Add(Util.ToUTF32(input, scratchIntsRef), BytesRef.DeepCopyOf(scratch));
}
FST<BytesRef> fst = builder.Finish();
return new SynonymMap(fst, words, maxHorizontalContext);
}
示例7: loadTermsIndex
private void loadTermsIndex()
{
if (Fst == null)
{
IndexInput clone = input.Clone();
clone.Seek(indexStart);
Fst = new FST<>(clone, fstOutputs);
clone.Close();
/*
final String dotFileName = segment + "_" + fieldInfo.name + ".dot";
Writer w = new OutputStreamWriter(new FileOutputStream(dotFileName));
Util.toDot(fst, w, false, false);
System.out.println("FST INDEX: SAVED to " + dotFileName);
w.close();
*/
if (indexDivisor > 1)
{
// subsample
IntsRef scratchIntsRef = new IntsRef();
PositiveIntOutputs outputs = PositiveIntOutputs.GetSingleton();
Builder<long> builder = new Builder<long>(FST.INPUT_TYPE.BYTE1, outputs);
BytesRefFSTEnum<long> fstEnum = new BytesRefFSTEnum<long>(fst);
BytesRefFSTEnum.InputOutput<long> result;
int count = indexDivisor;
while ((result = fstEnum.Next()) != null)
{
if (count == indexDivisor)
{
builder.Add(Util.ToIntsRef(result.Input, scratchIntsRef), result.Output);
count = 0;
}
count++;
}
Fst = builder.Finish();
}
}
}
示例8: TestReplacements
public virtual void TestReplacements()
{
Outputs<CharsRef> outputs = CharSequenceOutputs.Singleton;
Builder<CharsRef> builder = new Builder<CharsRef>(FST.INPUT_TYPE.BYTE2, outputs);
IntsRef scratchInts = new IntsRef();
// a -> b
Lucene.Net.Util.Fst.Util.ToUTF16("a", scratchInts);
builder.Add(scratchInts, new CharsRef("b"));
// ab -> c
Lucene.Net.Util.Fst.Util.ToUTF16("ab", scratchInts);
builder.Add(scratchInts, new CharsRef("c"));
// c -> de
Lucene.Net.Util.Fst.Util.ToUTF16("c", scratchInts);
builder.Add(scratchInts, new CharsRef("de"));
// def -> gh
Lucene.Net.Util.Fst.Util.ToUTF16("def", scratchInts);
builder.Add(scratchInts, new CharsRef("gh"));
FST<CharsRef> fst = builder.Finish();
StringBuilder sb = new StringBuilder("atestanother");
Dictionary.ApplyMappings(fst, sb);
assertEquals("btestbnother", sb.ToString());
sb = new StringBuilder("abtestanother");
Dictionary.ApplyMappings(fst, sb);
assertEquals("ctestbnother", sb.ToString());
sb = new StringBuilder("atestabnother");
Dictionary.ApplyMappings(fst, sb);
assertEquals("btestcnother", sb.ToString());
sb = new StringBuilder("abtestabnother");
Dictionary.ApplyMappings(fst, sb);
assertEquals("ctestcnother", sb.ToString());
sb = new StringBuilder("abtestabcnother");
Dictionary.ApplyMappings(fst, sb);
assertEquals("ctestcdenother", sb.ToString());
sb = new StringBuilder("defdefdefc");
Dictionary.ApplyMappings(fst, sb);
assertEquals("ghghghde", sb.ToString());
}