From cf420e72f30acae166eb7e6675c00251fdb158f8 Mon Sep 17 00:00:00 2001 From: "DESKTOP-3BO4HSG\\ksat" Date: Mon, 26 Dec 2022 11:42:24 +0800 Subject: [PATCH] feat: add English and Chinese language recognition --- STranslate/MainWindow.xaml | 41 ++++++++++++++------ STranslate/MainWindow.xaml.cs | 4 +- STranslate/Properties/AssemblyInfo.cs | 4 +- STranslate/ViewModel/MainVM.cs | 54 +++++++++++++++++++++++---- 4 files changed, 80 insertions(+), 23 deletions(-) diff --git a/STranslate/MainWindow.xaml b/STranslate/MainWindow.xaml index 233aada..b618f72 100644 --- a/STranslate/MainWindow.xaml +++ b/STranslate/MainWindow.xaml @@ -106,19 +106,36 @@ - + Command="{Binding CopyInputCmd}"> + + + + + + + + + + + + + + diff --git a/STranslate/MainWindow.xaml.cs b/STranslate/MainWindow.xaml.cs index 6d01640..ee3c49a 100644 --- a/STranslate/MainWindow.xaml.cs +++ b/STranslate/MainWindow.xaml.cs @@ -178,8 +178,10 @@ namespace STranslate { vm.InputTxt = string.Empty; vm.OutputTxt = string.Empty; - vm.SmallHumpRet = string.Empty; vm.SnakeRet = string.Empty; + vm.SmallHumpRet = string.Empty; + vm.LargeHumpRet = string.Empty; + vm.IdentifyLanguage = string.Empty; } /// diff --git a/STranslate/Properties/AssemblyInfo.cs b/STranslate/Properties/AssemblyInfo.cs index dfb2b7b..4a595f1 100644 --- a/STranslate/Properties/AssemblyInfo.cs +++ b/STranslate/Properties/AssemblyInfo.cs @@ -47,6 +47,6 @@ using System.Windows; //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 //通过使用 "*",如下所示: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.0.5.0")] -[assembly: AssemblyFileVersion("0.0.5.0")] +[assembly: AssemblyVersion("0.0.6.0")] +[assembly: AssemblyFileVersion("0.0.6.0")] [assembly: Guid("CE252DD8-179F-4544-9989-453F5DEA378D")] \ No newline at end of file diff --git a/STranslate/ViewModel/MainVM.cs b/STranslate/ViewModel/MainVM.cs index 2525932..1ffa67d 100644 --- a/STranslate/ViewModel/MainVM.cs +++ b/STranslate/ViewModel/MainVM.cs @@ -32,7 +32,7 @@ namespace STranslate.ViewModel InputCombo = LanguageEnumDict.Keys.ToList(); InputComboSelected = LanguageEnum.AUTO.GetDescription(); OutputCombo = LanguageEnumDict.Keys.ToList(); - OutputComboSelected = LanguageEnum.EN.GetDescription(); + OutputComboSelected = LanguageEnum.AUTO.GetDescription(); config = ConfigUtil.ReadConfig(ConfigPath); @@ -73,17 +73,47 @@ namespace STranslate.ViewModel } #region handle - + /// + /// 自动识别语种 + /// + /// 输入语言 + /// + /// Item1: SourceLang + /// Item2: TargetLang + /// + private Tuple AutomaticLanguageRecognition(string text) + { + //如果输入是中文 + if (System.Text.RegularExpressions.Regex.IsMatch(text, @"^[\u4e00-\u9fa5]+$")) + { + return new Tuple(LanguageEnum.ZH.GetDescription(), LanguageEnum.EN.GetDescription()); + } + else + { + return new Tuple(LanguageEnum.EN.GetDescription(), LanguageEnum.ZH.GetDescription()); + } + } + /// + /// 翻译 + /// + /// public async Task Translate() { try { OutputTxt = "翻译中..."; - //获取结果 - - //DeepL Api - var translateResp = await TranslateUtil.TranslateDeepLAsync(config.deepl?.url ?? defaultApi, InputTxt, LanguageEnumDict[OutputComboSelected], LanguageEnumDict[InputComboSelected]); + //自动选择目标语言 + if (OutputComboSelected == LanguageEnum.AUTO.GetDescription()) + { + var autoRet = AutomaticLanguageRecognition(InputTxt); + IdentifyLanguage = autoRet.Item1; + translateResp = await TranslateUtil.TranslateDeepLAsync(config.deepl?.url ?? defaultApi, InputTxt, LanguageEnumDict[autoRet.Item2], LanguageEnumDict[InputComboSelected]); + } + else + { + translateResp = await TranslateUtil.TranslateDeepLAsync(config.deepl?.url ?? defaultApi, InputTxt, LanguageEnumDict[OutputComboSelected], LanguageEnumDict[InputComboSelected]); + } //百度 Api //var translateResp = await TranslateUtil.TranslateBaiduAsync(config.baidu.appid, config.baidu.secretKey, InputTxt, LanguageEnumDict[OutputComboSelected], LanguageEnumDict[InputComboSelected]); @@ -96,7 +126,10 @@ namespace STranslate.ViewModel OutputTxt = translateResp; //如果不是英文则不进行转换 - if (LanguageEnumDict[OutputComboSelected] != LanguageEnum.EN) return; + if (AutomaticLanguageRecognition(InputTxt).Item2 != LanguageEnum.EN.GetDescription() && LanguageEnumDict[OutputComboSelected] != LanguageEnum.EN) + { + return; + } var splitList = OutputTxt.Split(' ').ToList(); if (splitList.Count > 1) @@ -157,7 +190,7 @@ namespace STranslate.ViewModel #endregion handle #region Params - + private string translateResp; public ICommand TranslateCmd { get; private set; } public ICommand CopyInputCmd { get; private set; } public ICommand CopyResultCmd { get; private set; } @@ -165,6 +198,11 @@ namespace STranslate.ViewModel public ICommand CopySmallHumpResultCmd { get; private set; } public ICommand CopyLargeHumpResultCmd { get; private set; } + /// + /// 识别语种 + /// + private string _IdentifyLanguage; + public string IdentifyLanguage { get => _IdentifyLanguage; set => UpdateProperty(ref _IdentifyLanguage, value); } /// /// 构造蛇形结果 ///