コード例 #1
0
        private SuggestionsResult GetSuggestionsInternal(string partialMatch, GeoCoordinatePortable.GeoCoordinate location, List <string> scoringStrategies)
        {
            if (string.IsNullOrEmpty(partialMatch)) // no input ==> no output!
            {
                return(new SuggestionsResult());
            }

            var matchingCities = Dataset.Where(c => c.Name.StartsWith(partialMatch, StringComparison.CurrentCultureIgnoreCase)).ToList();
            var scores         = new List <double>(matchingCities.Select(mc => 1.0));

            foreach (var strategyName in scoringStrategies)
            {
                var strat     = Factory.CreateScoringStrategy(strategyName);
                var newScores = strat.ComputeScore(matchingCities, partialMatch, location);
                scores = scores.Zip(newScores, (oldScore, newScore) => oldScore * newScore).ToList();
            }

            var resultItems = matchingCities.Zip(scores, (c, score) => new SuggestionsResultItem()
            {
                name = c.Name, latitude = c.Location.Latitude.ToString(), longitude = c.Location.Longitude.ToString(), score = Math.Round(score, 2)
            })
                              .OrderByDescending(mc => mc.score);
            var result = new SuggestionsResult();

            foreach (var i in resultItems)
            {
                result.AddItem(i);
            }
            return(result);
        }
コード例 #2
0
        /// <summary>
        /// searches all suggestions in a forum
        /// </summary>
        /// <param name="subdomain">the site's subdomain</param>
        /// <param name="forumId">the forumId</param>
        /// <param name="query">the search query</param>
        /// <param name="consumerKey">your conusmer key</param>
        /// <param name="consumerSecret">your consumer secret</param>
        /// <param name="page">the result's page</param>
        /// <param name="perPage">number of entries per page</param>
        /// <param name="sort">the status used to order results</param>
        /// <returns>all suggestions that match the search query in a forum</returns>
        public async Task <SuggestionsResult> SearchSuggesstions(string subdomain, string query, string consumerKey, string consumerSecret, int forumId, int page = 1, int perPage = 10, SuggestionSort sort = SuggestionSort.newest, SuggestionStatusesResult.Status status = null)
        {
            SuggestionsResult result = new SuggestionsResult();

            IRestResponse response = await GetSearchSuggestionsResponse(subdomain, query, consumerKey, consumerSecret, forumId, page, perPage, sort, status);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                string jsonContent = Encoding.UTF8.GetString(response.RawBytes, 0, response.RawBytes.Length);

                result = JsonConvert.DeserializeObject <SuggestionsResult>(jsonContent);
            }

            return(result);
        }
コード例 #3
0
        private FullSuggestionModel _CreateSuggestionsModel(string woeid, int languageId, string userId)
        {
            FullSuggestionModel output = new FullSuggestionModel();

            SuggestionsResult suggestionResult = this._suggestionService.GetSuggestion(woeid, languageId, userId);

            if (suggestionResult != null)
            {
                Dictionary <int, ItemType> itemTypes = this._dataStore.GetAllItemTypes().ToDictionary(k => k.Id);

                foreach (string key in suggestionResult.CommonSuggestions.Keys)
                {
                    output.CommonSuggestedItems.Add(key, suggestionResult.CommonSuggestions[key].Select(x => new SuggestionModel {
                        Name = x
                    }).ToList());
                }

                var itemsToUpdateShowTimes = new List <int>();

                foreach (string actionTypeName in suggestionResult.Suggestions.Keys)
                {
                    // Create HTML id from action type name.
                    string strongActionTypeName = actionTypeName.RemoveSpecialSymbols().ToLower();
                    if (!output.SuggestedItems.Keys.Contains(strongActionTypeName))
                    {
                        output.SuggestedItems.Add(strongActionTypeName, new List <SuggestionModel>());
                    }

                    var itemsCollection = suggestionResult.Suggestions[actionTypeName].SuggestionItems
                                          .Where(x => x.Item.ActionTypeId.HasValue)
                                          .Take(18); // <<<<< ===== TAKE ONLY 18 .

                    // Look through items to build model.
                    foreach (var item in itemsCollection)
                    {
                        string imagePath = String.Empty;
                        if (!string.IsNullOrEmpty(item.Item.DefaultImageUri))
                        {
                            imagePath = item.Item.DefaultImageUri;
                        }
                        else
                        {
                            var images = this._dataStore.GetImagesByItemId(item.ItemId.Value);
                            if (images.Any())
                            {
                                imagePath = images.First().ImageUrl;
                            }
                            else
                            {
                                imagePath = @"/img/default_wear/veshalka.png";
                            }
                        }

                        string   itemTypeFilter = "0";
                        ItemType itemType       = null;
                        if (item.Item.ItemTypeId.HasValue && itemTypes.TryGetValue(item.Item.ItemTypeId.Value, out itemType) && itemType.EnumType.HasValue)
                        {
                            itemTypeFilter = itemType.EnumType.Value.ToString();
                        }

                        output.SuggestedItems[strongActionTypeName].Add(
                            new SuggestionModel
                        {
                            ActionType             = actionTypeName,
                            ImageUrl               = imagePath,
                            ReferrerUrl            = item.Item.Referrer,
                            Price                  = (item.Item.Price ?? 0).ToString() + " " + ContentHelpers.GetLocalCurrencyName(item.Item.Currency),
                            Name                   = item.Item.Name,
                            Description            = item.Item.Description,
                            MadeBy                 = item.Item.MadeBy,
                            ProvideBy              = item.Item.ProvideBy ?? string.Empty,
                            Language               = CurrentLang.Code,
                            Gender                 = item.Item.Gender,
                            Season                 = item.Item.Season.ToString(),
                            WaterProtectionPercent =
                                item.Item.WaterProtectionPercent.HasValue
                                        ? item.Item.WaterProtectionPercent.Value
                                        : 0,
                            IceProtection =
                                item.Item.IceProtectionPercent.HasValue
                                        ? item.Item.IceProtectionPercent.Value
                                        : false,
                            ArmoringPercent =
                                item.Item.ArmoringPercent.HasValue
                                        ? item.Item.ArmoringPercent.Value
                                        : 0,
                            MinAge = item.Item.MinAge.HasValue ? item.Item.MinAge.Value : 0,
                            MaxAge = item.Item.MaxAge.HasValue ? item.Item.MaxAge.Value : 0,
                            SunProtectionPercent =
                                item.Item.SunProtectionPercent.HasValue
                                        ? item.Item.SunProtectionPercent.Value
                                        : 0,
                            ItemType   = itemTypeFilter,
                            IsWardrobe = item.Item.IsWardrobe
                        });
                    }

                    itemsToUpdateShowTimes.AddRange(itemsCollection.Where(x => x.ItemId.HasValue).Select(x => x.ItemId.Value));

                    if (string.IsNullOrEmpty(output.ForecastDescription))
                    {
                        output.ForecastDescription = suggestionResult.Suggestions[actionTypeName].FullDescription;
                    }
                }

                _dataStore.UpdateShowedTimes(itemsToUpdateShowTimes);
            }

            return(output);
        }