Ejemplo n.º 1
0
        internal void Classify()
        {
            try
            {
                ClassificationResult result = null;
                string url = PrepareUrl();

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.AutomaticDecompression = DecompressionMethods.GZip;

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    using (Stream stream = response.GetResponseStream())
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            string json = reader.ReadToEnd();
                            result = JsonConvert.DeserializeObject <ClassificationResult>(json);
                        }
                DoneDelegate?.Invoke(result);
            }
            catch (Exception ex)
            {
                ErrorDelegate?.Invoke("Request Error", ex.Message);
            }
        }
        private void done(ClassificationResult result)
        {
            if (labPrecision.InvokeRequired)
            {
                SetDoneCallback d = new SetDoneCallback(done);
                Invoke(d, new object[] { result });
            }
            else
            {
                if (result != null)
                {
                    List <NewsItem> news = new List <NewsItem>();

                    if (String.IsNullOrEmpty(textData))
                    {
                        labPrecision.Text = Math.Round(result.Precision, 4).ToString();
                        labRecall.Text    = Math.Round(result.Recall, 4).ToString();
                        labF1.Text        = Math.Round(result.F1, 4).ToString();

                        for (int i = 0; i < result.Predictions.Count; i++)
                        {
                            string prediction = String.Empty;
                            if (result.Predictions[i].Count != 0)
                            {
                                prediction = string.Join(", ", result.Predictions[i]);
                            }

                            string trueLabels = String.Empty;
                            if (result.TrueLabels[i].Count != 0)
                            {
                                trueLabels = string.Join(", ", result.TrueLabels[i]);
                            }

                            int correctPredictions = 0;
                            foreach (string pred in result.Predictions[i])
                            {
                                if (result.TrueLabels[i].Contains(pred))
                                {
                                    correctPredictions++;
                                }
                            }

                            news.Add(new NewsItem(i + 1, result.Text[i], prediction, trueLabels,
                                                  correctPredictions, result.TrueLabels[i].Count));
                        }
                    }
                    else
                    {
                        labPrecision.Text = "-";
                        labRecall.Text    = "-";
                        labF1.Text        = "-";

                        string prediction = String.Empty;
                        if (result.Predictions[0].Count != 0)
                        {
                            prediction = string.Join(", ", result.Predictions[0]);
                        }

                        news.Add(new NewsItem(1, result.SingleText, prediction, "-"));
                    }

                    dataGridView1.DataSource = news;

                    groupBox2.Enabled = true;
                    groupBox3.Enabled = true;
                    picLoader.Hide();
                    labLoader.Hide();
                }
                else
                {
                    error("Error", "Something went wrong.");
                }
            }
        }