Exemplo n.º 1
0
        public FlatLinguist(AcousticModel acousticModel, Grammar grammar, UnitManager unitManager,
                            double wordInsertionProbability, double silenceInsertionProbability, double fillerInsertionProbability,
                            double unitInsertionProbability, float languageWeight, Boolean dumpGStates, Boolean showCompilationProgress,
                            Boolean spreadWordProbabilitiesAcrossPronunciations, Boolean addOutOfGrammarBranch,
                            double outOfGrammarBranchProbability, double phoneInsertionProbability, AcousticModel phoneLoopAcousticModel)
        {
            _acousticModel = acousticModel;
            LogMath        = LogMath.GetLogMath();
            Grammar        = grammar;
            UnitManager    = unitManager;

            _logWordInsertionProbability   = LogMath.LinearToLog(wordInsertionProbability);
            LogSilenceInsertionProbability = LogMath.LinearToLog(silenceInsertionProbability);
            _logFillerInsertionProbability = LogMath.LinearToLog(fillerInsertionProbability);
            _logUnitInsertionProbability   = LogMath.LinearToLog(unitInsertionProbability);
            _languageWeight = languageWeight;

            _dumpGStates             = dumpGStates;
            _showCompilationProgress = showCompilationProgress;
            _spreadWordProbabilitiesAcrossPronunciations = spreadWordProbabilitiesAcrossPronunciations;

            AddOutOfGrammarBranch = addOutOfGrammarBranch;

            if (addOutOfGrammarBranch)
            {
                LogOutOfGrammarBranchProbability = LogMath.LinearToLog(outOfGrammarBranchProbability);
                LogPhoneInsertionProbability     = LogMath.LinearToLog(phoneInsertionProbability);
                PhoneLoopAcousticModel           = phoneLoopAcousticModel;
            }

            Name = null;
        }
Exemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GaussianMixture"/> class.
 /// </summary>
 /// <param name="mixtureWeights">The mixture weights for this senone in LogMath log base.</param>
 /// <param name="mixtureComponents">The mixture components for this senone.</param>
 /// <param name="id">The identifier.</param>
 public GaussianMixture(GaussianWeights mixtureWeights, MixtureComponent[] mixtureComponents, int id)
 {
     LogMath            = LogMath.GetLogMath();
     _mixtureComponents = mixtureComponents;
     MixtureWeights     = mixtureWeights;
     _Id = id;
 }
Exemplo n.º 3
0
        /**
         * /// Create a MixtureComponent with the given sub components.
         *
         * /// @param mean                         the mean vector for this PDF
         * /// @param meanTransformationMatrix     transformation matrix for this pdf
         * /// @param meanTransformationVector     transform vector for this PDF
         * /// @param variance                     the variance for this PDF
         * /// @param varianceTransformationMatrix var. transform matrix for this PDF
         * /// @param varianceTransformationVector var. transform vector for this PDF
         * /// @param distFloor                    the lowest score value (in linear domain)
         * /// @param varianceFloor                the lowest value for the variance
         */
        public MixtureComponent(
            float[] mean,
            float[][] meanTransformationMatrix,
            float[] meanTransformationVector,
            float[] variance,
            float[][] varianceTransformationMatrix,
            float[] varianceTransformationVector,
            float distFloor,
            float varianceFloor)
        {
            Debug.Assert(variance.Length == mean.Length);

            LogMath = LogMath.GetLogMath();
            Mean    = mean;
            _meanTransformationMatrix = meanTransformationMatrix;
            _meanTransformationVector = meanTransformationVector;
            Variance = variance;
            _varianceTransformationMatrix = varianceTransformationMatrix;
            _varianceTransformationVector = varianceTransformationVector;

            Debug.Assert(distFloor >= 0.0, "distFloot seems to be already in log-domain");
            this.DistFloor = LogMath.LinearToLog(distFloor);
            _varianceFloor = varianceFloor;

            TransformStats();

            LogPreComputedGaussianFactor = PrecomputeDistance();
        }
Exemplo n.º 4
0
 public SimpleWordListGrammar(String path, Boolean isLooping, Boolean showGrammar, Boolean optimizeGrammar, Boolean addSilenceWords, Boolean addFillerWords, IDictionary dictionary)
     : base(showGrammar, optimizeGrammar, addSilenceWords, addFillerWords, dictionary)
 {
     this._path      = path;
     this._isLooping = isLooping;
     _logMath        = LogMath.GetLogMath();
 }
Exemplo n.º 5
0
        /*
         * /// (non-Javadoc)
         *
         * /// @see edu.cmu.sphinx.util.props.Configurable#newProperties(edu.cmu.sphinx.util.props.PropertySheet)
         */
        public override void NewProperties(PropertySheet ps)
        {
            _logMath = LogMath.GetLogMath();

            _acousticModel = (AcousticModel)ps.GetComponent(PropAcousticModel);
            _unitManager   = (UnitManager)ps.GetComponent(PropUnitManager);
            LanguageModel  = (LanguageModel)ps.GetComponent(PropLanguageModel);
            Dictionary     = (IDictionary)ps.GetComponent(PropDictionary);

            //fullWordHistories = ps.getBoolean(PROP_FULL_WORD_HISTORIES);
            _wantUnigramSmear               = ps.GetBoolean(PropWantUnigramSmear);
            _logWordInsertionProbability    = _logMath.LinearToLog(ps.GetDouble(PropWordInsertionProbability));
            _logSilenceInsertionProbability = _logMath.LinearToLog(ps.GetDouble(PropSilenceInsertionProbability));
            _logFillerInsertionProbability  = _logMath.LinearToLog(ps.GetDouble(PropFillerInsertionProbability));
            _logUnitInsertionProbability    = _logMath.LinearToLog(ps.GetDouble(PropUnitInsertionProbability));
            LanguageWeight      = ps.GetFloat(PropLanguageWeight);
            AddFillerWords      = (ps.GetBoolean(PropAddFillerWords));
            GenerateUnitStates  = (ps.GetBoolean(PropGenerateUnitStates));
            _unigramSmearWeight = ps.GetFloat(PropUnigramSmearWeight);
            _maxArcCacheSize    = ps.GetInt(PropCacheSize);

            CacheEnabled = _maxArcCacheSize > 0;
            if (CacheEnabled)
            {
                ArcCache = new LRUCache <LexTreeState, ISearchStateArc[]>(_maxArcCacheSize);
            }
        }
Exemplo n.º 6
0
        public LexTreeLinguist(AcousticModel acousticModel, UnitManager unitManager,
                               LanguageModel languageModel, IDictionary dictionary, Boolean fullWordHistories, Boolean wantUnigramSmear,
                               double wordInsertionProbability, double silenceInsertionProbability, double fillerInsertionProbability,
                               double unitInsertionProbability, float languageWeight, Boolean addFillerWords, Boolean generateUnitStates,
                               float unigramSmearWeight, int maxArcCacheSize)
        {
            _acousticModel = acousticModel;
            _logMath       = LogMath.GetLogMath();
            _unitManager   = unitManager;
            LanguageModel  = languageModel;
            Dictionary     = dictionary;

            //this.fullWordHistories = fullWordHistories;
            _wantUnigramSmear               = wantUnigramSmear;
            _logWordInsertionProbability    = _logMath.LinearToLog(wordInsertionProbability);
            _logSilenceInsertionProbability = _logMath.LinearToLog(silenceInsertionProbability);
            _logFillerInsertionProbability  = _logMath.LinearToLog(fillerInsertionProbability);
            _logUnitInsertionProbability    = _logMath.LinearToLog(unitInsertionProbability);
            LanguageWeight      = languageWeight;
            AddFillerWords      = addFillerWords;
            GenerateUnitStates  = generateUnitStates;
            _unigramSmearWeight = unigramSmearWeight;
            _maxArcCacheSize    = maxArcCacheSize;

            CacheEnabled = maxArcCacheSize > 0;
            if (CacheEnabled)
            {
                ArcCache = new LRUCache <LexTreeState, ISearchStateArc[]>(maxArcCacheSize);
            }
        }
