<commit_before>import argparse
import signal
from naoqi import ALProxy, ALBroker, ALModule
from google_client import *
from event_abstract import *
from os.path import expanduser


class SpeechRecognition(EventAbstractClass):
    WR_EVENT = "WordRecognized"
    TD_EVENT = "ALTextToSpeech/TextDone"
    ASR_ENABLE = "ASR_enable"
    FLAC_COMM = 'flac -f '
    CHANNELS = [0, 0, 1, 0]
    timeout = 0

    def __init__(self, ip, port, language, word_spotting, audio, visual, vocabulary_file, google_keys, asr_logging):
        super(self.__class__, self).__init__(self, ip, port)

        self.__shutdown_requested = False
        signal.signal(signal.SIGINT, self.signal_handler)

        vocabulary = slu_utils.lines_to_list(vocabulary_file)
        if (language == 'en') or (language == 'eng') or (language == 'english') or (language == 'English'):
            nuance_language = 'English'
            google_language = "en-US"

        self.logging = asr_logging

        self.nuance_asr = ALProxy("ALSpeechRecognition")

        self.audio_recorder = ALProxy("ALAudioRecorder")

        self.google_asr = GoogleClient(google_language, google_keys)

        self.memory_proxy = ALProxy("ALMemory")

        self.configure(
            vocabulary=vocabulary,
            nuance_language=nuance_language,
            word_spotting=word_spotting,
            audio=audio,
            visual=visual
        )

    def start(self, *args, **kwargs):
        self.subscribe(
            event=SpeechRecognition.TD_EVENT,
            callback=self.text_done_callback
        )

        self.subscribe(
            event=SpeechRecognition.ASR_ENABLE,
            callback=self.enable_callback
        )

        print "[" + self.inst.__class__.__name__ + "] Subscribers:", self.memory.getSubscribers(
            SpeechRecognition.WR_EVENT)
        print "[" + self.inst.__class__.__name__ + "] Subscribers:", self.memory.getSubscribers(
            SpeechRecognition.TD_EVENT)
        print "[" + self.inst.__class__.__name__ + "] Subscribers:", self.memory.getSubscribers(
            SpeechRecognition.ASR_ENABLE)

        self.is_enabled = False

        print "[" + self.inst.__class__.__name__ + "] ASR disabled"

        if self.logging:
            self.AUDIO_FILE_DIR = expanduser('~') + '/bags/asr_logs/'
        else:
            self.AUDIO_FILE_DIR = '/tmp/asr_logs/'
        if not os.path.exists(self.AUDIO_FILE_DIR):
            os.makedirs(self.AUDIO_FILE_DIR)
        self.AUDIO_FILE_PATH = self.AUDIO_FILE_DIR + 'SPQReL_mic_'

        self._spin()

        if self.is_enabled == True:
            self.unsubscribe(SpeechRecognition.WR_EVENT)
        self.unsubscribe(SpeechRecognition.TD_EVENT)
        self.unsubscribe(SpeechRecognition.ASR_ENABLE)
        self.broker.shutdown()

    def stop(self):
        self.audio_recorder.stopMicrophonesRecording()
        self.is_enabled = False
        self.__shutdown_requested = True
        print '[' + self.inst.__class__.__name__ + '] Good-bye'

    def configure(self, word_spotting, nuance_language, audio, visual, vocabulary):
        self.nuance_asr.pause(True)
        self.nuance_asr.setVocabulary(vocabulary, word_spotting)
        self.nuance_asr.setLanguage(nuance_language)
        self.nuance_asr.setAudioExpression(audio)
        self.nuance_asr.setVisualExpression(visual)
        self.nuance_asr.pause(False)

    def word_recognized_callback(self, *args, **kwargs):
        self.audio_recorder.stopMicrophonesRecording()
        self.nuance_asr.pause(True)
        """
        Convert Wave file into Flac file
        """
        os.system(self.FLAC_COMM + self.AUDIO_FILE + '.wav')
        f = open(self.AUDIO_FILE + '.flac', 'rb')
        flac_cont = f.read()
        f.close()

        results = {}
        results['GoogleASR'] = [r.encode('ascii', 'ignore').lower() for r in self.google_asr.recognize_data(flac_cont)]
        results['NuanceASR'] = [args[1][0].lower()]
        print "[" + self.inst.__class__.__name__ + "] " + str(results)
        self.timeout = 0
        self.nuance_asr.pause(False)
        self.audio_recorder.stopMicrophonesRecording()
        self.AUDIO_FILE = self.AUDIO_FILE_PATH + str(time.time())
        self.audio_recorder.startMicrophonesRecording(self.AUDIO_FILE + ".wav", "wav", 44100, self.CHANNELS)
        self.memory.raiseEvent("VordRecognized", results)

    def text_done_callback(self, *args, **kwargs):
        try:
            if self.is_enabled:
                if args[1] == 0:
                    self.audio_recorder.stopMicrophonesRecording()
                    self.nuance_asr.pause(True)
                else:
                    self.audio_recorder.stopMicrophonesRecording()
                    self.AUDIO_FILE = self.AUDIO_FILE_PATH + str(time.time())
                    self.audio_recorder.startMicrophonesRecording(self.AUDIO_FILE + ".wav", "wav", 44100, self.CHANNELS)
                    self.nuance_asr.pause(False)
        except Exception as e:
            print e.message

    def enable_callback(self, *args, **kwargs):
        if args[1] == "0":
            if self.is_enabled:
                self.is_enabled = False
                self.audio_recorder.stopMicrophonesRecording()
                self.unsubscribe(SpeechRecognition.WR_EVENT)
                print "[" + self.inst.__class__.__name__ + "] ASR disabled"
            else:
                print "[" + self.inst.__class__.__name__ + "] ASR already disabled"
        else:
            if not self.is_enabled:
                try:
                    self.AUDIO_FILE_DIR = self.memory_proxy.getData("CurrentLogFolder")
                except:
                    self.AUDIO_FILE_DIR = expanduser('~') + '/bags/no_data/asr_logs/'
                if not os.path.exists(self.AUDIO_FILE_DIR):
                    os.makedirs(self.AUDIO_FILE_DIR)<commit_msg>minor fix<commit_after>import argparse
