Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[strippabletext] - update part 2 #2813

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 37 additions & 24 deletions libse/StrippableText.cs
Expand Up @@ -116,40 +116,53 @@ private void ReplaceNames1Remove(List<string> nameList, List<string> replaceIds,
Post = Post.Remove(0, 1);
}

string lower = StrippedText.ToLower();
const string expectedChars = " ,.!?:;\')]- <”\"\r\n";
int idName = 0;
foreach (string name in nameList)
{
int start = lower.IndexOf(name.ToLowerInvariant(), StringComparison.Ordinal);
while (start >= 0 && start < lower.Length)
{
bool startOk = (start == 0) || (lower[start - 1] == ' ') || (lower[start - 1] == '-') ||
(lower[start - 1] == '"') || (lower[start - 1] == '\'') || (lower[start - 1] == '>') || (lower[start - 1] == '[') || (lower[start - 1] == '“') ||
Environment.NewLine.EndsWith(lower[start - 1]);
int idx = StrippedText.IndexOf(name, StringComparison.OrdinalIgnoreCase);
int nameLen = name.Length;

if (startOk && string.CompareOrdinal(name, "Don") == 0 && lower.Substring(start).StartsWith("don't", StringComparison.Ordinal))
startOk = false;
while (idx >= 0)
{
bool isWord = true;
if (idx > 0)
{
char preChar = StrippedText[idx - 1];
isWord = preChar == ' ' || preChar == '"' || preChar == '\'' || preChar == '-' ||
preChar == '\n' || preChar == '\r' || preChar == '>' || preChar == '“' ||
preChar == '"' || preChar == '[' || preChar == '(';
}

if (startOk)
if (isWord)
{
int end = start + name.Length;
bool endOk = end <= lower.Length;
if (endOk)
endOk = end == lower.Length || (@" ,.!?:;')]- <”""" + Environment.NewLine).Contains(lower[end]);
if (name.Equals("Don", StringComparison.Ordinal) && idx + 5 <= StrippedText.Length)
{
isWord = StrippedText.Substring(idx, 5).Equals("don't", StringComparison.OrdinalIgnoreCase);
}
}

if (endOk && StrippedText.Length >= start + name.Length)
int nextIdx = idx + nameLen;
if (isWord)
{
// check for word boundary
if (idx + nameLen == StrippedText.Length || expectedChars.Contains(StrippedText[idx + nameLen]))
{
string originalName = StrippedText.Substring(start, name.Length);
originalNames.Add(originalName);
StrippedText = StrippedText.Remove(start, name.Length);
StrippedText = StrippedText.Insert(start, GetAndInsertNextId(replaceIds, replaceNames, name, idName++));
lower = StrippedText.ToLower();
originalNames.Add(StrippedText.Substring(idx, nameLen));
int prevLen = StrippedText.Length;
StrippedText = StrippedText.Remove(idx, nameLen);
StrippedText = StrippedText.Insert(idx, GetAndInsertNextId(replaceIds, replaceNames, name, idName++));
// compute next lookup index
nextIdx -= (prevLen - StrippedText.Length);
}
}
if (start + 3 > lower.Length)
start = lower.Length + 1;
else
start = lower.IndexOf(name, start + 3, StringComparison.OrdinalIgnoreCase);

if (nextIdx >= StrippedText.Length)
{
break;
}

idx = StrippedText.IndexOf(name, nextIdx, StringComparison.OrdinalIgnoreCase);
}
}

Expand Down