Exemplo n.º 7
0
        /*
         * /// (non-Javadoc)
         *
         * /// @see edu.cmu.sphinx.util.props.Configurable#newProperties(edu.cmu.sphinx.util.props.PropertySheet)
         */
        public override void NewProperties(PropertySheet ps)
        {
            base.NewProperties(ps);

            _path      = ps.GetString(PropPath);
            _isLooping = ps.GetBoolean(PropLoop);
            _logMath   = LogMath.GetLogMath();
        }
        public void MixtureComponent_UnivariateMeanTransformation()
        {
            const float mean = 20;
            const float var  = 0.001f;

            var gaussian = new MixtureComponent(new[] { mean }, new[] { new float[] { 2 } }, new float[] { 5 }, new[] { var }, null, null);

            Assert.IsTrue(LogMath.GetLogMath().LogToLinear(gaussian.GetScore(new[] { 2 * mean + 5 })) > 10);
        }
Exemplo n.º 9
0
 public JSGFGrammar(URL baseUrl, String grammarName, bool showGrammar, bool optimizeGrammar, bool addSilenceWords, bool addFillerWords, IDictionary dictionary)
     : base(showGrammar, optimizeGrammar, addSilenceWords, addFillerWords, dictionary)
 {
     _logMath    = LogMath.GetLogMath();
     BaseUrl     = baseUrl;
     GrammarName = grammarName;
     LoadGrammar = true;
     //logger = Logger.getLogger(getClass().getName());
 }
Exemplo n.º 10
0
 /**
  * ///
  * /// @param linguist
  * /// @param pruner
  * /// @param scorer
  * /// @param activeListFactory
  * /// @param showTokenCount
  * /// @param relativeWordBeamWidth
  * /// @param growSkipInterval
  * /// @param wantEntryPruning
  */
 public SimpleBreadthFirstSearchManager(Linguist.Linguist linguist, IPruner pruner,
                                        IAcousticScorer scorer, ActiveListFactory activeListFactory,
                                        Boolean showTokenCount, double relativeWordBeamWidth,
                                        int growSkipInterval, Boolean wantEntryPruning, int totalHmms)
 {
     _name                     = GetType().Name;
     LogMath                   = LogMath.GetLogMath();
     Linguist                  = linguist;
     _pruner                   = pruner;
     _scorer                   = scorer;
     ActiveListFactory         = activeListFactory;
     _showTokenCount           = showTokenCount;
     _growSkipInterval         = growSkipInterval;
     _wantEntryPruning         = wantEntryPruning;
     _totalHmms                = totalHmms;
     _logRelativeWordBeamWidth = LogMath.LinearToLog(relativeWordBeamWidth);
     KeepAllTokens             = true;
 }
Exemplo n.º 11
0
 /// <summary>
 ///Creates a result
 ///@param activeList the active list associated with this result
 ///@param resultList the result list associated with this result
 ///@param frameNumber the frame number for this result.
 ///@param isFinal if true, the result is a final result. This means that the last frame in the
 ///       speech segment has been decoded.
 /// <summary>
 public Result(ActiveList activeList, List <Token> resultList,
               int frameNumber, bool isFinal, bool toCreateLattice)
 {
     this.ActiveTokens     = activeList;
     this.ResultTokens     = resultList;
     FrameNumber           = frameNumber;
     _isFinal              = isFinal;
     this._toCreateLattice = toCreateLattice;
     LogMath = LogMath.GetLogMath();
 }
Exemplo n.º 12
0
 public SimpleNGramModel(URL urlLocation, IDictionary dictionary, float unigramWeight, int desiredMaxDepth)
 {
     this._urlLocation     = urlLocation;
     this._unigramWeight   = unigramWeight;
     _logMath              = LogMath.GetLogMath();
     this._desiredMaxDepth = desiredMaxDepth;
     this._dictionary      = dictionary;
     _map        = new HashMap <WordSequence, Probability>();
     _vocabulary = new HashSet <String>();
     _tokens     = new LinkedList <WordSequence>();
 }
