Exemplo n.º 1
0
 internal void logAccumulate(float[] array, float num, LogMath logMath)
 {
     if (!Buffer.assertionsDisabled && this.numerator == null)
     {
         throw new AssertionError();
     }
     if (!Buffer.assertionsDisabled && array == null)
     {
         throw new AssertionError();
     }
     if (!Buffer.assertionsDisabled && this.numerator.Length != array.Length)
     {
         throw new AssertionError();
     }
     if (!Buffer.assertionsDisabled && !this.isLog)
     {
         throw new AssertionError();
     }
     for (int i = 0; i < this.numerator.Length; i++)
     {
         this.numerator[i] = (double)logMath.addAsLinear((float)this.numerator[i], array[i]);
     }
     this.denominator = (double)logMath.addAsLinear((float)this.denominator, num);
     this._wasUsed    = true;
 }
Exemplo n.º 2
0
 internal void logAccumulate(float num, int num2, LogMath logMath)
 {
     if (!Buffer.assertionsDisabled && this.numerator == null)
     {
         throw new AssertionError();
     }
     if (!Buffer.assertionsDisabled && !this.isLog)
     {
         throw new AssertionError();
     }
     this.numerator[num2] = (double)logMath.addAsLinear((float)this.numerator[num2], num);
     this.denominator     = (double)logMath.addAsLinear((float)this.denominator, num);
     this._wasUsed        = true;
 }
Exemplo n.º 3
0
        internal void logNormalizeToSum(LogMath logMath)
        {
            if (!Buffer.assertionsDisabled && !this.isLog)
            {
                throw new AssertionError();
            }
            float minValue = float.MinValue;
            float num      = minValue;

            double[] array = this.numerator;
            int      num2  = array.Length;

            for (int i = 0; i < num2; i++)
            {
                double num3 = array[i];
                if (num3 != (double)minValue)
                {
                    num = logMath.addAsLinear(num, (float)num3);
                }
            }
            for (int j = 0; j < this.numerator.Length; j++)
            {
                if (this.numerator[j] != (double)minValue)
                {
                    double[] array2 = this.numerator;
                    int      num4   = j;
                    double[] array3 = array2;
                    array3[num4] -= (double)num;
                }
            }
            this.denominator = (double)0f;
        }
        /**
         * Calculate the sum of posteriors in this cluster.
         *
         * @param cluster the cluster to sum over
         * @return the probability sum
         */
        protected double clusterProbability(LinkedList <Node> cluster)
        {
            float   p       = LogMath.LOG_ZERO;
            LogMath logMath = LogMath.getLogMath();

            foreach (Node node in cluster)
            {
                p = logMath.addAsLinear(p, (float)node.getPosterior());
            }

            return(p);
        }
Exemplo n.º 5
0
        protected internal override float calculateScore(Data data)
        {
            float   num     = float.MinValue;
            LogMath logMath = LogMath.getLogMath();

            float[] array = this.calculateComponentScore(data);
            int     num2  = array.Length;

            for (int i = 0; i < num2; i++)
            {
                Float @float = Float.valueOf(array[i]);
                num = logMath.addAsLinear(num, @float.floatValue());
            }
            return(num);
        }
Exemplo n.º 6
0
        /**
         * Calculate the distance between two clusters
         *
         * @param c1 the first cluster
         * @param c2 the second cluster
         * @return the inter cluster similarity, or Double.NEGATIVE_INFINITY if these clusters should never be clustered
         *         together.
         */
        protected double interClusterDistance(Cluster c1, Cluster c2)
        {
            if (areClustersInRelation(c1, c2))
            {
                return(Double.NegativeInfinity);
            }
            float            totalSim      = LogMath.LOG_ZERO;
            float            wordPairCount = (float)0.0;
            HashSet <String> wordsSeen1    = new HashSet <String>();
            LogMath          logMath       = LogMath.getLogMath();

            foreach (Node node1 in c1.getElements())
            {
                String word1 = node1.getWord().getSpelling();
                if (wordsSeen1.Contains(word1))
                {
                    continue;
                }
                wordsSeen1.Add(word1);
                HashSet <String> wordsSeen2 = new HashSet <String>();
                foreach (Node node2 in c2.getElements())
                {
                    String word2 = node2.getWord().getSpelling();
                    if (wordsSeen2.Contains(word2))
                    {
                        continue;
                    }
                    wordsSeen2.Add(word2);
                    float sim = (float)computePhoneticSimilarity(node1, node2);
                    sim      = logMath.linearToLog(sim);
                    sim     += (float)wordSubClusterProbability(c1, word1);
                    sim     += (float)wordSubClusterProbability(c2, word2);
                    totalSim = logMath.addAsLinear(totalSim, sim);
                    wordPairCount++;
                }
            }
            return(totalSim - logMath.logToLinear(wordPairCount));
        }
