Exemple #1
0
        public void PredictionWalkThrough()
        {
            var environment = new SingleThreadedPredictionEnvironment
            {
                History = "the them they them they the the the" + Environment.NewLine
            };

            var predictor = PredictionEngineFactory.Create(environment);

            Assert.IsNotNull(predictor, "We got a predictor");

            CheckPrediction(predictor, "");

            Assert.AreEqual(1, environment.workItems.Count, "Queue contains the refresh action");
            environment.workItems[0]();
            environment.workItems.RemoveAt(0);
            Assert.AreEqual(0, environment.workItems.Count, "Queue is now empty");

            CheckPrediction(predictor, "", "the", "they", "them");

            Assert.AreEqual(0, environment.workItems.Count, "Queue is still empty");

            CheckPrediction(predictor, "t", "the", "they", "them");
            CheckPrediction(predictor, "th", "they", "the", "them");
            CheckPrediction(predictor, "the", "them", "the", "they");

            predictor.RecordHistory("Wibble", false);
            Assert.AreEqual("the them they them they the the the" + Environment.NewLine + "Wibble" + Environment.NewLine, environment.History, "History should have updated");
        }
        public void FirstPrediction()
        {
            var predictor   = PredictionEngineFactory.Create(new SingleThreadedPredictionEnvironment());
            var prediction  = predictor.CreatePrediction("", 0, 0, false, null);
            var suggestions = prediction.GetSuggestions(SuggestionType.Character);

            var count = suggestions.Count();

            Assert.AreEqual(26, count, "Expect to get 26 lowercase letter suggestions");

            var suggestionT = (from s in suggestions where s.Text == "t" select s).First();
            var suggestionZ = (from s in suggestions where s.Text == "z" select s).First();

            Assert.IsTrue(suggestionZ.Confidence < suggestionT.Confidence);
        }
Exemple #3
0
        public void ComplexCreation()
        {
            var environment = new SingleThreadedPredictionEnvironment(true);
            var queue       = new List <Action>();

            var predictor = PredictionEngineFactory.Create(environment);

            Assert.IsNotNull(predictor, "We got a predictor");

            Assert.AreEqual(1, environment.workItems.Count, "Queue contains the refresh action");

            var updated = false;

            predictor.PredictionChanged += (s, e) => updated = true;
            environment.workItems[0]();
            Assert.IsTrue(updated);
            environment.workItems.RemoveAt(0);

            Assert.AreEqual(0, environment.workItems.Count, "Queue is now empty");
        }
        static void Main()
        {
            var predictionEngineFactory = new PredictionEngineFactory();
            var predictionEngine        = predictionEngineFactory.Create();

            while (true)
            {
                Console.Write("Text? ");
                var line = Console.ReadLine();
                if (string.IsNullOrEmpty(line))
                {
                    break;
                }
                var input = new ModelInput
                {
                    SentimentText = line
                };

                var prediction = predictionEngine.Predict(input);
                Console.WriteLine($"{(prediction.Prediction ? string.Empty : "not ")}toxic {prediction.Score}");
            }
        }
        public void FirstAndSecondPredictionsDifferent()
        {
            var predictor = PredictionEngineFactory.Create(new SingleThreadedPredictionEnvironment());

            var prediction0  = predictor.CreatePrediction("", 0, 0, false, null);
            var suggestions0 = prediction0.GetSuggestions(SuggestionType.Character);

            var predictionT  = predictor.CreatePrediction("T", 1, 0, false, null);
            var suggestionsT = predictionT.GetSuggestions(SuggestionType.Character);

            var sameProbabilityCount    = 0;
            var similarProbabilityCount = 0;

            using (var enumerator0 = suggestions0.GetEnumerator())
            {
                foreach (var suggestionT in suggestionsT)
                {
                    Assert.IsTrue(enumerator0.MoveNext());
                    var suggestion0 = enumerator0.Current;

                    var probabilityDifference = suggestion0.Confidence - suggestionT.Confidence;

                    if (Math.Abs(probabilityDifference) < 0.01)
                    {
                        similarProbabilityCount++;

                        Assert.IsFalse(probabilityDifference == 0, "Differences should not be absolutely zero");
                    }
                }

                Assert.IsFalse(enumerator0.MoveNext());

                Assert.IsTrue(similarProbabilityCount < 13);
                Assert.IsTrue(sameProbabilityCount == 0);
            }
        }
        public void SimpleCreation()
        {
            var predictor = PredictionEngineFactory.Create(new SingleThreadedPredictionEnvironment());

            Assert.IsNotNull(predictor);
        }
