示例#1
0
        private void LoadUserDictionaryFromFile(string filePath)
        {
            if (suggestionMethod == SuggestionMethods.Presage)
            {
                // Don't bother loading user dictionary if using external predictions.
                return;
            }

            Log.DebugFormat("Loading user dictionary from file '{0}'", filePath);

            StreamReader           reader    = null;
            List <DictionaryEntry> tempStore = new List <DictionaryEntry>();

            string hash       = string.Empty;
            string line       = string.Empty;
            int    usageCount = 0;

            try
            {
                using (reader = new StreamReader(filePath))
                {
                    while (reader.Peek() >= 0)
                    {
                        line = reader.ReadLine();

                        var entryWithUsageCount = line.Trim().Split('|');
                        if (entryWithUsageCount.Length == 2)
                        {
                            var entry = entryWithUsageCount[0];
                            if (!int.TryParse(entryWithUsageCount[1], out usageCount))
                            {
                                usageCount = 0;
                            }

                            hash = entry.NormaliseAndRemoveRepeatingCharactersAndHandlePhrases(false);
                            managedSuggestions.AddEntry(entry, new DictionaryEntry(entry, usageCount), hash);
                        }
                    }
                }

                if (managedSuggestions.GetWordsHashes().Count == 0)
                {
                    // Loading from user dictionary yield empty dict, then try load from
                    // source of truth -- this will flush any previous user entry counts.
                    LoadDictionaryFromLanguageFile();
                }
            }
            catch (Exception exception)
            {
                PublishError(this, exception);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Dispose();
                }
            }
        }
示例#2
0
        public void AddEntry_called_with_existing_entry_does_not_update_usage_count()
        {
            ConfigureProvider();

            // try to make this the "t"-word with the highest usage
            autoComplete.AddEntry("these", new DictionaryEntry("these", 101));

            var suggestions = autoComplete.GetSuggestions("t");

            Assert.AreNotEqual("these", suggestions.First());
        }
示例#3
0
        private void AddEntryToDictionary(string entry)
        {
            if (string.IsNullOrWhiteSpace(entry))
            {
                return;
            }

            var hash = entry.NormaliseAndRemoveRepeatingCharactersAndHandlePhrases(false);

            if (string.IsNullOrWhiteSpace(hash))
            {
                return;
            }

            var newEntryWithUsageCount = new DictionaryEntry(entry);

            managedSuggestion.AddEntry(entry, newEntryWithUsageCount);
        }
        private void AddEntryToDictionary(string entry, bool loadedFromDictionaryFile, int usageCount = 0)
        {
            if (entries != null &&
                !string.IsNullOrWhiteSpace(entry) &&
                (loadedFromDictionaryFile || !ExistsInDictionary(entry)))
            {
                //Add to in-memory (hashed) dictionary (and then save to custom dictionary file if new entry entered by user)
                var hash = entry.NormaliseAndRemoveRepeatingCharactersAndHandlePhrases(log: !loadedFromDictionaryFile);
                if (!string.IsNullOrWhiteSpace(hash))
                {
                    var newEntryWithUsageCount = new DictionaryEntry(entry, usageCount);

                    if (entries.ContainsKey(hash))
                    {
                        if (entries[hash].All(nwwuc => nwwuc.Entry != entry))
                        {
                            entries[hash].Add(newEntryWithUsageCount);
                        }
                    }
                    else
                    {
                        entries.Add(hash, new List <DictionaryEntry> {
                            newEntryWithUsageCount
                        });
                    }

                    //Also add to entries for auto complete
                    managedSuggestions.AddEntry(entry, newEntryWithUsageCount);

                    if (!loadedFromDictionaryFile)
                    {
                        Log.DebugFormat("Adding new (not loaded from dictionary file) entry '{0}' to in-memory dictionary with hash '{1}'", entry, hash);
                        SaveUserDictionaryToFile();
                    }
                }
            }
        }