Example #1
0
        // prints results until the connection closes or a delimeterMessage is recieved
        async Task HandleResults(ClientWebSocket ws, int userID)
        {
            var buffer = new byte[1024];

            while (true)
            {
                var segment = new ArraySegment <byte>(buffer);

                var result = await ws.ReceiveAsync(segment, CancellationToken.None);

                if (result.MessageType == WebSocketMessageType.Close)
                {
                    return;
                }

                int count = result.Count;
                while (!result.EndOfMessage)
                {
                    if (count >= buffer.Length)
                    {
                        await ws.CloseAsync(WebSocketCloseStatus.InvalidPayloadData, "That's too long", CancellationToken.None);

                        return;
                    }

                    segment = new ArraySegment <byte>(buffer, count, buffer.Length - count);
                    result  = await ws.ReceiveAsync(segment, CancellationToken.None);

                    count += result.Count;
                }

                var message = Encoding.UTF8.GetString(buffer, 0, count);

                // you'll probably want to parse the JSON into a useful object here,
                // see ServiceState and IsDelimeter for a light-weight example of that.
                Console.WriteLine(message);


                string listeningMessage = "{\n   \"state\": \"listening\"\n}";
                if (message != listeningMessage)
                {
                    getText(message, userID);
                    DBManagement db = new DBManagement(userID);
                    db.AddRequestHistory(message);
                }

                if (IsDelimeter(message))
                {
                    return;
                }
            }
        }
Example #2
0
        //Packages à installer via NuGet
        //Install-Package IBM.WatsonDeveloperCloud.NaturalLanguageUnderstanding.v1
        //Install-Package IBM.WatsonDeveloperCloud.SpeechToText.v1
        //Install-Package IBM.WatsonDeveloperCloud.ToneAnalyzer.v3
        //Install-Package NAudio -Version 1.8.5
        //Install-Package Whatsapp.NET -Version 1.2.2
        //Install-Package Microsoft.AspNet.SignalR -Version 2.4.0


        static void Main(string[] args)
        {
            NLU nluElement = SetupNLU();      //Création d'un élément Natural Language Understanding
            int userID     = 211594;
            STT sttElement = new STT(userID); //Génération d'un élément de classe speech to text en fonction de l'ID utiisateur.

            RecordAndPlayAudio();             //Enregistrement d'un fichier audio du PC
            //testsApiNLU(nluElement); //Pour démontrer le fonctionnement de NLU

            sttElement.SpeechToText();                  //Envoi d'une requête speech to text

            DBManagement DB = new DBManagement(userID); //Gestion de la database

            //DB.AddUserLog("first test");
            Console.WriteLine("Dernière transcription: " + DB.LastVoiceTranscript()); //Affichage de la transcription Speech to text depuis la database
            ApiNLU(nluElement, DB.LastVoiceTranscript());                             //Exécute la requête natural language understanding en fonction du dernier élément enregistré dans la DB.
            Console.ReadKey();
        }
Example #3
0
        async void getText(String message, int userID)
        {
            int    Start, End;
            string strStart   = "transcript\": \"";
            string strEnd     = "\"\n            }\n         ], \n         \"fin";
            string confidence = "0";

            if (message.Contains(strStart) && message.Contains(strEnd))
            {
                Start = message.IndexOf(strStart, 0) + strStart.Length;
                End   = message.IndexOf(strEnd, Start);
                //Console.WriteLine(message.Substring(Start, End - Start));
                string answer = message.Substring(Start, End - Start);
                confidence = getConfidence(message);
                DBManagement dB = new DBManagement(userID);
                dB.AddVoiceTranscript(answer, confidence);
            }
            else
            {
                string       answer = "Incomplete.";
                DBManagement dB     = new DBManagement(userID);
                dB.AddVoiceTranscript(answer, confidence);
            }
        }