Skip to content

Commit 032c241

Browse files
committed
Refactor line processing logic for text formatting
Introduced a `Formatting` utility to handle text normalization and reapplication of formatting tags during processing. Replaced old logic to ensure better handling of trimmed lines and adjusted dash-prefix removal. Updated comparison logic to utilize comma counts for improved consistency checking.
1 parent 05aeec4 commit 032c241

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

source/Commas/Main.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Diagnostics;
22
using Nikse.SubtitleEdit.Core.Common;
3+
using Nikse.SubtitleEdit.Core.Translate;
34
using Nikse.SubtitleEdit.PluginLogic;
45
using Nikse.SubtitleEdit.PluginLogic.Helpers;
56

@@ -113,19 +114,39 @@ await Task.Run(async () =>
113114

114115
// try sending each line individually to avoid messing up with the line that
115116
// doesn't require fixing which tends to happen when using less capable models
117+
118+
var formatting = new Formatting();
119+
120+
// todo:
121+
// - skip hi text e.g.: (Footsteps approaching)
122+
// - for narrator text like: "narrator: hello world", only process "hello world"
116123
for (var i = 0; i < lines.Count; i++)
117124
{
118125
string line = lines[i];
119126

120127
// remove formatting before sending as some models also remove formattings unintentionally
121-
var st = new StrippableText(line);
122-
st.StrippedText = await lmStudioClient.SendAsync(st.StrippedText).ConfigureAwait(false);
123-
lines[i] = st.MergedString;
128+
// var st = new StrippableText(line);
129+
// st.StrippedText = await lmStudioClient.SendAsync(st.StrippedText).ConfigureAwait(false);
130+
// lines[i] = st.MergedString;
131+
132+
line = formatting.SetTagsAndReturnTrimmed(line, "en");
133+
line = await lmStudioClient.SendAsync(line).ConfigureAwait(false);
134+
line = formatting.ReAddFormatting(line);
135+
if (line.StartsWith("- ", StringComparison.Ordinal) && !lines[i].Contains('-'))
136+
{
137+
line = line.TrimStart('-').Trim();
138+
}
139+
140+
lines[i] = line;
124141
}
125142

126143
string result = string.Join(Environment.NewLine, lines);
144+
result = Utilities.AutoBreakLine(result);
127145

128-
if (!result.Equals(paragraph.Text, StringComparison.Ordinal))
146+
var beforeFixCommaCount = Utilities.CountTagInText(paragraph.Text, ',');
147+
var afterFixCommaCount = Utilities.CountTagInText(result, ',');
148+
149+
if (beforeFixCommaCount != afterFixCommaCount)
129150
{
130151
progress.Report((new ListViewItem(new[] { paragraph.Text, result })
131152
{

0 commit comments

Comments
 (0)