/// <summary> /// TryParseConditionLine. /// </summary> /// <param name="line">Line.</param> /// <param name="phoneSet">PhoneSet.</param> /// <param name="polyphonyWord">PolyphonyWord.</param> /// <param name="errorSet">ErrorSet.</param> /// <returns>Whether the line is condition line.</returns> private bool TryParseConditionLine(string line, TtsPhoneSet phoneSet, PolyphonyRule polyphonyWord, ErrorSet errorSet) { bool isConditionLine = false; if (IsConditionLine(line)) { isConditionLine = true; if (polyphonyWord == null) { errorSet.Add(PolyRuleError.MissKeyValueLine, line); } errorSet.AddRange(ParseConditionLine(line, phoneSet, polyphonyWord)); } return isConditionLine; }
/// <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; }
/// <summary> /// Load. /// </summary> /// <param name="filePath">FilePath.</param> /// <param name="phoneSet">PhoneSet.</param> /// <returns>ErrorSet.</returns> public ErrorSet Load(string filePath, TtsPhoneSet phoneSet) { // This validation is needed by Fxcop checking parameters. if (phoneSet == null) { phoneSet = null; } if (string.IsNullOrEmpty(filePath)) { throw new ArgumentNullException("filePath"); } if (!File.Exists(filePath)) { throw Helper.CreateException(typeof(FileNotFoundException), filePath); } if (!Helper.IsUnicodeFile(filePath)) { throw new InvalidDataException(Helper.NeutralFormat( "Polyphony rule file [{0}] is not unicode.", filePath)); } ErrorSet errorSet = new ErrorSet(); _keyTypes.Clear(); bool finishReadHead = false; bool firstKeyString = true; PolyphonyRule polyphonyWord = null; int lineNum = 0; string domain = DomainItem.GeneralDomain; foreach (string line in Helper.FileLines(filePath, Encoding.Unicode, false)) { lineNum++; string trimedLine = line.Trim(); if (string.IsNullOrEmpty(trimedLine)) { continue; } if (IsComment(trimedLine)) { continue; } if (IsDomainTag(trimedLine)) { ParseDomainKey(trimedLine, ref domain); continue; } ErrorSet parseErrorSet = new ErrorSet(); if (!finishReadHead) { bool isKeyDeclear = TryParseKeyDeclear(trimedLine, ref firstKeyString, parseErrorSet); AddParseError(errorSet, lineNum, parseErrorSet); if (isKeyDeclear) { continue; } else { finishReadHead = true; } } PolyruleKeys.Instance.KeyTypes = _keyTypes; parseErrorSet.Clear(); bool isKeyLine = TryParseKeyLine(trimedLine, ref polyphonyWord, parseErrorSet, domain); domain = DomainItem.GeneralDomain; AddParseError(errorSet, lineNum, parseErrorSet); if (isKeyLine) { continue; } parseErrorSet.Clear(); bool isConditionLine = TryParseConditionLine(trimedLine, phoneSet, polyphonyWord, parseErrorSet); AddParseError(errorSet, lineNum, parseErrorSet); if (isConditionLine) { continue; } errorSet.Add(PolyRuleError.InvalidLineFormat, lineNum.ToString(CultureInfo.InvariantCulture), trimedLine); } if (polyphonyWord != null) { _polyphonyWords.Add(polyphonyWord); } if (string.IsNullOrEmpty(_keyString)) { errorSet.Add(PolyRuleError.MissPrimaryKey, filePath); } errorSet.AddRange(CheckDupWordDefinitions()); foreach (PolyphonyRule rule in _polyphonyWords) { errorSet.AddRange(rule.CheckDupRuleConditions()); } return errorSet; }
/// <summary> /// Parse. /// </summary> /// <param name="line">Line.</param> /// <param name="attributeSchema">LexicalAttributeSchema.</param> /// <returns>ErrorSet.</returns> public ErrorSet Parse(string line, LexicalAttributeSchema attributeSchema) { if (string.IsNullOrEmpty(line)) { throw new ArgumentNullException("line"); } ErrorSet errorSet = new ErrorSet(); _words.Clear(); string[] wordWithPosTags = line.Split(_wordDelimeters, StringSplitOptions.RemoveEmptyEntries); foreach (string wordWithPosTag in wordWithPosTags) { PosCorpusWord word = new PosCorpusWord(); ErrorSet wordErrorSet = word.Parse(wordWithPosTag, attributeSchema); errorSet.AddRange(wordErrorSet); if (wordErrorSet.Count == 0) { _words.Add(word); } } return errorSet; }