import signal
from naoqi import ALProxy, ALBroker, ALModule
from google_client import *
from event_abstract import *
from os.path import expanduser


class SpeechRecognition(EventAbstractClass):
    WR_EVENT = "WordRecognized"
    TD_EVENT = "ALTextToSpeech/TextDone"
    ASR_ENABLE = "ASR_enable"
    FLAC_COMM = 'flac -f '
    CHANNELS = [0, 0, 1, 0]
    timeout = 0

    def __init__(self, ip, port, language, word_spotting, audio, visual, vocabulary_file, google_keys, asr_logging):
        super(self.__class__, self).__init__(self, ip, port)

        self.__shutdown_requested = False
        signal.signal(signal.SIGINT, self.signal_handler)

        vocabulary = slu_utils.lines_to_list(vocabulary_file)
        if (language == 'en') or (language == 'eng') or (language == 'english') or (language == 'English'):
            nuance_language = 'English'
            google_language = "en-US"

        self.logging = asr_logging

        self.nuance_asr = ALProxy("ALSpeechRecognition")

        self.audio_recorder = ALProxy("ALAudioRecorder")

        self.google_asr = GoogleClient(google_language, google_keys)

        self.memory_proxy = ALProxy("ALMemory")

        self.configure(
            vocabulary=vocabulary,
            nuance_language=nuance_language,
            word_spotting=word_spotting,
            audio=audio,
            visual=visual
        )

    def start(self, *args, **kwargs):
        self.subscribe(
            event=SpeechRecognition.TD_EVENT,
            callback=self.text_done_callback
        )

        self.subscribe(
            event=SpeechRecognition.ASR_ENABLE,
            callback=self.enable_callback
        )

        print "[" + self.inst.__class__.__name__ + "] Subscribers:", self.memory.getSubscribers(
            SpeechRecognition.WR_EVENT)
        print "[" + self.inst.__class__.__name__ + "] Subscribers:", self.memory.getSubscribers(
            SpeechRecognition.TD_EVENT)
        print "[" + self.inst.__class__.__name__ + "] Subscribers:", self.memory.getSubscribers(
            SpeechRecognition.ASR_ENABLE)

        self.is_enabled = False

        print "[" + self.inst.__class__.__name__ + "] ASR disabled"

        if self.logging:
            self.AUDIO_FILE_DIR = expanduser('~') + '/bags/asr_logs/'
        else:
            self.AUDIO_FILE_DIR = '/tmp/asr_logs/'
        if not os.path.exists(self.AUDIO_FILE_DIR):
            os.makedirs(self.AUDIO_FILE_DIR)
        self.AUDIO_FILE_PATH = self.AUDIO_FILE_DIR + 'SPQReL_mic_'

        self._spin()

        if self.is_enabled == True:
            self.unsubscribe(SpeechRecognition.WR_EVENT)
        self.unsubscribe(SpeechRecognition.TD_EVENT)
        self.unsubscribe(SpeechRecognition.ASR_ENABLE)
        self.broker.shutdown()

    def stop(self):
        self.audio_recorder.stopMicrophonesRecording()
        self.is_enabled = False
        self.__shutdown_requested = True
        print '[' + self.inst.__class__.__name__ + '] Good-bye'

    def configure(self, word_spotting, nuance_language, audio, visual, vocabulary):
        self.nuance_asr.pause(True)
        self.nuance_asr.setVocabulary(vocabulary, word_spotting)
        self.nuance_asr.setLanguage(nuance_language)
        self.nuance_asr.setAudioExpression(audio)
        self.nuance_asr.setVisualExpression(visual)
        self.nuance_asr.pause(False)

    def word_recognized_callback(self, *args, **kwargs):
        self.audio_recorder.stopMicrophonesRecording()
        self.nuance_asr.pause(True)
        """
        Convert Wave file into Flac file
        """
        os.system(self.FLAC_COMM + self.AUDIO_FILE + '.wav')
        f = open(self.AUDIO_FILE + '.flac', 'rb')
        flac_cont = f.read()
        f.close()

        results = {}
        results['GoogleASR'] = [r.encode('ascii', 'ignore').lower() for r in self.google_asr.recognize_data(flac_cont)]
        results['NuanceASR'] = [args[1][0].lower()]
        print "[" + self.inst.__class__.__name__ + "] " + str(results)
        self.timeout = 0
        self.nuance_asr.pause(False)
        self.audio_recorder.stopMicrophonesRecording()
        self.AUDIO_FILE = self.AUDIO_FILE_PATH + str(time.time())
        self.audio_recorder.startMicrophonesRecording(self.AUDIO_FILE + ".wav", "wav", 44100, self.CHANNELS)
        self.memory.raiseEvent("VordRecognized", results)

    def text_done_callback(self, *args, **kwargs):
        try:
            if self.is_enabled:
                if args[1] == 0:
                    self.audio_recorder.stopMicrophonesRecording()
                    self.nuance_asr.pause(True)
                else:
                    self.audio_recorder.stopMicrophonesRecording()
                    self.AUDIO_FILE = self.AUDIO_FILE_PATH + str(time.time())
                    self.audio_recorder.startMicrophonesRecording(self.AUDIO_FILE + ".wav", "wav", 44100, self.CHANNELS)
                    self.nuance_asr.pause(False)
        except Exception as e:
            print e.message

    def enable_callback(self, *args, **kwargs):
        if args[1] == "0":
            if self.is_enabled:
                self.is_enabled = False
                self.audio_recorder.stopMicrophonesRecording()
                self.unsubscribe(SpeechRecognition.WR_EVENT)
                print "[" + self.inst.__class__.__name__ + "] ASR disabled"
            else:
                print "[" + self.inst.__class__.__name__ + "] ASR already disabled"
        else:
            if not self.is_enabled:
                try:
                    self.AUDIO_FILE_DIR = self.memory_proxy.getData("CurrentLogFolder") + "/asr_logs"
                except:
                    self.AUDIO_FILE_DIR = expanduser('~') + '/bags/no_data/asr_logs/'
                if not os.path.exists(self.AUDIO_FILE_DIR):
                    os.makedirs(self.AUDIO_FILE_DIR)
{'fraction_non_alphanumeric': 0.10838026967059225, 'fraction_numerical': 0.0037549069807134325, 'mean_word_length': 2.3549957056971085, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, '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': '2000316', 'n_tokens_mistral': 3756, 'n_tokens_neox': 3577, 'n_words': 573}