private IEnumerable <String> GetSpellingDictionaryFilesList() { var dictionaryFiles = new HashSet <String>(); var wsManager = m_cache.ServiceLocator.WritingSystemManager; foreach (IWritingSystem ws in wsManager.LocalWritingSystems) { var spellCheckingDictionary = ws.SpellCheckingId; if (string.IsNullOrEmpty(spellCheckingDictionary) || spellCheckingDictionary == "<None>") { continue; // no spelling dictionary for WS } if (SpellingHelper.DictionaryExists(spellCheckingDictionary)) { foreach (var path in SpellingHelper.PathsToBackup(spellCheckingDictionary)) { dictionaryFiles.Add(path); } } } //Now that we have the list of spelling files if (!Directory.Exists(m_settings.SpellingDictionariesPath)) { Directory.CreateDirectory(m_settings.SpellingDictionariesPath); } else { RemoveAllFilesFromFolder(m_settings.SpellingDictionariesPath); } CopyAllFilesToFolder(dictionaryFiles, m_settings.SpellingDictionariesPath); return(AllFilesInADirectory(m_settings.SpellingDictionariesPath)); }
/// <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 bool Undo() { if (m_rootb != null && m_dataAccess != null) { SpellingHelper.SetSpellingStatus(m_word, m_wsText, m_dataAccess.WritingSystemFactory, false); m_rootb.PropChanged(m_hvoObj, m_tag, m_wsAlt, 1, 1); return(true); } return(false); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Make the external spelling dictionary conform as closely as possible to the spelling /// status recorded in the Wordforms. We try to keep these in sync, but when we first /// create an external spelling dictionary we need to make it match, and even later, on /// restoring a backup or when a user on another computer changed the database, we may /// need to re-synchronize. The best we can do is to Add all the words we know are /// correct and remove all the others we know about at all; it's possible that a /// wordform that was previously correct and is now deleted will be thought correct by /// the dictionary. In the case of a major language, of course, it's also possible that /// words that were never in our inventory at all will be marked correct. This is the /// best we know how to do. /// /// We also force there to be an external spelling dictionary for the default vernacular WS; /// others are updated only if they already exist. /// </summary> /// ------------------------------------------------------------------------------------ public static void ConformSpellingDictToWordforms(LcmCache cache) { // Force a dictionary to exist for the default vernacular writing system. ILcmServiceLocator servloc = cache.ServiceLocator; var lgwsFactory = servloc.GetInstance <ILgWritingSystemFactory>(); SpellingHelper.EnsureDictionary(cache.DefaultVernWs, lgwsFactory); // Make all existing spelling dictionaries give as nearly as possible the right answers. foreach (CoreWritingSystemDefinition wsObj in cache.ServiceLocator.WritingSystems.VernacularWritingSystems) { int ws = wsObj.Handle; ConformOneSpellingDictToWordforms(ws, cache); } }
/// <summary> /// Implemented to reset spell-checking everywhere when the spelling status of a wordform changes. /// </summary> public void PropChanged(int hvo, int tag, int ivMin, int cvIns, int cvDel) { if (tag == WfiWordformTags.kflidSpellingStatus) { RestartSpellChecking(); // This keeps the spelling dictionary in sync with the WFI. // Arguably this should be done in FDO. However the spelling dictionary is used to // keep the UI showing squiggles, so it's also arguable that it is a UI function. // In any case it's easier to do it in PropChanged (which also fires in Undo/Redo) // than in a data-change method which does not. var wf = m_wordformRepos.GetObject(hvo); string text = wf.Form.VernacularDefaultWritingSystem.Text; if (!string.IsNullOrEmpty(text)) { SpellingHelper.SetSpellingStatus(text, m_cache.DefaultVernWs, m_cache.LanguageWritingSystemFactoryAccessor, wf.SpellingStatus == (int)SpellingStatusStates.correct); } } }
/// ------------------------------------------------------------------------------------ /// <summary> /// Copies the spelling override files from the restore location to the place where /// our spelling engine expects to find them. Does not overwrite. /// </summary> /// ------------------------------------------------------------------------------------ private void CopySpellingOverrideFilesFromBackupToLocal() { SpellingHelper.AddAnySpellingExceptionsFromBackup(m_restoreSettings.SpellingDictionariesPath); }
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()); }
/// ------------------------------------------------------------------------------------ /// <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); }
public bool Redo() { SpellingHelper.SetSpellingStatus(m_word, m_wsText, m_rootb.DataAccess.WritingSystemFactory, true); m_rootb.PropChanged(m_hvoObj, m_tag, m_wsAlt, 1, 1); return(true); }
/// <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()); }