Exemplo n.º 1
0
        public static List <Pinyin> Convert(string text, bool removeTone = false)
        {
            var list   = new List <Pinyin>();
            var tokens = _trie.Tokenize(text);

            foreach (var t in tokens)
            {
                var frag = t.Fragment;
                if (t.IsMatch)
                {
                    var py = ConvertSingle(frag);
                    if (removeTone)
                    {
                        py = Convert2Tone5(py);
                    }
                    list.Add(py);
                }
                else
                {
                    var pys = PinyinDictionary.Translate2Pinyin(frag);
                    if (removeTone)
                    {
                        for (int i = 0; i < pys.Count; i++)
                        {
                            pys[i] = Convert2Tone5(pys[i]);
                        }
                    }
                    list.AddRange(pys);
                }
            }
            return(list);
        }
Exemplo n.º 2
0
        public static Tuple <List <Pinyin>, List <bool> > Convert2Tuple(string text, bool removeTone)
        {
            var pinyins = new List <Pinyin>();
            var bools   = new List <bool>();

            var tokens = _trie.Tokenize(text);

            foreach (var t in tokens)
            {
                var frag = t.Fragment;
                if (t.IsMatch)
                {
                    var py = ConvertSingle(frag);
                    if (removeTone)
                    {
                        py = Convert2Tone5(py);
                    }
                    pinyins.Add(py);
                    bools.Add(frag.Length == py.Pinyin_.Length);    // true表示是一个拼音,false表示是一个输入法头
                }
                else
                {
                    var pys = PinyinDictionary.Translate2Pinyin(frag);

                    pinyins.AddRange(pys);
                    for (int i = 0; i < pys.Count; i++)
                    {
                        bools.Add(true);            // 表示这是一个拼音
                        if (removeTone)
                        {
                            pys[i] = Convert2Tone5(pys[i]);
                        }
                    }
                }
            }
            return(new Tuple <List <Pinyin>, List <bool> >(pinyins, bools));
        }