public void Should_GetZhuyinKeys_Work() { string aChar = "料"; string[] expected = { "xul4" }; string[] actual; actual = ZhuyinQueryHelper.GetZhuyinKeys(aChar); CollectionAssert.AreEqual(expected, actual); }
public void Should_GetZhuyinSymbols_Work() { string aChar = "料"; string[] expected = { "ㄌㄧㄠˋ" }; string[] actual; actual = ZhuyinQueryHelper.GetZhuyinSymbols(aChar, false); CollectionAssert.AreEqual(expected, actual); aChar = "們"; expected = new string[] { "ㄇㄣˊ", "ㄇㄣ˙" }; actual = ZhuyinQueryHelper.GetZhuyinSymbols(aChar, false); CollectionAssert.AreEqual(expected, actual); }
public void Should_IsPolyphonic_Work() { string aChar = "的"; bool expected = true; bool actual; actual = ZhuyinQueryHelper.IsPolyphonic(aChar); Assert.AreEqual(expected, actual); aChar = "料"; expected = false; actual = ZhuyinQueryHelper.IsPolyphonic(aChar); Assert.AreEqual(expected, actual); }
/// <summary> /// 從現存的雙視點字檔案載入(反序列化)成新的 BailleDocument 物件。 /// </summary> /// <param name="filename">檔名</param> /// <returns>新的 BailleDocument 物件</returns> public static BrailleDocument LoadBrailleFile(string filename) { if (!File.Exists(filename)) { throw new FileNotFoundException("檔案不存在: " + filename); } BrailleDocument brDoc = null; // 舊版的 .btx 反序列化. if (Path.GetExtension(filename).Equals(".btx", StringComparison.CurrentCultureIgnoreCase)) { using (FileStream fs = new FileStream(filename, FileMode.Open)) { BinaryReader br = new BinaryReader(fs); int fileVersion = br.ReadInt32(); int lineCount = br.ReadInt32(); int reserved1 = br.ReadInt32(); int reserved2 = br.ReadInt32(); IFormatter fmter = new BinaryFormatter(); brDoc = (BrailleDocument)fmter.Deserialize(fs); br.Close(); // 設定新增的屬性:BrailleWord.IsPolyphnic。等到 .btx 正式除役,這裡就不需要了。 foreach (BrailleLine brLine in brDoc.Lines) { foreach (BrailleWord brWord in brLine.Words) { if (!String.IsNullOrEmpty(brWord.PhoneticCode)) { brWord.IsPolyphonic = ZhuyinQueryHelper.IsPolyphonic(brWord.Text); } } } return(brDoc); } } string jsonStr = File.ReadAllText(filename); brDoc = JsonHelper.Deserialize <BrailleDocument>(jsonStr); return(brDoc); }
public BrailleConverter() { _zhuyinConverter = CreateZhuyinConverter(AppGlobals.Config.PreferIFELanguage); _processor = BrailleProcessor.GetInstance(_zhuyinConverter); _doc = new BrailleDocument(_processor); _processor.ConvertionFailed += new ConvertionFailedEventHandler(BrailleProcessor_ConvertionFailed); _processor.TextConverted += new TextConvertedEventHandler(BrailleProcessor_TextConverted); //m_Processor.ChineseConverter = null; // 保護 m_CvtResultFileName = Path.Combine(AppGlobals.TempPath, Constant.Files.CvtResultFileName); m_CvtErrorCharFileName = Path.Combine(AppGlobals.TempPath + Constant.Files.CvtErrorCharFileName); _verboseMode = false; LoadPhraseFiles(); // TODO: 還需要這個嗎? ZhuyinQueryHelper.Initialize(); // 初始化注音字根查詢器(載入注音字根表)。 }
private void UpdateUI() { m_IsUpdatingUI = true; try { txtChar.Text = m_BrWord.Text; cboPhCode.Items.Clear(); if (m_BrWord.IsPolyphonic) { string[] zhuyinCodes = ZhuyinQueryHelper.GetZhuyinSymbols(txtChar.Text, true); cboPhCode.Items.AddRange(zhuyinCodes); } cboPhCode.SelectedIndex = cboPhCode.Items.IndexOf(m_BrWord.PhoneticCode); txtBraille.Text = BrailleFontConverter.ToString(m_BrWord); } finally { m_IsUpdatingUI = false; } }
/// <summary> /// 把一個字元轉換成點字。 /// </summary> /// <param name="text">一個中文字或標點符號。</param> /// <returns>若指定的字串是中文字且轉換成功,則傳回轉換之後的點字物件,否則傳回 null。</returns> private BrailleWord InternalConvert(string text) { BrailleWord brWord = new BrailleWord { Text = text }; string brCode; if (text.Length == 1) { char ch = text[0]; // 如果輸入的明眼字是注音符號,就直接傳回注音的點字碼。 if (Zhuyin.IsBopomofo(ch)) { // 注意: 不要指定 brWord.PhoneticCode,因為注音符號本身只是個中文符號, // 它並不是中文字,沒有合法的注音組字字根,因此不可指定注音碼。 brCode = _brailleTable.FindPhonetic(text); brWord.AddCell(brCode); return(brWord); } // 如果輸入的明眼字是注音符號的音調記號,就直接傳回對應的點字碼。 if (Zhuyin.IsTone(ch)) { // 注意: 不要指定 brWord.PhoneticCode,因為音調記號本身只是個中文符號, // 它並不是中文字,沒有合法的注音組字字根,因此不可指定注音碼。 brCode = _brailleTable.FindTone(text); brWord.AddCell(brCode); return(brWord); } } // 嘗試取得該字的注音字根,若可成功取得,則將注音字根轉換成點字碼,並傳回 BrailleWord 物件。 string phcode = null; if (text.IsCJK()) // 若是漢字 { /* 2010-01-03: 不取得所有的注音字根,只取得一組預設的字根,且判斷是否為多音字。等到編輯時使用者要更換注音,才取出所有字根。 * // 取得破音字的所有組字字根,每一組字根長度固定為 4 個字元,不足者以全型空白填補。 * string[] phCodes = ZhuyinQueryHelper.GetZhuyinSymbols(text, true); * if (phCodes.Length > 0) * { * brWord.SetPhoneticCodes(phCodes); * phcode = phCodes[0]; // 指定第一組字根為預設的字根。 * } */ // 取得注音字根 string[] zhuyinCodes = ZhuyinConverter.GetZhuyin(text); //if (zhuyinCodes == null || zhuyinCodes.Length == 0) //{ // // 若 IFELanguage 無法轉換,就用內建的注音字根查詢功能。 // zhuyinCodes = ZhuyinQueryHelper.GetZhuyinSymbols(text, true); // 此方法會傳回一個中文字的所有注音字根。 //} if (zhuyinCodes.Length >= 1) { phcode = zhuyinCodes[0]; } if (!String.IsNullOrEmpty(phcode)) { // 設定多音字旗號屬性. brWord.IsPolyphonic = ZhuyinQueryHelper.IsPolyphonic(text); // TODO: 以下「將注音字根轉換成點字碼」的處理應可省略,因為 FixPhoneticCodes 會重新修正所有中文字的點字碼。 // 將注音字根轉換成點字碼 BrailleCellList cellList = CreatePhoneticCellList(phcode); if (cellList != null) { brWord.CellList.Assign(cellList); brWord.PhoneticCode = phcode; return(brWord); } } } // 不是中文字,或者無法取得注音字根. // 處理標點符號 string puncBrCode = _brailleTable.FindPunctuation(text); if (!String.IsNullOrEmpty(puncBrCode)) { brWord.AddCell(puncBrCode); return(brWord); } // 其它符號 brCode = _brailleTable.Find(text); if (!String.IsNullOrEmpty(brCode)) { brWord.AddCell(brCode); return(brWord); } brWord.Clear(); brWord = null; return(null); }