Skip to content

Commit

Permalink
Fix Gborad import export bug
Browse files Browse the repository at this point in the history
  • Loading branch information
studyzy committed Jul 12, 2020
1 parent ac83ad9 commit 2129cdc
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 40 deletions.
37 changes: 19 additions & 18 deletions src/IME WL Converter Win/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ComboBoxShowAttribute>();
var cbxExportItems = new List<ComboBoxShowAttribute>();
Expand All @@ -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;
Expand Down Expand Up @@ -93,6 +93,7 @@ private void InitOpenFileDialogFilter(string select)
"搜狗备份词库|*.bin",
"紫光分类词库|*.uwl",
"微软拼音词库|*.dat",
"Gboard词库|*.zip",
"灵格斯词库|*.ld2",
"所有文件|*.*"
};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -212,15 +213,15 @@ private void btnConvert_Click(object sender, EventArgs e)
return;
}
}

if (!mergeTo1File)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
outputDir = folderBrowserDialog1.SelectedPath;
}
}
mainBody=new MainBody();
mainBody = new MainBody();
mainBody.SelectedWordRankGenerater = wordRankGenerater;
mainBody.Import = import;
mainBody.Export = export;
Expand All @@ -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();
}
Expand All @@ -250,7 +251,7 @@ private IList<IBatchFilter> GetBatchFilters()
}
if (filterConfig.WordRankPercentage < 100)
{
var filter = new RankPercentageFilter {Percentage = filterConfig.WordRankPercentage};
var filter = new RankPercentageFilter { Percentage = filterConfig.WordRankPercentage };
filters.Add(filter);
}
return filters;
Expand All @@ -276,7 +277,7 @@ private IList<IReplaceFilter> GetReplaceFilters()
{
filters.Add(new NumberFilter());
}
if(cbxFrom.Text== ConstantString.EMOJI)
if (cbxFrom.Text == ConstantString.EMOJI)
{
filters.Add(new EmojiReplacer(txbWLPath.Text));
}
Expand Down Expand Up @@ -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 = "";


Expand Down Expand Up @@ -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) //流转换,只有文本类型的才支持。
{
Expand All @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
32 changes: 23 additions & 9 deletions src/ImeWlConverterCore/Helpers/FileOperationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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);
}
}
}
}
Expand Down
49 changes: 36 additions & 13 deletions src/ImeWlConverterCore/IME/Gboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Studyzy.IMEWLConverter.IME
/// Gboard输入法
/// </summary>
[ComboBoxShow(ConstantString.GBOARD, ConstantString.GBOARD_C, 111)]
public class Gboard : BaseTextImport, IWordLibraryExport, IWordLibraryTextImport
public class Gboard : BaseImport, IWordLibraryExport, IWordLibraryImport
{
#region IWordLibraryExport 成员

Expand All @@ -37,42 +37,65 @@ public IList<string> 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);
return new List<string>() { "词库文件在:" + zipPath };
//return new List<string>() { 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
}
}

0 comments on commit 2129cdc

Please sign in to comment.