<commit_before>'use strict';

var path = require('path'),
    uuid = require('node-uuid'),
    Q = require('q'),
    ContextWrapper = require('./contextWrapper');

var Handler = function (source, recognizer, config) {

    var counter = 1;
    var logger;
    var sounds = config.asterisk['sounds'];

    var getCallId = function () {
        var length = 7;
        //magic from https://gist.github.com/aemkei/1180489#file-index-js
        var q = function (a, b) {
            return([1e15]+a).slice(-b)
        };
        return q(counter++, length);
    };

    this.setLogger = function (loggerIn) {
        logger = loggerIn;
    };

    this.handle = function (context) {
        
        var callId = getCallId();
        var currentAttempt = 1,
            totalAttempts = config.processing['totalAttempts'] || 1;

        function log(message, object) {
            if (logger) {                
                if (object) {
                    logger.info(callId, message, object);
                } else {
                    logger.info(callId, message);
                }
            }
        };

        if (logger) {
            recognizer.setLogFunction(log);
        }

        var contextWrapper = new ContextWrapper(context);
        var filename = uuid.v4();

        var stepGreeting = function () {            
            log('stepGreeting');
            return contextWrapper.answer()
                .then(function () {
                    if (config.processing['playGreeting']) {
                        return contextWrapper.streamFile(sounds['greeting'], '#');
                    } else {
                        return Q.resolve();
                    }
                });
        };        

        var stepRecord = function () {
            var conf = config.record,
                filepath = conf['directory'] + '/' + filename;
            var beep = (config.processing['playBeepBeforeRecording']) ? 'beep' : '';

            log('stepRecord', filepath);
            return contextWrapper.recordFile(filepath, conf['type'], '#', conf['duration'], 0, beep, '');
        };

        var stepRecognize = function () {
            var conf = config.recognize,
                file = conf['directory'] + '/' + filename + '.wav';

            log('stepRecognize', file);
            return recognizer.recognize(file);
        };
      
        var stepLookup = function (text) {
            log('stepLookup', text);
            return source.lookup(text);
        };

        var stepDial = function (channel) {
            log('stepDial', channel);
            return contextWrapper.dial(channel);
        };

        var stepFinish = function () {
            log('stepFinish');
            return contextWrapper.end();
        };

        var stepError = function (error) {
            log('stepError', error);
            return contextWrapper.streamFile('invalid', '#');
        };

        var stepRepeatOnError = function (error) {
            log('stepRepeatOnError', error);
            return contextWrapper.streamFile('invalid', '#');                
        };

        var FlowProcess = function () {            
            return stepRecord()
                .then(stepRecognize)
                .then(stepLookup)
                .then(stepDial)
                .then(stepFinish)
        };

        var loop = function (promise) {            
            if (currentAttempt < totalAttempts) {
                currentAttempt++;

                return promise()
                    .fail(function (error) {                        
                        return stepRepeatOnError(error)
                            .then(function () {                                
                                log('fail, make next attempt', {
                                        current: currentAttempt, total: totalAttempts
                                    });
                                return loop(promise);
                            });
                    });
            } else {
                return promise()
                    .fail(function (error) {                        
                        return stepError(error)
                            .then(stepFinish);
                    });
            }
        };        

        contextWrapper.on('variables')
            .then(function () {
                log('-- start processing');
                return stepGreeting()
            })         
            .then(function () {
                return loop(FlowProcess);
            });

        contextWrapper.on('error')
            .then(function () {
                log('-- error');
                return stepFinish();
            });

        contextWrapper.on('close')
            .then(function () {
                log('-- close');
            });

        contextWrapper.on('hangup')
            .then(function () {
                log('-- hangup');
                return stepFinish();
            });
    };
};

module.exports = Handler;<commit_msg>use config asterisk.sounds for all sounds in handler<commit_after>'use strict';

var path = require('path'),
    uuid = require('node-uuid'),
    Q = require('q'),
    ContextWrapper = require('./contextWrapper');

