<commit_before>﻿using System;
using System.Linq;
using System.Threading.Tasks;
using Windows.Media.SpeechRecognition;
using AdaSDK;
using AdaW10.Models.VoiceInterface.SpeechToText;
using AdaW10.Models.VoiceInterface.TextToSpeech;

namespace AdaW10.Models.VoiceInterface
{
    public class VoiceInterface
    {
        #region Speaking section

        public async Task SayHelloAsync(PersonDto[] persons)
        {
            if (persons == null) return;

            var listPersons = persons
                .Select(p => p)
                .Where(p => !string.IsNullOrEmpty(p.FirstName) && p.NbPasses <= 1)
                .ToArray();

            if (listPersons.Any())
            {
                await TtsService.SayAsync(SpeechDictionnary.GetHelloSentence(listPersons));
            }
        }

        public async Task SayHelloAsync(PersonDto person)
        {
            if (person == null) return;

            if (person.FirstName == null) await TtsService.SayAsync("Bonjour, il semblerait que je ne te connaisse pas.");
            else await TtsService.SayAsync($"Bonjour {person.FirstName}");
        }

        public async Task SayNotAvailableService()
        {
            await TtsService.SayAsync("Ce service n'est pas encore disponible !");
        }

        public async Task SayEventsAvailable()
        {
            await TtsService.SayAsync(SpeechDictionnary.GetEventsAvailableSentence());
        }

        public async Task SayDescriptionOfSomeone(PersonDto person)
        {
            if (person == null) return;

            var genderSentence = person.Gender == GenderValues.Male ? "un homme" : "une femme";

            await TtsService.SayAsync($"Il semblerait que tu soies {genderSentence} de {person.Age} ans");
        }

        public async Task SayGoodBye()
        {
            await TtsService.SayAsync("Passe une bonne journée !");
        }

        #endregion

        #region Listening section

        private SttService _continuousRecognitionSession;

        public async Task PrepareListening()
        {
            if (_continuousRecognitionSession != null){
                await StopListening(); 
            }

            _continuousRecognitionSession = new SttService();
        }

        public async Task StopListening()
        {
            if (_continuousRecognitionSession != null)
            {
                if (await _continuousRecognitionSession.CancelContinuousRecognitionAsync())
                {
                    _continuousRecognitionSession.Dispose();
                    _continuousRecognitionSession = null;
                }
            }
        }

        public async Task ListeningHelloAda()
        {
            await PrepareListening();

            await _continuousRecognitionSession.AddConstraintAsync(ConstraintsDictionnary.ConstraintForHelloAda);
            await _continuousRecognitionSession.StartContinuousRecognitionAsync();
        }

        public async Task ListeningCancellation()
        {
            await PrepareListening(); 

            await _continuousRecognitionSession.CleanConstraintsAsync();
            await _continuousRecognitionSession.AddConstraintAsync(ConstraintsDictionnary.ConstraintForAbortWords);
            await _continuousRecognitionSession.StartContinuousRecognitionAsync();
        }

        public async Task ListeningWhatToDo()
        {
            await PrepareListening(); 

            await TtsService.SayAsync(SpeechDictionnary.GetAskWhatToDoSentence());

            await _continuousRecognitionSession.AddConstraintAsync(ConstraintsDictionnary.ConstraintForEvents, false);
            await _continuousRecognitionSession.AddConstraintAsync(ConstraintsDictionnary.ConstraintForAbortWords, false);
            await _continuousRecognitionSession.AddConstraintAsync(ConstraintsDictionnary.ConstraintForSandwichs, false);
            await _continuousRecognitionSession.AddConstraintAsync(ConstraintsDictionnary.ConstraintForCallingSomeone, false);
            await _continuousRecognitionSession.AddConstraintAsync(ConstraintsDictionnary.ConstraintForToDescribeSomeOne, false);
            await _continuousRecognitionSession.AddConstraintAsync(ConstraintsDictionnary.ConstraintForReservation);

            await _continuousRecognitionSession.StartContinuousRecognitionAsync();
        }

        #endregion

        #region Instant recognitions section 

        public async Task<string> Listen()
        {
            using (var sttService = new SttService())
            {
                await TtsService.SayAsync("Bonjour");

                var result = await sttService.RecognizeAsync();

                var firedConstraint = (SpeechRecognitionListConstraint)result.Constraint;
                return firedConstraint.Commands.First();
            }
        }

