private void BuildFromHomogenousNGrams(INGrams <T> grams, NGram <T>[] array, NGramDistribution <T> distrubution)
        {
            var blockedGrams = NGramHomogenousMatrixMarkovChainHelper <T> .BuildBlockedNGrams(grams.ToArray());

            var transitionMatrix = Matrix <float> .Build.Sparse(
                array.Length,
                array.Length,
                new Func <int, int, float>(
                    (x, y) => NGramHomogenousMatrixMarkovChainHelper <T> .CalculateProbability(blockedGrams, distrubution, array, x, y)));

            int rowIndex = 0;

            foreach (var row in transitionMatrix.EnumerateRows())
            {
                this.graph.Add(array[rowIndex], new List <NonRecursiveNGramProbabilisticEdge <T> >());

                for (int i = 0; i < row.Count; i++)
                {
                    if (row[i] != 0)
                    {
                        this.graph[array[rowIndex]].Add(new NonRecursiveNGramProbabilisticEdge <T>(row[i], array[i]));
                    }
                }
                rowIndex++;
            }
        }
Beispiel #2
0
        public HomogenousNGramMatrixMarkovChain(HomogenousNGrams <T> grams)
        {
            grams.NullCheck();
            (grams as IHeterogenousNGrams <T> == null).AssertTrue();

            var array        = new HashSet <NGram <T> >(grams).ToArray();
            var blockedGrams = NGramHomogenousMatrixMarkovChainHelper <T> .BuildBlockedNGrams(grams.ToArray());

            this.distrubution = new NGramDistribution <T>(grams);

            this.transitionMatrix = Matrix <float> .Build.Sparse(
                array.Length,
                array.Length,
                new Func <int, int, float>(
                    (x, y) => NGramHomogenousMatrixMarkovChainHelper <T> .CalculateProbability(blockedGrams, distrubution, array, x, y)));

            this.indices = new Dictionary <int, NGram <T> >();

            for (int i = 0; i < array.Length; i++)
            {
                this.indices.Add(i, array[i]);
            }
        }