From 2129cdc1180e0a6beda962a8a84fde1816498409 Mon Sep 17 00:00:00 2001 From: studyzy Date: Mon, 13 Jul 2020 00:08:44 +0800 Subject: [PATCH] Fix Gborad import export bug --- src/IME WL Converter Win/Forms/MainForm.cs | 37 +++++++------- .../Helpers/FileOperationHelper.cs | 32 ++++++++---- src/ImeWlConverterCore/IME/Gboard.cs | 49 ++++++++++++++----- 3 files changed, 78 insertions(+), 40 deletions(-) diff --git a/src/IME WL Converter Win/Forms/MainForm.cs b/src/IME WL Converter Win/Forms/MainForm.cs index c9549172..c0e8f789 100644 --- a/src/IME WL Converter Win/Forms/MainForm.cs +++ b/src/IME WL Converter Win/Forms/MainForm.cs @@ -31,7 +31,7 @@ public MainForm() private void LoadImeList() { - Assembly assembly =typeof(MainBody).Assembly; + Assembly assembly = typeof(MainBody).Assembly; Type[] d = assembly.GetTypes(); var cbxImportItems = new List(); var cbxExportItems = new List(); @@ -40,7 +40,7 @@ private void LoadImeList() { if (type.Namespace != null && type.Namespace.StartsWith("Studyzy.IMEWLConverter.IME")) { - object[] att = type.GetCustomAttributes(typeof (ComboBoxShowAttribute), false); + object[] att = type.GetCustomAttributes(typeof(ComboBoxShowAttribute), false); if (att.Length > 0) { var cbxa = att[0] as ComboBoxShowAttribute; @@ -93,6 +93,7 @@ private void InitOpenFileDialogFilter(string select) "搜狗备份词库|*.bin", "紫光分类词库|*.uwl", "微软拼音词库|*.dat", + "Gboard词库|*.zip", "灵格斯词库|*.ld2", "所有文件|*.*" }; @@ -140,7 +141,7 @@ private IWordLibraryImport GetImportInterface(string str) #endregion //private Encoding ld2WordEncoding=Encoding.UTF8; - private MainBody mainBody; + private MainBody mainBody; private IWordLibraryExport export; private bool exportDirectly; @@ -179,12 +180,12 @@ private void btnOpenFileDialog_Click(object sender, EventArgs e) } private bool CheckCanRun() { - if(import==null || export==null) + if (import == null || export == null) { MessageBox.Show("请先选择导入词库类型和导出词库类型"); return false; } - if(this.txbWLPath.Text=="") + if (this.txbWLPath.Text == "") { MessageBox.Show("请先选择源词库文件"); return false; @@ -212,7 +213,7 @@ private void btnConvert_Click(object sender, EventArgs e) return; } } - + if (!mergeTo1File) { if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) @@ -220,7 +221,7 @@ private void btnConvert_Click(object sender, EventArgs e) outputDir = folderBrowserDialog1.SelectedPath; } } - mainBody=new MainBody(); + mainBody = new MainBody(); mainBody.SelectedWordRankGenerater = wordRankGenerater; mainBody.Import = import; mainBody.Export = export; @@ -231,7 +232,7 @@ private void btnConvert_Click(object sender, EventArgs e) mainBody.ReplaceFilters = GetReplaceFilters(); mainBody.Import.ImportLineErrorNotice += WriteErrorMessage; mainBody.Export.ExportErrorNotice += WriteErrorMessage; - mainBody.ProcessNotice+=RichTextBoxShow; + mainBody.ProcessNotice += RichTextBoxShow; timer1.Enabled = true; backgroundWorker1.RunWorkerAsync(); } @@ -250,7 +251,7 @@ private IList GetBatchFilters() } if (filterConfig.WordRankPercentage < 100) { - var filter = new RankPercentageFilter {Percentage = filterConfig.WordRankPercentage}; + var filter = new RankPercentageFilter { Percentage = filterConfig.WordRankPercentage }; filters.Add(filter); } return filters; @@ -276,7 +277,7 @@ private IList GetReplaceFilters() { filters.Add(new NumberFilter()); } - if(cbxFrom.Text== ConstantString.EMOJI) + if (cbxFrom.Text == ConstantString.EMOJI) { filters.Add(new EmojiReplacer(txbWLPath.Text)); } @@ -370,7 +371,7 @@ private void MainForm_DragEnter(object sender, DragEventArgs e) private void MainForm_DragDrop(object sender, DragEventArgs e) { - var array = (Array) e.Data.GetData(DataFormats.FileDrop); + var array = (Array)e.Data.GetData(DataFormats.FileDrop); string files = ""; @@ -509,8 +510,8 @@ private void timer1_Tick(object sender, EventArgs e) private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { - var files = FileOperationHelper.GetFilesPath( txbWLPath.Text); - + var files = FileOperationHelper.GetFilesPath(txbWLPath.Text); + if (streamExport && import.IsText) //流转换,只有文本类型的才支持。 { @@ -527,7 +528,7 @@ private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { fileContent = mainBody.Convert(files); } - catch(Exception ex) + catch (Exception ex) { mainBody.Dispose(); RichTextBoxShow(ex.Message); @@ -552,13 +553,13 @@ private void RichTextBoxShow(string msg) richTextBox1.AppendText(msg + "\r\n"); } } - private string errorMessages=""; + private string errorMessages = ""; private void WriteErrorMessage(string msg) { errorMessages += msg + "\r\n"; } - private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) + private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { timer1.Enabled = false; mainBody.StopNotice(); @@ -587,12 +588,12 @@ private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerComple { richTextBox1.Text = "为提高处理速度,“高级设置”中选中了“不显示结果,直接导出”,本文本框中不显示转换后的结果,若要查看转换后的结果再确定是否保存请取消该设置。"; } - else if(mergeTo1File) + else if (mergeTo1File) { richTextBox1.Text = fileContent; //btnExport.Enabled = true; } - if (!mergeTo1File || export is Win10MsPinyin || export is Win10MsWubi|| export is Win10MsPinyinSelfStudy)//微软拼音是二进制文件,不需要再提示保存 + if (!mergeTo1File || export is Win10MsPinyin || export is Win10MsWubi || export is Win10MsPinyinSelfStudy || export is Gboard)//微软拼音是二进制文件,不需要再提示保存 { MessageBox.Show("转换完成!"); return; diff --git a/src/ImeWlConverterCore/Helpers/FileOperationHelper.cs b/src/ImeWlConverterCore/Helpers/FileOperationHelper.cs index d58c13c2..89e84153 100644 --- a/src/ImeWlConverterCore/Helpers/FileOperationHelper.cs +++ b/src/ImeWlConverterCore/Helpers/FileOperationHelper.cs @@ -64,6 +64,10 @@ public static string AutoMatchSourceWLType(string filePath) { return ConstantString.LINGOES_LD2; } + if (ext == ".zip") + { + return ConstantString.GBOARD; + } string example = ""; Encoding code = GetEncodingType(filePath); using (var sr = new StreamReader(filePath, code)) @@ -645,16 +649,26 @@ public static bool UnZip(string fileToUnZip, string zipedFolder) continue; } - fs = File.Create(fileName); - int size = 2048; - byte[] data = new byte[size]; - while (true) + //fs = File.Create(fileName); + //int size = 2048; + //byte[] data = new byte[size]; + //while (true) + //{ + // size = zipStream.Read(data, 0, data.Length); + // if (size > 0) + // fs.Write(data, 0, data.Length); + // else + // break; + //} + using (FileStream streamWriter = File.Create(fileName)) { - size = zipStream.Read(data, 0, data.Length); - if (size > 0) - fs.Write(data, 0, data.Length); - else - break; + byte[] buffer = new byte[10240]; + int size = zipStream.Read(buffer, 0, buffer.Length); + while (size > 0) + { + streamWriter.Write(buffer, 0, size); + size = zipStream.Read(buffer, 0, buffer.Length); + } } } } diff --git a/src/ImeWlConverterCore/IME/Gboard.cs b/src/ImeWlConverterCore/IME/Gboard.cs index 449f0188..dbc58696 100644 --- a/src/ImeWlConverterCore/IME/Gboard.cs +++ b/src/ImeWlConverterCore/IME/Gboard.cs @@ -11,7 +11,7 @@ namespace Studyzy.IMEWLConverter.IME /// Gboard输入法 /// [ComboBoxShow(ConstantString.GBOARD, ConstantString.GBOARD_C, 111)] - public class Gboard : BaseTextImport, IWordLibraryExport, IWordLibraryTextImport + public class Gboard : BaseImport, IWordLibraryExport, IWordLibraryImport { #region IWordLibraryExport 成员 @@ -37,7 +37,7 @@ public IList Export(WordLibraryList wlList) sb.Append(ExportLine(wlList[i])); sb.Append("\n"); } - FileOperationHelper.WriteFile(tempPath, Encoding.UTF8, sb.ToString()); + FileOperationHelper.WriteFile(tempPath, new UTF8Encoding(false), sb.ToString()); string zipPath = Path.Combine(FileOperationHelper.GetCurrentFolderPath(), "Gboard词库.zip"); if (File.Exists(zipPath)) { File.Delete(zipPath); } FileOperationHelper.ZipFile(tempPath, zipPath); @@ -45,34 +45,57 @@ public IList Export(WordLibraryList wlList) //return new List() { sb.ToString() }; } - - public override Encoding Encoding - { - get - { - return Encoding.UTF8; - - } - } - #endregion #region IWordLibraryImport 成员 - public override WordLibraryList ImportLine(string line) + public WordLibraryList ImportLine(string line) { string[] c = line.Split('\t'); var wl = new WordLibrary(); wl.Word = c[1]; + wl.CodeType = CodeType.UserDefinePhrase; //wl.Rank = Convert.ToInt32(c[1]); //wl.PinYin = c[2].Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries); var wll = new WordLibraryList(); wll.Add(wl); return wll; } + + public WordLibraryList Import(string path) + { + var tempUnzipFolder = FileOperationHelper.GetCurrentFolderPath(); + FileOperationHelper.UnZip(path, tempUnzipFolder); + var tempFilePath= Path.Combine(FileOperationHelper.GetCurrentFolderPath(), "dictionary.txt"); + string str = FileOperationHelper.ReadFile(tempFilePath,new UTF8Encoding(false)); + File.Delete(tempFilePath); + return ImportText(str); + } + public WordLibraryList ImportText(string str) + { + var wlList = new WordLibraryList(); + string[] lines = str.Split(new[] { "\n" }, StringSplitOptions.RemoveEmptyEntries); + CountWord = lines.Length; + for (int i = 1; i < lines.Length; i++) + { + string line = lines[i]; + CurrentStatus = i; + try + { + wlList.AddWordLibraryList(ImportLine(line)); + } + catch + { + SendImportLineErrorNotice("无效的词条,解析失败:" + line); + } + } + return wlList; + } public override CodeType CodeType { get => CodeType.UserDefinePhrase; set => base.CodeType = value; } + + public Encoding Encoding => throw new NotImplementedException(); #endregion } } \ No newline at end of file