var Handler = function (source, recognizer, config) {

    var counter = 1;
    var logger;
    var sounds = config.asterisk['sounds'];

    var getCallId = function () {
        var length = 7;
        //magic from https://gist.github.com/aemkei/1180489#file-index-js
        var q = function (a, b) {
            return([1e15]+a).slice(-b)
        };
        return q(counter++, length);
    };

    this.setLogger = function (loggerIn) {
        logger = loggerIn;
    };

    this.handle = function (context) {
        
        var callId = getCallId();
        var currentAttempt = 1,
            totalAttempts = config.processing['totalAttempts'] || 1;

        function log(message, object) {
            if (logger) {                
                if (object) {
                    logger.info(callId, message, object);
                } else {
                    logger.info(callId, message);
                }
            }
        };

        if (logger) {
            recognizer.setLogFunction(log);
        }

        var contextWrapper = new ContextWrapper(context);
        var filename = uuid.v4();

        var stepGreeting = function () {            
            log('stepGreeting');
            return contextWrapper.answer()
                .then(function () {
                    if (config.processing['playGreeting']) {
                        return contextWrapper.streamFile(sounds['greeting'], '#');
                    } else {
                        return Q.resolve();
                    }
                });
        };        

        var stepRecord = function () {
            var conf = config.record,
                filepath = conf['directory'] + '/' + filename;
            var beep = (config.processing['playBeepBeforeRecording']) ? 'beep' : '';

            log('stepRecord', filepath);
            return contextWrapper.recordFile(filepath, conf['type'], '#', conf['duration'], 0, beep, '');
        };

        var stepRecognize = function () {
            var conf = config.recognize,
                file = conf['directory'] + '/' + filename + '.wav';

            log('stepRecognize', file);
            return recognizer.recognize(file);
        };
      
        var stepLookup = function (text) {
            log('stepLookup', text);
            return source.lookup(text);
        };

        var stepDial = function (channel) {
            log('stepDial', channel);
            return contextWrapper.dial(channel);
        };

        var stepFinish = function () {
            log('stepFinish');
            return contextWrapper.end();
        };

        var stepErrorBeforeFinish = function (error) {
            log('stepError', error);
            return contextWrapper.streamFile(sounds['onErrorBeforeFinish'], '#');
        };

        var stepErrorBeforeRepeat = function (error) {
            log('stepRepeatOnError', error);
            return contextWrapper.streamFile(sounds['onErrorBeforeRepeat'], '#');
        };

        var FlowProcess = function () {
            return stepRecord()
                .then(stepRecognize)
                .then(stepLookup)
                .then(stepDial)
                .then(stepFinish)
        };

        var loop = function (promise) {
            if (currentAttempt < totalAttempts) {
                currentAttempt++;

                return promise()
                    .fail(function (error) {
                        return stepErrorBeforeRepeat(error)
                            .then(function () {
                                log('fail, make next attempt', {
                                        current: currentAttempt, total: totalAttempts
                                    });
                                return loop(promise);
                            });
                    });
            } else {
                return promise()
                    .fail(function (error) {
                        return stepErrorBeforeFinish(error)
                            .then(stepFinish);
                    });
            }
        };        

        contextWrapper.on('variables')
            .then(function () {
                log('-- start processing');
                return stepGreeting();
            })         
            .then(function () {
                return loop(FlowProcess);
            });

        contextWrapper.on('error')
            .then(function () {
                log('-- error');
                return stepFinish();
            });

        contextWrapper.on('close')
            .then(function () {
                log('-- close');
            });

        contextWrapper.on('hangup')
            .then(function () {
                log('-- hangup');
                return stepFinish();
            });
    };
};

module.exports = Handler;
{'fraction_non_alphanumeric': 0.11868787276341948, 'fraction_numerical': 0.0031809145129224653, 'mean_word_length': 1.2283499446290145, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2431283', 'n_tokens_mistral': 2511, 'n_tokens_neox': 2330, 'n_words': 486}