Example #1
0
        public async Task <TranslatedSearchResult <ICard> > FindCard(SearchParameters parameters)
        {
            string  sTerm = parameters.SearchTerm;
            Locale  sLoc  = parameters.SearchLocale;
            Locale? tLoc  = parameters.TranslationLocale;
            Version ver   = parameters.Version;

            Catalog sCat = await CatalogService.GetCatalog(sLoc, ver);

            Catalog?tCat = tLoc is null ? null : await CatalogService.GetCatalog(tLoc, ver);

            if (TryFindCardFromCode(sTerm, tCat ?? sCat, out TranslatedSearchResult <ICard>?codeResult))
            {
                return(codeResult !);
            }

            var cardSearcher = new CatalogItemSearcher <ICard>
                               (
                sCat,
                new CardNameGrouper()
                               )
            {
                TermTargetCanonicalizer = new TrimmedAndLowerCanonicalizer(),
                TermMatcherFactory      = new LevenshteinSubstringMatcher.Factory(GreengladeSettings.SubstringBookendWeight, GreengladeSettings.SubstringBookendTaper),
                KeyStrengthThreshold    = GreengladeSettings.StringMatchThreshold,
                KeyConflictResolution   = KeyConflictResolution.Flattened,
                MatchGroupResolver      = new BasicCardGroupResolver(),
                ItemStrengthDownscalers = new[] { new UncollectibleCardStrengthDownscaler(GreengladeSettings.UncollectibleCardDownscaleFactor) },
                ItemDownscaleCurve      = ItemDownscaleCurve.Biased,
                ItemMatchSorter         = new DescendingMatchStrengthSorter <ICard>(),
            };

            SearchResult <ICard> result = cardSearcher.Search(sTerm);

            if (tCat is null)
            {
                return(TranslatedSearchResult <ICard> .FromUntranslatedSearch(result));
            }

            var tMap = new Dictionary <ICard, ICard>();

            foreach (var match in result.Matches)
            {
                ICard sItem = match.Item;
                if (tCat.Cards.TryGetValue(sItem.Code, out var tItem))
                {
                    tMap.Add(sItem, tItem);
                }
                else
                {
                    LogCouldNotTranslateWarning(sItem, sLoc, tCat.Locale, ver);
                }
            }

            return(TranslatedSearchResult <ICard> .FromTranslatedSearch(result, tCat.Locale, tMap));
        }
Example #2
0
        public async Task <TranslatedSearchResult <LorKeyword> > FindKeyword(SearchParameters parameters)
        {
            string  sTerm = parameters.SearchTerm;
            Locale  sLoc  = parameters.SearchLocale;
            Locale? tLoc  = parameters.TranslationLocale;
            Version ver   = parameters.Version;

            Catalog sCat = await CatalogService.GetCatalog(sLoc, ver);

            Catalog?tCat = tLoc is null ? null : await CatalogService.GetCatalog(tLoc, ver);

            var keywordSearcher = new CatalogItemSearcher <LorKeyword>
                                  (
                sCat,
                new KeywordNameGrouper {
                IncludeVocabTerms = true, IncludeDescriptionlessKeywords = false
            }
                                  )
            {
                TermTargetCanonicalizer = new TrimmedAndLowerCanonicalizer(),
                TermMatcherFactory      = new LevenshteinSubstringMatcher.Factory(GreengladeSettings.SubstringBookendWeight, GreengladeSettings.SubstringBookendTaper),
                KeyStrengthThreshold    = GreengladeSettings.StringMatchThreshold,
                ItemMatchSorter         = new DescendingMatchStrengthSorter <LorKeyword>(),
            };

            SearchResult <LorKeyword> result = keywordSearcher.Search(sTerm);

            if (tCat is null)
            {
                return(TranslatedSearchResult <LorKeyword> .FromUntranslatedSearch(result));
            }

            var tMap = new Dictionary <LorKeyword, LorKeyword>();

            foreach (var match in result.Matches)
            {
                LorKeyword sItem = match.Item;
                if (tCat.Keywords.TryGetValue(sItem.Key, out var tItem))
                {
                    tMap.Add(sItem, tItem);
                }
                else if (tCat.VocabTerms.TryGetValue(sItem.Key, out var vtItem))
                {
                    tItem = new LorKeyword(sItem.Key, vtItem.Name, vtItem.Description);
                    tMap.Add(sItem, tItem);
                }
                else
                {
                    LogCouldNotTranslateWarning(sItem, sLoc, tCat.Locale, ver);
                }
            }

            return(TranslatedSearchResult <LorKeyword> .FromTranslatedSearch(result, tCat.Locale, tMap));
        }