천현우

TTP: update TTP functions, insert/delete tab

1 '기본 함수 사용', 1 '기본 함수 사용',
2 '인풋', -> input() 2 '인풋', -> input()
3 - '스플릿', -> .split()
4 -
5 '프린트', -> print( 3 '프린트', -> print(
6 '섬', -> sum( 4 '섬', -> sum(
7 '맥스', -> max( 5 '맥스', -> max(
......
...@@ -1368,15 +1368,23 @@ function transcribeText() { ...@@ -1368,15 +1368,23 @@ function transcribeText() {
1368 } 1368 }
1369 1369
1370 let input_line = "" 1370 let input_line = ""
1371 -function uploadSoundData(blob) { 1371 +let tab = '';
1372 +let before_val = '';
1373 +let chk = 1;
1374 +let tmp = [];
1375 +async function uploadSoundData(blob) {
1372 let filename = new Date().toISOString(); 1376 let filename = new Date().toISOString();
1373 let xhr = new XMLHttpRequest(); 1377 let xhr = new XMLHttpRequest();
1374 let formData = new FormData(); 1378 let formData = new FormData();
1375 xhr.onload = function(e) { 1379 xhr.onload = function(e) {
1376 if(this.readyState === 4) { 1380 if(this.readyState === 4) {
1377 - let transcript = JSON.parse(e.target.responseText) 1381 + let transcript = JSON.parse(e.target.responseText) + '';
1378 document.getElementById("output").innerHTML += `${cnt++}: ${transcript}<br><br>`; 1382 document.getElementById("output").innerHTML += `${cnt++}: ${transcript}<br><br>`;
1379 - uploadTranscriptData(transcript); 1383 + tmp = transcript.split(' ');
1384 + if (tmp[0] == '종료') tab = tab.slice(1);
1385 + else{
1386 + uploadTranscriptData(tab, transcript);
1387 + }
1380 } 1388 }
1381 }; 1389 };
1382 formData.append("audio_data", blob, filename); 1390 formData.append("audio_data", blob, filename);
...@@ -1384,7 +1392,7 @@ function uploadSoundData(blob) { ...@@ -1384,7 +1392,7 @@ function uploadSoundData(blob) {
1384 xhr.send(formData); 1392 xhr.send(formData);
1385 } 1393 }
1386 1394
1387 -function uploadTranscriptData(txt) { 1395 +async function uploadTranscriptData(tabs, txt) {
1388 let request = new XMLHttpRequest(); 1396 let request = new XMLHttpRequest();
1389 let form = new FormData(); 1397 let form = new FormData();
1390 request.onload = function(e) { 1398 request.onload = function(e) {
...@@ -1392,9 +1400,13 @@ function uploadTranscriptData(txt) { ...@@ -1392,9 +1400,13 @@ function uploadTranscriptData(txt) {
1392 console.log(e.target.responseText) 1400 console.log(e.target.responseText)
1393 input_line += e.target.responseText; 1401 input_line += e.target.responseText;
1394 sourceEditor.setValue(input_line); 1402 sourceEditor.setValue(input_line);
1403 + if ((tmp[0] == '함수' || tmp[0] == '반복문' || tmp[0] == '조건문') && e.target.responseText != '') {
1404 + tab += '\t';
1405 + }
1395 } 1406 }
1396 }; 1407 };
1397 form.append("transcript_data", txt); 1408 form.append("transcript_data", txt);
1409 + form.append("tabs", tabs);
1398 request.open("POST", "/ttp", true); 1410 request.open("POST", "/ttp", true);
1399 request.send(form); 1411 request.send(form);
1400 } 1412 }
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -23,8 +23,8 @@ async function testGoogleTextToSpeech(audioBuffer) { ...@@ -23,8 +23,8 @@ async function testGoogleTextToSpeech(audioBuffer) {
23 '$OOV_CLASS_ALPHANUMERIC_SEQUENCE 는 $OOV_CLASS_ALPHANUMERIC_SEQUENCE 와 같다', 23 '$OOV_CLASS_ALPHANUMERIC_SEQUENCE 는 $OOV_CLASS_ALPHANUMERIC_SEQUENCE 와 같다',
24 '$OOV_CLASS_ALPHANUMERIC_SEQUENCE 는 $OOV_CLASS_ALPHANUMERIC_SEQUENCE 보다 작거나 같다', 24 '$OOV_CLASS_ALPHANUMERIC_SEQUENCE 는 $OOV_CLASS_ALPHANUMERIC_SEQUENCE 보다 작거나 같다',
25 '$OOV_CLASS_ALPHANUMERIC_SEQUENCE 는 $OOV_CLASS_ALPHANUMERIC_SEQUENCE 보다 크거나 같다', 25 '$OOV_CLASS_ALPHANUMERIC_SEQUENCE 는 $OOV_CLASS_ALPHANUMERIC_SEQUENCE 보다 크거나 같다',
26 - 'for $OOV_CLASS_ALPHA_SEQUENCE in', 26 + '반복문 선언 for $OOV_CLASS_ALPHA_SEQUENCE in range',
27 - 'for $OOV_CLASS_ALPHA_SEQUENCE in', 27 + '반복문 선언 for $OOV_CLASS_ALPHA_SEQUENCE in',
28 '$$OOV_CLASS_ALPHANUMERIC_SEQUENCE 는 빈 리스트', 28 '$$OOV_CLASS_ALPHANUMERIC_SEQUENCE 는 빈 리스트',
29 '$$OOV_CLASS_ALPHANUMERIC_SEQUENCE 는 숫자 리스트 $OOV_CLASS_ALPHANUMERIC_SEQUENCE', 29 '$$OOV_CLASS_ALPHANUMERIC_SEQUENCE 는 숫자 리스트 $OOV_CLASS_ALPHANUMERIC_SEQUENCE',
30 '$$OOV_CLASS_ALPHANUMERIC_SEQUENCE 는 문자 리스트 $OOV_CLASS_ALPHANUMERIC_SEQUENCE', 30 '$$OOV_CLASS_ALPHANUMERIC_SEQUENCE 는 문자 리스트 $OOV_CLASS_ALPHANUMERIC_SEQUENCE',
...@@ -41,8 +41,10 @@ async function testGoogleTextToSpeech(audioBuffer) { ...@@ -41,8 +41,10 @@ async function testGoogleTextToSpeech(audioBuffer) {
41 'parameter', 41 'parameter',
42 '변수 선언', 42 '변수 선언',
43 '함수 선언', 43 '함수 선언',
44 - '반복문 선언', 44 + '반복문 선언 while',
45 + '반복문 선언 와일',
45 '조건문 선언', 46 '조건문 선언',
47 + '논리 식 선언',
46 '기본 함수 사용', 48 '기본 함수 사용',
47 '인풋', 49 '인풋',
48 '스플릿', 50 '스플릿',
...@@ -73,22 +75,19 @@ async function testGoogleTextToSpeech(audioBuffer) { ...@@ -73,22 +75,19 @@ async function testGoogleTextToSpeech(audioBuffer) {
73 .map(result => result.alternatives[0].transcript); 75 .map(result => result.alternatives[0].transcript);
74 return transcription; 76 return transcription;
75 } 77 }
78 +
76 let before_val = ''; 79 let before_val = '';
77 -let tab = ''; 80 +async function TextToPython(trans_data, tab) {
78 -let ent = '';
79 -async function TextToPython(trans_data) {
80 let text_list = trans_data.split(' '); 81 let text_list = trans_data.split(' ');
81 console.log(text_list); 82 console.log(text_list);
82 switch (text_list[0]){ 83 switch (text_list[0]){
83 case '기본': 84 case '기본':
84 - before_val = text_list[0]; 85 + before_val = '기본';
85 switch (text_list[3]){ 86 switch (text_list[3]){
86 case '프린트': 87 case '프린트':
87 return tab + 'print('; 88 return tab + 'print(';
88 case '인풋': 89 case '인풋':
89 - return tab + 'input()'; 90 + return 'input()\n';
90 - case '스플릿':
91 - return '.split()';
92 case '섬': 91 case '섬':
93 return 'sum('; 92 return 'sum(';
94 case '맥스': 93 case '맥스':
...@@ -97,8 +96,10 @@ async function TextToPython(trans_data) { ...@@ -97,8 +96,10 @@ async function TextToPython(trans_data) {
97 return 'min('; 96 return 'min(';
98 case '렌': 97 case '렌':
99 return 'len('; 98 return 'len(';
99 + case '수식':
100 + return text_list.slice(4).join(' ') + '\n';
101 + default: return '';
100 } 102 }
101 - break;
102 case '파라미터': 103 case '파라미터':
103 if (text_list.length < 3) return ''; 104 if (text_list.length < 3) return '';
104 if (before_val == '기본'){ 105 if (before_val == '기본'){
...@@ -107,99 +108,74 @@ async function TextToPython(trans_data) { ...@@ -107,99 +108,74 @@ async function TextToPython(trans_data) {
107 } 108 }
108 else{ 109 else{
109 before_val = '파라미터'; 110 before_val = '파라미터';
110 - tab += '\t';
111 return text_list.slice(2).join() + '):\n'; 111 return text_list.slice(2).join() + '):\n';
112 } 112 }
113 break; 113 break;
114 case '변수': 114 case '변수':
115 - if (before_val == '기본'){
116 - ent = '\n';
117 - }
118 - else{
119 - ent = '';
120 - }
121 - before_val = '변수';
122 if (text_list.length < 4) return ''; 115 if (text_list.length < 4) return '';
116 + before_val = '변수';
123 if (text_list[2] == '문자'){ 117 if (text_list[2] == '문자'){
124 if (text_list[3][text_list[3].length-1] == '는'){ 118 if (text_list[3][text_list[3].length-1] == '는'){
125 - return ent + tab + text_list[3].slice(0, text_list[3].length-1) + ' = "' + text_list.slice(4).join(' ') + '"\n'; 119 + return tab + text_list[3].slice(0, text_list[3].length-1) + ' = "' + text_list.slice(4).join(' ') + '"\n';
126 } 120 }
127 else{ 121 else{
128 text_list[4] = '= "'; 122 text_list[4] = '= "';
129 - return ent + tab + text_list.slice(3).join(' ') + '"\n'; 123 + return tab + text_list.slice(3).join(' ') + '"\n';
130 } 124 }
131 } 125 }
132 else if (text_list[2] == '숫자'){ 126 else if (text_list[2] == '숫자'){
133 if (text_list[3][text_list[3].length-1] == '는'){ 127 if (text_list[3][text_list[3].length-1] == '는'){
134 - return ent + tab + text_list[3].slice(0, text_list[3].length-1) + ' = ' + text_list.slice(4).join(' ') + '\n'; 128 + return tab + text_list[3].slice(0, text_list[3].length-1) + ' = ' + text_list.slice(4).join(' ') + '\n';
135 } 129 }
136 else if (text_list[4] == '-'){ 130 else if (text_list[4] == '-'){
137 text_list[4] = '='; 131 text_list[4] = '=';
138 - return ent + tab + text_list.slice(3).join(' ') + '\n'; 132 + return tab + text_list.slice(3).join(' ') + '\n';
139 } 133 }
140 } 134 }
141 else if (text_list[2] == '함수'){ 135 else if (text_list[2] == '함수'){
142 if (text_list[3][text_list[3].length-1] == '는'){ 136 if (text_list[3][text_list[3].length-1] == '는'){
143 - return ent + tab + text_list[3].slice(0, text_list[3].length-1) + ' = '; 137 + return tab + text_list[3].slice(0, text_list[3].length-1) + ' = ';
144 } 138 }
145 else{ 139 else{
146 text_list[4] = '='; 140 text_list[4] = '=';
147 - return ent + tab + text_list.slice(3) 141 + return tab + text_list.slice(3).join(' ');
148 } 142 }
149 } 143 }
150 else if (text_list[2] == '리스트'){ 144 else if (text_list[2] == '리스트'){
151 if (text_list[3][text_list[3].length-1] == '는'){ 145 if (text_list[3][text_list[3].length-1] == '는'){
152 - if (text_list[4] == '빈') return ent + tab + text_list[3].slice(0, text_list[3].length-1) + ' = []\n'; 146 + if (text_list[4] == '빈') return tab + text_list[3].slice(0, text_list[3].length-1) + ' = []\n';
153 - else if (text_list[4] == '숫자') ent + tab + text_list[3].slice(0, text_list[3].length-1) + ' = [' + text_list.slice(6).join(',') + ']\n'; 147 + else if (text_list[4] == '숫자') return tab + text_list[3].slice(0, text_list[3].length-1) + ' = [' + text_list.slice(6).join(',') + ']\n';
154 - else return ent + tab + text_list[3].slice(0, text_list[3].length-1) + ' = ["' + text_list.slice(6).join('","') + '"]\n'; 148 + else return tab + text_list[3].slice(0, text_list[3].length-1) + ' = ["' + text_list.slice(6).join('","') + '"]\n';
155 } 149 }
156 } 150 }
157 else return ''; 151 else return '';
158 break; 152 break;
159 case '반복문': 153 case '반복문':
160 - if (before_val == '기본'){ 154 + before_val = '반복문';
161 - ent = '\n'; 155 + if (text_list[2] == 'for'){
162 - } 156 + return tab + text_list.slice(2).join(' ') + ' ('
163 - else{
164 - ent = '';
165 } 157 }
166 - if (text_list[3] == 'for'){ 158 + else if (text_list[2] == 'while' || text_list[2] == '와일'){
167 - return ent + tab + text_list.slice(3).join(' ') + ' (' 159 + return tab + 'while (';
168 - }
169 - else if (text_list[3] == 'while' || text_list[3] == '와일'){
170 - return ent + tab + 'while (';
171 } 160 }
172 else return ''; 161 else return '';
173 break; 162 break;
174 - case '완료':
175 - tab.slice(1);
176 - return '';
177 - break;
178 case '함수': 163 case '함수':
179 - if (before_val == '기본'){ 164 + if (text_list.length >= 3){
180 - ent = '\n'; 165 + before_val = '함수'
181 - } 166 + return tab + 'def ' + text_list.slice(2).join('') + '(';
182 - else{
183 - ent = '';
184 } 167 }
185 - if (text_list.length >= 3) return 'def ' + text_list.slice(2).join('') + '(';
186 else return ''; 168 else return '';
187 case '조건문': 169 case '조건문':
188 - if (before_val == '기본'){
189 - ent = '\n';
190 - }
191 - else{
192 - ent = '';
193 - }
194 if (text_list.length >= 3){ 170 if (text_list.length >= 3){
195 - if (text_list[3] == 'if' || text_list[3] == '이프'){ 171 + before_val = '조건문';
172 + if (text_list[2] == 'if' || text_list[2] == '이프'){
196 return tab + 'if ('; 173 return tab + 'if (';
197 } 174 }
198 - else if ((text_list[3] == 'else' || text_list[3] == '엘스')){ 175 + else if ((text_list[2] == 'else' || text_list[2] == '엘스')){
199 - tab += 1; 176 + return tab + 'else:\n'
200 - return '\t'*(tab-1) + 'else:\n'
201 } 177 }
202 - else if((text_list[3] == 'else' || text_list[3] == '엘스') && (text_list[4] == 'if' || text_list[4] == '이프')){ 178 + else if((text_list[2] == 'else' || text_list[2] == '엘스') && (text_list[3] == 'if' || text_list[3] == '이프')){
203 return tab + 'elif ('; 179 return tab + 'elif (';
204 } 180 }
205 } 181 }
...@@ -207,21 +183,20 @@ async function TextToPython(trans_data) { ...@@ -207,21 +183,20 @@ async function TextToPython(trans_data) {
207 break; 183 break;
208 case '논리': 184 case '논리':
209 if (text_list.length >= 7){ 185 if (text_list.length >= 7){
210 - tab += 1;
211 if (text_list[6] == '같다'){ 186 if (text_list[6] == '같다'){
212 - return tab + text_list[3].slice(0, text_list[3].length-1) + ' == ' + text_list[4] + '):\n'; 187 + return text_list[3].slice(0, text_list[3].length-1) + ' == ' + text_list[4] + '):\n';
213 } 188 }
214 else if(text_list[6] == '크다'){ 189 else if(text_list[6] == '크다'){
215 - return tab + text_list[3].slice(0, text_list[3].length-1) + ' > ' + text_list[4] + '):\n'; 190 + return text_list[3].slice(0, text_list[3].length-1) + ' > ' + text_list[4] + '):\n';
216 } 191 }
217 else if(text_list[6] == '작다'){ 192 else if(text_list[6] == '작다'){
218 - return tab + text_list[3].slice(0, text_list[3].length-1) + ' < ' + text_list[4] + '):\n'; 193 + return text_list[3].slice(0, text_list[3].length-1) + ' < ' + text_list[4] + '):\n';
219 } 194 }
220 else if(text_list[6] == '크거나'){ 195 else if(text_list[6] == '크거나'){
221 - return tab + text_list[3].slice(0, text_list[3].length-1) + ' >= ' + text_list[4] + '):\n'; 196 + return text_list[3].slice(0, text_list[3].length-1) + ' >= ' + text_list[4] + '):\n';
222 } 197 }
223 else if(text_list[6] == '작거나'){ 198 else if(text_list[6] == '작거나'){
224 - return tab + text_list[3].slice(0, text_list[3].length-1) + ' <= ' + text_list[4] + '):\n'; 199 + return text_list[3].slice(0, text_list[3].length-1) + ' <= ' + text_list[4] + '):\n';
225 } 200 }
226 } 201 }
227 else return ''; 202 else return '';
...@@ -239,7 +214,7 @@ app.post('/upload_sound', upload.any(), async (req, res) => { ...@@ -239,7 +214,7 @@ app.post('/upload_sound', upload.any(), async (req, res) => {
239 }); 214 });
240 app.post('/ttp', upload.any(), async (req, res) => { 215 app.post('/ttp', upload.any(), async (req, res) => {
241 console.log("Getting python code.."); 216 console.log("Getting python code..");
242 - let python_code = await TextToPython(req.body.transcript_data); 217 + let python_code = await TextToPython(req.body.transcript_data, req.body.tabs);
243 console.log("Pyton code: " + python_code); 218 console.log("Pyton code: " + python_code);
244 res.status(200).send(python_code); 219 res.status(200).send(python_code);
245 }); 220 });
......