<commit_before>/*
    docs: http://api.yandex.ru/speechkit/cloud-api/doc/index.xml
*/

var YandexASR = function(options, callback){

    var request = require('request');
    var qs = require('querystring');
    var fs = require('fs');
    var path = require('path');

    var yandex_asr_url = 'http://asr.yandex.net/asr_xml?';
    
    var file = {};
    var params = {};   

    var generateUUID = function(){        
        function s8() {
            return Math.floor((1 + Math.random()) * 0x10000000).toString(16);
        }
        return s8()+s8()+s8()+s8();
    }

    var init = function(options){        
        //reqired
        params['key'] = options['developer_key'];        
        file['filepath'] = options['file'];
        //optional
        params['uuid'] = (options['uuid']) ? options['uuid'] : generateUUID();        
        params['topic'] = (options['topic']) ? options['topic'] : 'freeform';
        params['lang'] = (options['lang']) ? options['lang'] : 'ru-RU';
        file['filetype'] = (options['filetype']) ? options['filetype'] : 'audio/x-mpeg-3';
    }(options);    

    var full_url = yandex_asr_url + qs.stringify(params);

    var check_filesize = function(filepath){
        var fileSizeInBytes = fs.statSync(filepath)["size"];
        return (fileSizeInBytes < 1024*1024);
    }

    if(check_filesize(file['filepath'])){

        var recognize = function(callback){
            
            var r = request.post({
                    url: full_url,
                    headers: {
                        'Transfer-Encoding': 'chunked'
                    }
                }, 
                function (err, httpResponse, body) {
                    callback(err, httpResponse, body);
                }
            );

            var form = r.form();
            form.append('Content-Type', file['filetype']);
            form.append('audio', fs.createReadStream(file['filepath']));
        }(callback);

    }else{
        callback('filesize more 1Mb');
    }
};

module.exports = YandexASR;<commit_msg>Refactoring + Fix bug<commit_after>/*
 docs: http://api.yandex.ru/speechkit/cloud-api/doc/index.xml
 */
var request = require('request');
var qs = require('querystring');
var fs = require('fs');
var path = require('path');
var mime = require('mime');

var YANDEX_ASR_URL = 'http://asr.yandex.net/asr_xml?';

var YandexASR = function (options, callback) {
    var file = {};
    var params = {};
    var debug = (options['debug']) ? options['debug'] : false;

    var generateUUID = function () {
        function s8() {
            return Math.floor((1 + Math.random()) * 0x10000000).toString(16);
        }

        return s8() + s8() + s8() + s8();
    };

    //reqired
    params['key'] = options['developer_key'];
    file['filepath'] = options['file'];
    file['filetype'] = mime.lookup(options['file']);

    //optional
    params['uuid'] = (options['uuid']) ? options['uuid'] : generateUUID();
    params['topic'] = (options['topic']) ? options['topic'] : 'notes';

    options['lang'] && (params['lang'] = options['lang']);

    var full_url = YANDEX_ASR_URL + qs.stringify(params);

    if (debug) {
        console.log('url = ' + full_url + '\n' +
            'path = ' + file['filepath'] + '\n' +
            'type = ' + file['filetype']);
    }

    var check_filesize = function (filepath) {
        var fileSizeInBytes = fs.statSync(filepath)["size"];
        return (fileSizeInBytes < 1024 * 1024);
    };

    if (check_filesize(file['filepath'])) {
        var r = request.post({
                url: full_url
            },
            function (err, httpResponse, body) {
                callback(err, httpResponse, body);
            }
        );

        var form = r.form();
        form.append('Content-Type', file['filetype']);
        form.append('audio', fs.createReadStream(file['filepath']));
    } else {
        callback('filesize more 1Mb');
    }
};

module.exports = YandexASR;
{'fraction_non_alphanumeric': 0.17419843473870233, 'fraction_numerical': 0.01338045947992931, 'mean_word_length': 2.242225859247136, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 2, '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': '7343623', 'n_tokens_mistral': 1273, 'n_tokens_neox': 1197, 'n_words': 230}