Ejemplo n.º 1
0
        static void ExercisePhrase(PredictionDictionary dictionary, string prefixString, string expectedsString)
        {
            var prefix    = prefixString.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            var expecteds = expectedsString.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            ExercisePhrase(dictionary, prefix, expecteds);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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();
        }
Ejemplo n.º 4
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();
        }
Ejemplo n.º 5
0
        static void ExercisePrediction(PredictionDictionary dictionary, params string[] prefix)
        {
            for (var i = prefix.Length - 1; 0 <= i; i--)
            {
                Debug.Write(prefix[i] + " ");
            }
            Debug.Write(" =>");

            //foreach (var word in dictionary.MakePredictions(7, "", prefix))
            //{
            //    Debug.Write(" " + word);
            //}
            Debug.WriteLine("");
        }
Ejemplo n.º 6
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");
        }
Ejemplo n.º 7
0
        public void SaveAndLoadDictionary()
        {
            var originalDictionary = CreateDictionary();

            var stream = new MemoryStream();

            originalDictionary.Save(stream);

            stream.Position = 0;

            var reconstitutedDictionary = PredictionDictionary.Load(stream);

            Assert.AreEqual(stream.Length, stream.Position);

            ExercisePhrase(originalDictionary, "the quick brown", "fox jumped");
            ExercisePhrase(reconstitutedDictionary, "the quick brown", "fox jumped");
        }
Ejemplo n.º 8
0
        static void ExercisePhrase(PredictionDictionary dictionary, string[] prefix, params string[] expecteds)
        {
            //var prefixReversed = prefix;
            //Array.Reverse(prefixReversed);

            //var enumerable = dictionary.MakePhrasePrediction(prefixReversed);

            //using (var enumerator = enumerable.GetEnumerator())
            //{
            //    var count = 0;

            //    for (; count < 6 && enumerator.MoveNext(); count++)
            //    {
            //        Assert.AreEqual(expecteds[count], enumerator.Current);
            //    }

            //    if (count != 6)
            //    {
            //        Assert.AreEqual(expecteds.Length, count);
            //    }
            //}
        }
        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);
        }
 internal static void Reset()
 {
     _historicSuggester = null;
 }