-
Notifications
You must be signed in to change notification settings - Fork 2
New translation engine Ollama #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -30,6 +30,7 @@ public static class TranslatorManager | |||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| private static readonly ConcurrentDictionary<string, byte> _inQueue = new ConcurrentDictionary<string, byte>(); | ||||||||||||||||||||||||||||||||||||||||
| private static Task _translationThread; | ||||||||||||||||||||||||||||||||||||||||
| private static Task[] _translationThreads; | ||||||||||||||||||||||||||||||||||||||||
| private static Timer _cacheSaver; | ||||||||||||||||||||||||||||||||||||||||
| private static readonly Regex StringFormatSymbolsRegex = new Regex("{.*?}"); | ||||||||||||||||||||||||||||||||||||||||
| private static readonly StringBuilder sb = new StringBuilder(1024); | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -43,15 +44,13 @@ public static void Prepare() | |||||||||||||||||||||||||||||||||||||||
| foreach (var translatorType in translatorTypes) | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| var t = (ITranslator)Activator.CreateInstance(translatorType); | ||||||||||||||||||||||||||||||||||||||||
| t.Prepare(); | ||||||||||||||||||||||||||||||||||||||||
| translators.Add(t); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| CurrentTranslator = GetTranslator(Settings.TranslatorName); | ||||||||||||||||||||||||||||||||||||||||
| if (CurrentTranslator?.Ready == false) CurrentTranslator = null; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if (CurrentTranslator == null) | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| CurrentTranslator = translators.FirstOrDefault(x => x.Ready); | ||||||||||||||||||||||||||||||||||||||||
| CurrentTranslator = translators.FirstOrDefault(x => x.Name == "Google"); | ||||||||||||||||||||||||||||||||||||||||
| Log.Error(AutoTranslation.LogPrefix + | ||||||||||||||||||||||||||||||||||||||||
| $"Selected translator named {Settings.TranslatorName} is not ready, changing to other translator.. {CurrentTranslator?.Name}"); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -129,50 +128,15 @@ public static void StartThread() | |||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| Working = true; | ||||||||||||||||||||||||||||||||||||||||
| _translationThread = Task.Factory.StartNew(() => | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| while (true) | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| if (!Working) | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| Thread.Sleep(1000); | ||||||||||||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| while (_queue.Count > 0) | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| if (!_queue.TryDequeue(out var pair)) continue; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| _inQueue.TryRemove(pair.Key, out _); | ||||||||||||||||||||||||||||||||||||||||
| var translated = string.Empty; | ||||||||||||||||||||||||||||||||||||||||
| var success = true; | ||||||||||||||||||||||||||||||||||||||||
| if (pair.Key.Length > 200) | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| translated = pair.Key.Tokenize().Aggregate(translated, (current, token) => | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| success &= CurrentTranslator.TryTranslate(token, out var tmp); | ||||||||||||||||||||||||||||||||||||||||
| return current + ' ' + tmp; | ||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| success = CurrentTranslator.TryTranslate(pair.Key, out translated); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if (success) | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| translated = PolishText(translated); | ||||||||||||||||||||||||||||||||||||||||
| //translated = UnityWebRequest.UnEscapeURL(translated, Encoding.UTF8).Trim(); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| workCnt++; | ||||||||||||||||||||||||||||||||||||||||
| //_finished.Enqueue(pair); | ||||||||||||||||||||||||||||||||||||||||
| pair.Value(translated, success); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| Thread.Sleep(1000); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| }).ContinueWith(t => | ||||||||||||||||||||||||||||||||||||||||
| _translationThreads = new Task[Settings.ConcurrentWorkerCount]; | ||||||||||||||||||||||||||||||||||||||||
| for (int i = 0; i < Settings.ConcurrentWorkerCount; i++) | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| Log.Warning($"Translation thread was killed! {t.Exception?.Message}"); | ||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||
| _translationThreads[i] = Task.Factory.StartNew(DoInnerThreadWork).ContinueWith(t => | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| Log.Warning($"Translation thread {i} was killed! {t.Exception?.Message}"); | ||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+132
to
+139
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix closure capture bug in thread completion handlers. The loop variable 🔎 Proposed fix _translationThreads = new Task[Settings.ConcurrentWorkerCount];
for (int i = 0; i < Settings.ConcurrentWorkerCount; i++)
{
- _translationThreads[i] = Task.Factory.StartNew(DoInnerThreadWork).ContinueWith(t =>
+ int threadIndex = i; // Capture loop variable in local scope
+ _translationThreads[i] = Task.Factory.StartNew(DoInnerThreadWork).ContinueWith(t =>
{
- Log.Warning($"Translation thread {i} was killed! {t.Exception?.Message}");
+ Log.Warning($"Translation thread {threadIndex} was killed! {t.Exception?.Message}");
});
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| _cacheSaver = new Timer(state => | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -194,6 +158,50 @@ public static void StartThread() | |||||||||||||||||||||||||||||||||||||||
| }, null, 0, 60000); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| private static void DoInnerThreadWork() | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| while (true) | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| if (!Working) | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| Thread.Sleep(1000); | ||||||||||||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| while (_queue.Count > 0) | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| if (Settings.SleepTime > 0) Thread.Sleep(Settings.SleepTime); | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if (!_queue.TryDequeue(out var pair)) continue; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| _inQueue.TryRemove(pair.Key, out _); | ||||||||||||||||||||||||||||||||||||||||
| var translated = string.Empty; | ||||||||||||||||||||||||||||||||||||||||
| var success = true; | ||||||||||||||||||||||||||||||||||||||||
| if (pair.Key.Length > 200) | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| translated = pair.Key.Tokenize().Aggregate(translated, (current, token) => | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| success &= CurrentTranslator.TryTranslate(token, out var tmp); | ||||||||||||||||||||||||||||||||||||||||
| return current + ' ' + tmp; | ||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| success = CurrentTranslator.TryTranslate(pair.Key, out translated); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if (success) | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| translated = PolishText(translated); | ||||||||||||||||||||||||||||||||||||||||
| //translated = UnityWebRequest.UnEscapeURL(translated, Encoding.UTF8).Trim(); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| workCnt++; | ||||||||||||||||||||||||||||||||||||||||
| //_finished.Enqueue(pair); | ||||||||||||||||||||||||||||||||||||||||
| pair.Value(translated, success); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| Thread.Sleep(1000); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| public static void Translate(string orig, Action<string> callBack) => Translate(orig, string.Empty, callBack); | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| public static void Translate(string orig, string additionalKey, Action<string> callBack) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: csh1668/AutoTranslation
Length of output: 10158
🏁 Script executed:
Repository: csh1668/AutoTranslation
Length of output: 3083
🏁 Script executed:
Repository: csh1668/AutoTranslation
Length of output: 9724
🏁 Script executed:
Repository: csh1668/AutoTranslation
Length of output: 961
Add null check for translator assignment.
The translator retrieved via
GetTranslator(TranslatorName)can be null (it usesFirstOrDefault()), but is assigned directly toCurrentTranslatorwithout validation. Assign only if the translator is valid, or fall back to a default translator as done inTranslatorManager.Prepare().