예제 #1
0
 public static T Build(string s, Dict.Node dict)
 {
     return(new T {
         SrcStr = s,
         Bos = Bos,
         Eos = Eos,
         EndLocMap = GenEndLocMap(s, dict),
     });
 }
예제 #2
0
        static SortedDictionary <int, List <Node> > GenEndLocMap(string s, Dict.Node dict)
        {
            var m = new SortedDictionary <int, List <Node> >();

            m[0] = new List <Node> {
                Bos
            };
            m[s.Length + 1] = new List <Node> {
                Eos
            };

            for (var b = 0; b < s.Length; b++)
            {
                var nd = dict;
                for (var l = 1; l <= s.Length - b; l++)
                {
                    var ss = s.Substring(b, l);
                    nd = nd.Children.FirstOrDefault(x => x.Char == ss[l - 1]);
                    if (nd == null)
                    {
                        break;
                    }

                    var idx = b + l;    // 1-based end index
                    if (!m.ContainsKey(idx))
                    {
                        m[idx] = new List <Node>();
                    }
                    var nodes = nd.Words.Select(word => new Node {
                        Word = word
                    });
                    m[idx].AddRange(nodes);
                }
            }
            return(m);
        }