/// <summary> /// Find whether current input word is matched to this exception rule. /// Check if current word is matched to any of certain regexes. /// </summary> /// <param name="processContext">A processing context which lives until the process is finished, /// and stores data for the process</param> /// <returns></returns> public override bool IsMatch(AnalysisProcessContext processContext) { bool isMatched = false; string inputWord = processContext.Word; char sign = processContext.Sign; for (int i = 0; i < RegexList.Count && !isMatched; i++) { var regex = new Regex(RegexList[i]); isMatched = regex.IsMatch(inputWord); if (!isMatched) { //Check if word without end stop signs is matched (eg. Check the word "Hi!!" as "Hi".) isMatched = regex.IsMatch(processContext.WordWithEndTrim); if (isMatched) { processContext.Output.WordCompleteSentenceAtIndex = inputWord.Count(); } else { isMatched = IsPartOfWordMatched(processContext, inputWord, regex); } } } return(isMatched); }
/// <summary> /// Find whether to add a space after the word location which has been found /// </summary> /// <param name="processContext">A processing context which lives until the process is finished, /// and stores data for the process</param> /// <param name="matchedWord"></param> protected virtual void AddSeparatorSpaceIfNecessary(AnalysisProcessContext processContext, string matchedWord) { string word = processContext.Word; if (!word.Equals(matchedWord)) { int matchedWordCount = matchedWord.Count(); processContext.Output.AddSpaceAtIndex = matchedWordCount - processContext.StopSignIndexIntoWord - 1; } }
/// <summary> /// Find whether current input word is matched to this exception rule. /// Check if current word start with any of certain words. /// </summary> /// <param name="processContext">A processing context which lives until the process is finished, /// and stores data for the process</param> /// <returns></returns> public override bool IsMatch(AnalysisProcessContext processContext) { bool isMatch = false; string word = processContext.Word; char sign = processContext.Sign; string wordFound = ReservedWords?.FirstOrDefault(_shortcut => word.StartsWith(_shortcut)); if (!string.IsNullOrEmpty(wordFound)) { isMatch = true; AddSeparatorSpaceIfNecessary(processContext, wordFound); } return(isMatch); }
/// <summary> /// Try seperrate word by available stop signs, /// and check if part of the word is matched to a specific regex /// </summary> /// <param name="processContext"></param> /// <param name="inputWord"></param> /// <param name="regex"></param> /// <returns></returns> private bool IsPartOfWordMatched(AnalysisProcessContext processContext, string inputWord, Regex regex) { int endSignIndex = inputWord.Count(); string newWord = string.Empty; bool isMatched = false; do { endSignIndex = inputWord.LastIndexOfAny(processContext.AnalysisConfiguration.Signs.ToArray(), endSignIndex - 1); if (endSignIndex > -1) { newWord = inputWord.Substring(0, endSignIndex); isMatched = regex.IsMatch(newWord); } }while (!isMatched && endSignIndex > 0); if (isMatched) { processContext.Output.WordCompleteSentenceAtIndex = newWord.Count(); } return(isMatched); }
public abstract bool IsMatch(AnalysisProcessContext processContext);