/// <summary> /// Creates a uniform distribution over any string starting and ending with a non-word character, /// with a length in given bounds. /// Characters other than the first and the last are restricted to be non-zero probability characters /// from a given distribution. /// </summary> /// <param name="minLength">The minimum allowed string length.</param> /// <param name="maxLength">The maximum allowed string length.</param> /// <param name="allowedChars">The distribution representing allowed characters.</param> /// <returns>The created distribution.</returns> public static StringDistribution WordMiddle(int minLength, int maxLength, ImmutableDiscreteChar allowedChars) { if (maxLength < minLength) { throw new ArgumentException("The maximum length cannot be less than the minimum length."); } if (minLength < 1) { throw new ArgumentException("The minimum length must be at least one."); } var nonWordChar = StringDistribution.Char(NonWordCharacter); if ((minLength == 1) && (maxLength == 1)) { return(nonWordChar); } // TODO: make a PartialUniform copy of allowedChars var suffix = StringDistribution.Repeat(allowedChars, minTimes: Math.Max(minLength - 2, 0), maxTimes: maxLength - 2); suffix.AppendInPlace(nonWordChar); if (minLength == 1) { var allowedChar = allowedChars.GetMode(); var allowedSuffix = new string(Enumerable.Repeat(allowedChar, Math.Max(minLength - 2, 0)).ToArray()) + ' '; var suffixLogProb = suffix.GetLogProb(allowedSuffix); suffix.SetToSumLog(suffixLogProb, StringDistribution.Empty(), 0.0, suffix); } return(nonWordChar + suffix); }
/// <summary> /// Creates a uniform distribution over strings of whitespace characters (see <see cref="DiscreteChar.Whitespace"/>), /// with length within the given bounds. /// If <paramref name="maxLength"/> is set to <see langword="null"/>, /// there will be no upper bound on the length, and the resulting distribution will thus be improper. /// </summary> /// <param name="minLength">The minimum possible string length. Defaults to 1.</param> /// <param name="maxLength"> /// The maximum possible sequence length, or <see langword="null"/> for no upper bound on length. /// Defaults to <see langword="null"/>. /// </param> /// <returns>The created distribution.</returns> public static StringDistribution Whitespace(int minLength = 1, int?maxLength = null) { return(StringDistribution.Repeat(DiscreteChar.Whitespace(), minLength, maxLength)); }
/// <summary> /// Creates a uniform distribution over strings of word characters (see <see cref="DiscreteChar.WordChar"/>), /// with length within the given bounds. /// If <paramref name="maxLength"/> is set to <see langword="null"/>, /// there will be no upper bound on the length, and the resulting distribution will thus be improper. /// </summary> /// <param name="minLength">The minimum possible string length. Defaults to 1.</param> /// <param name="maxLength"> /// The maximum possible sequence length, or <see langword="null"/> for no upper bound on length. /// Defaults to <see langword="null"/>. /// </param> /// <returns>The created distribution.</returns> public static StringDistribution WordChars(int minLength = 1, int?maxLength = null) { return(StringDistribution.Repeat(DiscreteChar.WordChar(), minLength, maxLength)); }
/// <summary> /// Creates a uniform distribution over strings of digits, lowercase and uppercase letters, with length within the given bounds. /// If <paramref name="maxLength"/> is set to <see langword="null"/>, /// there will be no upper bound on the length, and the resulting distribution will thus be improper. /// </summary> /// <param name="minLength">The minimum possible string length. Defaults to 1.</param> /// <param name="maxLength"> /// The maximum possible sequence length, or <see langword="null"/> for no upper bound on length. /// Defaults to <see langword="null"/>. /// </param> /// <returns>The created distribution.</returns> public static StringDistribution LettersOrDigits(int minLength = 1, int?maxLength = null) { return(StringDistribution.Repeat(DiscreteChar.LetterOrDigit(), minLength, maxLength)); }
/// <summary> /// Creates a uniform distribution over strings of digits, with length within the given bounds. /// If <paramref name="maxLength"/> is set to <see langword="null"/>, /// there will be no upper bound on the length, and the resulting distribution will thus be improper. /// </summary> /// <param name="minLength">The minimum possible string length. Defaults to 1.</param> /// <param name="maxLength"> /// The maximum possible sequence length, or <see langword="null"/> for no upper bound on length. /// Defaults to <see langword="null"/>. /// </param> /// <param name="uniformity">The type of uniformity. Defaults to <see cref="DistributionKind.UniformOverValue"/></param> /// <returns>The created distribution.</returns> public static StringDistribution Digits(int minLength = 1, int?maxLength = null, DistributionKind uniformity = DistributionKind.UniformOverValue) { return(StringDistribution.Repeat(DiscreteChar.Digit(), minLength, maxLength, uniformity)); }
/// <summary> /// Creates a uniform distribution over strings of lowercase and uppercase letters, with length in given bounds. /// If <paramref name="maxLength"/> is set to <see langword="null"/>, /// there will be no upper bound on the length, and the resulting distribution will thus be improper. /// </summary> /// <param name="minLength">The minimum possible string length. Defaults to 1.</param> /// <param name="maxLength"> /// The maximum possible sequence length, or <see langword="null"/> for no upper bound on length. /// Defaults to <see langword="null"/>. /// </param> /// <returns>The created distribution.</returns> public static StringDistribution Letters(int minLength = 1, int?maxLength = null) { return(StringDistribution.Repeat(ImmutableDiscreteChar.Letter(), minLength, maxLength)); }