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..c6c8202 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,32 @@ private async Task> GetTranslateDict(string[] fileNam button_Import.Text = $"翻译文本:{text}"; })); - var translationText = await TranslateText(text, appid, secret); + 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; - await Task.Delay(TimeSpan.FromMilliseconds(200)); } })); } - await Task.WhenAll(list.ToArray()); + try + { + await Task.WhenAll(list.ToArray()); + } + catch (Exception) + { + AppConfig.SetTranslateData(concurrentDictionary); + } + //对于请求失败的词语 再重试一次 var failKeys = dict @@ -147,9 +170,16 @@ private async Task> GetTranslateDict(string[] fileNam { var translationText = await TranslateText(key, appid, secret); dict[key] = translationText; + + if (!string.IsNullOrWhiteSpace(translationText)) + { + concurrentDictionary.TryAdd(key.ReplaceSpace(), translationText); + } await Task.Delay(TimeSpan.FromMilliseconds(200)); } + AppConfig.SetTranslateData(concurrentDictionary); + return dict; } @@ -184,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) { @@ -197,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)) { //如果不包含空格,说明是一个单词,不进行替换 @@ -216,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 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(); - } - - } }