Example #1
0
        public async Task RunAsync(CancellationToken token)
        {
            var sourceRows = await _tableStore.GetRowsAsync(_sourceTableName, CancellationToken.None);

            var targetRows   = new List <DataRow>();
            var targetSchema = new RowSchema(
                new FieldDefinition("ContactId", FieldKind.Key, FieldDataType.Guid),
                new FieldDefinition("MovieId", FieldKind.Key, FieldDataType.String),
                new FieldDefinition("Title", FieldKind.Attribute, FieldDataType.String),
                new FieldDefinition("Overview", FieldKind.Attribute, FieldDataType.String),
                new FieldDefinition("Image", FieldKind.Attribute, FieldDataType.String),
                new FieldDefinition("ImdbRating", FieldKind.Attribute, FieldDataType.String)
                );

            while (await sourceRows.MoveNext())
            {
                foreach (var row in sourceRows.Current)
                {
                    var movieIds = row["LastMoviesId"].ToString().Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    //grabbing only unique movie ID's
                    movieIds = movieIds.Distinct().ToArray();

                    if (!movieIds.Any())
                    {
                        continue;
                    }
                    var recommendations = _movieRecommendationsProvider.GetRecommendations(movieIds);

                    //clean recommendations to not have any dups
                    recommendations = recommendations.GroupBy(m => m.netflixid).Select(g => g.FirstOrDefault()).ToList();
                    if (recommendations.Any())
                    {
                        for (var i = 0; i < _limit; i++)
                        {
                            if (i < recommendations.Count)
                            {
                                var targetRow = new DataRow(targetSchema);
                                targetRow.SetGuid(0, row.GetGuid(0));
                                targetRow.SetString(1, recommendations[i].netflixid);
                                targetRow.SetString(2, recommendations[i].title);
                                targetRow.SetString(3, recommendations[i].synopsis);
                                targetRow.SetString(4, recommendations[i].image);
                                targetRow.SetString(5, recommendations[i].rating);
                                targetRows.Add(targetRow);
                            }
                        }
                    }
                }
            }

            var tableDefinition = new TableDefinition(_targetTableName, targetSchema);
            var targetTable     = new InMemoryTableData(tableDefinition, targetRows);
            await _tableStore.PutTableAsync(targetTable, TimeSpan.FromMinutes(30), CancellationToken.None);
        }
Example #2
0
        public async Task RunAsync(CancellationToken token)
        {
            var sourceRows = await _tableStore.GetRowsAsync(_sourceTableName, CancellationToken.None);

            var targetRows   = new List <DataRow>();
            var targetSchema = new RowSchema(
                new FieldDefinition("ContactId", FieldKind.Key, FieldDataType.Guid),
                new FieldDefinition("MovieId", FieldKind.Key, FieldDataType.Int64),
                new FieldDefinition("Title", FieldKind.Attribute, FieldDataType.String),
                new FieldDefinition("Overview", FieldKind.Attribute, FieldDataType.String),
                new FieldDefinition("PosterPath", FieldKind.Attribute, FieldDataType.String)
                );

            //ML context
            MLContext    mlContext  = new MLContext();
            ITransformer mlModel    = mlContext.Model.Load(GetAbsolutePath(MODEL_FILEPATH), out DataViewSchema inputSchema);
            var          predEngine = mlContext.Model.CreatePredictionEngine <TacoFlixML.Model.DataModels.ModelInput, ModelOutput>(mlModel);

            while (await sourceRows.MoveNext())
            {
                foreach (var row in sourceRows.Current)
                {
                    var movieId         = int.Parse(row["LastMovieId"].ToString());
                    var recommendations = _movieRecommendationsProvider.GetRecommendations(movieId);

                    if (recommendations.Any())
                    {
                        for (var i = 0; i < _limit; i++)
                        {
                            if (i < recommendations.Count)
                            {
                                //ML
                                ModelInput input = new ModelInput();
                                input.Movieid = recommendations[i].Id;
                                ModelOutput predictionResult = predEngine.Predict(input);

                                var targetRow = new DataRow(targetSchema);
                                targetRow.SetGuid(0, row.GetGuid(0));
                                targetRow.SetInt64(1, recommendations[i].Id);
                                targetRow.SetString(2, recommendations[i].Title + "score=" + predictionResult.Score);
                                targetRow.SetString(3, recommendations[i].Overview);
                                targetRow.SetString(4, recommendations[i].PosterPath);

                                targetRows.Add(targetRow);
                            }
                        }
                    }
                }
            }

            var tableDefinition = new TableDefinition(_targetTableName, targetSchema);
            var targetTable     = new InMemoryTableData(tableDefinition, targetRows);
            await _tableStore.PutTableAsync(targetTable, TimeSpan.FromMinutes(30), CancellationToken.None);
        }