Ejemplo n.º 1
0
        /// <summary>
        /// 找出在指定的字串中出現的所有片語。
        /// </summary>
        /// <param name="text">字串。</param>
        /// <returns>找到的片語集合,它是一個排序過的串列,排序的 key 是片語位於來源字串的的索引,Value 則是 ZhuyinPhrase 型別的物件。</returns>
        public SortedList <int, ZhuyinPhrase> FindPhrases(string text)
        {
            SortedList <int, ZhuyinPhrase> matchedPhrases = new SortedList <int, ZhuyinPhrase>();

            int           idx;
            int           start;
            int           end;
            int           count;
            ZhuyinPhrase  immPhrase;
            List <Zhuyin> zhuyinList;

            foreach (string phrase in _table.Keys)
            {
                start = 0;
                end   = text.Length - 1;
                while (start <= end)
                {
                    count = end - start + 1;
                    idx   = text.IndexOf(phrase, start, count);
                    if (idx < 0)
                    {
                        break;
                    }
                    // 有找到,記錄位置、取得注音字根,並繼續往後面找。
                    zhuyinList = _table[phrase];
                    immPhrase  = new ZhuyinPhrase(phrase, zhuyinList);
                    matchedPhrases.Add(idx, immPhrase);
                    start = idx + phrase.Length;
                }
            }
            return(matchedPhrases);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 找出在指定的字串中出現的所有片語。
        /// </summary>
        /// <param name="text">字串。</param>
        /// <returns>找到的片語集合,它是一個排序過的串列,排序的 key 是片語位於來源字串的的索引,Value 則是 ZhuyinPhrase 型別的物件。</returns>
        public SortedList <int, ZhuyinPhrase> FindPhrases(string text)
        {
            SortedList <int, ZhuyinPhrase> matchedPhrases = new SortedList <int, ZhuyinPhrase>();

            int           idx;
            int           start;
            int           end;
            int           count;
            ZhuyinPhrase  immPhrase;
            List <Zhuyin> zhuyinList;

            foreach (string phrase in _table.Keys)
            {
                start = 0;
                end   = text.Length - 1;
                while (start <= end)
                {
                    count = end - start + 1;
                    idx   = text.IndexOf(phrase, start, count);
                    if (idx < 0)
                    {
                        break;
                    }
                    if (matchedPhrases.ContainsKey(idx))
                    {
                        // 若先前已經有符合的片語,而且該片語長度比目前這個片語還要長,則保留既有的,並捨棄目前的片語。
                        var previousMatch = matchedPhrases[idx];
                        if (previousMatch.Text.Length > phrase.Length)
                        {
                            break;
                        }
                        matchedPhrases.Remove(idx);
                    }
                    // 有找到符合的片語。記錄位置、取得注音字根,並繼續往後面找。
                    zhuyinList = _table[phrase];
                    immPhrase  = new ZhuyinPhrase(phrase, zhuyinList);
                    matchedPhrases.Add(idx, immPhrase);
                    start = idx + phrase.Length;
                }
            }
            return(matchedPhrases);
        }