コード例 #1
0
        public async Task Summarize()
        {
            TextAnalysisClient Client = new TextAnalysisClient(Config.TextAnalysisApiKey);

            string        fname = @"Data\wap.txt";
            StorageFolder InstallationFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
            var           file = await InstallationFolder.GetFileAsync(fname);

            var stream = await file.OpenReadAsync();

            var sr = new StreamReader(stream.AsStreamForRead());
            int b  = 0; // BOOK
            int c  = 0; // Chapter
            int p  = 0; // Paragraph

            StringBuilder             sb    = new StringBuilder();
            TextAnalysisDocumentStore Store = new TextAnalysisDocumentStore();

            while (!sr.EndOfStream)
            {
                var s = await sr.ReadLineAsync();

                if (s.Contains("BOOK"))
                {
                    b++;
                    c = 0;
                    if (b > 2)
                    {
                        break;
                    }
                    continue;
                }
                if (s.Contains("CHAPTER"))
                {
                    if (sb.Length > 20)
                    {
                        var key = $"b{b}c{c}p{p}";
                        Store.documents.Add(new TextAnalysisDocument(key, "en", sb.ToString()));
                    }
                    sb.Clear();
                    if (Store.documents.Count > 0)
                    {
                        var R = await Client.ExtractKeyphrases(Store);

                        StringBuilder z = new StringBuilder();
                        z.AppendLine($"CHAPTER {c}");
                        foreach (var d in R.documents)
                        {
                            z.AppendLine(string.Join(",", d.keyPhrases));
                        }
                        Summary.Text += z.ToString();
                    }
                    Store.documents.Clear();
                    c++;
                    p = 0;
                    continue;
                }
                if (s.Trim().Equals(string.Empty))
                {
                    if (sb.Length > 20)
                    {
                        var key = $"b{b}c{c}p{p}";
                        if (Store.documents.Count > 0 && Store.documents.Last().text.Length + sb.ToString().Length < 5000)
                        {
                            System.Diagnostics.Debug.WriteLine($"Last {Store.documents.Last().text.Length}, sb {sb.Length}");
                            Store.documents.Last().text += "\n\r" + sb.ToString();
                        }
                        else
                        {
                            Store.documents.Add(new TextAnalysisDocument(key, "en", sb.ToString()));
                        }
                    }
                    sb.Clear();
                    p++;
                    continue;
                }
                sb.AppendLine(s);
            }
        }
コード例 #2
0
        public async Task Analyze()
        {
            TextAnalysisClient      Client      = new TextAnalysisClient(Config.TextAnalysisApiKey);
            TextAnalysisLocalClient LocalClient = new TextAnalysisLocalClient()
            {
                Sensitivity = 30, Bias = 1
            };

            string        fname = @"Data\wap.txt";
            StorageFolder InstallationFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
            var           file = await InstallationFolder.GetFileAsync(fname);

            var stream = await file.OpenReadAsync();

            var sr = new StreamReader(stream.AsStreamForRead());
            int b  = 0;    // BOOK
            int c  = 0;    // Chapter
            int p  = 0;    // Paragraph

            double mp = 0; // most positive score
            double mn = 1; // most negative score

            StringBuilder             sb    = new StringBuilder();
            TextAnalysisDocumentStore Store = new TextAnalysisDocumentStore();

            while (!sr.EndOfStream)
            {
                var s = await sr.ReadLineAsync();

                if (s.Contains("BOOK"))
                {
                    b++;
                    c = 0;
                    if (b > 2)
                    {
                        break;
                    }
                    continue;
                }
                if (s.Contains("CHAPTER"))
                {
                    if (sb.Length > 20)
                    {
                        var key = $"b{b}c{c}p{p}";
                        Store.documents.Add(new TextAnalysisDocument(key, "en", sb.ToString()));
                    }
                    sb.Clear();
                    if (Store.documents.Count > 2)
                    {
                        await Task.Delay(3000); // Pause to make sure service is not called to frequently

                        // Analyze sentiment locally using Local Client (list of keywords)
                        var RL = LocalClient.AnalyzeSentiment(Store);
                        var rl = RL.documents.Count == 0 ? 0 :
                                 (from x in RL.documents // compute average score for the subchapter
                                  select x.score).Average();
                        ItemsLocal.Add(new DataItem($"b{b}c{c}", (int)(rl * 100)));

                        // Anlyze sentiment properly using cognitive web service
                        var R = await Client.AnalyzeSentiment(Store);

                        var r = R.documents.Count == 0 ? 0 :
                                (from x in R.documents
                                 select x.score).Average();
                        Items.Add(new DataItem($"b{b}c{c}", (int)(r * 100)));

                        // Now go through each document passage and find
                        // passages with best /worst scores and display them
                        foreach (var x in R.documents)
                        {
                            if (x.score >= mp)
                            {
                                mp        = x.score;
                                pos.Text  = x.text;
                                posh.Text = $"Positive score={mp}";
                            }
                            if (x.score <= mn)
                            {
                                mn        = x.score;
                                neg.Text  = x.text;
                                negh.Text = $"Negative score={mn}";
                            }
                        }
                    }

                    Store.documents.Clear();
                    c++;
                    p = 0;
                    continue;
                }
                if (s.Trim().Equals(string.Empty))
                {
                    if (sb.Length > 20)
                    {
                        var key = $"b{b}c{c}p{p}";
                        Store.documents.Add(new TextAnalysisDocument(key, "en", sb.ToString()));
                    }
                    sb.Clear();
                    p++;
                    continue;
                }
                sb.AppendLine(s);
            }
        }
コード例 #3
0
 public ImapResponder(TextAnalysisClient textAnalysis, ImapOptions options)
 {
     _textAnalysisClient = textAnalysis;
     _options            = options;
 }