From fed1ecebb133b101f746e46fea1fa875055e648b Mon Sep 17 00:00:00 2001 From: aoxue Date: Sat, 9 Oct 2021 14:38:38 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=BF=BB=E8=AF=91=E8=BF=87=E7=9A=84?= =?UTF-8?q?=E6=96=87=E6=9C=AC=E4=BF=9D=E5=AD=98=E5=88=B0=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=EF=BC=8C=E9=81=BF=E5=85=8D=E9=87=8D=E5=A4=8D=E7=BF=BB=E8=AF=91?= =?UTF-8?q?=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NetCore.zh-hans/AppConfig.cs | 39 ++++++++++++++++++++++++++++---- NetCore.zh-hans/FormTranslate.cs | 35 +++++++++++++++++++++++++--- NetCore.zh-hans/Translate.cs | 34 ++-------------------------- 3 files changed, 69 insertions(+), 39 deletions(-) diff --git a/NetCore.zh-hans/AppConfig.cs b/NetCore.zh-hans/AppConfig.cs index ba73dd3..a6c9f53 100644 --- a/NetCore.zh-hans/AppConfig.cs +++ b/NetCore.zh-hans/AppConfig.cs @@ -1,4 +1,5 @@ using Newtonsoft.Json; +using System.Collections.Generic; using System.IO; using System.Text; @@ -7,11 +8,13 @@ namespace NetCore.zh_hans public static class AppConfig { private static readonly string configPath; + private static readonly string dataPath; static AppConfig() { //获得配置文件的全路径 configPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "app.json"; + dataPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "data.json"; } public static BaiduAccount GetBaiduAccount() { @@ -20,7 +23,7 @@ public static BaiduAccount GetBaiduAccount() return new BaiduAccount(); } - string json = File.ReadAllText(configPath, Encoding.UTF8); + var json = File.ReadAllText(configPath, Encoding.UTF8); if (string.IsNullOrWhiteSpace(json)) { return new BaiduAccount(); @@ -30,15 +33,15 @@ public static BaiduAccount GetBaiduAccount() } public static void SetBaiduAccount(string appid, string secret) { - using FileStream fs = System.IO.File.Open(configPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read); + using var fs = System.IO.File.Open(configPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read); - string json = JsonConvert.SerializeObject(new BaiduAccount + var json = JsonConvert.SerializeObject(new BaiduAccount { Appid = appid, Secret = secret }); - byte[] bytes = Encoding.UTF8.GetBytes(json); + var bytes = Encoding.UTF8.GetBytes(json); fs.Write(bytes, 0, bytes.Length); //刷新缓冲区 fs.Flush(); @@ -46,6 +49,34 @@ public static void SetBaiduAccount(string appid, string secret) // File.WriteAllText(configPath, json); } + + public static void SetTranslateData(IDictionary data) + { + using var fs = System.IO.File.Open(dataPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read); + + var json = JsonConvert.SerializeObject(data); + var bytes = Encoding.UTF8.GetBytes(json); + fs.Write(bytes, 0, bytes.Length); + //刷新缓冲区 + fs.Flush(); + } + + public static IDictionary GetTranslateData() + { + if (!File.Exists(dataPath)) + { + return new Dictionary(); + } + + var json = File.ReadAllText(dataPath, Encoding.UTF8); + if (string.IsNullOrWhiteSpace(json)) + { + return new Dictionary(); + } + + return JsonConvert.DeserializeObject>(json); + } + } public class BaiduAccount { diff --git a/NetCore.zh-hans/FormTranslate.cs b/NetCore.zh-hans/FormTranslate.cs index 003223c..9d3ffc2 100644 --- a/NetCore.zh-hans/FormTranslate.cs +++ b/NetCore.zh-hans/FormTranslate.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; @@ -106,6 +107,10 @@ private async Task> GetTranslateDict(string[] fileNam })); + var translateData = AppConfig.GetTranslateData(); + + var concurrentDictionary = new ConcurrentDictionary(translateData); + //百度翻译高级版(QPS=10) const int qps = 5; var bathCount = allText.Count / qps; @@ -129,14 +134,31 @@ private async Task> GetTranslateDict(string[] fileNam button_Import.Text = $"翻译文本:{text}"; })); - var translationText = await TranslateText(text, appid, secret); + if (!concurrentDictionary.TryGetValue(text, out var translationText)) + { + translationText = await TranslateText(text, appid, secret); + await Task.Delay(TimeSpan.FromMilliseconds(200)); + } + dict[text] = translationText; - await Task.Delay(TimeSpan.FromMilliseconds(200)); + + if (!string.IsNullOrWhiteSpace(translationText)) + { + concurrentDictionary.AddOrUpdate(text, translationText, (key, old) => key); + } } })); } - await Task.WhenAll(list.ToArray()); + try + { + await Task.WhenAll(list.ToArray()); + } + catch (Exception) + { + AppConfig.SetTranslateData(concurrentDictionary); + } + //对于请求失败的词语 再重试一次 var failKeys = dict @@ -147,9 +169,16 @@ private async Task> GetTranslateDict(string[] fileNam { var translationText = await TranslateText(key, appid, secret); dict[key] = translationText; + + if (!string.IsNullOrWhiteSpace(translationText)) + { + concurrentDictionary.AddOrUpdate(key, translationText, (key, old) => key); + } await Task.Delay(TimeSpan.FromMilliseconds(200)); } + AppConfig.SetTranslateData(concurrentDictionary); + return dict; } diff --git a/NetCore.zh-hans/Translate.cs b/NetCore.zh-hans/Translate.cs index 9c65e34..cc8dc5a 100644 --- a/NetCore.zh-hans/Translate.cs +++ b/NetCore.zh-hans/Translate.cs @@ -5,7 +5,6 @@ using System.Net.Http; using System.Security.Cryptography; using System.Text; -using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Web; @@ -102,7 +101,7 @@ public static async Task TranslateText(string str, string inputAppId, st } //https://api.fanyi.baidu.com/api/trans/product/apidoc - public class TranslateResult + private class TranslateResult { public int error_code { get; set; } public string from { get; set; } @@ -110,7 +109,7 @@ public class TranslateResult public Trans_Result[] trans_result { get; set; } } - public class Trans_Result + private class Trans_Result { /// /// 原文 @@ -122,34 +121,5 @@ public class Trans_Result public string dst { get; set; } } - - /// - /// Unicode转字符串 - /// - /// 经过Unicode编码的字符串 - /// 正常字符串 - public static string UnicodeToString(string source) - { - return new Regex(@"\\u([0-9A-F]{4})", RegexOptions.IgnoreCase | RegexOptions.Compiled).Replace( - source, x => string.Empty + Convert.ToChar(Convert.ToUInt16(x.Result("$1"), 16))); - } - - /// - /// 字符串转Unicode - /// - /// 源字符串 - /// Unicode编码后的字符串 - public static string String2Unicode(string source) - { - var bytes = Encoding.Unicode.GetBytes(source); - var stringBuilder = new StringBuilder(); - for (var i = 0; i < bytes.Length; i += 2) - { - stringBuilder.AppendFormat("\\u{0}{1}", bytes[i + 1].ToString("x").PadLeft(2, '0'), bytes[i].ToString("x").PadLeft(2, '0')); - } - return stringBuilder.ToString(); - } - - } } From 8d08295b0c4e8ee0920d4c87ad777e2ade1a77a6 Mon Sep 17 00:00:00 2001 From: aoxue Date: Sat, 9 Oct 2021 15:52:24 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A4=84=E7=90=86=E6=AD=A3=E5=88=99?= =?UTF-8?q?=E5=8C=B9=E9=85=8D=E5=AD=97=E7=AC=A6=E4=B8=B2=E6=97=B6=E4=B8=AD?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E6=8D=A2=E8=A1=8C=E5=AF=BC=E8=87=B4=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E5=8C=B9=E9=85=8D=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NetCore.zh-hans/FormTranslate.cs | 38 ++++++++++++++++++---------- NetCore.zh-hans/StringExtenstions.cs | 28 ++++++++++++++++++++ 2 files changed, 52 insertions(+), 14 deletions(-) create mode 100644 NetCore.zh-hans/StringExtenstions.cs diff --git a/NetCore.zh-hans/FormTranslate.cs b/NetCore.zh-hans/FormTranslate.cs index 9d3ffc2..c6c8202 100644 --- a/NetCore.zh-hans/FormTranslate.cs +++ b/NetCore.zh-hans/FormTranslate.cs @@ -134,18 +134,19 @@ private async Task> GetTranslateDict(string[] fileNam button_Import.Text = $"翻译文本:{text}"; })); - if (!concurrentDictionary.TryGetValue(text, out var translationText)) + if (!concurrentDictionary.TryGetValue(text.ReplaceSpace(), out var translationText)) { translationText = await TranslateText(text, appid, secret); + + if (!string.IsNullOrWhiteSpace(translationText)) + { + concurrentDictionary.TryAdd(text.ReplaceSpace(), translationText); + } + await Task.Delay(TimeSpan.FromMilliseconds(200)); } dict[text] = translationText; - - if (!string.IsNullOrWhiteSpace(translationText)) - { - concurrentDictionary.AddOrUpdate(text, translationText, (key, old) => key); - } } })); } @@ -172,7 +173,7 @@ private async Task> GetTranslateDict(string[] fileNam if (!string.IsNullOrWhiteSpace(translationText)) { - concurrentDictionary.AddOrUpdate(key, translationText, (key, old) => key); + concurrentDictionary.TryAdd(key.ReplaceSpace(), translationText); } await Task.Delay(TimeSpan.FromMilliseconds(200)); } @@ -213,7 +214,7 @@ private async Task> GetAllText(string[] fileNames) var matches = new List(); foreach (var pattern in regexPatterns) { - var regex = new Regex(pattern); + var regex = new Regex(pattern, RegexOptions.Multiline); var result = regex.Matches(readXmlList); if (result.Count > 0) { @@ -226,11 +227,13 @@ private async Task> GetAllText(string[] fileNames) foreach (var match in matches) { - var text = match.Value?.Trim() - .Replace("\r\n", "") - .Replace(" ", " ") - .Trim('\n', ' ') - ; + if (string.IsNullOrWhiteSpace(match.Value)) + { + continue; + } + + var text = match.Value.Trim(); + if (!string.IsNullOrWhiteSpace(text)) { //如果不包含空格,说明是一个单词,不进行替换 @@ -245,7 +248,14 @@ private async Task> GetAllText(string[] fileNames) } private static async Task TranslateText(string text, string appid, string secret) { - var okStr = await Translate.TranslateText(text, appid, secret); //执行翻译 + var originalText = text.ReplaceSpace(); + + if (string.IsNullOrWhiteSpace(text)) + { + return ""; + } + + var okStr = await Translate.TranslateText(originalText, appid, secret); //执行翻译 if (string.IsNullOrWhiteSpace(okStr)) { return ""; diff --git a/NetCore.zh-hans/StringExtenstions.cs b/NetCore.zh-hans/StringExtenstions.cs new file mode 100644 index 0000000..e857b9e --- /dev/null +++ b/NetCore.zh-hans/StringExtenstions.cs @@ -0,0 +1,28 @@ +using System.Text.RegularExpressions; + +namespace NetCore.zh_hans +{ + public static class StringExtensions + { + /// + /// 把\n \r 替换成空格 + /// 把多个空格替换成一个空格 + /// + /// + /// + public static string ReplaceSpace(this string str) + { + if (string.IsNullOrWhiteSpace(str)) + { + return str; + } + + var text = str + .Replace('\r', ' ') + .Replace('\n', ' '); + + var replaceSpaceRegex = new Regex(@"\s{1,}", RegexOptions.IgnoreCase); + return replaceSpaceRegex.Replace(text, " ").Trim(); + } + } +} \ No newline at end of file