Ejemplo n.º 1
0
        /// <summary>
        /// Loads the forest from the forest file, for example, .INF file.
        /// </summary>
        /// <param name="forestFileName">The location of the input file.</param>
        /// <returns>DecisionForest.</returns>
        public DecisionForest Load(string forestFileName)
        {
            Helper.ThrowIfNull(forestFileName);
            bool quesSection = true;
            bool treeSection = false;
            Collection<string> treeLines = new Collection<string>();
            foreach (string line in Helper.FileLines(forestFileName, Encoding.ASCII, false))
            {
                // Load question sets and count decision tree
                if (quesSection)
                {
                    if (line.IndexOf(Question.QuestionKeyword, StringComparison.Ordinal) < 0)
                    {
                        quesSection = false;
                    }
                    else
                    {
                        Question question = new Question(line);
                        _nameIndexedQuestions.Add(question.Name, question);
                    }
                }

                if (!quesSection)
                {
                    string trimLine = line.Trim();
                    if (!string.IsNullOrEmpty(trimLine))
                    {
                        // tree start
                        if (!treeSection)
                        {
                            treeSection = true;
                            treeLines.Clear();
                        }

                        treeLines.Add(trimLine);
                    }
                    else
                    {
                        // tree end
                        if (treeSection)
                        {
                            DecisionTree tree = new DecisionTree();
                            tree.Load(treeLines);
                            _treeList.Add(tree);
                            treeSection = false;
                        }
                    }
                }
            }

            if (treeSection)
            {
                throw new InvalidDataException(
                    Helper.NeutralFormat("Invalidate last line for Decision Tree {0}", forestFileName));
            }

            return this;
        }
        /// <summary>
        /// Gets the selective tri-phone for each leaf nodes in the tree.
        /// </summary>
        /// <param name="tree">The given tree.</param>
        /// <param name="questions">The related questions.</param>
        /// <param name="phonemes">The whole set of phoneme, which is used to initialize the tri-phone pairs.</param>
        /// <returns>The selective tri-phone for each leaf node in decision tree.
        /// The key is the leaf node, and the tri-phone pair set is the value.</returns>
        private static Dictionary<DecisionTreeNode, TriphoneSet> GetSelectiveTriphone(DecisionTree tree, IDictionary<string, Question> questions, HashSet<string> phonemes)
        {
            Dictionary<DecisionTreeNode, TriphoneSet> nodeToTriphone = new Dictionary<DecisionTreeNode, TriphoneSet>();

            // Firstly, generate a whole tri-phone set.
            TriphoneSet set = new TriphoneSet(phonemes);

            // Phone-dependent tree should resize the central phone set.
            string phone = tree.Phone;
            if (!Phoneme.IsAnyPhone(phone))
            {
                set.CentralPhones.Clear();
                set.CentralPhones.Add(phone);
            }

            // Assign the tri-phone set to the root of the tree.
            nodeToTriphone.Add(tree.NodeList[0], set);

            GetSelectiveTriphone(nodeToTriphone, questions);
            return nodeToTriphone;
        }