예제 #1
0
        /// <summary>
        /// Creates a transducer <c>T(a, b) = sum_i Ti(a, b)</c>, where <c>Ti(a, b)</c> is an element of a given transducer collection.
        /// </summary>
        /// <param name="transducers">The transducers to sum.</param>
        /// <returns>The created transducer.</returns>
        public static TThis Sum(IEnumerable <TThis> transducers)
        {
            Argument.CheckIfNotNull(transducers, "transducers");

            return(new TThis {
                sequencePairToWeight = PairListAutomaton.Sum(transducers.Select(t => t.sequencePairToWeight))
            });
        }
예제 #2
0
        /// <summary>
        /// Creates a transducer <c>T'(a, b) = sum_{k=Kmin}^{Kmax} sum_{a1 a2 ... ak = a} sum_{b1 b2 ... bk = b} T(a1, b1)T(a2, b2)...T(ak, bk)</c>,
        /// where <c>T(a, b)</c> is a given transducer, and <c>Kmin</c> and <c>Kmax</c> are the minimum
        /// and the maximum number of factors in a sum term.
        /// </summary>
        /// <param name="transducer">The transducer.</param>
        /// <param name="minTimes">The minimum number of factors in a sum term. Defaults to 1.</param>
        /// <param name="maxTimes">An optional maximum number of factors in a sum term.</param>
        /// <returns>The created transducer.</returns>
        /// <remarks>
        /// The result is the sum of Cauchy products of the given transducer with itself,
        /// each product having a different number of factors.
        /// </remarks>
        public static TThis Repeat(TThis transducer, int minTimes = 1, int?maxTimes = null)
        {
            Argument.CheckIfNotNull(transducer, "transducer");

            return(new TThis {
                sequencePairToWeight = PairListAutomaton.Repeat(transducer.sequencePairToWeight, minTimes, maxTimes)
            });
        }
예제 #3
0
        /// <summary>
        /// Creates a transducer <c>T'(a, b) = T(a, b) + I[a = "", b = ""]</c>, where <c>T(a, b)</c> is a given transducer.
        /// </summary>
        /// <param name="transducer">The transducer <c>U(a, b)</c>.</param>
        /// <returns>The created transducer.</returns>
        public static TThis Optional(TThis transducer)
        {
            Argument.CheckIfNotNull(transducer, "transducer");

            var emptySequence = new List <Pair <Option <TSrcElement>, Option <TDestElement> > >();

            return(Sum(
                       transducer,
                       new TThis {
                sequencePairToWeight = PairListAutomaton.ConstantOnLog(0.0, emptySequence)
            }));
        }
예제 #4
0
        /// <summary>
        /// Creates a transducer <c>T(a, b) = f(a) I[b = ""]</c>, where <c>f(a)</c> is a given automaton.
        /// </summary>
        /// <param name="srcAutomaton">The automaton defining weights for the first transducer argument.</param>
        /// <returns>The created transducer.</returns>
        public static TThis Consume(TSrcAutomaton srcAutomaton)
        {
            Argument.CheckIfNotNull(srcAutomaton, nameof(srcAutomaton));

            return(new TThis
            {
                sequencePairToWeight = srcAutomaton.ApplyFunction <
                    List <(Option <TSrcElement>, Option <TDestElement>)>,
                    (Option <TSrcElement>, Option <TDestElement>),
                    TPairDistribution,
                    ListManipulator <List <(Option <TSrcElement>, Option <TDestElement>)>, (Option <TSrcElement>, Option <TDestElement>)>,
                    PairListAutomaton>(
                    (dist, weight, group) => ValueTuple.Create(
                        dist.HasValue
                        ? Option.Some(ImmutablePairDistributionBase <TSrcElement, TSrcElementDistribution, TDestElement, TDestElementDistribution, TPairDistribution> .FromFirst(dist))
                        : Option.None,
                        weight))
            });