Beispiel #1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(
            IApplicationBuilder app,
            IHostingEnvironment env,
            ILoggerFactory loggerFactory,
            BookRecommenderContext db,
            SpreadingRecommenderCache spreadingRecommederCache)
        {
            // add values to the appsetting singleton from appsettings.json
            AppSettingsSingleton.Database.Connection     = Configuration["Database:Connection"];
            AppSettingsSingleton.Mining.WikiPagesStorage = Configuration["Mining:WikiPagesStorage"];
            AppSettingsSingleton.Mining.Password         = Configuration["Mining:Password"];


            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            if (env.IsProduction())
            {
                app.UseForwardedHeaders(new ForwardedHeadersOptions
                {
                    ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
                });
            }

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            // var options = new RewriteOptions()
            //     .AddRedirectToHttps();

            app.UseStaticFiles();

            app.UseAuthentication();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

            app.UseResponseCompression();

            System.Console.WriteLine("Spreading activation cache init started.");
            var sw = Stopwatch.StartNew();

            spreadingRecommederCache.Initialize(db);
            sw.Stop();
            System.Console.WriteLine($"Spreading activation cache initialized, it took: {sw.ElapsedMilliseconds}ms");
        }
Beispiel #2
0
        private static void TestRecSpreadingActivation(List <int> bookIds, int maxLevel,
                                                       int numberOfNSimilarNeighbors, int numberOfNeighborsForNextLevel)
        {
            List <Recommendation> recommendationsRSA = new List <Recommendation>();
            String commentRDEBS             = "userId=null, maxLevel=" + maxLevel + ", numberOfSimilarNeighbors=" + numberOfNSimilarNeighbors + ", numberOfNeighborsForNextLevel=" + numberOfNeighborsForNextLevel + ", backSpreading=yes";
            BookRecommenderContext    db    = new BookRecommenderContext();
            SpreadingRecommenderCache model = new SpreadingRecommenderCache();

            model.Initialize(db);
            foreach (int bookIdI in bookIds)
            {
                RecommenderSpreadingActivation recommender = new RecommenderSpreadingActivation(
                    maxLevel, numberOfNSimilarNeighbors, numberOfNeighborsForNextLevel, new SimilarityCacheModels(model));
                List <int> recommendationListI = recommender.Recommend(bookIdI, null, HOW_MANY_REC);

                Recommendation recommendationI = new Recommendation(bookIdI, recommendationListI);

                recommendationsRSA.Add(recommendationI);
                System.Console.WriteLine(recommendationI.exportAsString());
            }
            String fileName = "RecommenderSpreadingActivationLevel" + maxLevel + "SimilarNeighbors" + numberOfNSimilarNeighbors + "NeighborsForNextLevel" + numberOfNeighborsForNextLevel + ".rec";

            ExportsRecommendationsToFile(recommendationsRSA, commentRDEBS, fileName);
        }
        private void RunOneLevelBFS(int levelI, List <int> bookIDsPreviousLevel,
                                    GraphOfBooks graph)
        {
            // end of recursion
            if (levelI == 0)
            {
                return;
            }

            SpreadingRecommenderCache model = simCacheModel.spreadingRecommenderCache;

            // get next level of similar verteces
            List <int> bookIDsForNextLevel = new List <int>();

            foreach (int bookIdFromI in bookIDsPreviousLevel)
            {
                List <Tuple <int, int> > simBooksWithQuantitiesUnsortedI =
                    model.GetSimilaritiesBooksWithQuantitiesByAll(bookIdFromI);

                List <Tuple <int, int> > simBooksWithQuantitiesSortedI =
                    simBooksWithQuantitiesUnsortedI.OrderBy(v => v.Item2).Reverse().ToList();

                List <Tuple <int, int> > simBooksWithQuantitiesI =
                    simBooksWithQuantitiesSortedI.Take(numberOfSimilarNeighbors).ToList();

                // add neighbors for next level
                bookIDsForNextLevel.AddRange(
                    simBooksWithQuantitiesI.Select(b => b.Item1));

                // vertex was inserted to graph in the last iteration
                double?activationValueObjI = graph.GetActivationValueOfVertex(bookIdFromI);

                int numberOfneighborsI = simBooksWithQuantitiesI.Select(b => b.Item2).Sum();

                if (numberOfneighborsI == 0)
                {
                    continue;
                }

                // update current vertex
                graph.SetVertex(bookIdFromI, activationValueObjI.Value * MULTIPLICATOR_OF_ACTIVATION_PRESERVING);

                // computes difference of activation value for distribution to neighbors
                double activationValueForDistributionI =
                    activationValueObjI.Value * (1 - MULTIPLICATOR_OF_ACTIVATION_PRESERVING) / numberOfneighborsI;

                // re-compute weights of neighbors
                foreach (Tuple <int, int> neighborBookIdsWithQuantityI in simBooksWithQuantitiesI)
                {
                    int    neighborBookIdI   = neighborBookIdsWithQuantityI.Item1;
                    double neighborQuantityI = neighborBookIdsWithQuantityI.Item2;

                    double activationValueDiff =
                        neighborQuantityI * activationValueForDistributionI;

                    graph.IncreaseActivationValueOfVertex(neighborBookIdI, activationValueDiff);
                }
            }

            List <Tuple <int, int> > bookIDsForNextLevelAndTheirQuantities = bookIDsForNextLevel.GroupBy(b => b)
                                                                             .Select(group => new Tuple <int, int>(group.Key, group.Count())).ToList();

            List <Tuple <int, int> > sortedBookIDsForNextLevelAndTheirQuantities =
                bookIDsForNextLevelAndTheirQuantities.OrderBy(v => v.Item2).Reverse().ToList();


            List <int> bookIDsForNextLevelNoDuplicate = sortedBookIDsForNextLevelAndTheirQuantities
                                                        .Select(b => b.Item1).Take(numberOfNeighborsForNextLevel).ToList();

            if (DEBUG)
            {
                System.Console.WriteLine($"Level {levelI} finished, next leven itemCount: {bookIDsPreviousLevel.Count}");
                graph.printTopKOfGraph(10);
            }

            // recursion for next level
            RunOneLevelBFS(levelI - 1, bookIDsForNextLevelNoDuplicate, graph);
        }