Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public string FixCommonWordErrorsQuick(string originalWord)
- {
- var word = originalWord;
- // always replace list
- foreach (var replacementKey in _partialWordAlwaysReplaceList.Keys)
- {
- // ensure replacement can be in word
- if (word.Length >= replacementKey.Length)
- {
- word = word.Replace(replacementKey, _partialWordAlwaysReplaceList[replacementKey]);
- }
- }
- int l = 0;
- var prefixSymbols = new HashSet<char> { '-', '.', '"', '(', '\r', '\n' };
- var knownOpenTags = new HashSet<string> {"<i>", "<u>", "<b>"};
- for (int r = 0; r < word.Length; r++)
- {
- var c = word[r];
- if (c == '<')
- {
- l = r;
- }
- else if (c == '>' && word[l] == '<') // tag
- {
- var tag = word.Substring(l, r - l + 1);
- if (knownOpenTags.Contains(tag) || tag.StartsWith("<font ", StringComparison.Ordinal))
- {
- l = r + 1; // jump tag
- }
- else
- {
- l = r; // stay in tag and break the loop
- break;
- }
- }
- else if (!prefixSymbols.Contains(c))
- {
- l = r;
- break;
- }
- }
- // create new word without prefixes
- word = word.Substring(l);
- var suffixSymbols = new HashSet<char> { '"', '.', ',', '?', '!', ')' };
- var knownClosingTags = new HashSet<string> { "</i>", "</b>", "</u>", "</font>" };
- int k = word.Length - 1;
- for (int i = word.Length - 1; i >= 0; i--)
- {
- var c = word[i];
- if (c == '>')
- {
- k = i;
- }
- else if (c == '<' && word[k] == '>')
- {
- if (knownClosingTags.Contains(word.Substring(i, k - i - 1)))
- {
- k = i; // move k to the beginning of closing tag
- }
- // else k doesn't change anything after k is a suffix
- }
- else if (!suffixSymbols.Contains(c))
- {
- k = i;
- break;
- }
- }
- // create a word without prefix and suffix
- word = word.Substring(0, k + 1);
- var prefix = originalWord.Substring(0, l);
- var suffix = originalWord.Substring(k + 1);
- if (word.Length == 0)
- {
- return prefix + suffix;
- }
- if (GetReplaceWord(prefix, word, suffix, out var res))
- {
- return res;
- }
- return prefix + word + suffix;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement