コード例 #1
0
        public static StringDistribution EmptyOrStartsWith(ImmutableDiscreteChar charsInMainString, ImmutableDiscreteChar startsWith)
        {
            // TODO: fix equality and then use factory methods to create this
            var result = new StringAutomaton.Builder();

            result.Start.SetEndWeight(Weight.One);
            var otherState = result.Start.AddTransition(startsWith, Weight.FromLogValue(-startsWith.GetLogAverageOf(startsWith)));

            otherState.AddSelfTransition(charsInMainString, Weight.FromLogValue(-charsInMainString.GetLogAverageOf(charsInMainString)));
            otherState.SetEndWeight(Weight.One);

            return(StringDistribution.FromWeightFunction(result.GetAutomaton()));
        }
コード例 #2
0
        /// <summary>
        /// Creates a uniform distribution over any string starting and ending with a non-word character.
        /// Characters other than the first and the last are restricted to be non-zero probability characters
        /// from a given distribution.
        /// </summary>
        /// <param name="allowedChars">The distribution representing allowed characters.</param>
        /// <param name="nonWordCharacter">The word separating characters.</param>
        /// <returns>The created distribution.</returns>
        public static StringDistribution WordMiddle(ImmutableDiscreteChar allowedChars, ImmutableDiscreteChar?nonWordCharacter = null)
        {
            // TODO: fix equality and then use factory methods to create this
            nonWordCharacter = nonWordCharacter ?? NonWordCharacter;
            var result      = new StringAutomaton.Builder();
            var otherState1 = result.Start.AddTransition(
                Option.FromNullable(nonWordCharacter),
                Weight.FromLogValue(-nonWordCharacter.Value.GetLogAverageOf(nonWordCharacter.Value)));

            otherState1.SetEndWeight(Weight.One);
            var otherState2 = otherState1.AddEpsilonTransition(Weight.One)
                              .AddSelfTransition(allowedChars, Weight.FromLogValue(-allowedChars.GetLogAverageOf(allowedChars))).AddTransition(
                Option.FromNullable(nonWordCharacter),
                Weight.FromLogValue(-nonWordCharacter.Value.GetLogAverageOf(nonWordCharacter.Value)));

            otherState2.SetEndWeight(Weight.One);

            return(StringDistribution.FromWeightFunction(result.GetAutomaton()));
        }