Ejemplo n.º 1
0
        public void Request(AnalysisExecutionContext context)
        {
            _context = context;

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

            if (!context.Results.Values.First().GetServices().Contains("MechanicalTurk"))
            {
                context.OnExecutionProgress("MechanicalTurk", new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Canceled, 0, 0, 0));
                return;
            }

            MechanicalTurkSettings settings = context.CustomField as MechanicalTurkSettings;

            if (settings == null)
            {
                settings = new MechanicalTurkSettings(null, 3, new decimal(0.10), 300, 1800, 75);
            }

            string key    = context.Key;
            string secret = context.Secret;

            MTurkConfig  config = new MTurkConfig(_serviceURL, key, secret);
            SimpleClient client = new SimpleClient(config);

            int failed    = 0;
            int processed = 0;

            try
            {
                foreach (KeyValuePair <string, ResultSet> document in context.Results)
                {
                    string id    = document.Value.GetPolarity("MechanicalTurk");
                    double count = document.Value.GetScore("MechanicalTurk");
                    count = (double.IsNaN(count)) ? 0 : count;
                    if (id == "negative" || id == "neutral" || id == "positive" || id == "failed" || id == "undefined")
                    {
                        continue;
                    }

                    IList <Assignment> assignments = null;
                    if (context.UseDebugMode)
                    {
                        TimeSpan time = TimeSpan.Zero;
                        assignments = BenchmarkHelper.Invoke(new InvokeBenchmarkHandler(delegate(object state)
                        {
                            return(client.GetAssignmentsForHIT(id, 1, true));
                        }), null, out time) as IList <Assignment>;
                        Console.WriteLine("MechanicalTurk: Answers for {0} HIT have been received. Execution time is: {1}", id, time.TotalMilliseconds);
                    }
                    else
                    {
                        assignments = client.GetAssignmentsForHIT(id, 1, true);
                    }

                    if (assignments.Count < count)
                    {
                        processed++;
                        continue;
                    }

                    double         confidence = 0;
                    IList <string> answers    = DeserializeAnswers(assignments);
                    string         polarity   = MergeSentimentPolarity(answers, out confidence);

                    processed++;
                    document.Value.AddOutput("MechanicalTurk", double.NaN, polarity, confidence);
                    document.Value.AddReferencePolarity(polarity, "MechanicalTurk");
                    AnalysisExecutionProgressEventArgs e = new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Processed, context.Results.Count, processed, failed);
                    context.OnExecutionProgress("MechanicalTurk", e);

                    if (e.Cancel)
                    {
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                failed++;
                AnalysisExecutionProgressEventArgs ea = new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Failed, context.Results.Count, processed, failed);
                ea.Reason = ex.Message;
                context.OnExecutionProgress("MechanicalTurk", ea);

                if (ea.Cancel)
                {
                    return;
                }
            }

            context.OnExecutionProgress("MechanicalTurk", new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Success, context.Results.Count, processed, failed));
        }