コード例 #1
0
        /// <summary>
        /// Learns the transition probabilities from the specified training set.
        /// </summary>
        /// <param name="transition">The transition matrix.</param>
        /// <param name="trainingSet">The training set.</param>
        private void Learn([NotNull] TransitionMatrix transition, [NotNull] IEnumerable <IList <LabeledObservation> > trainingSet)
        {
            // generate bigrams from each training sentence and
            // group these pairs by the left item's state
            var pairsGroupedByLeftState = trainingSet
                                          .SelectMany(set => set.Pairwise())
                                          .GroupBy(pair => pair.Left.State);

            // for each left state, group the items by the right states
            foreach (var pairsOfSameStartingClass in pairsGroupedByLeftState)
            {
                var leftState = pairsOfSameStartingClass.Key;
                var pairsOfSameStartingStateGroupedByRightState =
                    pairsOfSameStartingClass.GroupBy(pair => pair.Right.State);
                var countOfSameStartingState = pairsOfSameStartingClass.Count();

                // determine each probability P(left -> right) by dividing
                // the occurrence count of each right state by the total number of all
                // occurrences starting in the left state
                foreach (var grouping in pairsOfSameStartingStateGroupedByRightState)
                {
                    var rightState  = grouping.Key;
                    var occurrences = grouping.Count();
                    var percentage  = (double)occurrences / countOfSameStartingState;

                    transition.SetTransition(leftState, rightState, percentage);
                }
            }
        }
コード例 #2
0
 /// <summary>
 /// Learns the parameters for the given system.
 /// </summary>
 /// <param name="initial">The initial.</param>
 /// <param name="transition">The transition.</param>
 /// <param name="emission">The emission.</param>
 /// <param name="trainingSet">The training set.</param>
 public void Learn([NotNull] InitialStateMatrix initial, [NotNull] TransitionMatrix transition,
                   [NotNull] EmissionMatrix emission,
                   [NotNull] IList <IList <LabeledObservation> > trainingSet)
 {
     Learn(initial, trainingSet);
     Learn(transition, trainingSet);
     Learn(emission, trainingSet);
 }
コード例 #3
0
ファイル: HiddenMarkovModel.cs プロジェクト: zhonghai66/hmm
 /// <summary>
 /// Initializes a new instance of the <see cref="HiddenMarkovModel" /> class.
 /// </summary>
 /// <param name="states">The states.</param>
 /// <param name="inital">The inital state matrix.</param>
 /// <param name="transition">The transmission matrix.</param>
 /// <param name="emission">The emission matrix.</param>
 public HiddenMarkovModel(
     [NotNull] ReadOnlyCollection <IState> states,
     [NotNull] InitialStateMatrix inital,
     [NotNull] TransitionMatrix transition,
     [NotNull] EmissionMatrix emission)
 {
     _states     = states;
     _inital     = inital;
     _transition = transition;
     _emission   = emission;
 }