voicoding.js 9.08 KB
const express = require('express');
const multer = require('multer');
const fs = require('fs');
const upload = multer();
const app = express(); 
const port = 5500;
app.use(express.static('Voicoding_web'));
app.use(express.static('./'));
async function testGoogleTextToSpeech(audioBuffer) {
    const speech = require('@google-cloud/speech');
    const client = new speech.SpeechClient();
    const audio = {
    content: audioBuffer.toString('base64'),
    };
    const encoding = 'LINEAR16';
    const sampleRateHertz = 48000;
    const languageCode = 'ko-KR';
    const speechContexts = [{
        phrases: [
        '$$OOV_CLASS_ALPHANUMERIC_SEQUENCE 는 $OOV_CLASS_ALPHANUMERIC_SEQUENCE',
        '$OOV_CLASS_ALPHANUMERIC_SEQUENCE 는 $OOV_CLASS_ALPHANUMERIC_SEQUENCE 보다 크다',
        '$OOV_CLASS_ALPHANUMERIC_SEQUENCE 는 $OOV_CLASS_ALPHANUMERIC_SEQUENCE 보다 작다',
        '$OOV_CLASS_ALPHANUMERIC_SEQUENCE 는 $OOV_CLASS_ALPHANUMERIC_SEQUENCE 와 같다',
        '$OOV_CLASS_ALPHANUMERIC_SEQUENCE 는 $OOV_CLASS_ALPHANUMERIC_SEQUENCE 보다 작거나 같다',
        '$OOV_CLASS_ALPHANUMERIC_SEQUENCE 는 $OOV_CLASS_ALPHANUMERIC_SEQUENCE 보다 크거나 같다',
        'for $OOV_CLASS_ALPHA_SEQUENCE in',
        'for $OOV_CLASS_ALPHA_SEQUENCE in',
        '$$OOV_CLASS_ALPHANUMERIC_SEQUENCE 는 빈 리스트',
        '$$OOV_CLASS_ALPHANUMERIC_SEQUENCE 는 숫자 리스트 $OOV_CLASS_ALPHANUMERIC_SEQUENCE',
        '$$OOV_CLASS_ALPHANUMERIC_SEQUENCE 는 문자 리스트 $OOV_CLASS_ALPHANUMERIC_SEQUENCE',
        'if',
        '이프',
        'else if',
        '엘스 이프',
        'else',
        '엘스',
        'while',
        '와일',
        '함수',
        '파라미터 선언',
        'parameter',
        '변수 선언',
        '함수 선언',
        '반복문 선언',
        '조건문 선언',
        '기본 함수 사용',
        '인풋',
        '스플릿',
        '프린트',
        '섬',
        '맥스',
        '민',
        '렌',
        '리턴',
        '리스트'
        ],
        boost: 20
    }]

    const request = {
        audio: audio,
        config: {
            encoding: encoding,
            sampleRateHertz: sampleRateHertz,
            languageCode: languageCode,
            alternativeLanguageCodes: ['en-US'],
            speechContexts: speechContexts
        },
        interimResults: false, // If you want interim results, set this to true
    };
    const [response] = await client.recognize(request);
    const transcription = response.results
    .map(result => result.alternatives[0].transcript);
    return transcription;
}
let before_val = '';
let tab = '';
let ent = '';
async function TextToPython(trans_data) {
    let text_list = trans_data.split(' ');
    console.log(text_list);
    switch (text_list[0]){
        case '기본':
            before_val = text_list[0];
        switch (text_list[3]){
            case '프린트':
                return tab + 'print(';
            case '인풋': 
                return tab + 'input()';
            case '스플릿':
                return '.split()';
            case '섬':
                return 'sum(';
            case '맥스':
                return 'max(';
            case '민':
                return 'min(';
            case '렌':
                return 'len(';
            }
            break;
        case '파라미터':
            if (text_list.length < 3) return '';
            if (before_val == '기본'){
                before_val = '파라미터';
                return text_list.slice(2).join() + ')\n';
            }
            else{
                before_val = '파라미터';
                tab += '\t';
                return text_list.slice(2).join() + '):\n';
            }
            break;
        case '변수':
            if (before_val == '기본'){
                ent = '\n';
            }
            else{
                ent = '';
            }
            before_val = '변수';
            if (text_list.length < 4) return '';
            if (text_list[2] == '문자'){
                if (text_list[3][text_list[3].length-1] == '는'){
                    return ent + tab + text_list[3].slice(0, text_list[3].length-1) + ' = "' + text_list.slice(4).join(' ') + '"\n';
                }
                else{
                    text_list[4] = '= "';
                    return ent + tab + text_list.slice(3).join(' ') + '"\n';
                }
            }
            else if (text_list[2] == '숫자'){
                if (text_list[3][text_list[3].length-1] == '는'){
                    return ent + tab + text_list[3].slice(0, text_list[3].length-1) + ' = ' + text_list.slice(4).join(' ') + '\n';
                }
                else if (text_list[4] == '-'){
                    text_list[4] = '=';
                    return ent + tab + text_list.slice(3).join(' ') + '\n';
                }
            }
            else if (text_list[2] == '함수'){
                if (text_list[3][text_list[3].length-1] == '는'){
                    return ent + tab + text_list[3].slice(0, text_list[3].length-1) + ' = ';
                }
                else{
                    text_list[4] = '=';
                    return ent + tab + text_list.slice(3)
                }
            }
            else if (text_list[2] == '리스트'){
                if (text_list[3][text_list[3].length-1] == '는'){
                    if (text_list[4] == '빈') return ent + tab + text_list[3].slice(0, text_list[3].length-1) + ' = []\n';
                    else if (text_list[4] == '숫자') ent + tab + text_list[3].slice(0, text_list[3].length-1) + ' = [' + text_list.slice(6).join(',') + ']\n';
                    else return ent + tab + text_list[3].slice(0, text_list[3].length-1) + ' = ["' + text_list.slice(6).join('","') + '"]\n';
                }
            }
            else return '';
            break;
        case '반복문':
            if (before_val == '기본'){
                ent = '\n';
            }
            else{
                ent = '';
            }
            if (text_list[3] == 'for'){
                return ent + tab + text_list.slice(3).join(' ') + ' ('
            }
            else if (text_list[3] == 'while' || text_list[3] == '와일'){
                return ent + tab + 'while (';
            }
            else return '';
            break;
        case '완료':
            tab.slice(1);
            return '';
            break;
        case '함수':
            if (before_val == '기본'){
                ent = '\n';
            }
            else{
                ent = '';
            }
            if (text_list.length >= 3) return 'def ' + text_list.slice(2).join('') + '(';
            else return '';
        case '조건문':
            if (before_val == '기본'){
                ent = '\n';
            }
            else{
                ent = '';
            }
            if (text_list.length >= 3){
                if (text_list[3] == 'if' || text_list[3] == '이프'){
                    return tab + 'if (';
                }
                else if ((text_list[3] == 'else' || text_list[3] == '엘스')){
                    tab += 1;
                    return '\t'*(tab-1) + 'else:\n'
                }
                else if((text_list[3] == 'else' || text_list[3] == '엘스') && (text_list[4] == 'if' || text_list[4] == '이프')){
                    return tab + 'elif (';
                }
            }
            else return '';
            break;
        case '논리':
            if (text_list.length >= 7){
                tab += 1;
                if (text_list[6] == '같다'){
                    return tab + text_list[3].slice(0, text_list[3].length-1) + ' == ' + text_list[4] + '):\n';
                }
                else if(text_list[6] == '크다'){
                    return tab + text_list[3].slice(0, text_list[3].length-1) + ' > ' + text_list[4] + '):\n';
                }
                else if(text_list[6] == '작다'){
                    return tab + text_list[3].slice(0, text_list[3].length-1) + ' < ' + text_list[4] + '):\n';
                }
                else if(text_list[6] == '크거나'){
                    return tab + text_list[3].slice(0, text_list[3].length-1) + ' >= ' + text_list[4] + '):\n';
                }
                else if(text_list[6] == '작거나'){
                    return tab + text_list[3].slice(0, text_list[3].length-1) + ' <= ' + text_list[4] + '):\n';
                }
            }
            else return '';
            break;
        default: return ''
    }

}

app.post('/upload_sound', upload.any(), async (req, res) => {
    console.log("Getting text transcription..");
    let transcription = await testGoogleTextToSpeech(req.files[0].buffer);
    console.log("Text transcription: " + transcription);        
    res.status(200).send(transcription);
});
app.post('/ttp', upload.any(), async (req, res) => {
    console.log("Getting python code..");
    let python_code = await TextToPython(req.body.transcript_data);
    console.log("Pyton code: " + python_code);        
    res.status(200).send(python_code);
});
app.listen(port, () => {
    console.log(`Express server listening on port: ${port}...`);
});