Exemplo n.º 7
0
        public override void allocate()
        {
            this.vocabulary.clear();
            this.logProbs.clear();
            this.logBackoffs.clear();
            HashMap  hashMap  = new HashMap();
            HashMap  hashMap2 = new HashMap();
            HashMap  hashMap3 = new HashMap();
            int      num      = 0;
            Iterator iterator = this.sentences.iterator();

            while (iterator.hasNext())
            {
                string    text      = (string)iterator.next();
                string[]  array     = String.instancehelper_split(text, "\\s+");
                ArrayList arrayList = new ArrayList();
                arrayList.add(this.dictionary.getSentenceStartWord());
                string[] array2 = array;
                int      num2   = array2.Length;
                for (int i = 0; i < num2; i++)
                {
                    string text2 = array2[i];
                    if (String.instancehelper_length(text2) != 0)
                    {
                        this.vocabulary.add(text2);
                        Word word = this.dictionary.getWord(text2);
                        if (word == null)
                        {
                            arrayList.add(Word.__UNKNOWN);
                        }
                        else
                        {
                            arrayList.add(word);
                        }
                    }
                }
                arrayList.add(this.dictionary.getSentenceEndWord());
                if (arrayList.size() > 0)
                {
                    HashMap hashMap4 = hashMap;
                    this.addSequence(hashMap4, new WordSequence(new Word[]
                    {
                        (Word)arrayList.get(0)
                    }));
                    num++;
                }
                if (arrayList.size() > 1)
                {
                    num++;
                    HashMap hashMap5 = hashMap;
                    this.addSequence(hashMap5, new WordSequence(new Word[]
                    {
                        (Word)arrayList.get(1)
                    }));
                    HashMap hashMap6 = hashMap2;
                    this.addSequence(hashMap6, new WordSequence(new Word[]
                    {
                        (Word)arrayList.get(0),
                        (Word)arrayList.get(1)
                    }));
                }
                for (int j = 2; j < arrayList.size(); j++)
                {
                    num++;
                    HashMap hashMap7 = hashMap;
                    this.addSequence(hashMap7, new WordSequence(new Word[]
                    {
                        (Word)arrayList.get(j)
                    }));
                    HashMap hashMap8 = hashMap2;
                    this.addSequence(hashMap8, new WordSequence(new Word[]
                    {
                        (Word)arrayList.get(j - 1),
                        (Word)arrayList.get(j)
                    }));
                    HashMap hashMap9 = hashMap3;
                    this.addSequence(hashMap9, new WordSequence(new Word[]
                    {
                        (Word)arrayList.get(j - 2),
                        (Word)arrayList.get(j - 1),
                        (Word)arrayList.get(j)
                    }));
                }
            }
            float    num3      = 0.5f;
            float    num4      = 1f - num3;
            HashMap  hashMap10 = new HashMap();
            Iterator iterator2 = hashMap.entrySet().iterator();

            while (iterator2.hasNext())
            {
                Map.Entry entry = (Map.Entry)iterator2.next();
                hashMap10.put(entry.getKey(), Float.valueOf((float)((Integer)entry.getValue()).intValue() * num4 / (float)num));
            }
            LogMath      logMath      = LogMath.getLogMath();
            float        num5         = logMath.linearToLog((double)this.unigramWeight);
            float        num6         = logMath.linearToLog((double)(1f - this.unigramWeight));
            float        num7         = -logMath.linearToLog((double)hashMap10.size());
            TreeSet      treeSet      = new TreeSet(hashMap.keySet());
            Iterator     iterator3    = new TreeSet(hashMap2.keySet()).iterator();
            WordSequence wordSequence = (!iterator3.hasNext()) ? null : ((WordSequence)iterator3.next());
            Iterator     iterator4    = treeSet.iterator();

            while (iterator4.hasNext())
            {
                WordSequence wordSequence2 = (WordSequence)iterator4.next();
                float        num8          = logMath.linearToLog((double)((Float)hashMap10.get(wordSequence2)).floatValue());
                num8 += num5;
                num8  = logMath.addAsLinear(num8, num7 + num6);
                this.logProbs.put(wordSequence2, Float.valueOf(num8));
                float num9 = 0f;
                while (wordSequence != null)
                {
                    int num10 = wordSequence.getOldest().compareTo(wordSequence2);
                    if (num10 > 0)
                    {
                        break;
                    }
                    if (num10 == 0)
                    {
                        num9 += ((Float)hashMap10.get(wordSequence.getNewest())).floatValue();
                    }
                    wordSequence = ((!iterator3.hasNext()) ? null : ((WordSequence)iterator3.next()));
                }
                this.logBackoffs.put(wordSequence2, Float.valueOf(logMath.linearToLog((double)(num3 / (1f - num9)))));
            }
            HashMap  hashMap11 = new HashMap();
            Iterator iterator5 = hashMap2.entrySet().iterator();

            while (iterator5.hasNext())
            {
                Map.Entry entry2 = (Map.Entry)iterator5.next();
                int       num11  = ((Integer)hashMap.get(((WordSequence)entry2.getKey()).getOldest())).intValue();
                hashMap11.put(entry2.getKey(), Float.valueOf((float)((Integer)entry2.getValue()).intValue() * num4 / (float)num11));
            }
            TreeSet treeSet2 = new TreeSet(hashMap2.keySet());

            iterator3    = new TreeSet(hashMap3.keySet()).iterator();
            wordSequence = ((!iterator3.hasNext()) ? null : ((WordSequence)iterator3.next()));
            Iterator iterator6 = treeSet2.iterator();

            while (iterator6.hasNext())
            {
                WordSequence wordSequence3 = (WordSequence)iterator6.next();
                this.logProbs.put(wordSequence3, Float.valueOf(logMath.linearToLog((double)((Float)hashMap11.get(wordSequence3)).floatValue())));
                float num12 = 0f;
                while (wordSequence != null)
                {
                    int num13 = wordSequence.getOldest().compareTo(wordSequence3);
                    if (num13 > 0)
                    {
                        break;
                    }
                    if (num13 == 0)
                    {
                        num12 += ((Float)hashMap11.get(wordSequence.getNewest())).floatValue();
                    }
                    wordSequence = ((!iterator3.hasNext()) ? null : ((WordSequence)iterator3.next()));
                }
                this.logBackoffs.put(wordSequence3, Float.valueOf(logMath.linearToLog((double)(num3 / (1f - num12)))));
            }
            iterator6 = hashMap3.entrySet().iterator();
            while (iterator6.hasNext())
            {
                Map.Entry entry3 = (Map.Entry)iterator6.next();
                float     num12  = (float)((Integer)entry3.getValue()).intValue() * num4;
                num12 /= (float)((Integer)hashMap2.get(((WordSequence)entry3.getKey()).getOldest())).intValue();
                this.logProbs.put(entry3.getKey(), Float.valueOf(logMath.linearToLog((double)num12)));
            }
        }
