private static int DoPrint(PrintOptions opts) { var timer = Stopwatch.StartNew(); timer.Restart(); var terms = 0; using (var inputFile = new FileStorage(opts.InputFile)) { using (var fst = new PersistentFST <int>(outputType, inputFile)) { var maxLength = fst.Header == null ? 1024 : fst.Header.MaxLength; if (fst.Header != null) { PrintConsole(ConsoleColor.White, $"FST header terms: {fst.Header.TermCount}, max length: {fst.Header.MaxLength}, states: {fst.Header.States}"); } foreach (var term in fst.Match(new WildcardMatcher(opts.Pattern, maxLength))) { if (!fst.TryMatch(term, out int value)) { throw new Exception("This is a bug"); } ++terms; Console.WriteLine($"{term}->{value}"); } PrintConsole(ConsoleColor.White, $"FST print terms: {terms}, time: {timer.Elapsed}"); } } return(0); }
private static int DoRender(RenderOptions opts) { var timer = Stopwatch.StartNew(); if (File.Exists(opts.OutputFile)) { File.Delete(opts.OutputFile); } timer.Restart(); using (var inputFile = new FileStorage(opts.InputFile)) { using (var outputFile = new FileStorage(opts.OutputFile)) { using (var fst = new PersistentFST <int>(outputType, inputFile)) { fst.ToDotNotation(outputFile); } } } PrintConsole(ConsoleColor.White, $"FST rendered to dot file {opts.OutputFile}, time: {timer.Elapsed}"); return(0); }
private static int DoBuild(BuildOptions opts) { var timer = Stopwatch.StartNew(); if (File.Exists(opts.OutputFile)) { File.Delete(opts.OutputFile); } timer.Restart(); var terms = 0; using (var outputFile = new FileStorage(opts.OutputFile)) { using (var fstBuilder = new FSTBuilder <int>(outputType, opts.CacheSize, outputFile)) { fstBuilder.Begin(); foreach (var(term, score) in ParseFromOptions(opts)) { fstBuilder.Add(term, score); ++terms; } fstBuilder.End(); PrintConsole(ConsoleColor.White, $"FST constructed time: {timer.Elapsed}, terms: {terms}, cache size: {opts.CacheSize}, Memory: {Process.GetCurrentProcess().WorkingSet64}, output size: {outputFile.Length}"); } } using (var outputFile = new FileStorage(opts.OutputFile)) { if (outputFile.Length < 64 * 1024 * 1024) { timer.Restart(); var data = new byte[outputFile.Length]; outputFile.ReadAll(0, data, 0, data.Length); var fst = FST <int> .FromBytesCompressed(data, outputType); foreach (var(term, score) in ParseFromOptions(opts)) { if (!fst.TryMatch(term, out var value) || value != score) { throw new Exception($"Bug at term {term}: {value} != {score}"); } } PrintConsole(ConsoleColor.White, $"FST (memory) verification time: {timer.Elapsed}"); } } timer.Restart(); using (var outputFile = new FileStorage(opts.OutputFile)) { using (var fst = new PersistentFST <int>(outputType, outputFile)) { foreach (var(term, score) in ParseFromOptions(opts)) { if (!fst.TryMatch(term, out var value) || value != score) { throw new Exception($"Bug at term {term}: {value} != {score}"); } } } } PrintConsole(ConsoleColor.White, $"FST (file) verification time: {timer.Elapsed}"); return(0); }