/// <summary>
        ///     Gets the suggestions.
        /// </summary>
        /// <param name="basedOn">
        ///     String (as object) with the text on which the suggestions are based (i.e. the currently displayed
        ///     text).
        /// </param>
        /// <returns>
        ///     Returns an <see cref="T:System.Collections.ObjectModel.ObservableCollection`1" /> of strings containing the
        ///     suggestions.
        /// </returns>
        /// <remarks>
        ///     <para>
        ///         The suggestions are sorted by Presage. Configuration of Presage is done through an XML file located in its
        ///         installation folder.
        ///     </para>
        ///     <para>
        ///         Conversion between UTF8 (for this module) and Default encoding (for Presage) is performed when communicating
        ///         with the Presage Service.
        ///     </para>
        /// </remarks>
        public ObservableCollection <string> GetSuggestions(object basedOn)
        {
            var currentText = basedOn as string;

            if (currentText == null)
            {
                return(new ObservableCollection <string>());
            }

            if ((this.channel.State != CommunicationState.Opened) && (this.channel.State != CommunicationState.Opening))
            {
                this.InitializeChannel();
            }

            string prefix, lastWord;

            StringEditHelper.SplitStringPrefixAndLastWord(currentText, out prefix, out lastWord);

            prefix   = StringEditHelper.ConvertToDefaultEncodingFromUtf8(prefix);
            lastWord = StringEditHelper.ConvertToDefaultEncodingFromUtf8(lastWord);

            try
            {
                var result =
                    this.channel.predict(prefix, lastWord)
                    .Select(StringEditHelper.ConvertToUtf8FromDefaultEncoding)
                    .ToList();

                return(new ObservableCollection <string>(result));
            }
            catch (Exception)
            {
                return(new ObservableCollection <string>());
            }
        }
Beispiel #2
0
        private ObservableCollection <string> AddSpaceIfNecessary(ObservableCollection <string> collection)
        {
            var currentPrefix = StringEditHelper.RemoveLastWord(this.textModel.CurrentText);
            var needsSpace    = (currentPrefix.Length > 0) &&
                                !CharacterClasses.Whitespace.Contains(currentPrefix[currentPrefix.Length - 1]);
            var result = collection.Select(
                s =>
            {
                if (needsSpace)
                {
                    s = " " + s;
                }

                return(s);
            });

            return(new ObservableCollection <string>(result));
        }
Beispiel #3
0
        private ObservableCollection <string> AddUppercaseIfNecessary(ObservableCollection <string> collection)
        {
            var currentPrefix  = StringEditHelper.RemoveLastWord(this.textModel.CurrentText);
            var trimmedPrefix  = currentPrefix.Trim();
            var needsUppercase = (this.textModel.CurrentText.Trim().Length == 0) || (trimmedPrefix.Length == 0) ||
                                 CharacterClasses.FollowedByUppercase.Contains(
                trimmedPrefix[trimmedPrefix.Length - 1]);
            var result = collection.Select(
                s =>
            {
                if (needsUppercase)
                {
                    s = s.Length > 1 ? char.ToUpper(s[0]) + s.Substring(1) : s.ToUpper();
                }

                return(s);
            });

            return(new ObservableCollection <string>(result));
        }
Beispiel #4
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="TextViewModel" /> class.
        /// </summary>
        /// <param name="eventAggregator">Provides pub/sub events (obtained through DI).</param>
        public TextViewModel(IEventAggregator eventAggregator)
        {
            this.eventAggregator            = eventAggregator;
            this.AppendTextCommand          = new DelegateCommand <string>(this.AppendText);
            this.ClearTextCommand           = new DelegateCommand(() => this.CurrentText = string.Empty);
            this.RemoveLastCharacterCommand =
                new DelegateCommand(
                    () =>
                    this.CurrentText =
                        this.CurrentText.Length > 0
                                ? this.CurrentText.Remove(this.CurrentText.Length - 1)
                                : this.CurrentText,
                    () => this.CurrentText.Length > 0).ObservesProperty(() => this.CurrentText);
            this.RemoveLastWordCommand =
                new DelegateCommand(
                    () =>
            {
                this.CurrentText =
                    StringEditHelper.RemoveLastWord(this.CurrentText.TrimEnd(CharacterClasses.Whitespace));
            });
            this.RemoveLastWordWithoutTrimCommand =
                new DelegateCommand(() => { this.CurrentText = StringEditHelper.RemoveLastWord(this.CurrentText); });
            this.SaveTextCommand =
                new DelegateCommand(() =>
            {
                var saveFileDialog    = new SaveFileDialog();
                saveFileDialog.Filter = "Text file (*.txt)|*.txt|All(*.*)|*";
                if (saveFileDialog.ShowDialog() == true)
                {
                    File.WriteAllText(saveFileDialog.FileName, this.CurrentText);
                }
                this.CurrentText = StringEditHelper.RemoveLastWord(this.CurrentText);
            });

            Commands.AppendTextCommand.RegisterCommand(this.AppendTextCommand);
            Commands.ClearTextCommand.RegisterCommand(this.ClearTextCommand);
            Commands.RemoveLastCharacterCommand.RegisterCommand(this.RemoveLastCharacterCommand);
            Commands.RemoveLastWordCommand.RegisterCommand(this.RemoveLastWordCommand);
            Commands.RemoveLastWordWithoutTrimCommand.RegisterCommand(this.RemoveLastWordWithoutTrimCommand);
            Commands.SaveTextCommand.RegisterCommand(this.SaveTextCommand);
        }
Beispiel #5
0
        /// <summary>
        ///     Adds a suggestion.
        /// </summary>
        /// <param name="s">The suggestion.</param>
        private void WriteSuggestion(string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return;
            }

            var currentText = this.textModel.CurrentText;
            var lastWord    = StringEditHelper.GetLastWord(currentText);

            if (s.ToLower().StartsWith(lastWord.ToLower()))
            {
                Commands.RemoveLastWordWithoutTrimCommand.Execute(null);
            }
            else if (!StringEditHelper.EndsWithWhitespace(currentText))
            {
                Commands.AppendTextCommand.Execute(" ");
            }

            Commands.AppendTextCommand.Execute(s + " ");
        }