コード例 #1
0
        static PredictionDictionary CreateDictionary(IEnumerable <string> sentences)
        {
            var environment = new TestPredictionEnvironment();
            var dictionary  = PredictionDictionary.Create(environment);

            foreach (var sentence in sentences)
            {
                var position = sentence.PunctuationLength(0);

                var words = new List <string>();
                while (position < sentence.Length)
                {
                    var wordLength = sentence.WordLength(position);
                    var word       = sentence.Substring(position, wordLength).ToLowerInvariant();
                    position += wordLength;
                    position += sentence.PunctuationLength(position);

                    words.Add(word);
                }

                dictionary.AddPhrase(words.ToArray());
            }

            return(dictionary);
        }
コード例 #2
0
        public void AllTestPhrases()
        {
            var environment = new TestPredictionEnvironment();
            var dictionary  = PredictionDictionary.Create(environment);

            foreach (var sentence in TestSentences.Instance)
            {
                var position = sentence.PunctuationLength(0);

                var words = new List <string>();
                while (position < sentence.Length)
                {
                    var wordLength = sentence.WordLength(position);
                    var word       = sentence.Substring(position, wordLength).ToLowerInvariant();
                    position += wordLength;
                    position += sentence.PunctuationLength(position);

                    words.Add(word);
                }

                dictionary.AddPhrase(words.ToArray());
            }

            dictionary.Dump();
        }
コード例 #3
0
        public void SimpleQuickBrownFox()
        {
            var environment = new TestPredictionEnvironment();
            var dictionary  = PredictionDictionary.Create(environment);

            dictionary.AddPhrase("the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog");
            dictionary.Dump();
        }
コード例 #4
0
        public void OrderNeutrality()
        {
            var environment = new TestPredictionEnvironment();

            var dictionaryAB = PredictionDictionary.Create(environment);

            dictionaryAB.AddPhrase("A");
            dictionaryAB.AddPhrase("B");

            var dictionaryBA = PredictionDictionary.Create(environment);

            dictionaryBA.AddPhrase("B");
            dictionaryBA.AddPhrase("A");
        }
コード例 #5
0
        internal static void UpdatePredictor(Predictor predictor)
        {
            if (_luceneWordSuggester == null)
            {
                var index = WordIndexFactory.CreateFromWordCountList(predictor.Environment, WordScorePairEnumerable.Instance);
                _luceneWordSuggester = new LuceneWordSuggester(index);
            }

            if (_historicSuggester == null)
            {
                _historicSuggester = PredictionDictionary.Create(predictor.Environment);
            }

            var newHistory = predictor.ConsumeNewHistory();

            var updated = false;

            foreach (var utterance in newHistory)
            {
                updated = true;
                _historicSuggester.AddRawPhrases(utterance);
            }

            if (updated)
            {
                using (var stream = predictor.Environment.CreateDynamicDictionaryCache())
                {
                    _historicSuggester.Save(stream);
                }
            }

            var historicWithFallbackSuggester = new CompoundWordSuggester(_historicSuggester, SingleLetterSuggester.Instance);

            var wordSuggester = CreateCompoundWordSuggester(historicWithFallbackSuggester);

            predictor.UpdateConfiguration(wordSuggester, _historicSuggester);
        }