Exemplo n.º 8
0
        /**
         * /// Compute the utterance-level posterior for every node in the lattice, i.e. the probability that this node occurs
         * /// on any path through the lattice. Uses a forward-backward algorithm specific to the nature of non-looping
         * /// left-to-right lattice structures.
         * /// <p/>
         * /// Node posteriors can be retrieved by calling getPosterior() on Node objects.
         *
         * /// @param languageModelWeightAdjustment   the weight multiplier that will be applied to language score already scaled by language weight
         * /// @param useAcousticScoresOnly use only the acoustic scores to compute the posteriors, ignore the language weight
         * ///                              and scores
         */
        public void computeNodePosteriors(float languageModelWeightAdjustment,
                                          Boolean useAcousticScoresOnly)
        {
            if (initialNode == null)
            {
                return;
            }
            //forward
            initialNode.setForwardScore(LogMath.LOG_ONE);
            initialNode.setViterbiScore(LogMath.LOG_ONE);
            List <Node> sortedNodes = sortNodes();

            Trace.Assert(sortedNodes[0] == initialNode);
            foreach (Node currentNode in sortedNodes)
            {
                foreach (Edge edge in currentNode.getLeavingEdges())
                {
                    double forwardProb = edge.getFromNode().getForwardScore();
                    double edgeScore   = computeEdgeScore
                                             (edge, languageModelWeightAdjustment, useAcousticScoresOnly);
                    forwardProb += edgeScore;
                    edge.getToNode().setForwardScore
                        (logMath.addAsLinear
                            ((float)forwardProb,
                            (float)edge.getToNode().getForwardScore()));
                    double vs = edge.getFromNode().getViterbiScore() +
                                edgeScore;
                    if (edge.getToNode().getBestPredecessor() == null ||
                        vs > edge.getToNode().getViterbiScore())
                    {
                        edge.getToNode().setBestPredecessor(currentNode);
                        edge.getToNode().setViterbiScore(vs);
                    }
                }
            }

            //backward
            terminalNode.setBackwardScore(LogMath.LOG_ONE);
            Trace.Assert(sortedNodes[sortedNodes.Count - 1] == terminalNode);

            int n = sortedNodes.Count - 1;

            while (n > 0)
            {
                Node        currentNode  = sortedNodes[n - 1];
                List <Edge> currentEdges = currentNode.getLeavingEdges();
                foreach (Edge edge in currentEdges)
                {
                    double backwardProb = edge.getToNode().getBackwardScore();
                    backwardProb += computeEdgeScore
                                        (edge, languageModelWeightAdjustment, useAcousticScoresOnly);
                    edge.getFromNode().setBackwardScore
                        (logMath.addAsLinear((float)backwardProb,
                                             (float)edge.getFromNode().getBackwardScore()));
                }
            }

            //inner
            double normalizationFactor = terminalNode.getForwardScore();

            foreach (Node node in nodes.Values)
            {
                node.setPosterior((node.getForwardScore() +
                                   node.getBackwardScore()) - normalizationFactor);
            }
        }