/// <summary>
        /// Saves the candidate into the lines in the format of master label file.
        /// </summary>
        /// <param name="typeOption">The given label type.</param>
        /// <param name="alignOption">The given alignment data.</param>
        /// <param name="keepRemainingPart">Whether to keep the remaining part.</param>
        /// <returns>The lines contains the candidate information.</returns>
        public string[] Save(LabelTypeOptions typeOption, LabelAlignOptions alignOption, bool keepRemainingPart)
        {
            if (alignOption == LabelAlignOptions.StateAlign)
            {
                string[] result = new string[StateAlignments.Length];
                for (int i = 0; i < StateAlignments.Length; ++i)
                {
                    LabelLine labelHelper = new LabelLine
                    {
                        Label = Label,
                        Segment = StateAlignments[i],
                        State = 2 + i
                    };

                    if (LabelRemainings != null && LabelRemainings.Count > 0)
                    {
                        labelHelper.Remaining = LabelRemainings[2 + i];
                    }

                    // Since Htk state index starts with 2.
                    result[i] = labelHelper.ToString(typeOption, keepRemainingPart);
                }

                return result;
            }

            LabelLine labelLine = new LabelLine { State = -1, Label = Label };
            if (LabelRemainings != null && LabelRemainings.Count > 0)
            {
                if (LabelRemainings.ContainsKey(-1))
                {
                    labelLine.Remaining = LabelRemainings[-1];
                }
                else if (LabelRemainings.ContainsKey(2))
                {
                    labelLine.Remaining = LabelRemainings[2];
                }
            }

            switch (alignOption)
            {
                case LabelAlignOptions.NoAlign:
                    labelLine.Segment = null;
                    break;
                case LabelAlignOptions.PhonemeAlign:
                    if (_stateSegments == null)
                    {
                        throw new InvalidOperationException("Unsupported operation since there is no alignment data in Candidate");
                    }

                    labelLine.Segment = new Segment(StartTime, EndTime);
                    break;
                default:
                    throw new InvalidDataException("Unknown HtkLabelAlignOptions value");
            }

            return new[] { labelLine.ToString(typeOption, keepRemainingPart) };
        }
예제 #2
0
        /// <summary>
        /// Parses a string and return a HtkLabelHelper instance by given LabelFeatureNameSet.
        /// </summary>
        /// <param name="value">The given string to be parsed.</param>
        /// <param name="featureNames">The given feature names.</param>
        /// <returns>The parsed HtkLabelHelper.</returns>
        public static LabelLine Parse(string value, LabelFeatureNameSet featureNames)
        {
            LabelLine labelLine = new LabelLine
            {
                // Initialize state as a negative value.
                State = -1,
            };

            string labelText;
            string[] parts = value.Split(SplitterChars, StringSplitOptions.RemoveEmptyEntries);
            switch (parts.Length)
            {
                case 1:
                    labelText = parts[0];
                    break;
                case 2:
                    // Invalid currently.
                    throw new InvalidDataException(Helper.NeutralFormat("Unsupported data \"{0}\"", value));
                default:
                    labelLine.Segment = new Segment(long.Parse(parts[0]), long.Parse(parts[1]));
                    labelText = parts[2];

                    // Keep the remaining values.
                    if (parts.Length > 3)
                    {
                        labelLine.Remaining = new string[parts.Length - 3];
                        Array.Copy(parts, 3, labelLine.Remaining, 0, labelLine.Remaining.Length);
                    }

                    break;
            }

            labelLine.Label = new Label(featureNames);

            // Check there is state information or not.
            if (labelText[labelText.Length - 1] == ']')
            {
                // The state info is appended to the label text as: a-b+c+x...x[state]
                // Find the previous "["
                int index = labelText.LastIndexOf('[');
                if (index < 0)
                {
                    throw new InvalidDataException(Helper.NeutralFormat("Unsupport format \"{0}\"", labelText));
                }

                labelLine.Label.Text = labelText.Substring(0, index);
                labelLine.State = int.Parse(labelText.Substring(index + 1, labelText.Length - index - 2));
            }
            else
            {
                labelLine.Label.Text = labelText;
            }

            return labelLine;
        }