mirror of
https://github.com/SubtitleEdit/subtitleedit.git
synced 2024-11-22 11:12:36 +01:00
Merge pull request #2093 from ivandrofly/unneded_periods
[FixUnneededPeriods] - Update/Optimize.
This commit is contained in:
commit
66d7aaf8b1
@ -4,57 +4,53 @@ namespace Nikse.SubtitleEdit.Core.Forms.FixCommonErrors
|
||||
{
|
||||
public class FixUnneededPeriods : IFixCommonError
|
||||
{
|
||||
|
||||
public void Fix(Subtitle subtitle, IFixCallbacks callbacks)
|
||||
{
|
||||
var language = Configuration.Settings.Language.FixCommonErrors;
|
||||
string fixAction = language.UnneededPeriod;
|
||||
int unneededPeriodsFixed = 0;
|
||||
int removedCount = 0;
|
||||
for (int i = 0; i < subtitle.Paragraphs.Count; i++)
|
||||
{
|
||||
Paragraph p = subtitle.Paragraphs[i];
|
||||
var oldText = p.Text;
|
||||
if (callbacks.AllowFix(p, fixAction))
|
||||
{
|
||||
if (p.Text.Contains("!." + Environment.NewLine))
|
||||
// Returns processed text.
|
||||
string procText = RemoveDotAfterPunctuation(p.Text);
|
||||
int diff = p.Text.Length - procText.Length;
|
||||
if (diff > 0)
|
||||
{
|
||||
p.Text = p.Text.Replace("!." + Environment.NewLine, "!" + Environment.NewLine);
|
||||
unneededPeriodsFixed++;
|
||||
// Calculate total removed dots.
|
||||
removedCount += diff;
|
||||
callbacks.AddFixToListView(p, fixAction, p.Text, procText);
|
||||
p.Text = procText;
|
||||
}
|
||||
if (p.Text.Contains("?." + Environment.NewLine))
|
||||
{
|
||||
p.Text = p.Text.Replace("?." + Environment.NewLine, "?" + Environment.NewLine);
|
||||
unneededPeriodsFixed++;
|
||||
}
|
||||
if (p.Text.EndsWith("!.", StringComparison.Ordinal))
|
||||
{
|
||||
p.Text = p.Text.TrimEnd('.');
|
||||
unneededPeriodsFixed++;
|
||||
}
|
||||
if (p.Text.EndsWith("?.", StringComparison.Ordinal))
|
||||
{
|
||||
p.Text = p.Text.TrimEnd('.');
|
||||
unneededPeriodsFixed++;
|
||||
}
|
||||
|
||||
var len = p.Text.Length;
|
||||
if (p.Text.Contains("!. "))
|
||||
{
|
||||
p.Text = p.Text.Replace("!. ", "! ");
|
||||
unneededPeriodsFixed += len - p.Text.Length;
|
||||
len = p.Text.Length;
|
||||
}
|
||||
if (p.Text.Contains("?. "))
|
||||
{
|
||||
p.Text = p.Text.Replace("?. ", "? ");
|
||||
unneededPeriodsFixed += len - p.Text.Length;
|
||||
}
|
||||
|
||||
if (p.Text != oldText)
|
||||
callbacks.AddFixToListView(p, fixAction, oldText, p.Text);
|
||||
}
|
||||
}
|
||||
callbacks.UpdateFixStatus(unneededPeriodsFixed, language.RemoveUnneededPeriods, string.Format(language.XUnneededPeriodsRemoved, unneededPeriodsFixed));
|
||||
callbacks.UpdateFixStatus(removedCount, language.RemoveUnneededPeriods, string.Format(language.XUnneededPeriodsRemoved, removedCount));
|
||||
}
|
||||
|
||||
public static string RemoveDotAfterPunctuation(string inp)
|
||||
{
|
||||
for (int i = inp.Length - 1; i > 0; i--)
|
||||
{
|
||||
// Expecting pre characters: [?!]
|
||||
if (inp[i] == '.' && (inp[i - 1] == '?' || inp[i - 1] == '!'))
|
||||
{
|
||||
int j = i;
|
||||
// Fix recursive dot after ?/!
|
||||
while (j + 1 < inp.Length && inp[j + 1] == '.')
|
||||
{
|
||||
j++;
|
||||
}
|
||||
// Expecting post characters: [\r\n ]
|
||||
if (j + 1 == inp.Length || inp[j + 1] == ' ' || inp[j + 1] == '\r' || inp[j + 1] == '\n')
|
||||
{
|
||||
inp = inp.Remove(i, j - i + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return inp;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1693,5 +1693,32 @@ namespace Test
|
||||
|
||||
#endregion
|
||||
|
||||
#region << Fix unneeded periods after [?!] >>
|
||||
|
||||
[TestMethod]
|
||||
public void FixUnneededPeriodsTest1()
|
||||
{
|
||||
string processedText = FixUnneededPeriods.RemoveDotAfterPunctuation("Foobar?.\r\nFoobar!.\r\nFoobar");
|
||||
Assert.AreEqual("Foobar?\r\nFoobar!\r\nFoobar", processedText);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FixUnneededPeriodsTest2()
|
||||
{
|
||||
string processedText = FixUnneededPeriods.RemoveDotAfterPunctuation("Foobar?.");
|
||||
Assert.AreEqual("Foobar?", processedText);
|
||||
|
||||
processedText = FixUnneededPeriods.RemoveDotAfterPunctuation("Foobar!.");
|
||||
Assert.AreEqual("Foobar!", processedText);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FixUnneededPeriodsTest3()
|
||||
{
|
||||
string processedText = FixUnneededPeriods.RemoveDotAfterPunctuation("Foobar?. Foobar!.... Foobar");
|
||||
Assert.AreEqual("Foobar? Foobar! Foobar", processedText);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user