Example #1
0
        /// <summary>
        /// Make an individual spelling dictionary conform as closely as possible to the spelling status
        /// recorded in Wordforms.
        /// </summary>
        /// <param name="ws"></param>
        /// <param name="cache"></param>
        public static void ConformOneSpellingDictToWordforms(int ws, LcmCache cache)
        {
            ILcmServiceLocator servloc = cache.ServiceLocator;
            var lgwsFactory            = servloc.GetInstance <ILgWritingSystemFactory>();
            var dict = SpellingHelper.GetSpellChecker(ws, lgwsFactory);

            if (dict == null)
            {
                return;
            }
            // we only force one to exist for the default, others might not have one.
            var words = new List <string>();

            foreach (IWfiWordform wf in servloc.GetInstance <IWfiWordformRepository>().AllInstances())
            {
                if (wf.SpellingStatus != (int)SpellingStatusStates.correct)
                {
                    continue;                     // don't put it in the list of correct words
                }
                string wordform = wf.Form.get_String(ws).Text;
                if (!string.IsNullOrEmpty(wordform))
                {
                    words.Add(wordform);
                }
            }
            SpellingHelper.ResetDictionary(SpellingHelper.DictionaryId(ws, lgwsFactory), words);
        }
        public override bool Matches(SIL.FieldWorks.Common.COMInterfaces.ITsString arg)
        {
            var dict = SpellingHelper.GetSpellChecker(m_ws, WritingSystemFactory);

            return(new SpellCheckMethod(arg, dict, m_ws, WritingSystemFactory.get_CharPropEngine(m_ws)).Run());
        }
