예제 #1
0
        public async Task <IList <RankableAction> > GetActions()
        {
            IList <RankableAction> actions = new List <RankableAction>();
            var catalogs = await GetAllCatelogs();

            foreach (Catalog catalog in catalogs)
            {
                var action = new RankableAction
                {
                    Id       = catalog?.Id.ToString(),
                    Features =
                        new List <object>()
                    {
                        new {
                            Id          = catalog.Id,
                            Name        = catalog.Name,
                            Description = catalog.Description,
                            ImageUrl    = catalog.ImageUrl,
                        }
                    }
                };
                actions.Add(action);
            }
            return(actions);
        }
예제 #2
0
        /// <summary>
        /// For a context (user), calculate its points for an action (i.e. how much does the user "like" the action)
        /// </summary>
        /// <param name="context"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        private int GetPoints(IList <object> context, RankableAction action)
        {
            if (action == null)
            {
                return(0);
            }

            dynamic user     = context[0];
            dynamic state    = context[1];
            dynamic features = action.Features[0];

            int points =
                GetPoints_TimeOfDay(state.timeOfDay, features) +
                GetPoints_DayOfWeek(state.dayOfWeek, features) +
                GetPoints_Country(state.country, features) +
                GetPoints_UserAttributes(user, features)
            ;

            return(points);
        }
예제 #3
0
        private IList <RankableAction> GetActions()
        {
            List <RankableAction> actions = new List <RankableAction>();

            var resourceTypes = GetResourceTypes();
            var lengths       = GetLengths();
            var levels        = GetLevels();
            var languages     = GetLanguages();

            for (int i = 1; i <= this.HowManyActions; i++)
            {
                string resourceType = RandomGenerator.Categorical.GetCategorical(resourceTypes);

                RankableAction action = new RankableAction()
                {
                    Id       = $"{i}",
                    Features = new List <object>
                    {
                        new
                        {
                            resourceType = resourceType,
                            isPublic     = RandomGenerator.Boolean.GetBoolean(),
                            isPaywall    = RandomGenerator.Boolean.GetBoolean(),
                            isFirstParty = RandomGenerator.Boolean.GetBoolean(),
                            length       = RandomGenerator.Categorical.GetCategorical(lengths),
                            level        = RandomGenerator.Categorical.GetCategorical(levels),
                            language     = RandomGenerator.Categorical.GetCategorical(languages),
                            pubYear      = Converter.GetInt32(RandomGenerator.Numeric.GetUniform(2000, 2019)),
                            pubMonth     = Converter.GetInt32(RandomGenerator.Numeric.GetUniform(1, 12)),
                            createdBy    = $"Creator {Converter.GetInt32(RandomGenerator.Numeric.GetUniform(1, 10))}",
                            title        = $"Title {resourceType} {i}",
                            summary      = $"Summary {resourceType} {i}"
                        }
                    }
                };

                actions.Add(action);
            }

            return(actions);
        }
        public string getBestNewsArticleID()
        {
            // Create context to personalize for, from user data or contextual information
            IList <object> currentContext = new List <object>()
            {
                new { time = "EarlyEvening" },
                new { weather = "Rainy" },
                new { userFavoriteTopic = "SpaceExploration" },
                new { userProfile = "PayingUser" }
                //Add your own features in your own schema
            };


            // Provide a list of possible actions -articles- to choose from
            var actionsB = new RankableAction[] {
                new RankableAction("news_article_1", new List <object> {
                    new {
                        topic        = "politics",
                        breakingNews = true,
                        controversy  = false
                    }
                }),
                new RankableAction("news_article_2", new List <object> {
                    new {
                        topic           = "technology",
                        breakingNews    = false,
                        publishedWithin = "week"
                    }
                })
            };

            var request = new RankRequest(actions, currentContext, null, eventId);

            // Call Personalizer and get the action that should be shown
            RankResponse response = personalizer.Rank(request);

            //Use the result given by Personalizer
            return(response.RewardActionId);
        }
예제 #5
0
        public async Task Init()
        {
            News.IsVisible    = false;
            Loading.IsVisible = true;

            var feeds = new List <System.Threading.Tasks.Task <Feed> >()
            {
                FeedReader.ReadAsync("https://feeds.expressen.se/sport/"),
                FeedReader.ReadAsync("https://www.aftonbladet.se/sportbladet/rss.xml"),
            };

            await Task.WhenAll(feeds);

            var items = new List <FeedItem>();

            foreach (var feed in feeds)
            {
                var result = feed.Result;

                items.AddRange(result.Items);
            }

            var actions = new List <RankableAction>();

            foreach (var item in items.OrderByDescending(x => x.PublishingDate).Take(50))
            {
                string source = null;

                foreach (var feed in feeds)
                {
                    if (feed.Result.Items.Contains(item))
                    {
                        source = feed.Result.Link;
                        break;
                    }
                }

                var features = new List <object>()
                {
                    new { item.Title },
                    new { item.Author },
                    new { item.Description },
                    new { source }
                };

                var action = new RankableAction(item.Id, features);
                actions.Add(action);
            }



            string timeOfDay = null;

            if (DateTime.Now.Hour > 22 || DateTime.Now.Hour < 5)
            {
                timeOfDay = "night";
            }
            else if (DateTime.Now.Hour >= 5 && DateTime.Now.Hour < 12)
            {
                timeOfDay = "morning";
            }
            else if (DateTime.Now.Hour >= 12 && DateTime.Now.Hour < 17)
            {
                timeOfDay = "afternoon";
            }
            else
            {
                timeOfDay = "evening";
            }

            var context = new List <object>()
            {
                new { timeOfDay }
            };

            eventId = Guid.NewGuid().ToString();

            var rankRequest = new RankRequest()
            {
                Actions         = actions,
                ContextFeatures = context,
                ExcludedActions = new List <string>(),
                EventId         = eventId
            };

            try
            {
                var rankResult = await client.RankAsync(rankRequest);

                source = new List <Item>();

                foreach (var ranked in rankResult.Ranking.OrderByDescending(x => x.Probability))
                {
                    var feedItem = items.Single(x => x.Id == ranked.Id);

                    var urls = Regex.Matches(feedItem.Description, @"(http|ftp|https):\/\/([\w\-_]+(?:(?:\.[\w\-_]+)+))([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?");

                    var itm = new Item()
                    {
                        FeedItem = feedItem,
                        Image    = urls.FirstOrDefault()?.Value
                    };

                    foreach (var feed in feeds)
                    {
                        if (feed.Result.Items.Contains(feedItem))
                        {
                            itm.Source = feed.Result.Link;
                            break;
                        }
                    }

                    source.Add(itm);
                }

                News.ItemsSource = source;
            }
            catch (Exception ex)
            {
                source = new List <Item>();

                foreach (var feedItem in items)
                {
                    var urls = Regex.Matches(feedItem.Description, @"(http|ftp|https):\/\/([\w\-_]+(?:(?:\.[\w\-_]+)+))([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?");

                    var itm = new Item()
                    {
                        FeedItem = feedItem,
                        Image    = urls.FirstOrDefault()?.Value
                    };

                    foreach (var feed in feeds)
                    {
                        if (feed.Result.Items.Contains(feedItem))
                        {
                            itm.Source = feed.Result.Link;
                            break;
                        }
                    }

                    source.Add(itm);
                }

                News.ItemsSource = source;
            }

            News.ScrollTo(source.First(), ScrollToPosition.Start, false);
            News.IsVisible    = true;
            Loading.IsVisible = false;
        }