Exemple #7
0
        KeyboardHost(IKeyboardApplicationEnvironment environment)
        {
            _environment = environment;
            _editor      = new TextBoxEditor(_environment.ClipboardProvider);

            SetTrainingPhrase();
            _environment.AppSettings.Keyboard.AttachPropertyChangedAction(nameof(_environment.AppSettings.Keyboard.IsTrainingMode), SetTrainingPhrase);

            _isUpdateAvailable            = _environment.IsUpdateAvailable;
            _environment.UpdateAvailable += (s, e) => IsUpdateAvailable = _environment.IsUpdateAvailable;

            var publicTextToAudio  = _environment.TextToAudioProviderFactory.Create(AppSettings.Instance.PublicNarration);
            var privateTextToAudio = _environment.TextToAudioProviderFactory.Create(AppSettings.Instance.PrivateNarration);

            ActivityDisplayStatus = _environment.ActivityDisplayProvider.Status;
            _environment.ActivityDisplayProvider.StatusChanged += (s, e) => ActivityDisplayStatus = _environment.ActivityDisplayProvider.Status;

            var publicNarrator  = new Narrator(environment.Random, AppSettings.Instance.PublicNarration, _environment.AudioProviderFactory, publicTextToAudio, _environment.ActivityDisplayProvider);
            var privateNarrator = new Narrator(environment.Random, AppSettings.Instance.PrivateNarration, _environment.AudioProviderFactory, privateTextToAudio, NullActivityDisplayProvider.Instance);

            _narrator = new CompoundNarrator(publicNarrator, privateNarrator);

            Speak             = new RelayCommand(SpeakAction);
            SendToSlack       = new RelayCommand(SendToSlackAction, (o) => AppSettings.Instance.General.SlackHostUri != GeneralSettings.UnsetSlackHostUri);
            Clear             = new RelayCommand(ClearAction);
            UpdateApplication = new RelayCommand(UpdateApplicationAction);
            ExitApplication   = new RelayCommand(ExitApplicationAction);
            Calibrate         = new RelayCommand(CalibrateAction);

            for (var index = 0; index < 7; index++)
            {
                suggestionItems.Add(new SuggestionItem(this, index));
            }
            for (var index = 0; index < 6; index++)
            {
                phraseItems.Add(new PhraseItem(this, index));
            }

            ShiftToggleState   = ToggleStates["Shift"];
            ControlToggleState = ToggleStates["Control"];
            AltToggleState     = ToggleStates["Alt"];

            ShiftToggleState.CheckChanged += OnShiftChanged;

            _predictor = PredictionEngineFactory.Create(PredictionEnvironment.Instance);
            _predictor.PredictionChanged += OnPredictionChanged;

            _ambiguousKeys = new List <List <string> >();

            _minuteTick.Tick += OnMinuteTick;
            ScheduleNextMinuteTick();

            ShowActiveListening  = new RelayCommand((o) => AppSettings.Instance.Prediction.PredictionLayout = PredictionLayout.ActiveListening);
            ShowWordPrediction   = new RelayCommand((o) => AppSettings.Instance.Prediction.PredictionLayout = PredictionLayout.WordsAlone);
            ShowPhrasePrediction = new RelayCommand((o) => AppSettings.Instance.Prediction.PredictionLayout = PredictionLayout.PhraseAlone);

            ActiveListeningLaugh       = new RelayCommand((o) => SpeakEffect(AudioGesture.Laugh));
            ActiveListeningHmm         = new RelayCommand((o) => SpeakEffect(AudioGesture.Hmm));
            ActiveListeningSarcasm     = new RelayCommand((o) => SpeakEffect(AudioGesture.Sarcasm));
            ActiveListeningOh          = new RelayCommand((o) => SpeakEffect(AudioGesture.Oh));
            ActiveListeningSharpBreath = new RelayCommand((o) => SpeakEffect(AudioGesture.SharpBreath));
            ActiveListeningArgh        = new RelayCommand((o) => SpeakEffect(AudioGesture.Argh));
            ActiveListeningCough       = new RelayCommand((o) => SpeakEffect(AudioGesture.Cough));
            ActiveListeningUgh         = new RelayCommand((o) => SpeakEffect(AudioGesture.Ugh));
        }