Exemplo n.º 13
0
 /**
  * Initializes the binary loader
  *
  * @param format                    file format
  * @param applyLanguageWeightAndWip if true apply language weight and word insertion penalty
  * @param languageWeight            language weight
  * @param wip                       word insertion probability
  * @param unigramWeight             unigram weight
  */
 public BinaryLoader(String format, Boolean applyLanguageWeightAndWip, float
                     languageWeight, double wip,
                     float unigramWeight)
 {
     _startWordID = -1;
     _endWordID   = -1;
     _applyLanguageWeightAndWip = applyLanguageWeightAndWip;
     _logMath        = LogMath.GetLogMath();
     _languageWeight = languageWeight;
     _wip            = wip;
     _unigramWeight  = unigramWeight;
 }
Exemplo n.º 14
0
 public override void NewProperties(PropertySheet ps)
 {
     logMath                   = LogMath.GetLogMath();
     location                  = ConfigurationManagerUtils.GetResource(PropLocation, ps);
     ngramLogFile              = ps.GetString(PROP_QUERY_LOG_FILE);
     MaxDepth                  = ps.GetInt(LanguageModel.PropMaxDepth);
     ngramCacheSize            = ps.GetInt(PROP_NGRAM_CACHE_SIZE);
     clearCacheAfterUtterance  = ps.GetBoolean(PROP_CLEAR_CACHES_AFTER_UTTERANCE);
     dictionary                = (IDictionary)ps.GetComponent(PropDictionary);
     applyLanguageWeightAndWip = ps.GetBoolean(PROP_APPLY_LANGUAGE_WEIGHT_AND_WIP);
     languageWeight            = ps.GetFloat(PROP_LANGUAGE_WEIGHT);
     logWip        = logMath.LinearToLog(ps.GetDouble(PROP_WORD_INSERTION_PROBABILITY));
     unigramWeight = ps.GetFloat(PropUnigramWeight);
 }
Exemplo n.º 15
0
        public override void NewProperties(PropertySheet ps)
        {
            AcousticModel      = (AcousticModel)ps.GetComponent(PropAcousticModel);
            PhoneInsertionProb = LogMath.GetLogMath().LinearToLog(ps.GetFloat(PropPip));

            UseContextDependentPhones = ps.GetBoolean(PropCD);
            if (UseContextDependentPhones)
            {
                CreateContextDependentSuccessors();
            }
            else
            {
                CreateContextIndependentSuccessors();
            }
        }
Exemplo n.º 16
0
 protected void Init(URL location, string model, string dataLocatoin,
                     UnitManager unitManager, float distFloor, float mixtureWeightFloor,
                     float varianceFloor, int topGauNum, Boolean useCDUnits)
 {
     LogMath            = LogMath.GetLogMath();
     Location           = location;
     Model              = model;
     DataLocation       = dataLocatoin;
     _unitManager       = unitManager;
     DistFloor          = distFloor;
     MixtureWeightFloor = mixtureWeightFloor;
     VarianceFloor      = varianceFloor;
     _topGauNum         = topGauNum;
     UseCDUnits         = useCDUnits;
 }
Exemplo n.º 17
0
        public override void NewProperties(PropertySheet ps)
        {
            _logMath = LogMath.GetLogMath();

            if (_allocated)
            {
                throw new RuntimeException("Can't change properties after allocation");
            }

            _urlLocation     = ConfigurationManagerUtils.GetResource(PropLocation, ps);
            _unigramWeight   = ps.GetFloat(PropUnigramWeight);
            _desiredMaxDepth = ps.GetInt(PropMaxDepth);
            _dictionary      = (IDictionary)ps.GetComponent(PropDictionary);
            _map             = new HashMap <WordSequence, Probability>();
            _vocabulary      = new HashSet <String>();
            _tokens          = new LinkedList <WordSequence>();
        }
