Skip to content

Commit 1cb7520

Browse files
committed
Update Main.cs
1 parent 38f2663 commit 1cb7520

1 file changed

Lines changed: 34 additions & 1 deletion

File tree

  • STranslate.Plugin.Ocr.WindowsNative

STranslate.Plugin.Ocr.WindowsNative/Main.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public async Task<OcrResult> RecognizeAsync(OcrRequest request, CancellationToke
193193
if (string.IsNullOrWhiteSpace(line.Text))
194194
continue;
195195

196-
var content = new OcrContent { Text = line.Text };
196+
var content = new OcrContent { Text = NormalizeRecognizedText(line.Text) };
197197

198198
// 计算该行所有词的综合边界框(还原到原始图片坐标)
199199
float minX = float.MaxValue, minY = float.MaxValue;
@@ -241,4 +241,37 @@ public async Task<OcrResult> RecognizeAsync(OcrRequest request, CancellationToke
241241

242242
return result;
243243
}
244+
245+
private static string NormalizeRecognizedText(string text)
246+
{
247+
if (string.IsNullOrWhiteSpace(text))
248+
return text;
249+
250+
var chars = text.ToCharArray();
251+
var writeIndex = 0;
252+
253+
for (var i = 0; i < chars.Length; i++)
254+
{
255+
var current = chars[i];
256+
if (current == ' ' &&
257+
i > 0 &&
258+
i < chars.Length - 1 &&
259+
IsCjk(chars[i - 1]) &&
260+
IsCjk(chars[i + 1]))
261+
{
262+
continue;
263+
}
264+
265+
chars[writeIndex++] = current;
266+
}
267+
268+
return writeIndex == chars.Length ? text : new string(chars, 0, writeIndex);
269+
}
270+
271+
private static bool IsCjk(char c) =>
272+
c is >= '\u3400' and <= '\u4DBF' or
273+
>= '\u4E00' and <= '\u9FFF' or
274+
>= '\uF900' and <= '\uFAFF' or
275+
>= '\u3040' and <= '\u30FF' or
276+
>= '\uAC00' and <= '\uD7AF';
244277
}

0 commit comments

Comments
 (0)