-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogleapi.php
74 lines (65 loc) · 2.85 KB
/
googleapi.php
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
<?php
include_once 'config.php';
class GoogleApi {
static $instance = null;
var $cmdtoken = "gcloud auth application-default print-access-token";
var $token;
var $hostAPI = "https://speech.googleapis.com/v1beta1/speech:syncrecognize";
static function & getInstance() {
if (null == GoogleApi::$instance) {
GoogleApi::$instance = new GoogleApi();
}
return GoogleApi::$instance;
}
function GoogleApi() {
$this->token = substr(shell_exec($this->cmdtoken), 0, -1);
}
function getJsonRequest($encoded_audio) {
$data = array("config" => array(
"encoding" => "FLAC",
"sampleRate" => 16000,
"languageCode" => "es-ES",
), "audio" => array(
"content" => $encoded_audio
)
);
$response = json_encode($data);
return $response;
}
function do_request($data) {
$error = false;
$ERROR_respuesta = "ERROR, ";
$ch = curl_init($this->hostAPI);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data),
'Authorization: Bearer '.$this->token)
);
$result = curl_exec($ch);
//file_put_contents("/home/jlotito/repositories/git/speech_recog/json/response.json", $result);
$array_result = json_decode($result);
if (isset($array_result->results[0]->alternatives[0]->confidence)){
$confidence = $array_result->results[0]->alternatives[0]->confidence;
if ($confidence>0.60){
$transcript = $array_result->results[0]->alternatives[0]->transcript;
} else {
$error = true;
$ERROR_respuesta .= "COINCIDENCIA NO SUPERIOR AL 60%";
}
} else {
$transcript = $array_result->results[0]->alternatives[0]->transcript;
}
$response['error'] = $error;
if ($error){
$response['error_msg'] = $ERROR_respuesta;
} else {
$response['transcript'] = $transcript;
}
return $response;
}
}
?>