Exemple #1
0
        // First-level query.
        // English -> Chinese, supports fuzzy search.
        private List <Result> FirstLevelQuery(Query query)
        {
            bool IsExistsInResults(List <Result> res, string word)
            {
                foreach (var item in res)
                {
                    if (item.Title == word)
                    {
                        return(true);
                    }
                }
                return(false);
            }

            string        queryWord = query.Search;
            List <Result> results   = new List <Result>();

            // Pull fully match first.
            Word fullMatch = ecdict.Query(query.Search);

            if (fullMatch != null)
            {
                results.Add(MakeWordResult(fullMatch));
            }

            // Then fuzzy search results. (since it's usually only a few)
            List <SymSpell.SuggestItem> suggestions = wordCorrection.Correct(queryWord);

            foreach (var suggestion in suggestions)
            {
                Word word = ecdict.Query(suggestion.term);

                if (!IsExistsInResults(results, word.word)) // to avoid repetitive results
                {
                    results.Add(MakeWordResult(word));
                }
            }

            // Lastly, the words beginning with the query.
            var result_begin = ecdict.QueryBeginningWith(queryWord);

            foreach (var word in result_begin)
            {
                if (!IsExistsInResults(results, word.word))
                {
                    results.Add(MakeWordResult(word));
                }
            }

            return(results);
        }
Exemple #2
0
        // First-level query.
        // English -> Chinese, supports fuzzy search.
        private async Task <List <Result> > FirstLevelQueryAsync(Query query, CancellationToken token)
        {
            string           queryWord = query.Search;
            HashSet <Result> results   = new HashSet <Result>(WordEqualityComparer.instance);

            // Pull fully match first.
            Word fullMatch = await ecdict.QueryAsync(query.Search, token).ConfigureAwait(false);

            if (fullMatch != null)
            {
                results.Add(MakeWordResult(fullMatch));
            }

            token.ThrowIfCancellationRequested();
            ResultsUpdated?.Invoke(this, new ResultUpdatedEventArgs {
                Results = results.ToList(), Query = query
            });

            // Then fuzzy search results. (since it's usually only a few)
            List <SymSpell.SuggestItem> suggestions = wordCorrection.Correct(queryWord);

            token.ThrowIfCancellationRequested();

            await foreach (var word in ecdict.QueryRange(suggestions.Select(x => x.term), token).Select(w => MakeWordResult(w)).ConfigureAwait(false))
            {
                results.Add(word);
            }

            token.ThrowIfCancellationRequested();
            ResultsUpdated?.Invoke(this, new ResultUpdatedEventArgs {
                Results = results.ToList(), Query = query
            });

            await foreach (var word in ecdict.QueryBeginningWith(queryWord, token).Select(w => MakeWordResult(w)).ConfigureAwait(false))
            {
                results.Add(word);
            }

            return(results.ToList());
        }
Exemple #3
0
        // First-level query.
        // English -> Chinese, supports fuzzy search.
        private List <Result> FirstLevelQuery(Query query)
        {
            string             queryWord = query.Search;
            IEnumerable <Word> results   = Enumerable.Empty <Word>();

            // Pull fully match first.
            Word fullMatch = ecdict.Query(query.Search);

            if (fullMatch != null)
            {
                results = results.Append(fullMatch);
            }

            // Then fuzzy search results. (since it's usually only a few)
            List <SymSpell.SuggestItem> suggestions = wordCorrection.Correct(queryWord);

            return(results.Concat(ecdict.QueryRange(suggestions))
                   .Concat(ecdict.QueryBeginningWith(queryWord))
                   .Distinct()
                   .Select(w => MakeWordResult(w))
                   .ToList());
        }