Skip to content

Commit 21a2c49

Browse files
committed
perf: improve similarity and regex
1 parent 49ee233 commit 21a2c49

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

src/models/Caption.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ public static Caption GetInstance()
9494

9595
public string GetPreviousText(int count, TextType textType)
9696
{
97-
if (count <= 0)
97+
if (count <= 0 || Contexts.Count == 0)
9898
return string.Empty;
9999

100100
var prev = Contexts
101101
.Reverse().Take(count).Reverse()
102-
.Select(entry => string.CompareOrdinal(entry.TranslatedText, "N/A") == 0 ||
102+
.Select(entry => entry == null || string.CompareOrdinal(entry.TranslatedText, "N/A") == 0 ||
103103
entry.TranslatedText.Contains("[ERROR]") || entry.TranslatedText.Contains("[WARNING]") ?
104104
"" : (textType == TextType.Caption ? entry.SourceText : entry.TranslatedText))
105105
.Aggregate((accu, cur) =>
@@ -126,12 +126,12 @@ public string GetPreviousText(int count, TextType textType)
126126

127127
public IEnumerable<TranslationHistoryEntry> GetPreviousContexts(int count)
128128
{
129-
if (count <= 0)
130-
return Enumerable.Empty<TranslationHistoryEntry>();
129+
if (count <= 0 || Contexts.Count == 0)
130+
return [];
131131

132132
return Contexts
133133
.Reverse().Take(count).Reverse()
134-
.Where(entry => string.CompareOrdinal(entry.TranslatedText, "N/A") != 0 &&
134+
.Where(entry => entry != null && string.CompareOrdinal(entry.TranslatedText, "N/A") != 0 &&
135135
!entry.TranslatedText.Contains("[ERROR]") &&
136136
!entry.TranslatedText.Contains("[WARNING]"));
137137
}

src/utils/RegexPatterns.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public static partial class RegexPatterns
3737

3838
[GeneratedRegex(@"[^0-9.]")]
3939
public static partial Regex VersionNumber();
40-
41-
[GeneratedRegex(@"🔤\s*(.*)\s*🔤")]
40+
41+
[GeneratedRegex(@"🔤\s*(.+?)\s*🔤")]
4242
public static partial Regex TargetSentence();
4343
}
4444
}

src/utils/TextUtil.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static class TextUtil
1212
public const int LONG_THRESHOLD = 160;
1313
public const int VERYLONG_THRESHOLD = 220;
1414

15-
public const double SIM_THRESHOLD = 0.7;
15+
public const double SIM_THRESHOLD = 0.6;
1616

1717
public static string ShortenDisplaySentence(string text, int maxByteLength)
1818
{

0 commit comments

Comments
 (0)