Esempio n. 1
0
        /**
         * /// Internal routine used when loading Lattices from .LAT files
         *
         * /// @param lattice
         * /// @param tokens
         */
        public static void Load(Lattice lattice, StringTokenizer tokens)
        {
            string id    = tokens.nextToken();
            string label = tokens.nextToken();

            lattice.AddNode(id, label, 0, 0);
        }
Esempio n. 2
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);
        }