        public async Task<string> AskReason()
        {
            using (var sttService = new SttService())
            {
                await TtsService.SayAsync(SpeechDictionnary.GetReasonSentence());

                await sttService.AddConstraintAsync(ConstraintsDictionnary.ConstraintForCertification, false);
                await sttService.AddConstraintAsync(ConstraintsDictionnary.ConstraintForMeeting, false);
                await sttService.AddConstraintAsync(ConstraintsDictionnary.ConstraintForSandwichs, false);
                await sttService.AddConstraintAsync(ConstraintsDictionnary.ConstraintForOtherWords);

                var result = await sttService.RecognizeAsync();

                if (result.Confidence != SpeechRecognitionConfidence.Rejected)
                {
                    var firedConstraint = (SpeechRecognitionListConstraint)result.Constraint;
                    return firedConstraint.Commands.First();
                }<commit_msg>change voice<commit_after>﻿using System;
using System.Linq;
using System.Threading.Tasks;
using Windows.Media.SpeechRecognition;
using AdaSDK;
using AdaW10.Models.VoiceInterface.SpeechToText;
using AdaW10.Models.VoiceInterface.TextToSpeech;

namespace AdaW10.Models.VoiceInterface
{
    public class VoiceInterface
    {
        #region Speaking section

        public async Task SayHelloAsync(PersonDto[] persons)
        {
            if (persons == null) return;

            var listPersons = persons
                .Select(p => p)
                .Where(p => !string.IsNullOrEmpty(p.FirstName) && p.NbPasses <= 1)
                .ToArray();

            if (listPersons.Any())
            {
                await TtsService.SayAsync(SpeechDictionnary.GetHelloSentence(listPersons));
            }
        }

        public async Task SayHelloAsync(PersonDto person)
        {
            if (person == null) return;

            if (person.FirstName == null) await TtsService.SayAsync("Bonjour, il semblerait que je ne te connaisse pas.");
            else await TtsService.SayAsync($"Bonjour {person.FirstName}");
        }

        public async Task SayNotAvailableService()
        {
            await TtsService.SayAsync("Ce service n'est pas encore disponible !");
        }

        public async Task SayEventsAvailable()
        {
            await TtsService.SayAsync(SpeechDictionnary.GetEventsAvailableSentence());
        }

        public async Task SayDescriptionOfSomeone(PersonDto person)
        {
            if (person == null) return;

            var genderSentence = person.Gender == GenderValues.Male ? "un homme" : "une femme";

            await TtsService.SayAsync($"Il semblerait que tu soies {genderSentence} de {person.Age} ans");
        }

        public async Task SayGoodBye()
        {
            await TtsService.SayAsync("Passe une bonne journée !");
        }

        #endregion

        #region Listening section

        private SttService _continuousRecognitionSession;

        public async Task PrepareListening()
        {
            if (_continuousRecognitionSession != null){
                await StopListening(); 
            }

            _continuousRecognitionSession = new SttService();
        }

        public async Task StopListening()
        {
            if (_continuousRecognitionSession != null)
            {
                if (await _continuousRecognitionSession.CancelContinuousRecognitionAsync())
                {
                    _continuousRecognitionSession.Dispose();
                    _continuousRecognitionSession = null;
                }
            }
        }

        public async Task ListeningHelloAda()
        {
            await PrepareListening();

            await _continuousRecognitionSession.AddConstraintAsync(ConstraintsDictionnary.ConstraintForHelloAda);
            await _continuousRecognitionSession.StartContinuousRecognitionAsync();
        }

        public async Task ListeningCancellation()
        {
            await PrepareListening(); 

            await _continuousRecognitionSession.CleanConstraintsAsync();
            await _continuousRecognitionSession.AddConstraintAsync(ConstraintsDictionnary.ConstraintForAbortWords);
            await _continuousRecognitionSession.StartContinuousRecognitionAsync();
        }

        public async Task ListeningWhatToDo()
        {
            await PrepareListening(); 

            await TtsService.SayAsync(SpeechDictionnary.GetAskWhatToDoSentence());

            await _continuousRecognitionSession.AddConstraintAsync(ConstraintsDictionnary.ConstraintForEvents, false);
            await _continuousRecognitionSession.AddConstraintAsync(ConstraintsDictionnary.ConstraintForAbortWords, false);
            await _continuousRecognitionSession.AddConstraintAsync(ConstraintsDictionnary.ConstraintForSandwichs, false);
            await _continuousRecognitionSession.AddConstraintAsync(ConstraintsDictionnary.ConstraintForCallingSomeone, false);
            await _continuousRecognitionSession.AddConstraintAsync(ConstraintsDictionnary.ConstraintForToDescribeSomeOne, false);
            await _continuousRecognitionSession.AddConstraintAsync(ConstraintsDictionnary.ConstraintForReservation);

            await _continuousRecognitionSession.StartContinuousRecognitionAsync();
        }

        #endregion

        #region Instant recognitions section 

        public async Task<string> Listen()
        {
            using (var sttService = new SttService())
            {
                await TtsService.SayAsync("Bonjour");

               // await sttService.SpeechRecognizer();

                var result = await sttService.RecognizeAsync();

                if (result.Confidence != SpeechRecognitionConfidence.Rejected)
                {
                    var firedConstraint = (SpeechRecognitionListConstraint)result.Constraint;
                    return firedConstraint.Commands.First();
                }

                return "Autre";
            }
        }

        public async Task<string> AskReason()
        {
            using (var sttService = new SttService())
            {
                await TtsService.SayAsync(SpeechDictionnary.GetReasonSentence());

                await sttService.AddConstraintAsync(ConstraintsDictionnary.ConstraintForCertification, false);
                await sttService.AddConstraintAsync(ConstraintsDictionnary.ConstraintForMeeting, false);
                await sttService.AddConstraintAsync(ConstraintsDictionnary.ConstraintForSandwichs, false);
                await sttService.AddConstraintAsync(ConstraintsDictionnary.ConstraintForOtherWords);

                var result = await sttService.RecognizeAsync();

                if (result.Confidence != SpeechRecognitionConfidence.Rejected)
                {
                    var firedConstraint = (SpeechRecognitionListConstraint)result.Constraint;
                    return firedConstraint.Commands.First();
                }
{'fraction_non_alphanumeric': 0.07055136783264165, 'fraction_numerical': 0.001185737274498179, 'mean_word_length': 2.3384223918575064, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '928864', 'n_tokens_mistral': 3046, 'n_tokens_neox': 2814, 'n_words': 536}