예제 #1
0
        /**
         * Finds or loads the NGram probability of the given NGram.
         *
         * @param wordSequence the NGram to load
         * @return a NGramProbability of the given NGram
         */
        private NGramProbability FindNGram(WordSequence wordSequence)
        {
            var numberWords        = wordSequence.Size;
            NGramProbability nGram = null;

            var oldest      = wordSequence.GetOldest();
            var nGramBuffer = _loadedNGramBuffers[numberWords - 1].Get(oldest);

            if (nGramBuffer == null)
            {
                nGramBuffer = GetNGramBuffer(oldest);
                if (nGramBuffer != null)
                {
                    _loadedNGramBuffers[numberWords - 1].Add(oldest, nGramBuffer);
                }
            }

            if (nGramBuffer != null)
            {
                var nthWordID = GetWordID(wordSequence.GetWord(numberWords - 1));
                nGram = nGramBuffer.FindNGram(nthWordID);
            }

            return(nGram);
        }
예제 #2
0
        /**
         * /// Finds the NGram probabilities for the given nth word in a NGram.
         *
         * /// @param nthWordID the ID of the nth word
         * /// @return the NGramProbability of the given nth word
         */
        public override NGramProbability FindNGram(int nthWordID)
        {
            int mid, start = 0, end = NumberNGrams;
            NGramProbability ngram = null;

            while ((end - start) > 0)
            {
                mid = (start + end) / 2;
                var midWordID = GetWordID(mid);
                if (midWordID < nthWordID)
                {
                    start = mid + 1;
                }
                else if (midWordID > nthWordID)
                {
                    end = mid;
                }
                else
                {
                    ngram = GetNGramProbability(mid);
                    break;
                }
            }

            return(ngram);
        }
예제 #3
0
        /**
         * Returns the index of the first NGram entry of the given N-1Gram
         *
         * @param nMinus1Gram the N-1Gram which first NGram entry we're looking for
         * @param firstNMinus1GramEntry the index of the first N-1Gram entry of the
         *        N-1Gram in question
         * @param n the order of the NGram
         * @return the index of the first NGram entry of the given N-1Gram
         */
        private int GetFirstNGramEntry(NGramProbability nMinus1Gram, int firstNMinus1GramEntry, int n)
        {
            var firstNGramEntry =
                _ngramSegmentTable[n - 1][(firstNMinus1GramEntry + nMinus1Gram.WhichFollower) >> _loader.GetLogNGramSegmentSize()]
                + nMinus1Gram.FirstNPlus1GramEntry;

            return(firstNGramEntry);
        }