Exemplo n.º 1
0
        /// <summary>
        /// ParsePolyCondition.
        /// </summary>
        /// <param name="expression">Expression.</param>
        /// <param name="condition">Condition.</param>
        /// <param name="errorSet">ErrorSet.</param>
        private void ParsePolyCondition(string expression,
            PolyphonyCondition condition, ErrorSet errorSet)
        {
            string subExpression = expression;

            // If the value is string, then search operator before """
            if (subExpression.IndexOf('"') > 0)
            {
                subExpression = subExpression.Substring(0, subExpression.IndexOf('"'));
            }

            foreach (string oper in _allOperators)
            {
                if (subExpression.IndexOf(oper) >= 0)
                {
                    condition.Operator = oper;
                    break;
                }
            }

            bool succeeded = true;
            if (string.IsNullOrEmpty(condition.Operator))
            {
                errorSet.Add(PolyRuleError.MissingOperatorInCondition,
                    expression);
                succeeded = false;
            }

            if (succeeded)
            {
                condition.Key = expression.Substring(0,
                    expression.IndexOf(condition.Operator)).Trim();
                if (!_keyTypes.ContainsKey(condition.Key))
                {
                    errorSet.Add(PolyRuleError.NotDeclearedConditionKey,
                        condition.Key, expression);
                    succeeded = false;
                }
            }

            if (succeeded)
            {
                string valueExpression = expression.Substring(
                    expression.IndexOf(condition.Operator) + condition.Operator.Length).Trim();
                if (_keyTypes[condition.Key] == KeyType.String)
                {
                    Match match = Regex.Match(valueExpression, @"^""(.*)""$");
                    if (match.Success)
                    {
                        valueExpression = match.Groups[1].ToString();
                    }
                    else
                    {
                        errorSet.Add(PolyRuleError.InvalidConditionFormat,
                            expression);
                        succeeded = false;
                    }
                }
                else
                {
                    int intValue;
                    if (!int.TryParse(valueExpression, out intValue))
                    {
                        errorSet.Add(PolyRuleError.InvalidConditionFormat,
                            expression);
                        succeeded = false;
                    }
                }

                condition.Value = valueExpression;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// ParseConditionLine.
        /// </summary>
        /// <param name="line">Line.</param>
        /// <param name="phoneSet">PhoneSet.</param>
        /// <param name="polyphonyWord">PolyphonyWord.</param>
        /// <returns>ErrorSet.</returns>
        private ErrorSet ParseConditionLine(string line, TtsPhoneSet phoneSet,
            PolyphonyRule polyphonyWord)
        {
            ErrorSet errorSet = new ErrorSet();
            Match match = Regex.Match(line, ConditionLineRegex);
            if (match.Groups.Count < 3)
            {
                errorSet.Add(PolyRuleError.InvalidConditionFormat,
                    line);
            }
            else
            {
                PolyphonyPron polyphonyPron = new PolyphonyPron();
                polyphonyPron.Pron = match.Groups[2].ToString().Trim();

                // Allow empty pronunciation for polyphony rule.
                if (!string.IsNullOrEmpty(polyphonyPron.Pron) && phoneSet != null)
                {
                    errorSet.AddRange(Pronunciation.Validate(polyphonyPron.Pron, phoneSet));
                }

                string conditions = match.Groups[1].ToString().Trim();
                bool hasMatched = false;
                foreach (Match conditionMatch in Regex.Matches(conditions, ConditionRegex))
                {
                    hasMatched = true;
                    string expression = conditionMatch.Value;
                    PolyphonyCondition condition = new PolyphonyCondition();
                    ParsePolyCondition(expression.Trim(), condition, errorSet);
                    polyphonyPron.Conditions.Add(condition);
                }

                if (hasMatched)
                {
                    if (errorSet.GetSeverityCount(ErrorSeverity.MustFix) == 0)
                    {
                        if (polyphonyWord == null)
                        {
                            errorSet.Add(PolyRuleError.MissKeyValueLine, line);
                        }
                        else
                        {
                            polyphonyWord.PolyphonyProns.Add(polyphonyPron);
                        }
                    }
                }
                else
                {
                    errorSet.Add(PolyRuleError.InvalidConditionFormat, line);
                }
            }

            return errorSet;
        }