/// <summary> /// Converts the current <see cref="StringDictionaryWeightFunction"/> to a deterministic automaton, /// compressing shared suffixes. When the current <see cref="StringDictionaryWeightFunction"/> is /// normalized, the resulting automaton is stochastic. /// </summary> /// <returns>A deterministic <see cref="StringAutomaton"/>.</returns> public override StringAutomaton AsAutomaton() { var dict = (SortedList <string, Weight>)Dictionary; if (dict.Count == 0) { return(Automaton <string, char, ImmutableDiscreteChar, StringManipulator, StringAutomaton> .Zero()); } if (dict.Count == 1) { return(Automaton <string, char, ImmutableDiscreteChar, StringManipulator, StringAutomaton> .ConstantOnLog(dict.Values[0].LogValue, dict.Keys[0])); } var result = new Automaton <string, char, ImmutableDiscreteChar, StringManipulator, StringAutomaton> .Builder(); var end = result .AddState() .SetEndWeight(Weight.One); var sharedPrefixWithPreviousLength = new int[dict.Count]; sharedPrefixWithPreviousLength[0] = 0; for (int i = 1; i < dict.Count; ++i) { sharedPrefixWithPreviousLength[i] = GetSharedPrefixLength(dict.Keys[i - 1], dict.Keys[i]); } var suffixInfo = new (int suffixLength, int prefixEndStateIndex, char suffixTransitionChar, Weight suffixTransitionWeight)[dict.Count];