forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.cs
More file actions
1184 lines (1057 loc) · 47.5 KB
/
Copy pathUtil.cs
File metadata and controls
1184 lines (1057 loc) · 47.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using J2N;
using J2N.Text;
using Lucene.Net.Diagnostics;
using Lucene.Net.Support;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Runtime.CompilerServices;
using BitSet = Lucene.Net.Util.OpenBitSet;
using JCG = J2N.Collections.Generic;
using Int64 = J2N.Numerics.Int64;
namespace Lucene.Net.Util.Fst
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/// <summary>
/// Static helper methods.
/// <para/>
/// @lucene.experimental
/// </summary>
public static class Util // LUCENENET specific - made static // LUCENENET TODO: Fix naming conflict with containing namespace
{
/// <summary>
/// Looks up the output for this input, or <c>null</c> if the
/// input is not accepted.
/// </summary>
public static T Get<T>(FST<T> fst, Int32sRef input) where T : class // LUCENENET specific - added class constraint, since we compare reference equality
{
// TODO: would be nice not to alloc this on every lookup
var arc = fst.GetFirstArc(new FST.Arc<T>());
var fstReader = fst.GetBytesReader();
// Accumulate output as we go
T output = fst.Outputs.NoOutput;
for (int i = 0; i < input.Length; i++)
{
if (fst.FindTargetArc(input.Int32s[input.Offset + i], arc, arc, fstReader) is null)
{
return default;
}
output = fst.Outputs.Add(output, arc.Output);
}
if (arc.IsFinal)
{
return fst.Outputs.Add(output, arc.NextFinalOutput);
}
else
{
return default;
}
}
// TODO: maybe a CharsRef version for BYTE2
/// <summary>
/// Looks up the output for this input, or <c>null</c> if the
/// input is not accepted
/// </summary>
public static T Get<T>(FST<T> fst, BytesRef input) where T : class // LUCENENET specific - added class constraint, since we compare reference equality
{
if (Debugging.AssertsEnabled) Debugging.Assert(fst.InputType == FST.INPUT_TYPE.BYTE1);
var fstReader = fst.GetBytesReader();
// TODO: would be nice not to alloc this on every lookup
var arc = fst.GetFirstArc(new FST.Arc<T>());
// Accumulate output as we go
T output = fst.Outputs.NoOutput;
for (int i = 0; i < input.Length; i++)
{
if (fst.FindTargetArc(input.Bytes[i + input.Offset] & 0xFF, arc, arc, fstReader) is null)
{
return default;
}
output = fst.Outputs.Add(output, arc.Output);
}
if (arc.IsFinal)
{
return fst.Outputs.Add(output, arc.NextFinalOutput);
}
else
{
return default;
}
}
/// <summary>
/// Reverse lookup (lookup by output instead of by input),
/// in the special case when your FSTs outputs are
/// strictly ascending. This locates the input/output
/// pair where the output is equal to the target, and will
/// return <c>null</c> if that output does not exist.
///
/// <para/>NOTE: this only works with <see cref="FST{T}"/>, only
/// works when the outputs are ascending in order with
/// the inputs.
/// For example, simple ordinals (0, 1,
/// 2, ...), or file offets (when appending to a file)
/// fit this.
/// </summary>
public static Int32sRef GetByOutput(FST<Int64> fst, long targetOutput)
{
var @in = fst.GetBytesReader();
// TODO: would be nice not to alloc this on every lookup
FST.Arc<Int64> arc = fst.GetFirstArc(new FST.Arc<Int64>());
FST.Arc<Int64> scratchArc = new FST.Arc<Int64>();
Int32sRef result = new Int32sRef();
return GetByOutput(fst, targetOutput, @in, arc, scratchArc, result);
}
/// <summary>
/// Expert: like <see cref="Util.GetByOutput(FST{Int64}, long)"/> except reusing
/// <see cref="FST.BytesReader"/>, initial and scratch Arc, and result.
/// </summary>
public static Int32sRef GetByOutput(FST<Int64> fst, long targetOutput, FST.BytesReader @in, FST.Arc<Int64> arc, FST.Arc<Int64> scratchArc, Int32sRef result)
{
long output = arc.Output;
int upto = 0;
//System.out.println("reverseLookup output=" + targetOutput);
while (true)
{
//System.out.println("loop: output=" + output + " upto=" + upto + " arc=" + arc);
if (arc.IsFinal)
{
long finalOutput = output + arc.NextFinalOutput;
//System.out.println(" isFinal finalOutput=" + finalOutput);
if (finalOutput == targetOutput)
{
result.Length = upto;
//System.out.println(" found!");
return result;
}
else if (finalOutput > targetOutput)
{
//System.out.println(" not found!");
return null;
}
}
if (FST<Int64>.TargetHasArcs(arc))
{
//System.out.println(" targetHasArcs");
if (result.Int32s.Length == upto)
{
result.Grow(1 + upto);
}
fst.ReadFirstRealTargetArc(arc.Target, arc, @in);
if (arc.BytesPerArc != 0)
{
int low = 0;
int high = arc.NumArcs - 1;
int mid = 0;
//System.out.println("bsearch: numArcs=" + arc.numArcs + " target=" + targetOutput + " output=" + output);
bool exact = false;
while (low <= high)
{
mid = (low + high) >>> 1;
@in.Position = arc.PosArcsStart;
@in.SkipBytes(arc.BytesPerArc * mid);
var flags = (sbyte)@in.ReadByte();
fst.ReadLabel(@in);
long minArcOutput;
if ((flags & FST.BIT_ARC_HAS_OUTPUT) != 0)
{
long arcOutput = fst.Outputs.Read(@in);
minArcOutput = output + arcOutput;
}
else
{
minArcOutput = output;
}
if (minArcOutput == targetOutput)
{
exact = true;
break;
}
else if (minArcOutput < targetOutput)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
if (high == -1)
{
return null;
}
else if (exact)
{
arc.ArcIdx = mid - 1;
}
else
{
arc.ArcIdx = low - 2;
}
fst.ReadNextRealArc(arc, @in);
result.Int32s[upto++] = arc.Label;
output += arc.Output;
}
else
{
FST.Arc<Int64> prevArc = null;
while (true)
{
//System.out.println(" cycle label=" + arc.label + " output=" + arc.output);
// this is the min output we'd hit if we follow
// this arc:
long minArcOutput = output + arc.Output;
if (minArcOutput == targetOutput)
{
// Recurse on this arc:
//System.out.println(" match! break");
output = minArcOutput;
result.Int32s[upto++] = arc.Label;
break;
}
else if (minArcOutput > targetOutput)
{
if (prevArc is null)
{
// Output doesn't exist
return null;
}
else
{
// Recurse on previous arc:
arc.CopyFrom(prevArc);
result.Int32s[upto++] = arc.Label;
output += arc.Output;
//System.out.println(" recurse prev label=" + (char) arc.label + " output=" + output);
break;
}
}
else if (arc.IsLast)
{
// Recurse on this arc:
output = minArcOutput;
//System.out.println(" recurse last label=" + (char) arc.label + " output=" + output);
result.Int32s[upto++] = arc.Label;
break;
}
else
{
// Read next arc in this node:
prevArc = scratchArc;
prevArc.CopyFrom(arc);
//System.out.println(" after copy label=" + (char) prevArc.label + " vs " + (char) arc.label);
fst.ReadNextRealArc(arc, @in);
}
}
}
}
else
{
//System.out.println(" no target arcs; not found!");
return null;
}
}
}
/// <summary>
/// Represents a path in TopNSearcher.
/// <para/>
/// @lucene.experimental
/// </summary>
public class FSTPath<T> where T : class // LUCENENET specific - added class constraint, since we compare reference equality
{
public FST.Arc<T> Arc { get; set; }
public T Cost { get; set; }
public Int32sRef Input { get; private set; }
/// <summary>
/// Sole constructor </summary>
public FSTPath(T cost, FST.Arc<T> arc, Int32sRef input)
{
this.Arc = (new FST.Arc<T>()).CopyFrom(arc);
this.Cost = cost;
this.Input = input;
}
public override string ToString()
{
return "input=" + Input + " cost=" + Cost;
}
}
/// <summary>
/// Compares first by the provided comparer, and then
/// tie breaks by <see cref="FSTPath{T}.Input"/>.
/// </summary>
private class TieBreakByInputComparer<T> : IComparer<FSTPath<T>> where T : class // LUCENENET specific - added class constraint, since we compare reference equality
{
internal readonly IComparer<T> comparer;
public TieBreakByInputComparer(IComparer<T> comparer)
{
this.comparer = comparer;
}
public virtual int Compare(FSTPath<T> a, FSTPath<T> b)
{
int cmp = comparer.Compare(a.Cost, b.Cost);
if (cmp == 0)
{
return a.Input.CompareTo(b.Input);
}
else
{
return cmp;
}
}
}
/// <summary>
/// Utility class to find top N shortest paths from start
/// point(s).
/// </summary>
public class TopNSearcher<T> where T : class // LUCENENET specific - added class constraint, since we compare reference equality
{
private readonly FST<T> fst;
private readonly FST.BytesReader bytesReader;
private readonly int topN;
private readonly int maxQueueDepth;
private readonly FST.Arc<T> scratchArc = new FST.Arc<T>();
internal readonly IComparer<T> comparer;
internal JCG.SortedSet<FSTPath<T>> queue = null;
/// <summary>
/// Creates an unbounded TopNSearcher </summary>
/// <param name="fst"> the <see cref="Lucene.Net.Util.Fst.FST{T}"/> to search on </param>
/// <param name="topN"> the number of top scoring entries to retrieve </param>
/// <param name="maxQueueDepth"> the maximum size of the queue of possible top entries </param>
/// <param name="comparer"> the comparer to select the top N </param>
public TopNSearcher(FST<T> fst, int topN, int maxQueueDepth, IComparer<T> comparer)
{
this.fst = fst;
this.bytesReader = fst.GetBytesReader();
this.topN = topN;
this.maxQueueDepth = maxQueueDepth;
this.comparer = comparer;
queue = new JCG.SortedSet<FSTPath<T>>(new TieBreakByInputComparer<T>(comparer));
}
/// <summary>
/// If back plus this arc is competitive then add to queue:
/// </summary>
protected virtual void AddIfCompetitive(FSTPath<T> path)
{
if (Debugging.AssertsEnabled) Debugging.Assert(queue != null);
T cost = fst.Outputs.Add(path.Cost, path.Arc.Output);
//System.out.println(" addIfCompetitive queue.size()=" + queue.size() + " path=" + path + " + label=" + path.arc.label);
if (queue.Count == maxQueueDepth)
{
if (!queue.TryGetLast(out FSTPath<T> bottom))
// LUCENENET: Java would throw NoSuchElementException in this case, so we are throwing also.
throw new InvalidOperationException("Expected the queue to contain an element, but it was empty.");
int comp = comparer.Compare(cost, bottom.Cost);
if (comp > 0)
{
// Doesn't compete
return;
}
else if (comp == 0)
{
// Tie break by alpha sort on the input:
path.Input.Grow(path.Input.Length + 1);
path.Input.Int32s[path.Input.Length++] = path.Arc.Label;
int cmp = bottom.Input.CompareTo(path.Input);
path.Input.Length--;
// We should never see dups:
if (Debugging.AssertsEnabled) Debugging.Assert(cmp != 0);
if (cmp < 0)
{
// Doesn't compete
return;
}
}
// Competes
}
else
{
// Queue isn't full yet, so any path we hit competes:
}
// copy over the current input to the new input
// and add the arc.label to the end
Int32sRef newInput = new Int32sRef(path.Input.Length + 1);
Arrays.Copy(path.Input.Int32s, 0, newInput.Int32s, 0, path.Input.Length);
newInput.Int32s[path.Input.Length] = path.Arc.Label;
newInput.Length = path.Input.Length + 1;
FSTPath<T> newPath = new FSTPath<T>(cost, path.Arc, newInput);
queue.Add(newPath);
if (queue.Count == maxQueueDepth + 1)
{
queue.RemoveLast(out _);
}
}
/// <summary>
/// Adds all leaving arcs, including 'finished' arc, if
/// the node is final, from this node into the queue.
/// </summary>
public virtual void AddStartPaths(FST.Arc<T> node, T startOutput, bool allowEmptyString, Int32sRef input)
{
// De-dup NO_OUTPUT since it must be a singleton:
if (startOutput.Equals(fst.Outputs.NoOutput))
{
startOutput = fst.Outputs.NoOutput;
}
FSTPath<T> path = new FSTPath<T>(startOutput, node, input);
fst.ReadFirstTargetArc(node, path.Arc, bytesReader);
//System.out.println("add start paths");
// Bootstrap: find the min starting arc
while (true)
{
if (allowEmptyString || path.Arc.Label != FST.END_LABEL)
{
AddIfCompetitive(path);
}
if (path.Arc.IsLast)
{
break;
}
fst.ReadNextArc(path.Arc, bytesReader);
}
}
public virtual TopResults<T> Search()
{
IList<Result<T>> results = new JCG.List<Result<T>>();
//System.out.println("search topN=" + topN);
var fstReader = fst.GetBytesReader();
T NO_OUTPUT = fst.Outputs.NoOutput;
// TODO: we could enable FST to sorting arcs by weight
// as it freezes... can easily do this on first pass
// (w/o requiring rewrite)
// TODO: maybe we should make an FST.INPUT_TYPE.BYTE0.5!?
// (nibbles)
int rejectCount = 0;
// For each top N path:
while (results.Count < topN)
{
//System.out.println("\nfind next path: queue.size=" + queue.size());
FSTPath<T> path;
if (queue is null)
{
// Ran out of paths
//System.out.println(" break queue=null");
break;
}
// Remove top path since we are now going to
// pursue it:
if (!queue.RemoveFirst(out path) || path is null)
{
// There were less than topN paths available:
//System.out.println(" break no more paths");
break;
}
if (path.Arc.Label == FST.END_LABEL)
{
//System.out.println(" empty string! cost=" + path.cost);
// Empty string!
path.Input.Length--;
results.Add(new Result<T>(path.Input, path.Cost));
continue;
}
if (results.Count == topN - 1 && maxQueueDepth == topN)
{
// Last path -- don't bother w/ queue anymore:
queue = null;
}
//System.out.println(" path: " + path);
// We take path and find its "0 output completion",
// ie, just keep traversing the first arc with
// NO_OUTPUT that we can find, since this must lead
// to the minimum path that completes from
// path.arc.
// For each input letter:
while (true)
{
//System.out.println("\n cycle path: " + path);
fst.ReadFirstTargetArc(path.Arc, path.Arc, fstReader);
// For each arc leaving this node:
bool foundZero = false;
while (true)
{
//System.out.println(" arc=" + (char) path.arc.label + " cost=" + path.arc.output);
// tricky: instead of comparing output == 0, we must
// express it via the comparer compare(output, 0) == 0
if (comparer.Compare(NO_OUTPUT, path.Arc.Output) == 0)
{
if (queue is null)
{
foundZero = true;
break;
}
else if (!foundZero)
{
scratchArc.CopyFrom(path.Arc);
foundZero = true;
}
else
{
AddIfCompetitive(path);
}
}
else if (queue != null)
{
AddIfCompetitive(path);
}
if (path.Arc.IsLast)
{
break;
}
fst.ReadNextArc(path.Arc, fstReader);
}
if (Debugging.AssertsEnabled) Debugging.Assert(foundZero);
if (queue != null)
{
// TODO: maybe we can save this copyFrom if we
// are more clever above... eg on finding the
// first NO_OUTPUT arc we'd switch to using
// scratchArc
path.Arc.CopyFrom(scratchArc);
}
if (path.Arc.Label == FST.END_LABEL)
{
// Add final output:
//Debug.WriteLine(" done!: " + path);
T finalOutput = fst.Outputs.Add(path.Cost, path.Arc.Output);
if (AcceptResult(path.Input, finalOutput))
{
//Debug.WriteLine(" add result: " + path);
results.Add(new Result<T>(path.Input, finalOutput));
}
else
{
rejectCount++;
}
break;
}
else
{
path.Input.Grow(1 + path.Input.Length);
path.Input.Int32s[path.Input.Length] = path.Arc.Label;
path.Input.Length++;
path.Cost = fst.Outputs.Add(path.Cost, path.Arc.Output);
}
}
}
return new TopResults<T>(rejectCount + topN <= maxQueueDepth, results);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected virtual bool AcceptResult(Int32sRef input, T output)
{
return true;
}
}
/// <summary>
/// Holds a single input (<see cref="Int32sRef"/>) + output, returned by
/// <see cref="ShortestPaths"/>.
/// </summary>
public sealed class Result<T> where T : class // LUCENENET specific - added class constraint because we compare reference equality
{
public Int32sRef Input { get; private set; }
public T Output { get; private set; }
public Result(Int32sRef input, T output)
{
this.Input = input;
this.Output = output;
}
}
/// <summary>
/// Holds the results for a top N search using <see cref="TopNSearcher{T}"/>
/// </summary>
public sealed class TopResults<T> : IEnumerable<Result<T>> where T : class // LUCENENET specific - added class constraint, since we compare reference equality
{
/// <summary>
/// <c>true</c> iff this is a complete result ie. if
/// the specified queue size was large enough to find the complete list of results. this might
/// be <c>false</c> if the <see cref="TopNSearcher{T}"/> rejected too many results.
/// </summary>
public bool IsComplete { get; private set; }
/// <summary>
/// The top results
/// </summary>
public IList<Result<T>> TopN { get; private set; }
internal TopResults(bool isComplete, IList<Result<T>> topN)
{
this.TopN = topN;
this.IsComplete = isComplete;
}
public IEnumerator<Result<T>> GetEnumerator()
{
return TopN.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
/// <summary>
/// Starting from node, find the top N min cost
/// completions to a final node.
/// </summary>
public static TopResults<T> ShortestPaths<T>(FST<T> fst, FST.Arc<T> fromNode, T startOutput, IComparer<T> comparer, int topN, bool allowEmptyString)
where T : class // LUCENENET specific - added class constraint, since we compare reference equality
{
// All paths are kept, so we can pass topN for
// maxQueueDepth and the pruning is admissible:
TopNSearcher<T> searcher = new TopNSearcher<T>(fst, topN, topN, comparer);
// since this search is initialized with a single start node
// it is okay to start with an empty input path here
searcher.AddStartPaths(fromNode, startOutput, allowEmptyString, new Int32sRef());
return searcher.Search();
}
/// <summary>
/// Dumps an <see cref="FST{T}"/> to a GraphViz's <c>dot</c> language description
/// for visualization. Example of use:
///
/// <code>
/// using (TextWriter sw = new StreamWriter("out.dot"))
/// {
/// Util.ToDot(fst, sw, true, true);
/// }
/// </code>
///
/// and then, from command line:
///
/// <code>
/// dot -Tpng -o out.png out.dot
/// </code>
///
/// <para/>
/// Note: larger FSTs (a few thousand nodes) won't even
/// render, don't bother. If the FST is > 2.1 GB in size
/// then this method will throw strange exceptions.
/// <para/>
/// See also <a href="http://www.graphviz.org/">http://www.graphviz.org/</a>.
/// </summary>
/// <param name="sameRank">
/// If <c>true</c>, the resulting <c>dot</c> file will try
/// to order states in layers of breadth-first traversal. This may
/// mess up arcs, but makes the output FST's structure a bit clearer.
/// </param>
/// <param name="labelStates">
/// If <c>true</c> states will have labels equal to their offsets in their
/// binary format. Expands the graph considerably.
/// </param>
public static void ToDot<T>(FST<T> fst, TextWriter @out, bool sameRank, bool labelStates) where T : class // LUCENENET specific - added class constraint, since we compare reference equality
{
const string expandedNodeColor = "blue";
// this is the start arc in the automaton (from the epsilon state to the first state
// with outgoing transitions.
FST.Arc<T> startArc = fst.GetFirstArc(new FST.Arc<T>());
// A queue of transitions to consider for the next level.
IList<FST.Arc<T>> thisLevelQueue = new JCG.List<FST.Arc<T>>();
// A queue of transitions to consider when processing the next level.
IList<FST.Arc<T>> nextLevelQueue = new JCG.List<FST.Arc<T>>
{
startArc
};
//System.out.println("toDot: startArc: " + startArc);
// A list of states on the same level (for ranking).
IList<int> sameLevelStates = new JCG.List<int>();
// A bitset of already seen states (target offset).
BitSet seen = new BitSet();
seen.Set((int)startArc.Target);
// Shape for states.
const string stateShape = "circle";
const string finalStateShape = "doublecircle";
// Emit DOT prologue.
@out.Write("digraph FST {\n");
@out.Write(" rankdir = LR; splines=true; concentrate=true; ordering=out; ranksep=2.5; \n");
if (!labelStates)
{
@out.Write(" node [shape=circle, width=.2, height=.2, style=filled]\n");
}
EmitDotState(@out, "initial", "point", "white", "");
T NO_OUTPUT = fst.Outputs.NoOutput;
var r = fst.GetBytesReader();
// final FST.Arc<T> scratchArc = new FST.Arc<>();
{
string stateColor;
if (fst.IsExpandedTarget(startArc, r))
{
stateColor = expandedNodeColor;
}
else
{
stateColor = null;
}
bool isFinal;
T finalOutput;
if (startArc.IsFinal)
{
isFinal = true;
finalOutput = startArc.NextFinalOutput == NO_OUTPUT ? null : startArc.NextFinalOutput;
}
else
{
isFinal = false;
finalOutput = null;
}
EmitDotState(@out, Convert.ToString(startArc.Target), isFinal ? finalStateShape : stateShape, stateColor, finalOutput is null ? "" : fst.Outputs.OutputToString(finalOutput));
}
@out.Write(" initial -> " + startArc.Target + "\n");
int level = 0;
while (nextLevelQueue.Count > 0)
{
// we could double buffer here, but it doesn't matter probably.
//System.out.println("next level=" + level);
thisLevelQueue.AddRange(nextLevelQueue);
nextLevelQueue.Clear();
level++;
@out.Write("\n // Transitions and states at level: " + level + "\n");
while (thisLevelQueue.Count > 0)
{
FST.Arc<T> arc = thisLevelQueue[thisLevelQueue.Count - 1];
thisLevelQueue.RemoveAt(thisLevelQueue.Count - 1);
//System.out.println(" pop: " + arc);
if (FST<T>.TargetHasArcs(arc))
{
// scan all target arcs
//System.out.println(" readFirstTarget...");
long node = arc.Target;
fst.ReadFirstRealTargetArc(arc.Target, arc, r);
//System.out.println(" firstTarget: " + arc);
while (true)
{
//System.out.println(" cycle arc=" + arc);
// Emit the unseen state and add it to the queue for the next level.
if (arc.Target >= 0 && !seen.Get((int)arc.Target))
{
/*
boolean isFinal = false;
T finalOutput = null;
fst.readFirstTargetArc(arc, scratchArc);
if (scratchArc.isFinal() && fst.targetHasArcs(scratchArc)) {
// target is final
isFinal = true;
finalOutput = scratchArc.output == NO_OUTPUT ? null : scratchArc.output;
System.out.println("dot hit final label=" + (char) scratchArc.label);
}
*/
string stateColor;
if (fst.IsExpandedTarget(arc, r))
{
stateColor = expandedNodeColor;
}
else
{
stateColor = null;
}
string finalOutput;
if (arc.NextFinalOutput != null && arc.NextFinalOutput != NO_OUTPUT)
{
finalOutput = fst.Outputs.OutputToString(arc.NextFinalOutput);
}
else
{
finalOutput = "";
}
EmitDotState(@out, Convert.ToString(arc.Target, CultureInfo.InvariantCulture), stateShape, stateColor, finalOutput);
// To see the node address, use this instead:
//emitDotState(out, Integer.toString(arc.target), stateShape, stateColor, String.valueOf(arc.target));
seen.Set((int)arc.Target);
nextLevelQueue.Add((new FST.Arc<T>()).CopyFrom(arc));
sameLevelStates.Add((int)arc.Target);
}
string outs;
if (arc.Output != NO_OUTPUT)
{
outs = "/" + fst.Outputs.OutputToString(arc.Output);
}
else
{
outs = "";
}
if (!FST<T>.TargetHasArcs(arc) && arc.IsFinal && arc.NextFinalOutput != NO_OUTPUT)
{
// Tricky special case: sometimes, due to
// pruning, the builder can [sillily] produce
// an FST with an arc into the final end state
// (-1) but also with a next final output; in
// this case we pull that output up onto this
// arc
outs = outs + "/[" + fst.Outputs.OutputToString(arc.NextFinalOutput) + "]";
}
string arcColor;
if (arc.Flag(FST.BIT_TARGET_NEXT))
{
arcColor = "red";
}
else
{
arcColor = "black";
}
if (Debugging.AssertsEnabled) Debugging.Assert(arc.Label != FST.END_LABEL);
@out.Write(" " + node + " -> " + arc.Target + " [label=\"" + PrintableLabel(arc.Label) + outs + "\"" + (arc.IsFinal ? " style=\"bold\"" : "") + " color=\"" + arcColor + "\"]\n");
// Break the loop if we're on the last arc of this state.
if (arc.IsLast)
{
//System.out.println(" break");
break;
}
fst.ReadNextRealArc(arc, r);
}
}
}
// Emit state ranking information.
if (sameRank && sameLevelStates.Count > 1)
{
@out.Write(" {rank=same; ");
foreach (int state in sameLevelStates)
{
@out.Write(state + "; ");
}
@out.Write(" }\n");
}
sameLevelStates.Clear();
}
// Emit terminating state (always there anyway).
@out.Write(" -1 [style=filled, color=black, shape=doublecircle, label=\"\"]\n\n");
@out.Write(" {rank=sink; -1 }\n");
@out.Write("}\n");
@out.Flush();
}
/// <summary>
/// Emit a single state in the <c>dot</c> language.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void EmitDotState(TextWriter @out, string name, string shape, string color, string label)
{
@out.Write(" " + name
+ " ["
+ (shape != null ? "shape=" + shape : "") + " "
+ (color != null ? "color=" + color : "") + " "
+ (label != null ? "label=\"" + label + "\"" : "label=\"\"") + " "
+ "]\n");
}
/// <summary>
/// Ensures an arc's label is indeed printable (dot uses US-ASCII).
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static string PrintableLabel(int label)
{
// Any ordinary ascii character, except for " or \, are
// printed as the character; else, as a hex string:
if (label >= 0x20 && label <= 0x7d && label != 0x22 && label != 0x5c) // " OR \
{
return char.ToString((char)label);
}
return "0x" + label.ToString("x", CultureInfo.InvariantCulture);
}
/// <summary>
/// Just maps each UTF16 unit (char) to the <see cref="int"/>s in an
/// <see cref="Int32sRef"/>.
/// </summary>
public static Int32sRef ToUTF16(string s, Int32sRef scratch)
{
int charLimit = s.Length;
scratch.Offset = 0;
scratch.Length = charLimit;
scratch.Grow(charLimit);
for (int idx = 0; idx < charLimit; idx++)
{
scratch.Int32s[idx] = (int)s[idx];
}
return scratch;
}
/// <summary>
/// Decodes the Unicode codepoints from the provided
/// <see cref="ICharSequence"/> and places them in the provided scratch
/// <see cref="Int32sRef"/>, which must not be <c>null</c>, returning it.
/// </summary>
public static Int32sRef ToUTF32(string s, Int32sRef scratch)
{
int charIdx = 0;
int intIdx = 0;
int charLimit = s.Length;
while (charIdx < charLimit)
{
scratch.Grow(intIdx + 1);
int utf32 = Character.CodePointAt(s, charIdx);
scratch.Int32s[intIdx] = utf32;
charIdx += Character.CharCount(utf32);