Example #3
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Get a list of suggested corrections if the selection is a spelling or similar error.
        /// Returns null if there is no problem at the selection location.
        /// Note that it may also return an empty list; this has a distinct meaning, namely,
        /// that there IS a problem, but we have no useful suggestions for what to change it to.
        /// nonSpellingError is set true when the error is not simply a mis-spelled word in a
        /// single writing system; currently this should disable or hide the commands to add
        /// the word to the dictionary or change multiple occurrences.
        /// The input arguments indicate where the user clicked and allow us to find the
        /// text he might be trying to correct. The other output arguments indicate which WS
        /// (wasAlt -- 0 for simple string) of which property (tag) of which object (hvoObj)
        /// is affected by the change, the ws of the mis-spelled word, and the corresponding
        /// spelling engine. Much of this information is already known to the
        /// SpellCorrectMenuItems returned, but some clients use it in creating other menu options.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        public ICollection <SpellCorrectMenuItem> GetSuggestions(Point mousePos,
                                                                 SimpleRootSite rootsite, out int hvoObj, out int tag, out int wsAlt, out int wsText,
                                                                 out string word, out ISpellEngine dict, out bool nonSpellingError)
        {
            hvoObj           = tag = wsAlt = wsText = 0;   // make compiler happy for early returns
            word             = null;
            dict             = null;
            nonSpellingError = true;

            IVwRootBox rootb = rootsite != null ? rootsite.RootBox : null;

            if (rootb == null)
            {
                return(null);
            }

            // Get a selection at the indicated point.
            IVwSelection sel = rootsite.GetSelectionAtPoint(mousePos, false);

            // Get the selected word and verify that it is a single run within a single
            // editable string.
            if (sel != null)
            {
                sel = sel.GrowToWord();
            }
            if (sel == null || !sel.IsRange || sel.SelType != VwSelType.kstText || !SelectionHelper.IsEditable(sel))
            {
                return(null);
            }
            ITsString tss;
            bool      fAssocPrev;
            int       ichAnchor;

            sel.TextSelInfo(false, out tss, out ichAnchor, out fAssocPrev, out hvoObj, out tag, out wsAlt);
            int ichEnd, hvoObjE, tagE, wsE;

            sel.TextSelInfo(true, out tss, out ichEnd, out fAssocPrev, out hvoObjE, out tagE, out wsE);
            if (hvoObj != hvoObjE || tag != tagE || wsAlt != wsE)
            {
                return(null);
            }

            int ichMin = Math.Min(ichEnd, ichAnchor);
            int ichLim = Math.Max(ichEnd, ichAnchor);

            ILgWritingSystemFactory wsf = rootsite.RootBox.DataAccess.WritingSystemFactory;

            // May need to enlarge the word beyond what GrowToWord does, if there is adjacent wordforming material.
            int ichMinAdjust = AdjustWordBoundary(wsf, tss, false, ichMin, 0) + 1;             // further expanded start of word.
            int ichLimAdjust = AdjustWordBoundary(wsf, tss, true, ichLim - 1, tss.Length);     // further expanded lim of word.
            // From the ends we can strip stuff with different spell-checking properties.
            IVwStylesheet styles     = rootsite.RootBox.Stylesheet;
            int           spellProps = SpellCheckProps(tss, ichMin, styles);

            while (ichMinAdjust < ichMin && SpellCheckProps(tss, ichMinAdjust, styles) != spellProps)
            {
                ichMinAdjust++;
            }
            while (ichLimAdjust > ichLim && SpellCheckProps(tss, ichLimAdjust - 1, styles) != spellProps)
            {
                ichLimAdjust--;
            }
            ichMin = ichMinAdjust;
            ichLim = ichLimAdjust;

            // Now we have the specific range we will check. Get the actual string.
            ITsStrBldr bldr = tss.GetBldr();

            if (ichLim < bldr.Length)
            {
                bldr.ReplaceTsString(ichLim, bldr.Length, null);
            }
            if (ichMin > 0)
            {
                bldr.ReplaceTsString(0, ichMin, null);
            }
            ITsString tssWord = bldr.GetString();

            // See whether we need the special blue underline, which is used mainly for adjacent words in different writing systems.
            List <int> wss = TsStringUtils.GetWritingSystems(tssWord);

            if (wss.Count > 1)
            {
                return(MakeWssSuggestions(tssWord, wss, rootb, hvoObj, tag, wsAlt, ichMin, ichLim));
            }
            ITsString keepOrcs;             // holds any ORCs we found in the original word that we need to keep rather than reporting.
            IList <SpellCorrectMenuItem> result = MakeEmbeddedNscSuggestion(ref tssWord, styles, rootb,
                                                                            hvoObj, tag, wsAlt, ichMin, ichLim, out keepOrcs);

            if (result.Count > 0)
            {
                return(result);
            }

            // Determine whether it is a spelling problem.
            wsText = TsStringUtils.GetWsOfRun(tssWord, 0);
            dict   = SpellingHelper.GetSpellChecker(wsText, wsf);
            if (dict == null)
            {
                return(null);
            }
            word = tssWord.get_NormalizedForm(FwNormalizationMode.knmNFC).Text;
            if (word == null)
            {
                return(null);                // don't think this can happen, but...
            }
            if (dict.Check(word))
            {
                return(null);                // not mis-spelled.
            }
            // Get suggestions. Make sure to return an empty collection rather than null, even if no suggestions,
            // to indicate an error.
            ICollection <string> suggestions = dict.Suggest(word);

            foreach (string suggest in suggestions)
            {
                ITsString replacement = TsStringUtils.MakeString(suggest, wsText);
                if (keepOrcs != null)
                {
                    ITsStrBldr bldrRep = keepOrcs.GetBldr();
                    bldrRep.ReplaceTsString(0, 0, replacement);
                    replacement = bldrRep.GetString();
                }
                result.Add(new SpellCorrectMenuItem(rootb, hvoObj, tag, wsAlt, ichMin, ichLim, suggest,
                                                    replacement));
            }
            nonSpellingError = false;             // it IS a spelling problem.
            return(result);
        }
Example #4
0
        /// <summary>
        /// Succeed if some word in the argument is mis-spelled.
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        public override bool Matches(ITsString arg)
        {
            ISpellEngine dict = SpellingHelper.GetSpellChecker(m_ws, WritingSystemFactory);

            return(new SpellCheckMethod(arg, dict, WritingSystemFactory.get_EngineOrNull(m_ws)).Run());
        }