bluejoyq

Merge branch 'changshin' into 'master'

Changshin

Add machineRead.js
Add STT.js

See merge request !8
1 +const apiRequest = require('./apiRequest');
2 +const isBase64 = require('is-base64');
3 +//const Lame = require("node-lame").Lame;
4 +const fs = require("fs");
5 +//const Mp32Wav = require('mp3-to-wav')
6 +
7 +
8 +/**
9 + * @param {Object} clientData 클라이언트에서 보낸 데이터
10 + * @param {String} clientData.audio base64로 인코딩된 음성 데이터 16hz로 샘플링 되야함
11 + * @returns {text:String} 음성을 인식한 결과
12 + * @description 음성 인식을 요청해 결과를 내놓는 함수이다.
13 + */
14 +
15 +
16 +const apiReq = async ( clientData ) => {
17 + fs.writeFileSync('./audio/audio_input.mp3', Buffer.from(clientData.audio.replace('data:audio/mp3; codecs=opus;base64,', ''), 'base64'));
18 + const mp32Wav = new Mp32Wav('./audio/audio_input.mp3')
19 + mp32Wav.saveForWav(Buffer.from(clientData.audio.replace('data:audio/mp3; codecs=opus;base64,', ''), 'base64'),'./audio/audio_input.wav',_,16000,1)
20 + //let audiodata = fs.readFileSync('./audio/audio_input.mp3').toString('base64')
21 + console.log(Buffer(clientData.audio,'base64'))
22 +
23 +
24 + let getSTT = {};
25 + try {
26 + getSTT = await apiRequest.ETRI( "WiseASR/Recognition", { "language_code" : "korean", "audio" : audiodata } );
27 + }
28 + catch ( err ) {
29 + throw new Error ( err.message );
30 + }
31 +
32 + return { "text" : getSTT.return_object.recognized };
33 +}
34 +
35 +/**
36 + * @param req request
37 + * @param req.bdoy.data req.body.data.audio에 요청하는 데이터가 들어있어야한다
38 + * @description 오디로를 텍스트로 바꿔준다.
39 + */
40 +const STT = async ( req, res ) => {
41 + let clientData,
42 + voiceTemp;
43 +
44 + try {
45 + clientData = req.body.data
46 + if( !clientData.audio.length ) {
47 + throw new Error( "client audio empty" );
48 + }
49 + else if( !isBase64( clientData.audio ) ) {
50 + throw new Error( "Type error : audio type should be base64" );
51 + }
52 + }
53 + catch( err ) {
54 + console.log( err );
55 + res.json( { "return_code" : -1, "error_code" : err.message } );
56 + res.status( 403 );
57 + return false;
58 + }
59 +
60 + try {
61 + voiceTemp = await apiReq( clientData );
62 + }
63 + catch( err ) {
64 + console.log( err );
65 + res.json( { "return_code" : -1, "error_code" : err.message } );
66 + res.status( 502 );
67 + return false;
68 + }
69 +
70 + res.send( { "return_code" : 0, "return_data" : voiceTemp } );
71 + res.status( 200 );
72 +};
73 +
74 +module.exports=STT;
...\ No newline at end of file ...\ No newline at end of file
1 +const apiRequest = require('./apiRequest');
2 +
3 +/**
4 + * @param {{url:string,title:string,passage:string}[]} searchResults 검색 결과 정리한것.
5 + * @param {string} keywordText 기계독해 시킬 질문의 텍스트
6 + * @returns {{url:string,title:string,passage:string,confidence:number}[]} searchResults의 각 object에 confidence 속성을 추가해 돌려준다.
7 + * @description 기계독해를 사용하는 함수
8 + */
9 +const machineRead = async ( searchResults, keywordText ) => {
10 + let keyNum = 6,
11 + ResultArray = [],
12 + divideSearchResults = [];
13 +
14 + for( let i = 0; i <= searchResults.length; i += keyNum ) {
15 + divideSearchResults.push( searchResults.slice( i, i + keyNum ) );
16 + }
17 +
18 + for( let divideSearchResult of divideSearchResults ) {
19 + try{
20 + await apiRequest.multiETRI( divideSearchResult, keywordText );
21 + }
22 + catch ( err ) {
23 + throw new Error( err.message );
24 + }
25 + ResultArray = ResultArray.concat( divideSearchResult );
26 + }
27 +
28 + searchResults = ResultArray;
29 + return searchResults;
30 +}
31 +
32 +module.exports = machineRead;