Example #1
0
        public ActionResult DownloadOpinions(int id = 0)
        {
            var company = _companyRepo.GetCompanyById(id);

            // Download Data From Internet
            var opinions = (company.AbsolventWebPage != null) ? DownloadOpinionsAbsolvent(company.AbsolventWebPage) : new List <string>();

            opinions = opinions.Concat((company.GoldenLineWebPage != null) ? DownloadOpinionsGoldenLine(company.GoldenLineWebPage) : null).ToList();
            opinions = opinions.Concat((company.GoworkWebPage != null) ? DownloadOpinionsGowork(company.GoworkWebPage) : null).ToList();

            for (int i = 0; i < opinions.Count; i++)
            {
                _opinionRepo.Add(new Opinion {
                    Id             = i, Content = opinions[i],
                    Classification = Convert.ToDouble(RepustateClient.Sentiment(opinions[0], "pl").Split(',')[0].Split(':')[1]),
                    CompanyId      = company.Id
                });
            }
            _opinionRepo.SaveChanges();

            return(RedirectToAction("Details", new { id = company.Id }));
        }
Example #2
0
        public void Execute(AnalysisExecutionContext context)
        {
            _context = context;

            if (context.Results.Count <= 0)
            {
                context.OnExecutionProgress("Repustate", new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Canceled, 0, 0, 0));
                return;
            }

            int  processed = 0;
            int  failed    = 0;
            bool isTrial   = false;

            try
            {
                string                      lang              = LocaleHelper.GetDoubleLanguageAbbreviation(context.Language);
                RepustateClient             Repustate         = new RepustateClient(context.Key);
                Dictionary <string, string> TotalScoreDataMap = new Dictionary <string, string>();
                int count = 0;
                foreach (KeyValuePair <string, ResultSet> document in context.Results)
                {
                    count++;
                    string ky = "text" + count;
                    TotalScoreDataMap.Add(ky, document.Value.ToString());
                }

                int BatchSize        = 500;
                int processedBatches = 0;

                if (TotalScoreDataMap.Count < BatchSize)
                {
                    Dictionary <string, string> scoreDataMap = new Dictionary <string, string>();
                    scoreDataMap = Repustate.GetDocumentsQueue(processedBatches, BatchSize, TotalScoreDataMap);

                    scoreDataMap.Add("lang", lang);
                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                    Array RepustateSentiments;

                    if (context.UseDebugMode)
                    {
                        TimeSpan time = TimeSpan.Zero;
                        RepustateSentiments = (Array)BenchmarkHelper.Invoke(new InvokeBenchmarkHandler(delegate(object state)
                        {
                            string response = Repustate.GetSentimentBulk(scoreDataMap);
                            IDictionary <string, object> deserializedResponse = serializer.Deserialize <IDictionary <string, object> >(response) as IDictionary <string, object>;
                            return(deserializedResponse["results"] as Array);
                        }), null, out time);

                        Console.WriteLine("\tRepustate: Batch of {0} documents has been recieved. Eexecution time is: {1} ms", scoreDataMap.Count - 1, time.TotalMilliseconds);
                    }
                    else
                    {
                        string response = Repustate.GetSentimentBulk(scoreDataMap);
                        IDictionary <string, object> deserializedResponse = serializer.Deserialize <IDictionary <string, object> >(response) as IDictionary <string, object>;
                        RepustateSentiments = deserializedResponse["results"] as Array;
                    }

                    SortedDictionary <int, string> rs = new SortedDictionary <int, string>();

                    for (int i = 0; i < RepustateSentiments.Length; i++)
                    {
                        int    id    = Int32.Parse(((Dictionary <string, object>)RepustateSentiments.GetValue(i))["id"].ToString());
                        string score = ((Dictionary <string, object>)RepustateSentiments.GetValue(i))["score"].ToString();
                        rs.Add(id, score);
                    }

                    int c = 1;
                    foreach (KeyValuePair <string, ResultSet> document in context.Results)
                    {
                        double score = double.Parse(rs[c]);
                        processed++;

                        if (score <= -0.05)
                        {
                            document.Value.AddOutput("Repustate", score, "negative");
                        }
                        if (score >= 0.05)
                        {
                            document.Value.AddOutput("Repustate", score, "positive");
                        }
                        else
                        {
                            document.Value.AddOutput("Repustate", score, "neutral");
                        }

                        AnalysisExecutionProgressEventArgs ea = new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Processed, context.Results.Count, processed, failed);
                        context.OnExecutionProgress("Repustate", ea);

                        c++;

                        if (ea.Cancel)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    int totalBatches = (TotalScoreDataMap.Count / BatchSize) + 1;
                    processedBatches = 0;

                    for (int i = 0; i < totalBatches; i++)
                    {
                        Dictionary <string, string> scoreDataMap = new Dictionary <string, string>();

                        scoreDataMap = Repustate.GetDocumentsQueue(processedBatches, BatchSize, TotalScoreDataMap);

                        scoreDataMap.Add("lang", lang);
                        JavaScriptSerializer serializer = new JavaScriptSerializer();
                        Array RepustateSentiments;

                        if (context.UseDebugMode)
                        {
                            TimeSpan time = TimeSpan.Zero;
                            RepustateSentiments = (Array)BenchmarkHelper.Invoke(new InvokeBenchmarkHandler(delegate(object state)
                            {
                                string response = Repustate.GetSentimentBulk(scoreDataMap);
                                IDictionary <string, object> deserializedResponse = serializer.Deserialize <IDictionary <string, object> >(response) as IDictionary <string, object>;
                                return(deserializedResponse["results"] as Array);
                            }), null, out time);

                            Console.WriteLine("\tRepustate: Batch of {0} documents has been recieved. Eexecution time is: {1} ms", scoreDataMap.Count - 1, time.TotalMilliseconds);
                        }
                        else
                        {
                            string response = Repustate.GetSentimentBulk(scoreDataMap);
                            IDictionary <string, object> deserializedResponse = serializer.Deserialize <IDictionary <string, object> >(response) as IDictionary <string, object>;
                            RepustateSentiments = deserializedResponse["results"] as Array;
                        }

                        SortedDictionary <int, string> rs = new SortedDictionary <int, string>();

                        for (int j = 0; j < RepustateSentiments.Length; j++)
                        {
                            int    id    = Int32.Parse(((Dictionary <string, object>)RepustateSentiments.GetValue(j))["id"].ToString());
                            string score = ((Dictionary <string, object>)RepustateSentiments.GetValue(j))["score"].ToString();
                            rs.Add(id, score);
                        }

                        int key          = 1;
                        int UpperCounter = processedBatches;
                        int LowerCounter = BatchSize;
                        foreach (KeyValuePair <string, ResultSet> document in context.Results)
                        {
                            if (UpperCounter > 0)
                            {
                                key++;
                                UpperCounter--;
                                continue;
                            }
                            if (LowerCounter < 1)
                            {
                                break;
                            }
                            LowerCounter--;

                            double score = double.Parse(rs[key]);
                            processed++;

                            if (score <= -0.05)
                            {
                                document.Value.AddOutput("Repustate", score, "negative");
                            }
                            if (score >= 0.05)
                            {
                                document.Value.AddOutput("Repustate", score, "positive");
                            }
                            else
                            {
                                document.Value.AddOutput("Repustate", score, "neutral");
                            }

                            AnalysisExecutionProgressEventArgs ea = new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Processed, context.Results.Count, processed, failed);
                            context.OnExecutionProgress("Repustate", ea);

                            key++;

                            if (ea.Cancel)
                            {
                                break;
                            }
                        }

                        processedBatches = processedBatches + BatchSize;
                    }
                }
            }
            catch (WebException ex)
            {
                if (!(ex.Response.ContentLength == -1))
                {
                    isTrial = true;
                    AnalysisExecutionProgressEventArgs ea = new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Canceled, context.Results.Count, 0, 0);
                    ea.Reason = "Your Repustate account doesn’t support " + context.Language + " language";
                    context.OnExecutionProgress("Repustate", ea);
                }
                else
                {
                    isTrial = false;
                    foreach (KeyValuePair <string, ResultSet> document in context.Results)
                    {
                        failed++;
                        document.Value.AddOutput("Repustate", 0, "failed");
                        AnalysisExecutionProgressEventArgs ea = new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Failed, context.Results.Count, processed, failed);
                        ea.Reason = ex.Message;
                        context.OnExecutionProgress("Repustate", ea);

                        if (ea.Cancel)
                        {
                            break;
                        }
                    }
                }
            }

            if (!isTrial)
            {
                context.OnExecutionProgress("Repustate", new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Success, context.Results.Count, processed, failed));
            }
        }