Example #1
0
        /// <summary>
        /// 根据中文分词返回全拼与拼音首字母
        /// </summary>
        public static List<string> GetSpellSegment(string word)
        {
            if (!IsChinese(word))
                return null;
            var rootstr = "root";
            var parents = new List<SpellNode>();
            var isAll = true;
            var isFirst = true;
            var root = new SpellNode(isAll, isFirst) { AllSpell = rootstr, FirstSpell = rootstr };
            parents.Add(root);
            for (int i = 0; i < word.Length; i++)
            {
                var ps = new List<SpellNode>();
                var node = SpellContainer.Instance[word[i]];
                if (node == null)
                    return null;
                for (int j = 0; j < node.AllSpells.Count; j++)
                {
                    foreach (var parent in parents)
                    {
                        var c = new SpellNode(isAll, isFirst) { AllSpell = node.AllSpells[j], FirstSpell = node.FirstSpells[j], Parent = parent };
                        parent.AddChild(c);
                        ps.Add(c);
                    }
                }
                parents = ps;

            }
            var result = new List<string>();
            LoadChildren(root, result, isAll, isFirst);
            return result.Distinct().ToList();
        }
Example #2
0
 internal void AddChild(SpellNode child)
 {
     if (_children == null)
     {
         _children = new List <SpellNode>();
     }
     _children.Add(child);
     child.InsertSpell(this);
 }
Example #3
0
        /// <summary>
        /// 根据中文分词返回拼音与首字母
        /// </summary>
        public static List <string> GetSpellSegment(string word, bool isAll, bool isFirst)
        {
            if (IsNumOrWord(word))
            {
                var list = new List <string>(1);
                list.Add(word);
            }
            var en      = FormatEnWord(word);
            var cn      = FormatCnWord(word);
            var rootstr = "root";

            if (!string.IsNullOrEmpty(en))
            {
                rootstr = en;
            }
            var parents = new List <SpellNode>();
            var root    = new SpellNode(isAll, isFirst)
            {
                AllSpell = rootstr, FirstSpell = rootstr
            };

            parents.Add(root);
            for (int i = 0; i < cn.Length; i++)
            {
                var ps   = new List <SpellNode>();
                var n    = cn[i];
                var node = SpellContainer.Instance[n];
                if (node != null)
                {
                    for (int j = 0; j < node.AllSpells.Count; j++)
                    {
                        foreach (var parent in parents)
                        {
                            var c = new SpellNode(isAll, isFirst)
                            {
                                AllSpell = node.AllSpells[j], FirstSpell = node.FirstSpells[j], Parent = parent
                            };
                            parent.AddChild(c);
                            ps.Add(c);
                        }
                    }
                    parents = ps;
                }
                else
                {
                    foreach (var parent in parents)
                    {
                        parent.EditSpell(n);
                    }
                }
            }
            var result = new List <string>();

            LoadChildren(root, result, isAll, isFirst);
            return(result.Distinct().ToList());
        }
Example #4
0
 internal void InsertSpell(SpellNode parent)
 {
     if (parent.AllSpell != "root" && IsALL)
     {
         _allspell.Insert(0, Parent.AllSpell);
     }
     if (parent.FirstSpell != "root" && IsFirst)
     {
         _firstspell.Insert(0, parent.FirstSpell);
     }
 }
Example #5
0
 /// <summary>
 /// 根据中文分词返回拼音与首字母
 /// </summary>
 public static List<string> GetSpellSegment(string word, bool isAll, bool isFirst)
 {
     if (IsNumOrWord(word))
     {
         var list = new List<string>(1);
         list.Add(word);
     }
     var en = FormatEnWord(word);
     var cn = FormatCnWord(word);
     var rootstr = "root";
     if (!string.IsNullOrEmpty(en))
         rootstr = en;
     var parents = new List<SpellNode>();
     var root = new SpellNode(isAll, isFirst) { AllSpell = rootstr, FirstSpell = rootstr };
     parents.Add(root);
     for (int i = 0; i < cn.Length; i++)
     {
         var ps = new List<SpellNode>();
         var n = cn[i];
         var node = SpellContainer.Instance[n];
         if (node != null)
         {
             for (int j = 0; j < node.AllSpells.Count; j++)
             {
                 foreach (var parent in parents)
                 {
                     var c = new SpellNode(isAll, isFirst) { AllSpell = node.AllSpells[j], FirstSpell = node.FirstSpells[j], Parent = parent };
                     parent.AddChild(c);
                     ps.Add(c);
                 }
             }
             parents = ps;
         }
         else
         {
             foreach (var parent in parents)
             {
                 parent.EditSpell(n);
             }
         }
     }
     var result = new List<string>();
     LoadChildren(root, result, isAll, isFirst);
     return result.Distinct().ToList();
 }
Example #6
0
        /// <summary>
        /// 根据中文分词返回全拼与拼音首字母
        /// </summary>
        public static List <string> GetSpellSegment(string word)
        {
            if (!IsChinese(word))
            {
                return(null);
            }
            var rootstr = "root";
            var parents = new List <SpellNode>();
            var isAll   = true;
            var isFirst = true;
            var root    = new SpellNode(isAll, isFirst)
            {
                AllSpell = rootstr, FirstSpell = rootstr
            };

            parents.Add(root);
            for (int i = 0; i < word.Length; i++)
            {
                var ps   = new List <SpellNode>();
                var node = SpellContainer.Instance[word[i]];
                if (node == null)
                {
                    return(null);
                }
                for (int j = 0; j < node.AllSpells.Count; j++)
                {
                    foreach (var parent in parents)
                    {
                        var c = new SpellNode(isAll, isFirst)
                        {
                            AllSpell = node.AllSpells[j], FirstSpell = node.FirstSpells[j], Parent = parent
                        };
                        parent.AddChild(c);
                        ps.Add(c);
                    }
                }
                parents = ps;
            }
            var result = new List <string>();

            LoadChildren(root, result, isAll, isFirst);
            return(result.Distinct().ToList());
        }
Example #7
0
 private static void LoadChildren(SpellNode parent, List <string> result, bool isAll, bool isFirst)
 {
     if (parent.Chindren != null)
     {
         foreach (var child in parent.Chindren)
         {
             LoadChildren(child, result, isAll, isFirst);
         }
     }
     else
     {
         if (isAll)
         {
             result.Add(parent.AllSpell);
         }
         if (isFirst)
         {
             result.Add(parent.FirstSpell);
         }
     }
 }
Example #8
0
 private static void LoadChildren(SpellNode parent, List<string> result, bool isAll, bool isFirst)
 {
     if (parent.Chindren != null)
     {
         foreach (var child in parent.Chindren)
         {
             LoadChildren(child, result, isAll, isFirst);
         }
     }
     else
     {
         if (isAll)
             result.Add(parent.AllSpell);
         if (isFirst)
             result.Add(parent.FirstSpell);
     }
 }
Example #9
0
 internal void AddChild(SpellNode child)
 {
     if (_children == null)
         _children = new List<SpellNode>();
     _children.Add(child);
     child.InsertSpell(this);
 }
Example #10
0
 internal void InsertSpell(SpellNode parent)
 {
     if (parent.AllSpell  != "root" && IsALL)
         _allspell.Insert(0, Parent.AllSpell);
     if (parent.FirstSpell != "root" && IsFirst)
         _firstspell.Insert(0, parent.FirstSpell);
 }