Exemplo n.º 18
0
 public NgramTrieModel(string format, URL location, string ngramLogFile,
                       int maxNGramCacheSize, bool clearCacheAfterUtterance,
                       int maxDepth, IDictionary dictionary,
                       bool applyLanguageWeightAndWip, float languageWeight,
                       double wip, float unigramWeight)
 {
     this.format                   = format;
     this.location                 = location;
     this.ngramLogFile             = ngramLogFile;
     this.ngramCacheSize           = maxNGramCacheSize;
     this.clearCacheAfterUtterance = clearCacheAfterUtterance;
     this.MaxDepth                 = maxDepth;
     logMath         = LogMath.GetLogMath();
     this.dictionary = dictionary;
     this.applyLanguageWeightAndWip = applyLanguageWeightAndWip;
     this.languageWeight            = languageWeight;
     this.logWip        = logMath.LinearToLog(wip);
     this.unigramWeight = unigramWeight;
 }
        public override void NewProperties(PropertySheet ps)
        {
            base.NewProperties(ps);

            _logMath = LogMath.GetLogMath();

            Linguist           = (Linguist.Linguist)ps.GetComponent(PropLinguist);
            Pruner             = (IPruner)ps.GetComponent(PropPruner);
            Scorer             = (IAcousticScorer)ps.GetComponent(PropScorer);
            _activeListManager = (ActiveListManager)ps.GetComponent(PropActiveListManager);
            _showTokenCount    = ps.GetBoolean(PropShowTokenCount);
            _growSkipInterval  = ps.GetInt(PropGrowSkipInterval);

            _checkStateOrder         = ps.GetBoolean(PropCheckStateOrder);
            _maxLatticeEdges         = ps.GetInt(PropMaxLatticeEdges);
            _acousticLookaheadFrames = ps.GetFloat(PropAcousticLookaheadFrames);

            _relativeBeamWidth = _logMath.LinearToLog(ps.GetDouble(PropRelativeBeamWidth));
        }
Exemplo n.º 20
0
        public void TestPosterior()
        {
            var logMath = LogMath.GetLogMath();

            var lattice = new Lattice();

            var a = lattice.AddNode("A", "A", 0, 0);
            var b = lattice.AddNode("B", "B", 0, 0);
            var c = lattice.AddNode("C", "C", 0, 0);
            var d = lattice.AddNode("D", "D", 0, 0);

            const double acousticAb = 4;
            const double acousticAc = 6;
            const double acousticCb = 1;
            const double acousticBd = 5;
            const double acousticCD = 2;

            lattice.InitialNode  = a;
            lattice.TerminalNode = d;

            lattice.AddEdge(a, b, logMath.LinearToLog(acousticAb), 0);
            lattice.AddEdge(a, c, logMath.LinearToLog(acousticAc), 0);
            lattice.AddEdge(c, b, logMath.LinearToLog(acousticCb), 0);
            lattice.AddEdge(b, d, logMath.LinearToLog(acousticBd), 0);
            lattice.AddEdge(c, d, logMath.LinearToLog(acousticCD), 0);

            lattice.ComputeNodePosteriors(1.0f);
            const double pathAbd  = acousticAb * acousticBd;
            const double pathAcbd = acousticAc * acousticCb * acousticBd;
            const double pathAcd  = acousticAc * acousticCD;
            const double allPaths = pathAbd + pathAcbd + pathAcd;

            const double bPosterior = (pathAbd + pathAcbd) / allPaths;
            const double cPosterior = (pathAcbd + pathAcd) / allPaths;

            const double delta = 1e-4;

            Assert.AreEqual(logMath.LogToLinear((float)a.Posterior), 1.0, delta);
            Assert.AreEqual(logMath.LogToLinear((float)b.Posterior), bPosterior, delta);
            Assert.AreEqual(logMath.LogToLinear((float)c.Posterior), cPosterior, delta);
            Assert.AreEqual(logMath.LogToLinear((float)d.Posterior), 1.0, delta);
        }
