Esempio n. 1
0
 private IEnumerable <LazyTrie> ReadChildren()
 {
     if (!File.Exists(_fileName))
     {
         yield break;
     }
     using (var fs = File.Open(_fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
         using (var reader = new StreamReader(fs, Encoding.Unicode))
         {
             Ffw(reader);
             var rowIndex = _rowIndex;
             while (true)
             {
                 var line = reader.ReadLine();
                 if (string.IsNullOrWhiteSpace(line))
                 {
                     break;
                 }
                 var id    = line.Substring(0, line.IndexOf(':'));
                 var c     = line[0];
                 var eow   = int.Parse(line.Substring(id.Length + 1)) == 1;
                 var depth = Int32.Parse(id.Substring(id.IndexOf('.') + 1));
                 if (depth == _depth + 1)
                 {
                     rowIndex++;
                     var trie = new LazyTrie(c, depth, eow, rowIndex, _fileName);
                     yield return(trie);
                 }
                 else if (depth == _depth)
                 {
                     break;
                 }
             }
         }
 }
Esempio n. 2
0
        public void Add(IEnumerable <char> word, int depth = 0)
        {
            if (word == null)
            {
                throw new ArgumentNullException("word");
            }
            var list = word.ToArray();

            if (list.Length == 0)
            {
                throw new ArgumentOutOfRangeException("word");
            }

            LazyTrie child;

            if (!TryResolveChild(list[0], depth, out child))
            {
                child = new LazyTrie(list, depth, _fileName);
                _nodes.Add(list[0], child);
            }
            else
            {
                child.Append(list, depth);
            }
        }
Esempio n. 3
0
        private IEnumerable <LazyTrie> GetBeginningOfWords()
        {
            var rowIndex = 0;

            using (var fs = File.Open(_fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                using (var reader = new StreamReader(fs, Encoding.Unicode))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        rowIndex++;
                        if (string.IsNullOrWhiteSpace(line))
                        {
                            break;
                        }
                        var id    = line.Substring(0, line.IndexOf(':'));
                        var c     = line[0];
                        var depth = Int32.Parse(id.Substring(id.IndexOf('.') + 1));
                        if (depth == 0)
                        {
                            var eow  = int.Parse(line.Substring(id.Length + 1)) == 1;
                            var trie = new LazyTrie(c, depth, eow, rowIndex, _fileName);
                            yield return(trie);
                        }
                    }
                }
        }
Esempio n. 4
0
        public void Put(LazyTrie trie, string directory)
        {
            var id       = string.Format("{0}.{1}", trie.Val, trie.Depth);
            var fileName = Path.Combine(directory, _containerId + ".tc");

            InitWriteSession(fileName);
            _writer.WriteLine("{0}:{1}", id, trie.Eow ? 1 : 0);
        }
Esempio n. 5
0
        public bool TryResolveChild(char c, int depth, out LazyTrie trie)
        {
            if (_nodes == null)
            {
                ResolveChildren();
            }
// ReSharper disable once PossibleNullReferenceException
            return(_nodes.TryGetValue(c, out trie));
        }