mirror of
https://github.com/SubtitleEdit/subtitleedit.git
synced 2024-10-28 06:52:35 +01:00
2fb27aca26
Fix #4137
93 lines
3.7 KiB
C#
93 lines
3.7 KiB
C#
using Nikse.SubtitleEdit.Core.Interfaces;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace Nikse.SubtitleEdit.Core.Forms.FixCommonErrors
|
|
{
|
|
public class FixCommas : IFixCommonError
|
|
{
|
|
public void Fix(Subtitle subtitle, IFixCallbacks callbacks)
|
|
{
|
|
var commaDouble = new Regex(@"([\p{L}\d\s]),,([\p{L}\d\s])");
|
|
var commaTriple = new Regex(@"([\p{L}\d\s]) *, *, *,([\p{L}\d\s])");
|
|
var commaTripleEndOfLine = new Regex(@"([\p{L}\d\s]), *, *,$");
|
|
var commaWhiteSpaceBetween = new Regex(@"([\p{L}\d\s]),\s+,([\p{L}\d\s])");
|
|
var commaFollowedByLetter = new Regex(@",(\p{L})");
|
|
|
|
string fixAction = Configuration.Settings.Language.FixCommonErrors.FixCommas;
|
|
int fixCount = 0;
|
|
for (int i = 0; i < subtitle.Paragraphs.Count; i++)
|
|
{
|
|
var p = subtitle.Paragraphs[i];
|
|
if (callbacks.AllowFix(p, fixAction))
|
|
{
|
|
var s = p.Text;
|
|
var oldText = s;
|
|
|
|
if (p.Text.IndexOf(',') >= 0)
|
|
{
|
|
s = commaDouble.Replace(s, "$1,$2");
|
|
s = commaTriple.Replace(s, "$1...$2");
|
|
s = commaTripleEndOfLine.Replace(s, "$1...");
|
|
s = commaWhiteSpaceBetween.Replace(s, "$1,$2");
|
|
s = commaFollowedByLetter.Replace(s, ", $1");
|
|
|
|
s = RemoveCommaBeforeSentenceEndingChar(s, ',');
|
|
}
|
|
|
|
if (p.Text.IndexOf('،') >= 0)
|
|
{
|
|
var commaDoubleAr = new Regex(@"([\p{L}\d\s]) *،،([\p{L}\d\s])");
|
|
var commaTripleAr = new Regex(@"([\p{L}\d\s]) *، *، *،([\p{L}\d\s])");
|
|
var commaTripleEndOfLineAr = new Regex(@"([\p{L}\d\s]) *، *، *،$");
|
|
var commaWhiteSpaceBetweenAr = new Regex(@"([\p{L}\d\s]) *،\s+،([\p{L}\d\s])");
|
|
var commaFollowedByLetterAr = new Regex(@"،(\p{L})");
|
|
s = commaDoubleAr.Replace(s, "$1،$2");
|
|
s = commaTripleAr.Replace(s, "$1...$2");
|
|
s = commaTripleEndOfLineAr.Replace(s, "$1...");
|
|
s = commaWhiteSpaceBetweenAr.Replace(s, "$1،$2");
|
|
s = commaFollowedByLetterAr.Replace(s, "، $1");
|
|
|
|
s = RemoveCommaBeforeSentenceEndingChar(s, '،');
|
|
}
|
|
|
|
if (oldText != s)
|
|
{
|
|
fixCount++;
|
|
callbacks.AddFixToListView(p, fixAction, oldText, s);
|
|
p.Text = s;
|
|
}
|
|
}
|
|
}
|
|
callbacks.UpdateFixStatus(fixCount, Configuration.Settings.Language.FixCommonErrors.FixCommas, fixCount.ToString());
|
|
}
|
|
|
|
private static string RemoveCommaBeforeSentenceEndingChar(string input, char comma)
|
|
{
|
|
var s = input;
|
|
for (int i = s.Length - 1; i >= 0; i--)
|
|
{
|
|
char ch = s[i];
|
|
if (i - 1 >= 0 && s[i - 1] == comma && IsSentenceEndingChar(ch))
|
|
{
|
|
int k = i;
|
|
|
|
do
|
|
{
|
|
i--;
|
|
} while (i - 1 >= 0 && s[i - 1] == comma);
|
|
|
|
// remove commas
|
|
if (k - i > 0)
|
|
{
|
|
s = s.Remove(i, k - i);
|
|
}
|
|
}
|
|
}
|
|
|
|
return s;
|
|
}
|
|
|
|
private static bool IsSentenceEndingChar(char ch) => ch == '.' || ch == '!' || ch == '?' || ch == ')' || ch == ']' || ch == ']' || ch == '؟';
|
|
}
|
|
}
|