C#编写的日英字典程序 - 基于EDict
關于EDict, 詳細資料可以訪問?http://www.csse.monash.edu.au/~jwb/edict_doc_old.html
EDict的文件組織形式:
FORMAT
EDICT's format is that of the original "EDICT" format used by the early PC Japanese word-processor MOKE (Mark's Own Kanji Editor). It uses EUC-JP coding for kana and kanji, however this can be converted to JIS (ISO-2022-JP) or Shift-JIS by any of the several conversion programs around. It is a text file with one entry per line. The format of entries is:
KANJI [KANA] /English_1/English_2/.../or
KANA /English_1/.../(NB: Only the KANJI and KANA are in EUC; all the other characters, including spaces, must be ASCII.)
The English translations were initially deliberately brief, as the application of the dictionary was expected to be primarily on-line look-ups, etc. Over time the translations have become more extended.
The EDICT file is not intended to have its entries in any particular order. In fact it almost always is in order as a by-product of the update method I use, however there is no guarantee of this. (The order is almost always JIS + alphabetical, starting with the head-word.)
主要代碼:
MainForm.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO;namespace EdictLookup {public partial class MainForm : Form{private Edict myEdict;public MainForm(){InitializeComponent();}private void MainForm_Load(object sender, EventArgs e){myEdict = new Edict();try{myEdict.EdictLoad(this.cb_fulldic.Checked);}catch (Exception ex){MessageBox.Show(ex.ToString(), "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);this.Close();}this.edit_input.Focus();}private void btn_lookup_Click(object sender, EventArgs e){myEdict.EdictLookup(this.edit_input.Text, this.list_result, this.cb_exact.Checked);}private void cb_fulldic_CheckedChanged(object sender, EventArgs e){try{myEdict.EdictLoad(this.cb_fulldic.Checked);}catch (Exception ex){MessageBox.Show(ex.ToString(), "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);this.Close();}myEdict.EdictLookup(this.edit_input.Text, this.list_result, this.cb_exact.Checked);}private void cb_exact_CheckedChanged(object sender, EventArgs e){myEdict.EdictLookup(this.edit_input.Text, this.list_result, this.cb_exact.Checked);}}public class Edict{private const string DIC_SUB_FILENAME = "edict_sub"; // filename of the edit_sub fileprivate const string DIC_FILENAME = "edict"; // filename of the edit fileprivate int dicFileSize; // edict file sizeprivate char[] dicBuffer; // buffer of edict file, contains all text of edictprivate string dicText; // text of edict, a copy of dicBufferprivate void EdictLoad_Sub(){FileInfo fi = new FileInfo(DIC_SUB_FILENAME);this.dicFileSize = (int)fi.Length;FileStream fs = new FileStream(DIC_SUB_FILENAME, FileMode.Open);StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("EUC-JP"));this.dicBuffer = new char[this.dicFileSize + 1];sr.Read(this.dicBuffer, 0, this.dicFileSize);sr.Close();fs.Close();this.dicText = new string(this.dicBuffer);}private void EdictLoad_Full(){FileInfo fi = new FileInfo(DIC_FILENAME);this.dicFileSize = (int)fi.Length;FileStream fs = new FileStream(DIC_FILENAME, FileMode.Open);StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("EUC-JP"));this.dicBuffer = new char[this.dicFileSize + 1];sr.Read(this.dicBuffer, 0, this.dicFileSize);sr.Close();fs.Close();this.dicText = new string(this.dicBuffer);}public void EdictLoad(bool full){GC.Collect();if (full){this.EdictLoad_Full();}else{this.EdictLoad_Sub();}}private void EdictLookup_Fuzzy(string search, ListBox result){result.Items.Clear();if (search.Length > 0){int index = -1;int pos1 = -2;int pos2;while ((index = this.dicText.IndexOf(search, index + 1)) != -1){pos1 = this.dicText.LastIndexOf('\n', index) + 1;pos2 = this.dicText.IndexOf('\n', index);result.Items.Add(this.dicText.Substring(pos1, pos2 - pos1));}if (pos1 == -2){result.Items.Add("Not Found!");}}else{result.Items.Add("Empty Input!");}}private void EdictLookup_Exact(string search, ListBox result){try{result.Items.Clear();if (search.Length > 0){int index = -1;int pos1 = -2;int pos2;// kanji matchstring temp = '\n' + search + ' ';while ((index = this.dicText.IndexOf(temp, index + 1)) != -1){pos1 = index + 1;pos2 = this.dicText.IndexOf('\n', pos1);result.Items.Add(this.dicText.Substring(pos1, pos2 - pos1));}// kana matchindex = -1;temp = '[' + search + ']';while ((index = this.dicText.IndexOf(temp, index + 1)) != -1){pos1 = this.dicText.LastIndexOf('\n', index) + 1;pos2 = this.dicText.IndexOf('\n', index);result.Items.Add(this.dicText.Substring(pos1, pos2 - pos1));}if (pos1 == -2){result.Items.Add("Not Found!");}}else{result.Items.Add("Empty Input!");}}catch (Exception ex){MessageBox.Show(ex.ToString());}}public void EdictLookup(string search, ListBox result, bool exact){if (exact){this.EdictLookup_Exact(search, result);}else{this.EdictLookup_Fuzzy(search, result);}}}}完整源代碼:
EdictLookup.rar
在bin\Release中已包含 edict(完整字典文件) 和 edict_sub(常用字典文件) 文件
總結
以上是生活随笔為你收集整理的C#编写的日英字典程序 - 基于EDict的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 成都信息工程大学 2014-2019 《
- 下一篇: 启锐 打印机相关配件报价 售后