Speech Recognition and TTS for Ionic

In this post I am attempting to outline the steps for implementing the following :

The ability for a user to speak his/her request and the app to respond from an array of possible answers based on keywords.

Pretty basic. This is by no means AI 🙂

(make sure tat your environment is ready for Ionic)

Install the speech recognition plugin - cordova-plugin-speechrecognition 1.1.2 "Speech Recognition"

Please note that this is untested code. It is adopted from working code,

Index.html

<ion-view>


    <ion-nav-title>
        <p style="color:white;text-align:center">Reservations</p>

    </ion-nav-title>

    <ion-content has-header="true" overflow-scroll=”true” >
        <div class="bar bar-header item-input-inset">

        </div>

        <div class="item">
            <button class="button button-block button-positive" ng-click="record()">Push to Speak</button>
        </div>

        <div class="item" ng-show="hasdata">
            <span>{{b_date}}  {{b_time}} {{b_ampm}} for {{b_cnt}} tickets</span>
        </div>
        <div class="item" ng-show="no_data">
            <span>Nothing found for {{b_date}} {{b_time}} for {{b_cnt}} tickets</span>
        </div>

      

    </ion-content>


</ion-view>

In the Controller

      $scope.record = function() {
            if (device.platform !== 'android') {
                $rootScope.toggle($scope.settings.pspeak, 5000);
            }

            $scope.init();
            var options = {
                language: $scope.settings.lang,
                matches:1,
                prompt:'',      // Android only
                showPopup:true,  // Android only
                showPartial:true // iOS only
            }

            function successCallback(x) {

                if (x[0]) {
                    var command = x[0];
                    $scope.command = x[0];
                } else {
                    $scope.speak($scope.settings.sorry);
                    return ('');
                }
                if (device.platform === 'Android') {
                    processCommand();
                }
            }

            function processCommand() {
                var command = $scope.command;
                console.log(command);
                var hassearch = command.indexOf('search') !== -1 ||
                    command.indexOf('find') !== -1;
                if (hassearch) {
                    $scope.finalQuery = ‘ command + ' ' ;
                    $rootScope.toggle(command, 2000);
                    $scope.speak(command);
                }
                else {
                    $scope.finalQuery = command  +  ' ' ;
                    $rootScope.toggle($scope.settings.searching + command, 2000);
                    $scope.speak($scope.settings.searching + command);

                }
            }

            function errorCallback(x) {
                $scope.speak($scope.settings.sorry);
                console.log (x);
            }

            window.plugins.speechRecognition.startListening(
                successCallback,  errorCallback,  options);

            if (device.platform !== 'Android') {

                setTimeout(function() {
                    window.plugins.speechRecognition.stopListening();

                    if ($scope.command == '') {

                    } else {
                        processCommand();
                    }

                }, 5000);
            }
        };

        $scope.init = function() {
            $scope.command = '';

            requestPerms();
            function requestPerms() {

                window.plugins.speechRecognition.requestPermission(
                    gotPerms, errorPerms)
            }

            function gotPerms() {

            }
            function errorPerms() {

            }
        }

        $scope.speak = function (text) {
            var rate = .8;
            if (device.platform !== 'android') {
                rate = 1.5;
            }
            if ($scope.settings.talkBack) {
                TTS.speak({
                    text: text,
                    locale: $scope.settings.lang,
                    rate: rate
                }, function () {
                    console.log('Text succesfully spoken');
                }, function (reason) {
                    console.log(reason);
                });
            }
        }