Beispiel #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);
        }
Beispiel #2
0
        /** @see edu.cmu.sphinx.result.ConfidenceScorer#score(edu.cmu.sphinx.result.Result) */
        public override IConfidenceResult score(Result result)
        {
            lattice = new Lattice(result);
            LatticeOptimizer lop = new LatticeOptimizer(lattice);

            lop.optimize();
            lattice.computeNodePosteriors(languageWeight);
            return(makeSausage());
        }
Beispiel #3
0
        /**
         * /// Self test for LatticeOptimizer
         *
         * /// @param args
         */
        public static void Main(String[] args)
        {
            var lattice = new Lattice(args[0]);

            var optimizer = new LatticeOptimizer(lattice);

            optimizer.Optimize();

            lattice.Dump(args[1]);
        }
Beispiel #4
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);
        }
Beispiel #5
0
 public Nbest(Lattice lattice)
 {
     Lattice = lattice;
 }
Beispiel #6
0
 /**
  * /// Create a new Lattice optimizer
  *
  * /// @param lattice
  */
 public LatticeOptimizer(Lattice lattice)
 {
     this.Lattice = lattice;
 }
Beispiel #7
0
 /**
  * Construct a sausage maker
  *
  * @param l the lattice to construct a sausage from
  */
 public SausageMaker(Lattice l)
 {
     lattice = l;
 }
Beispiel #8
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);
        }
Beispiel #9
0
 /**
  * /// Returns true if the given Lattice is equivalent to this Lattice. Two lattices are equivalent if all their nodes
  * /// and edges are equivalent.
  *
  * /// @param other the Lattice to compare this Lattice against
  * /// @return true if the Lattices are equivalent; false otherwise
  */
 public Boolean IsEquivalent(Lattice other)
 {
     return(CheckNodesEquivalent(InitialNode, other.InitialNode));
 }