コード例 #1
0
        public void Learn(Stream words)
        {
            var wordStream = new WordStream(words);
            LinkedList <string> wordHistory = new LinkedList <string>();

            for (int i = 0; i < 4; i++)
            {
                wordHistory.AddLast(String.Empty);
            }
            foreach (var word in wordStream)
            {
                using (var transaction = new CommittableTransaction())
                {
                    wordHistory.RemoveFirst();
                    wordHistory.AddLast(word);

                    HandleWord(word);
                    HandleTwoGram(wordHistory.AsEnumerable().Skip(2).Take(2).ToList());
                    var gram = HandleThreeGram(wordHistory.AsEnumerable().Skip(1).Take(3).ToList());
                    HandleFourGram(wordHistory.AsEnumerable().ToList(), gram);

                    context.SaveChanges();
                    transaction.Commit();
                }
            }
        }
コード例 #2
0
        public virtual void Learn(Stream words)
        {
            context.Configuration.AutoDetectChangesEnabled = false;
            context.Configuration.ValidateOnSaveEnabled    = false;
            StreamReader reader = new StreamReader(words);
            int          l      = 0;

            while (!reader.EndOfStream)
            {
                l++;
                expectedCount++;
                string line = reader.ReadLine();
                FillGram(line.Split(' ', '\t'));
                if (expectedCount == 1000)
                {
                    Console.WriteLine(l);
                    context.SaveChanges();
                    context.Dispose();
                    context = new WordPredictionContext();
                    context.Configuration.AutoDetectChangesEnabled = false;
                    context.Configuration.ValidateOnSaveEnabled    = false;
                    expectedCount = 0;
                }
            }
            context.SaveChanges();
        }