Exemplo n.º 21
0
 public LargeNGramModel(String format, URL location, string ngramLogFile,
                        int maxNGramCacheSize, Boolean clearCacheAfterUtterance,
                        int maxDepth, IDictionary dictionary,
                        Boolean applyLanguageWeightAndWip, float languageWeight,
                        double wip, float unigramWeight, Boolean fullSmear)
 {
     Format                    = format;
     Location                  = location;
     NgramLogFile              = ngramLogFile;
     NgramCacheSize            = maxNGramCacheSize;
     ClearCacheAfterUtterance  = clearCacheAfterUtterance;
     MaxDepth                  = maxDepth;
     LogMath                   = LogMath.GetLogMath();
     Dictionary                = dictionary;
     ApplyLanguageWeightAndWip = applyLanguageWeightAndWip;
     LanguageWeight            = languageWeight;
     Wip           = wip;
     UnigramWeight = unigramWeight;
     FullSmear     = fullSmear;
 }
        public void MixtureComponent_UnivariateDensity()
        {
            const float minX       = 10;
            const float maxX       = 30;
            const float resolution = 0.1f;

            const float mean = 20;
            const float var  = 3;

            var gaussian = new MixtureComponent(new[] { mean }, new[] { var });

            for (var curX = minX; curX <= maxX; curX += resolution)
            {
                var gauLogScore = gaussian.GetScore(new FloatData(new[] { curX }, 16000, 0));

                var manualScore = (1 / Math.Sqrt(var * 2 * Math.PI)) * Math.Exp((-0.5 / var) * (curX - mean) * (curX - mean));
                var gauScore    = LogMath.GetLogMath().LogToLinear((float)gauLogScore);

                Assert.AreEqual(manualScore, gauScore, 1E-5);
            }
        }
        public WordPruningBreadthFirstSearchManager(Linguist.Linguist linguist, IPruner pruner,
                                                    IAcousticScorer scorer, ActiveListManager activeListManager,
                                                    Boolean showTokenCount, double relativeWordBeamWidth,
                                                    int growSkipInterval,
                                                    Boolean checkStateOrder, Boolean buildWordLattice,
                                                    int maxLatticeEdges, float acousticLookaheadFrames,
                                                    Boolean keepAllTokens)
        {
            _logMath                 = LogMath.GetLogMath();
            Linguist                 = linguist;
            Pruner                   = pruner;
            Scorer                   = scorer;
            _activeListManager       = activeListManager;
            _showTokenCount          = showTokenCount;
            _growSkipInterval        = growSkipInterval;
            _checkStateOrder         = checkStateOrder;
            BuildWordLattice         = buildWordLattice;
            _maxLatticeEdges         = maxLatticeEdges;
            _acousticLookaheadFrames = acousticLookaheadFrames;
            KeepAllTokens            = keepAllTokens;

            _relativeBeamWidth = _logMath.LinearToLog(relativeWordBeamWidth);
        }
