예제 #1
0
        /**
         * /// given edges e1 and e2 from node n to nodes n1 and n2
         * /// <p/>
         * /// merge e1 and e2, that is, merge the scores of e1 and e2 create n' that is a merge of n1 and n2 add n' add edge e'
         * /// from n to n'
         * /// <p/>
         * /// remove n1 and n2 and all associated edges
         *
         * /// @param n
         * /// @param e1
         * /// @param e2
         */
        protected void MergeNodesAndEdgesForward(Node n, Edge e1, Edge e2)
        {
            Debug.Assert(Lattice.HasNode(n));
            Debug.Assert(Lattice.HasEdge(e1));
            Debug.Assert(Lattice.HasEdge(e2));

            Debug.Assert(e1.FromNode == n);
            Debug.Assert(e2.FromNode == n);

            var n1 = e1.ToNode;
            var n2 = e2.ToNode;

            Debug.Assert(n1.HasEquivalentEnteringEdges(n1));
            Debug.Assert(n1.Word.Equals(n2.Word));

            // merge the scores of e1 and e2 into e1
            e1.AcousticScore = MergeAcousticScores
                                   (e1.AcousticScore, e2.AcousticScore);
            e1.LMScore = MergeLanguageScores(e1.LMScore,
                                             e2.LMScore);

            // add n2's edges to n1
            foreach (var e in n2.LeavingEdges)
            {
                e2 = n1.GetEdgeToNode(e.ToNode);
                if (e2 == null)
                {
                    Lattice.AddEdge(n1, e.ToNode,
                                    e.AcousticScore, e.LMScore);
                }
                else
                {
                    // if we got here then n1 and n2 had edges to the same node
                    // choose the edge with best score
                    e2.AcousticScore = MergeAcousticScores
                                           (e.AcousticScore, e2.AcousticScore);
                    e2.LMScore = MergeLanguageScores(e.LMScore,
                                                     e2.LMScore);
                }
            }

            // remove n2 and all associated edges
            Lattice.RemoveNodeAndEdges(n2);
        }
예제 #2
0
        /**
         * /// Internal routine used when creating a Lattice from a .LAT file
         *
         * /// @param lattice
         * /// @param tokens
         */
        public static void Load(Lattice lattice, StringTokenizer tokens)
        {
            string from  = tokens.nextToken();
            string to    = tokens.nextToken();
            int    score = int.Parse(tokens.nextToken(), CultureInfo.InvariantCulture.NumberFormat);

            Node fromNode = lattice.GetNode(from);

            if (fromNode == null)
            {
                throw new Exception("Edge fromNode \"" + from + "\" does not exist");
            }

            Node toNode = lattice.GetNode(to);

            if (toNode == null)
            {
                throw new Exception("Edge toNode \"" + to + "\" does not exist");
            }

            lattice.AddEdge(fromNode, toNode, score, 0.0);
        }
예제 #3
0
        public static Lattice ReadSlf(String fileName)
        {
            var    lattice = new Lattice();
            var    @in     = new LineNumberReader(new StreamReader(fileName));
            String line;
            var    readingNodes = false;
            var    readingEdges = false;
            var    startIdx     = 0;
            var    endIdx       = 1;
            var    lmscale      = 9.5;

            while ((line = @in.ReadLine()) != null)
            {
                if (line.Contains("Node definitions"))
                {
                    readingEdges = false;
                    readingNodes = true;
                    continue;
                }
                if (line.Contains("Link definitions"))
                {
                    readingEdges = true;
                    readingNodes = false;
                    continue;
                }
                if (line.StartsWith("#"))
                {
                    //skip commented line
                    continue;
                }
                if (readingNodes)
                {
                    //reading node info, format:
                    //I=id   t=start_time_sec   W=word_transcription
                    var parts = line.Split("\\s+");
                    if (parts.Length != 3 || !parts[0].StartsWith("I=") || !parts[1].StartsWith("t=") || !parts[2].StartsWith("W="))
                    {
                        @in.Close();
                        throw new IOException("Unknown node definition: " + line);
                    }
                    var idx = Convert.ToInt32(parts[0].Substring(2), CultureInfo.InvariantCulture.NumberFormat);
                    //convert to milliseconds inplace
                    var beginTime = (long)(Convert.ToDouble(parts[1].Substring(2), CultureInfo.InvariantCulture.NumberFormat) * 1000);
                    var wordStr   = parts[2].Substring(2);
                    var isFiller  = false;
                    if (idx == startIdx || wordStr.Equals("!ENTER"))
                    {
                        wordStr  = "<s>";
                        isFiller = true;
                    }
                    if (idx == endIdx || wordStr.Equals("!EXIT"))
                    {
                        wordStr  = "</s>";
                        isFiller = true;
                    }
                    if (wordStr.Equals("!NULL"))
                    {
                        wordStr  = "<sil>";
                        isFiller = true;
                    }
                    if (wordStr.StartsWith("["))
                    {
                        isFiller = true;
                    }
                    var word = new Word(wordStr, new Pronunciation[0], isFiller);
                    var node = lattice.AddNode(idx.ToString(CultureInfo.InvariantCulture), word, beginTime, -1);
                    if (wordStr.Equals("<s>"))
                    {
                        lattice.InitialNode = node;
                    }
                    if (wordStr.Equals("</s>"))
                    {
                        lattice.TerminalNode = node;
                    }
                }
                else if (readingEdges)
                {
                    //reading edge info, format:
                    //J=id   S=from_node   E=to_node   a=acoustic_score   l=language_score
                    var parts = line.Split("\\s+");
                    if (parts.Length != 5 || !parts[1].StartsWith("S=") || !parts[2].StartsWith("E=") ||
                        !parts[3].StartsWith("a=") || !parts[4].StartsWith("l="))
                    {
                        @in.Close();
                        throw new IOException("Unknown edge definition: " + line);
                    }
                    var fromId = parts[1].Substring(2);
                    var toId   = parts[2].Substring(2);
                    var ascore = Convert.ToDouble(parts[3].Substring(2), CultureInfo.InvariantCulture.NumberFormat);
                    var lscore = Convert.ToDouble(parts[4].Substring(2), CultureInfo.InvariantCulture.NumberFormat) * lmscale;
                    lattice.AddEdge(lattice.Nodes.Get(fromId), lattice.Nodes.Get(toId), ascore, lscore);
                }
                else
                {
                    //reading header here if needed
                    if (line.StartsWith("start="))
                    {
                        startIdx = Convert.ToInt32(line.Replace("start=", ""), CultureInfo.InvariantCulture.NumberFormat);
                    }
                    if (line.StartsWith("end="))
                    {
                        endIdx = Convert.ToInt32(line.Replace("end=", ""), CultureInfo.InvariantCulture.NumberFormat);
                    }
                    if (line.StartsWith("lmscale="))
                    {
                        lmscale = Convert.ToDouble(line.Replace("lmscale=", ""), CultureInfo.InvariantCulture.NumberFormat);
                    }
                }
            }
            foreach (var node in lattice.Nodes.Values)
            {
                //calculate end time of nodes depending successors begin time
                foreach (var edge in node.LeavingEdges)
                {
                    if (node.EndTime < 0 || node.EndTime > edge.ToNode.BeginTime)
                    {
                        node.EndTime = edge.ToNode.BeginTime;
                    }
                }
            }
            @in.Close();
            return(lattice);
        }