/// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="StringOfLengthOp"]/message_doc[@name="StrAverageConditional(DiscreteChar, Discrete)"]/*'/> public static StringDistribution StrAverageConditional(DiscreteChar allowedChars, Discrete length) { Argument.CheckIfNotNull(length, "length"); Argument.CheckIfValid(allowedChars.IsPartialUniform(), "allowedChars", "The set of allowed characters must be passed as a partial uniform distribution."); double logNormalizer = allowedChars.GetLogAverageOf(allowedChars); var oneCharacter = StringAutomaton.ConstantOnElementLog(logNormalizer, allowedChars.WrappedDistribution); var manyCharacters = StringAutomaton.Repeat(oneCharacter, length.GetWorkspace()); return(StringDistribution.FromWeightFunction(manyCharacters)); }
public static StringDistribution EmptyOrStartsWith(DiscreteChar charsInMainString, DiscreteChar 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())); }
/// <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(DiscreteChar allowedChars, DiscreteChar?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())); }