示例#1
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());
        }
示例#2
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());
        }