mirror of
https://github.com/SubtitleEdit/subtitleedit.git
synced 2024-11-25 20:52:44 +01:00
Fix issue with merge text and incrementing lines - thx tigre7575 :)
Fix #23
This commit is contained in:
parent
0c793ab239
commit
b6ac2dc820
@ -1,53 +1,68 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Nikse.SubtitleEdit.Core
|
||||
{
|
||||
public static class MergeLinesSameTextUtils
|
||||
{
|
||||
public static Subtitle MergeLinesWithSameTextInSubtitle(Subtitle subtitle, bool fixIncrementing, bool lineAfterNext, int maxMsBetween)
|
||||
public static Subtitle MergeLinesWithSameTextInSubtitle(Subtitle subtitle, bool fixIncrementing, int maxMsBetween)
|
||||
{
|
||||
var mergedIndexes = new List<int>();
|
||||
var removed = new List<int>();
|
||||
var mergedSubtitle = new Subtitle();
|
||||
bool lastMerged = false;
|
||||
Paragraph p = null;
|
||||
for (int i = 1; i < subtitle.Paragraphs.Count; i++)
|
||||
{
|
||||
if (!lastMerged)
|
||||
if (removed.Contains(i - 1))
|
||||
{
|
||||
p = new Paragraph(subtitle.GetParagraphOrDefault(i - 1));
|
||||
mergedSubtitle.Paragraphs.Add(p);
|
||||
continue;
|
||||
}
|
||||
var next = subtitle.GetParagraphOrDefault(i);
|
||||
var afterNext = subtitle.GetParagraphOrDefault(i + 1);
|
||||
if (next != null)
|
||||
|
||||
var p = new Paragraph(subtitle.GetParagraphOrDefault(i - 1));
|
||||
mergedSubtitle.Paragraphs.Add(p);
|
||||
|
||||
for (int j = i; j < subtitle.Paragraphs.Count; j++)
|
||||
{
|
||||
if (QualifiesForMerge(p, next, maxMsBetween) || fixIncrementing && QualifiesForMergeIncrement(p, next, maxMsBetween))
|
||||
if (removed.Contains(j))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var next = subtitle.GetParagraphOrDefault(j);
|
||||
var incrementText = string.Empty;
|
||||
if ((QualifiesForMerge(p, next, maxMsBetween) || fixIncrementing && QualifiesForMergeIncrement(p, next, maxMsBetween, out incrementText)))
|
||||
{
|
||||
p.Text = next.Text;
|
||||
p.EndTime = next.EndTime;
|
||||
lastMerged = true;
|
||||
if (!string.IsNullOrEmpty(incrementText))
|
||||
{
|
||||
p.Text = incrementText;
|
||||
}
|
||||
|
||||
p.EndTime.TotalMilliseconds = next.EndTime.TotalMilliseconds;
|
||||
removed.Add(j);
|
||||
if (!mergedIndexes.Contains(j))
|
||||
{
|
||||
mergedIndexes.Add(j);
|
||||
}
|
||||
|
||||
if (!mergedIndexes.Contains(i - 1))
|
||||
{
|
||||
mergedIndexes.Add(i - 1);
|
||||
}
|
||||
}
|
||||
else if (lineAfterNext && QualifiesForMerge(p, afterNext, maxMsBetween) && p.Duration.TotalMilliseconds > afterNext.Duration.TotalMilliseconds)
|
||||
{
|
||||
lastMerged = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
lastMerged = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
lastMerged = false;
|
||||
}
|
||||
}
|
||||
if (!lastMerged)
|
||||
|
||||
if (!mergedIndexes.Contains(subtitle.Paragraphs.Count - 1))
|
||||
{
|
||||
mergedSubtitle.Paragraphs.Add(new Paragraph(subtitle.GetParagraphOrDefault(subtitle.Paragraphs.Count - 1)));
|
||||
}
|
||||
|
||||
mergedSubtitle.Renumber();
|
||||
return mergedSubtitle;
|
||||
}
|
||||
|
||||
|
||||
public static bool QualifiesForMerge(Paragraph p, Paragraph next, int maxMsBetween)
|
||||
{
|
||||
if (p == null || next == null)
|
||||
@ -62,31 +77,45 @@ namespace Nikse.SubtitleEdit.Core
|
||||
|
||||
if (p.Text != null && next.Text != null)
|
||||
{
|
||||
string s = HtmlUtil.RemoveHtmlTags(p.Text.Trim());
|
||||
string s2 = HtmlUtil.RemoveHtmlTags(next.Text.Trim());
|
||||
return string.Compare(s, s2, StringComparison.OrdinalIgnoreCase) == 0;
|
||||
string currentTextNoTags = HtmlUtil.RemoveHtmlTags(p.Text.Trim());
|
||||
string nextTextNoTags = HtmlUtil.RemoveHtmlTags(next.Text.Trim());
|
||||
return string.Compare(currentTextNoTags, nextTextNoTags, StringComparison.OrdinalIgnoreCase) == 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool QualifiesForMergeIncrement(Paragraph p, Paragraph next, int maxMsBetween)
|
||||
public static bool QualifiesForMergeIncrement(Paragraph current, Paragraph next, int maxMsBetween, out string text)
|
||||
{
|
||||
if (p == null || next == null)
|
||||
text = string.Empty;
|
||||
if (current == null || next == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (next.StartTime.TotalMilliseconds - p.EndTime.TotalMilliseconds > maxMsBetween)
|
||||
if (next.StartTime.TotalMilliseconds - current.EndTime.TotalMilliseconds > maxMsBetween)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (p.Text != null && next.Text != null)
|
||||
if (current.Text != null && next.Text != null)
|
||||
{
|
||||
var s = HtmlUtil.RemoveHtmlTags(p.Text.Trim());
|
||||
var s2 = HtmlUtil.RemoveHtmlTags(next.Text.Trim());
|
||||
if (!string.IsNullOrEmpty(s) && s2.Length > 0 && s2.StartsWith(s, StringComparison.OrdinalIgnoreCase))
|
||||
var currentTextNoTags = HtmlUtil.RemoveHtmlTags(current.Text.Trim());
|
||||
var nextTextNoTags = HtmlUtil.RemoveHtmlTags(next.Text.Trim());
|
||||
if (string.IsNullOrEmpty(currentTextNoTags) || string.IsNullOrEmpty(nextTextNoTags))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (nextTextNoTags.StartsWith(currentTextNoTags, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
text = next.Text;
|
||||
return true;
|
||||
}
|
||||
|
||||
var lines = currentTextNoTags.SplitToLines();
|
||||
if (lines.Count > 1 && lines.Last().Equals(nextTextNoTags, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
text = current.Text;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -1246,7 +1246,7 @@ namespace Nikse.SubtitleEdit.Forms
|
||||
|
||||
if (IsActionEnabled(CommandLineConverter.BatchAction.MergeSameTexts))
|
||||
{
|
||||
var mergedSameTextsSub = MergeLinesSameTextUtils.MergeLinesWithSameTextInSubtitle(sub, true, true, 250);
|
||||
var mergedSameTextsSub = MergeLinesSameTextUtils.MergeLinesWithSameTextInSubtitle(sub, true, 250);
|
||||
if (mergedSameTextsSub.Paragraphs.Count != sub.Paragraphs.Count)
|
||||
{
|
||||
sub.Paragraphs.Clear();
|
||||
|
@ -138,9 +138,15 @@ namespace Nikse.SubtitleEdit.Forms
|
||||
}
|
||||
|
||||
var next = subtitle.GetParagraphOrDefault(j);
|
||||
if ((MergeLinesSameTextUtils.QualifiesForMerge(p, next, maxMsBetween) || fixIncrementing && MergeLinesSameTextUtils.QualifiesForMergeIncrement(p, next, maxMsBetween)) && IsFixAllowed(p))
|
||||
var incrementText = string.Empty;
|
||||
if ((MergeLinesSameTextUtils.QualifiesForMerge(p, next, maxMsBetween) || fixIncrementing && MergeLinesSameTextUtils.QualifiesForMergeIncrement(p, next, maxMsBetween, out incrementText)) && IsFixAllowed(p))
|
||||
{
|
||||
p.Text = next.Text;
|
||||
if (!string.IsNullOrEmpty(incrementText))
|
||||
{
|
||||
p.Text = incrementText;
|
||||
}
|
||||
|
||||
p.EndTime.TotalMilliseconds = next.EndTime.TotalMilliseconds;
|
||||
if (lineNumbers.Length > 0)
|
||||
{
|
||||
|
@ -1156,7 +1156,7 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
|
||||
}
|
||||
break;
|
||||
case BatchAction.MergeSameTexts:
|
||||
var mergedSameTextsSub = MergeLinesSameTextUtils.MergeLinesWithSameTextInSubtitle(sub, true, true, 250);
|
||||
var mergedSameTextsSub = MergeLinesSameTextUtils.MergeLinesWithSameTextInSubtitle(sub, true, 250);
|
||||
if (mergedSameTextsSub.Paragraphs.Count != sub.Paragraphs.Count)
|
||||
{
|
||||
sub.Paragraphs.Clear();
|
||||
|
Loading…
Reference in New Issue
Block a user