Example #1
0
        /// <summary>
        /// Search the lemma inside the local SQLite database.
        /// </summary>
        /// <param name="singleWord">The word to search for.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        private static async Task <string> CallDB(string singleWord)
        {
            string result = null;

            try
            {
                Expression <Func <OfflineLemma, bool> > exp = l => l.Lemma.Equals(singleWord);
                OfflineLemma  query;
                SQLiteManager dbManager = SQLiteManager.Instance;

                query = await dbManager.LocalLemmasDictionaryConnection.Table <OfflineLemma>().Where(exp).FirstOrDefaultAsync().ConfigureAwait(false);

                if (query != null)
                {
                    if (query.Base != null)
                    {
                        result = query.Base;
                    }
                }
            }
            catch (Exception ex)
            {
                // TODO: remove the exception
                Tools.Logger.Log("DBSearch", ex);
            }

            return(result);
        }
Example #2
0
        public static async Task <IList <IWord> > Search(string word, SupportedLanguage language)
        {
            // Words are stored as lowercase in the DB, so convert the search term.
            word = word.ToLower();

            string msg = "Searched word: '" + word + "'";

            // Avoid looking up "simple" words like numbers or "I" in the database, just return a preparsed result
            // instead
            ReadOnlyCollection <IWord> preparsedResult = SearchPreparse(word);

            if (preparsedResult != null)
            {
                return(preparsedResult);
            }

            string wordLCID = (string)new SupportedLanguageToLcidConverter().Convert(language, typeof(string), null, CultureInfo.InvariantCulture);

            IList <IWord> retList = new List <IWord>();

            // First, check if there is an exact match in the database.
            SQLiteManager dbManager = SQLiteManager.Instance;
            Expression <Func <OfflineWord, bool> > exp = w => w.Term.Equals(word);
            var query = dbManager.LocalWordsDictionaryConnection.Table <OfflineWord>().Where(exp);

            if (await query.CountAsync() > 0)
            {
                msg += "\tFound:\t";
            }

            foreach (OfflineWord w in await query.ToListAsync())
            {
                msg += " (" + w.Term + " as " + w.PartOfSpeech + ")";
                retList.Add(w);
            }

            // If, and only if, there aren't valid results, expand the search algorithm.
            // Moreover, the word must be larger than 3 chars.(too many results otherwise).
            if (retList.Count == 0 && word.Length >= 3)
            {
                if (word.EndsWith("s"))
                {
                    word = word.Substring(0, word.Length - 1);
                    Tools.Logger.Log("OfflineWord", msg);
                    IList <IWord> result = await Search(word, language);

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

            Tools.Logger.Log("OfflineWord", msg);

            return(new ReadOnlyCollection <IWord>(retList));
        }