voicoding.js
9.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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}...`);
});