Ejemplo n.º 1
0
        /// <summary>
        /// 加入一個片語。
        /// </summary>
        /// <param name="phraseStr">片語字串,格式為 [中文] [注音字根],例如:「不禁 ㄅㄨˋ ㄐㄧㄣ」。</param>
        /// <returns></returns>
        public bool AddPhrase(string phraseStr)
        {
            string[] fields;
            char[]   sep = new char[] { ' ' };
            string   phrase;

            fields = phraseStr.Split(sep, 5, StringSplitOptions.RemoveEmptyEntries);
            if (fields.Length < 2)  // 若只定義中文字,未指定字根,則不處理。
            {
                return(false);
            }

            // 取出中文辭彙。
            phrase = fields[0];

            // 移除既有的項目,亦即後來載入的資料會蓋過之前載入的。
            if (_table.ContainsKey(phrase))
            {
                _table.Remove(phrase);
            }
            List <Zhuyin> zyList = new List <Zhuyin>();

            for (int i = 1; i < fields.Length; i++)
            {
                Zhuyin zy = new Zhuyin(fields[i]);
                zyList.Add(zy);
            }
            _table.Add(phrase, zyList);

            return(true);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 從串流中讀取對照表。
        /// </summary>
        /// <param name="sr"></param>
        public void Load(StreamReader sr)
        {
            String line;

            string[] fields;
            char[]   sep = new char[] { ' ' };
            char     ch;


            while ((line = sr.ReadLine()) != null)
            {
                fields = line.Split(sep, 5, StringSplitOptions.RemoveEmptyEntries);
                if (fields.Length < 2)  // 若只定義中文字,未指定字根,則不處理。
                {
                    continue;
                }

                // 取出中文字,且不管中文字有幾個,都固定取第一個字。
                ch = fields[0][0];

                // 移除既有的項目,亦即後來載入的資料會蓋過之前載入的。
                if (_table.ContainsKey(ch))
                {
                    _table.Remove(ch);
                }
                List <Zhuyin> zyList = new List <Zhuyin>();
                for (int i = 1; i < fields.Length; i++)
                {
                    Zhuyin zy = new Zhuyin(fields[i]);
                    zyList.Add(zy);
                }
                _table.Add(ch, zyList);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 把輸入字串轉換成注音字根陣列。
        /// </summary>
        /// <param name="input">一個或一串中文字。</param>
        /// <returns>字串陣列。每個元素是一個中文字的注音字根,長度固定為 4,例如:"ㄅ ㄢ "。</returns>
        public string[] Convert(string input)
        {
            const int MaxPhraseLength = 10;

            var result = new List <string>();

            GetZhuyinFromDictionary(input, result, MaxPhraseLength);

            if (result.Count == 0) // 防錯:萬一找不到任何注音字根,還是要返回一個符合字串長度的陣列。
            {
                for (int i = 0; i < input.Length; i++)
                {
                    result.Add(string.Empty);
                }
            }

            if (AutoFillSpaces)
            {
                for (int i = 0; i < result.Count; i++)
                {
                    result[i] = Zhuyin.FillSpaces(result[i]);
                }
            }

            return(result.ToArray());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 剖析傳入的注音按鍵字串,並建立 Zhuyin 物件。
        /// </summary>
        /// <param name="zhuyinKeys"></param>
        /// <returns></returns>
        public static Zhuyin ParseKeyString(string zhuyinKeys)
        {
            Zhuyin zy      = new Zhuyin();
            string symbols = ZhuyinKeyMappings.KeyToSymbol(zhuyinKeys);

            zy.Parse(symbols);
            return(zy);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 取得整串中文字的注音碼。
        /// </summary>
        /// <param name="aChineseText">中文字串。</param>
        /// <returns>字串陣列。每個元素是一個中文字的注音字根,長度固定為 4,例如:"ㄅ ㄢ "。</returns>
        public string[] GetZhuyin(string aChineseText)
        {
            string[] zhuyinArray = _converter.Convert(aChineseText);

            // 調整注音碼,使其長度補滿四個字元.
            for (int i = 0; i < zhuyinArray.Length; i++)
            {
                zhuyinArray[i] = Zhuyin.FillSpaces(zhuyinArray[i]);
            }

            return(zhuyinArray);
        }
Ejemplo n.º 6
0
        public Zhuyin(Zhuyin zy)
            : this()
        {
            if (!zy.Validate())
            {
                throw new Exception("無效的注音符號: " + zy.ToString());
            }

            // 拷貝成員
            this.Tone = zy.Tone;
            zy.m_Symbols.CopyTo(m_Symbols, 0);
        }
Ejemplo n.º 7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="input">一個或一串中文字。</param>
        /// <returns>字串陣列。每個元素是一個中文字的注音字根,長度固定為 4,例如:"ㄅ ㄢ "。</returns>
        public string[] Convert(string input)
        {
            const int MaxPhraseLength = 10;

            var result = new List <string>();

            GetZhuyinFromDictionary(input, result, MaxPhraseLength);

            if (AutoFillSpaces)
            {
                for (int i = 0; i < result.Count; i++)
                {
                    result[i] = Zhuyin.FillSpaces(result[i]);
                }
            }

            return(result.ToArray());
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 剖析傳入的注音字根,並建立成 Zhuyin 物件傳回。
        /// </summary>
        /// <param name="zhuyinStr">代表某個中文字的注音符號,共四個字符,最後一個是音調符號。可包含全型空白。</param>
        /// <returns></returns>
        private void Parse(string zhuyinStr)
        {
            string s = Zhuyin.FillSpaces(zhuyinStr);

            if (s.Trim().Length < 1)
            {
                throw new Exception("指定的注音符號為空字串!");
            }

            m_Symbols[0] = s[0];
            m_Symbols[1] = s[1];
            m_Symbols[2] = s[2];

            // 取出音調
            Tone = ZhuyinTone.Tone1;  // 預設為一聲
            switch (s[3])
            {
            case 'ˊ':
                Tone = ZhuyinTone.Tone2;
                break;

            case 'ˇ':
                Tone = ZhuyinTone.Tone3;
                break;

            case 'ˋ':
                Tone = ZhuyinTone.Tone4;
                break;

            case Tone0Char:
                Tone = ZhuyinTone.Tone0;
                break;

            default:
                break;
            }

            if (!Validate())
            {
                throw new Exception("無效的注音符號: " + zhuyinStr);
            }
        }
Ejemplo n.º 9
0
        public override bool Equals(object obj)
        {
            Zhuyin zy = (Zhuyin)obj;

            if (this == zy)
            {
                return(true);
            }

            if (this.Tone != zy.Tone)
            {
                return(false);
            }
            for (int i = 0; i < m_Symbols.Length; i++)
            {
                if (m_Symbols[i] != zy.m_Symbols[i])
                {
                    return(false);
                }
            }
            return(true);
        }