Exemplo n.º 24
0
        public override void Allocate()
        {
            _vocabulary.Clear();
            _logProbs.Clear();
            _logBackoffs.Clear();
            HashMap <WordSequence, Integer> unigrams = new HashMap <WordSequence, Integer>();
            HashMap <WordSequence, Integer> bigrams  = new HashMap <WordSequence, Integer>();
            HashMap <WordSequence, Integer> trigrams = new HashMap <WordSequence, Integer>();
            int wordCount = 0;

            foreach (string sentence in _sentences)
            {
                string[] textWords = sentence.Split("\\s+");
                var      words     = new List <Word>();
                words.Add(_dictionary.GetSentenceStartWord());
                foreach (String wordString in textWords)
                {
                    if (wordString.Length == 0)
                    {
                        continue;
                    }
                    _vocabulary.Add(wordString);
                    Word word = _dictionary.GetWord(wordString);
                    if (word == null)
                    {
                        words.Add(Word.Unknown);
                    }
                    else
                    {
                        words.Add(word);
                    }
                }
                words.Add(_dictionary.GetSentenceEndWord());

                if (words.Count > 0)
                {
                    AddSequence(unigrams, new WordSequence(words[0]));
                    wordCount++;
                }

                if (words.Count > 1)
                {
                    wordCount++;
                    AddSequence(unigrams, new WordSequence(words[1]));
                    AddSequence(bigrams, new WordSequence(words[0], words[1]));
                }

                for (int i = 2; i < words.Count; ++i)
                {
                    wordCount++;
                    AddSequence(unigrams, new WordSequence(words[i]));
                    AddSequence(bigrams, new WordSequence(words[i - 1], words[i]));
                    AddSequence(trigrams, new WordSequence(words[i - 2], words[i - 1], words[i]));
                }
            }

            float discount = .5f;
            float deflate  = 1 - discount;
            var   uniprobs = new HashMap <WordSequence, Float>();

            foreach (var e in unigrams)
            {
                uniprobs.Put(e.Key, e.Value * deflate / wordCount);
            }

            LogMath lmath               = LogMath.GetLogMath();
            float   logUnigramWeight    = lmath.LinearToLog(_unigramWeight);
            float   invLogUnigramWeight = lmath.LinearToLog(1 - _unigramWeight);
            float   logUniformProb      = -lmath.LinearToLog(uniprobs.Count);

            var          sorted1Grams = new SortedSet <WordSequence>(unigrams.Keys);
            var          iter         = new SortedSet <WordSequence>(bigrams.KeySet()).GetEnumerator();
            WordSequence ws           = iter.MoveNext() ? iter.Current : null;

            foreach (WordSequence unigram in sorted1Grams)
            {
                float p = lmath.LinearToLog(uniprobs.Get(unigram));
                p += logUnigramWeight;
                p  = lmath.AddAsLinear(p, logUniformProb + invLogUnigramWeight);
                _logProbs.Put(unigram, p);

                float sum = 0f;
                while (ws != null)
                {
                    int cmp = ws.GetOldest().CompareTo(unigram);
                    if (cmp > 0)
                    {
                        break;
                    }
                    if (cmp == 0)
                    {
                        sum += uniprobs.Get(ws.GetNewest());
                    }
                    ws = iter.MoveNext() ? iter.Current : null;
                }

                _logBackoffs.Put(unigram, lmath.LinearToLog(discount / (1 - sum)));
            }

            var biprobs = new HashMap <WordSequence, Float>();

            foreach (var entry in bigrams)
            {
                int unigramCount = unigrams.Get(entry.Key.GetOldest());
                biprobs.Put(entry.Key, entry.Value * deflate / unigramCount);
            }

            var sorted2Grams = new SortedSet <WordSequence>(bigrams.KeySet());

            iter = new SortedSet <WordSequence>(trigrams.KeySet()).GetEnumerator();
            ws   = iter.MoveNext() ? iter.Current : null;
            foreach (WordSequence biword in sorted2Grams)
            {
                _logProbs.Put(biword, lmath.LinearToLog(biprobs.Get(biword)));

                float sum = 0f;
                while (ws != null)
                {
                    int cmp = ws.GetOldest().CompareTo(biword);
                    if (cmp > 0)
                    {
                        break;
                    }
                    if (cmp == 0)
                    {
                        sum += biprobs.Get(ws.GetNewest());
                    }
                    ws = iter.MoveNext() ? iter.Current : null;
                }
                _logBackoffs.Put(biword, lmath.LinearToLog(discount / (1 - sum)));
            }

            foreach (var e in trigrams)
            {
                float p = e.Value * deflate;
                p /= bigrams.Get(e.Key.GetOldest());
                _logProbs.Put(e.Key, lmath.LinearToLog(p));
            }
        }
Exemplo n.º 25
0
 /** Create an empty Lattice. */
 public Lattice()
 {
     Edges   = new HashSet <Edge>();
     Nodes   = new HashMap <string, Node>();
     LogMath = LogMath.GetLogMath();
 }
Exemplo n.º 26
0
 public FSTGrammar(String path, bool showGrammar, bool optimizeGrammar, bool addSilenceWords, bool addFillerWords, IDictionary dictionary)
     : base(showGrammar, optimizeGrammar, addSilenceWords, addFillerWords, dictionary)
 {
     this._path = path;
     _logMath   = LogMath.GetLogMath();
 }
Exemplo n.º 27
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ActiveListFactory"/> class.
 /// </summary>
 /// <param name="absoluteBeamWidth">Width of the absolute beam.</param>
 /// <param name="relativeBeamWidth">Width of the relative beam.</param>
 protected ActiveListFactory(int absoluteBeamWidth, double relativeBeamWidth)
 {
     LogMath              = LogMath.GetLogMath();
     AbsoluteBeamWidth    = absoluteBeamWidth;
     LogRelativeBeamWidth = LogMath.LinearToLog(relativeBeamWidth);
 }