Exemple #1
0
        /// <summary>
        /// Replaces a misspelt word starting at startPos with the correct
        /// spelling and notifies the app of what needs to be replaced and
        /// where.
        /// </summary>
        /// <param name="word">word to replace</param>
        /// <param name="replacement">the replacemenet word</param>
        /// <param name="startPos">starting position of the word</param>
        /// <param name="isFirstWord">is this the first word of the sentence</param>
        private void replaceMisspeltWord(String word, String replacement, int startPos, bool isFirstWord)
        {
            try
            {
                using (AgentContext context = Context.AppAgentMgr.ActiveContext())
                {
                    String textToCaret = context.TextAgent().GetStringToCaret(startPos);
                    Log.Debug("textToCaret : [" + textToCaret + "]");
                    replacement = textToCaret.Replace(word, replacement);
                    Log.Debug("After replacement, replacement : [" + replacement + "]");
                    if (isFirstWord)
                    {
                        String cap = TextUtils.Capitalize(replacement);
                        if (cap != null)
                        {
                            replacement = cap;
                        }
                    }

                    Log.Debug("Replace word at " + startPos + ". Length: " + replacement.Length + ". replacement: " +
                              replacement);

                    context.TextAgent().Replace(startPos, word.Length + 1, replacement);
                }
            }
            catch (Exception ex)
            {
                Log.Exception(ex);
            }
        }
Exemple #2
0
        /// <summary>
        /// Call this function to check if an abbreviation has been detected
        /// in the input text stream.  It checks the word at the caret position and
        /// looks up the mapping table to detect an abbreviation.  If detected, it
        /// raises an event to replace the abbreviation and the app can do it.
        /// </summary>
        /// <param name="handled">was the abbr handled</param>
        /// <returns>The abbreviation object</returns>
        public Abbreviation CheckAndReplaceAbbreviation(ref bool handled)
        {
            Abbreviation abbr = null;

            handled = false;

            try
            {
                using (AgentContext context = Context.AppAgentMgr.ActiveContext())
                {
                    char charAtCaret;
                    if (!context.TextAgent().GetCharLeftOfCaret(out charAtCaret))
                    {
                        return(null);
                    }

                    if (CoreGlobals.AppPreferences.ExpandAbbreviationsOnSeparator &&
                        !TextUtils.IsTerminatorOrWhiteSpace(charAtCaret))
                    {
                        Log.Debug("no sentence terminator or white space here.  returning");
                        return(null);
                    }

                    String word;
                    int    startPos = context.TextAgent().GetPreviousWordAtCaret(out word);
                    Log.Debug("Prev word: " + word);
                    if (String.IsNullOrEmpty(word))
                    {
                        return(null);
                    }

                    // if there is a preceeding sentence terminator, we have to capitalize the word
                    bool isFirstWord = context.TextAgent().IsPreviousWordAtCaretTheFirstWord();

                    abbr = Context.AppAbbreviations.Lookup(word);

                    // do we detect something?
                    if (abbr != null)
                    {
                        String replacement = abbr.Expansion;

                        String stringToCaret = context.TextAgent().GetStringToCaret(startPos);

                        Log.Debug("String to caret from startPos " + startPos + ": [" + stringToCaret + "]");
                        replacement = stringToCaret.Replace(word, replacement);
                        Log.Debug("After replacement, replacement : [" + replacement + "]");

                        if (isFirstWord)
                        {
                            String capitalized = TextUtils.Capitalize(replacement);
                            if (capitalized != null)
                            {
                                replacement = capitalized;
                            }
                        }

                        int wordLength = word.Length +
                                         (CoreGlobals.AppPreferences.ExpandAbbreviationsOnSeparator ? 1 : 0);
                        String replaceWith = (abbr.Mode == Abbreviation.AbbreviationMode.Write)
                            ? replacement
                            : String.Empty;

                        context.TextAgent().Replace(startPos, wordLength, replaceWith);

                        if (abbr.Mode == Abbreviation.AbbreviationMode.Write)
                        {
                            handled = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Exception(ex);
            }

            return(abbr);
        }