Example #1
0
        //Call sentiment and key phrase APIs and process results
        private static async Task <Sentence> CreateOutputCSV(string caller, string sentenceId, TextAnalyticsClient ta_client, SegmentResults c, TimeSpan time)
        {
            string    script;
            Sentence  outSentence  = new Sentence();
            Sentiment outSentiment = new Sentiment();

            outSentence.sentence = sentenceId;
            outSentence.speaker  = caller;
            outSentence.time     = time.ToString();
            script           = c.NBest[0].Display;
            outSentence.text = script;

            //Sentiment
            double?sentimentScore = await SentimentAnalysisAsync(ta_client, script);

            outSentiment.sentimentscore = sentimentScore.ToString();
            if (sentimentScore > 0.6)
            {
                outSentiment.sentimenttext = "positive";
            }
            else if (sentimentScore < 0.4)
            {
                outSentiment.sentimenttext = "negative";
            }
            else
            {
                outSentiment.sentimenttext = "neutral";
            }
            outSentence.sentiment = outSentiment;

            //KPE
            List <string> kpResult = await KeyPhraseAsync(ta_client, script);

            StringBuilder kpeSb = new StringBuilder();

            foreach (string s in kpResult)
            {
                kpeSb.Append(s + ",");
            }
            string kpeSbString = kpeSb.ToString().TrimEnd(',');

            outSentence.keyphrases = kpeSbString;

            return(outSentence);
        }
Example #2
0
        //Use speech service to convert mp3 to text
        static async Task TranscribeAsync(List <string> fileUriList)
        {
            Console.WriteLine("Starting transcriptions client...");
            //Timer
            var watch = System.Diagnostics.Stopwatch.StartNew();

            // create the client object and authenticate
            var client = BatchClient.BatchClient.CreateApiV2Client(Speech_SubscriptionKey, HostName, Port);

            // get all transcriptions for the subscription
            var transcriptions = await client.GetTranscriptionsAsync().ConfigureAwait(false);

            Console.WriteLine("Deleting all existing completed transcriptions.");
            // delete all pre-existing completed transcriptions. If transcriptions are still running or not started, they will not be deleted
            foreach (var item in transcriptions)
            {
                // delete a transcription
                await client.DeleteTranscriptionAsync(item.Id).ConfigureAwait(false);
            }

            Console.WriteLine("Creating transcriptions.");

            var createdTranscriptions = new List <Guid>();

            foreach (string fileUri in fileUriList)
            {
                //Add to transcriptions
                var transcriptionLocation = await client.PostTranscriptionAsync(Name, Description, Locale, new Uri(fileUri), modelList).ConfigureAwait(false);

                // get the transcription Id from the location URI
                createdTranscriptions.Add(new Guid(transcriptionLocation.ToString().Split('/').LastOrDefault()));
            }
            // Text Analytics client
            var credentials = new ApiKeyServiceClientCredentials(TextAnalytics_SubscriptionKey);
            var ta_client   = new TextAnalyticsClient(credentials)
            {
                Endpoint = TA_Entpoint
            };

            Console.WriteLine("Checking status.");

            // check for the status of our transcriptions every 30 sec. (can also be 1, 2, 5 min depending on usage)
            int completed = 0, running = 0, notStarted = 0;

            while (completed < fileUriList.Count)
            {
                // <batchstatus>
                // get all transcriptions for the user
                transcriptions = await client.GetTranscriptionsAsync().ConfigureAwait(false);

                completed = 0; running = 0; notStarted = 0;
                // for each transcription in the list we check the status
                foreach (var transcription in transcriptions)
                {
                    switch (transcription.Status)
                    {
                    case "Failed":
                        Console.WriteLine("Failed");
                        break;

                    case "Succeeded":
                        // we check to see if it was one of the transcriptions we created from this client.
                        if (!createdTranscriptions.Contains(transcription.Id))
                        {
                            // not created form here, continue
                            continue;
                        }
                        completed++;

                        // if the transcription was successfull, check the results
                        if (transcription.Status == "Succeeded")
                        {
                            //Output
                            List <Sentence> outSentenceList = new List <Sentence>();

                            List <string> json = new List <string>();
                            //Process 1 or more channels
                            for (int i = 0; i < transcription.ResultsUrls.Count; i++)
                            {
                                string channelName = "channel_" + i.ToString();
                                string result      = ProcessSpeechOuput(transcription, channelName);
                                json.Add(result);
                            }
                            //Merge the channels' output
                            List <SegmentResults> conversation = ParseAndMergeJson(json);
                            Console.WriteLine($"Sorting {conversation.Count()} lines");
                            conversation.Sort();

                            int sentenceCnt_agent = 1;
                            int sentenceCnt_cust  = 1;
                            //start
                            StringBuilder sb        = new StringBuilder();
                            String        csvHeader = "id,caller,time,text,sentiment_score,sentiment,keyphrases";
                            sb.AppendLine(csvHeader);

                            foreach (var c in conversation)
                            {
                                var    time      = TimeSpan.FromMilliseconds(c.Offset / 10000); //From time offset in 100-nanosecond units
                                string newLine   = string.Empty;
                                string sentiment = string.Empty;
                                string kpe       = string.Empty;

                                //Create Output Json object for PowerBI
                                Sentence  outSentence  = new Sentence();
                                Sentiment outSentiment = new Sentiment();

                                //Check for which channel is talking (stereo file creates two channels)
                                int id = c.id;
                                if (id == 0)    //usually the agent
                                {
                                    outSentence = await CreateOutputCSV("agent", sentenceCnt_agent.ToString(), ta_client, c, time);

                                    sentenceCnt_agent++;
                                }
                                else    //usually the customer
                                {
                                    outSentence = await CreateOutputCSV("customer", sentenceCnt_cust.ToString(), ta_client, c, time);

                                    sentenceCnt_cust++;
                                }
                                //Add to output
                                outSentenceList.Add(outSentence);
                            }
                            Console.WriteLine($"Success, {conversation.Count()} lines processed.");
                            //Set filename
                            string fName = transcription.RecordingsUrl.LocalPath.Split('/').LastOrDefault().Split('.').First();
                            //Save as CSV file
                            await SaveCSVtoStorageAsync(fName, outSentenceList);

                            //Stop the timer
                            watch.Stop();
                            var elapsedMs = watch.ElapsedMilliseconds;
                            Console.WriteLine(value: "Elapsed time: " + elapsedMs.ToString());
                        }
                        break;

                    case "Running":
                        running++;
                        break;

                    case "NotStarted":
                        notStarted++;
                        break;
                    }
                }
                // </batchstatus>

                Console.WriteLine(string.Format("Transcriptions status: {0} completed, {1} running, {2} not started yet", completed, running, notStarted));
                await Task.Delay(TimeSpan.FromSeconds(TranscriptionWaitTime)).ConfigureAwait(false);
            }
        }