Exemple #8
0
        static void Main(string[] args)
        {
            using (var writer = File.CreateText("output.txt"))
            {
                var text = Resources.Script;

                var environment = new SingleThreadedPredictionEnvironment();
                var predictor   = PredictionEngineFactory.Create(environment);

                DoBackgroundWork(environment);

                for (var pass = 0; pass < 2; pass++)
                {
                    var sentence = string.Empty;

                    var gainCount  = 0;
                    var totalCount = 0;

                    var position = text.PunctuationLength(0);
                    while (position < text.Length)
                    {
                        var wordStart  = position;
                        var wordLength = text.WordLength(position);
                        position += wordLength;

                        var word = text.Substring(wordStart, wordLength);

                        var punctuationStart  = position;
                        var punctuationLength = text.PunctuationLength(position);
                        position += punctuationLength;

                        var punctuation = text.Substring(punctuationStart, punctuationLength);

                        var initialPrediction  = predictor.CreatePrediction(sentence, 0, sentence.Length, false, null);
                        var initialSuggestions = initialPrediction.GetSuggestions(SuggestionType.Word);

                        var suggested = false;

                        using (var enumerator = initialSuggestions.GetEnumerator())
                        {
                            var suggestion = 0;
                            while (!suggested && enumerator.MoveNext())
                            {
                                if (string.Compare(enumerator.Current.Text, word, StringComparison.InvariantCultureIgnoreCase) == 0)
                                {
                                    suggested = true;
                                    enumerator.Current.Accepted(suggestion);
                                }

                                suggestion++;
                            }
                        }

                        var inputCount = 1;

                        if (!suggested)
                        {
                            var prefix = sentence;

                            var keys = 0;
                            while (keys < word.Length && !suggested)
                            {
                                prefix += word[keys];
                                keys++;
                                inputCount++;

                                var wordPrediction  = predictor.CreatePrediction(prefix, prefix.Length, 0, false, null);
                                var wordSuggestions = wordPrediction.GetSuggestions(SuggestionType.Word);

                                using (var enumerator = wordSuggestions.GetEnumerator())
                                {
                                    if (enumerator.MoveNext())
                                    {
                                        if (string.Compare(enumerator.Current.Text, word, StringComparison.InvariantCultureIgnoreCase) == 0)
                                        {
                                            suggested = true;
                                            enumerator.Current.Accepted(0);
                                        }
                                    }
                                }
                            }
                        }

                        var textCount = word.Length + 1;
                        gainCount  += textCount - inputCount;
                        totalCount += textCount;

                        var line = string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", word, textCount, inputCount, gainCount, totalCount, suggested);
                        Console.WriteLine(line);
                        writer.WriteLine(line);

                        if (punctuation.IsSentenceEnding() || position == text.Length)
                        {
                            predictor.RecordHistory(sentence, false);
                            sentence = string.Empty;
                        }
                        else if (sentence.Length == 0)
                        {
                            sentence = word;
                        }
                        else
                        {
                            sentence += " " + word;
                        }

                        DoBackgroundWork(environment);
                    }
                }
            }
        }
Exemple #9
0
 public void WarAndPeaceFireUp()
 {
     var environment = new TestPredictionEnvironment(Resources.spoken);
     var predictor   = PredictionEngineFactory.Create(environment);
 }