diff --git a/dlp/composer.json b/dlp/composer.json index f84ff3cd07..8412486102 100644 --- a/dlp/composer.json +++ b/dlp/composer.json @@ -3,6 +3,7 @@ "type": "project", "require": { "google/cloud-dlp": "^0.26.1", - "google/cloud-pubsub": "^1.11.1" + "google/cloud-pubsub": "^1.11.1", + "ext-json": "*" } } diff --git a/speech/src/transcribe_enhanced_model.php b/speech/src/transcribe_enhanced_model.php index 5fdd6ee3e9..444ca2d801 100644 --- a/speech/src/transcribe_enhanced_model.php +++ b/speech/src/transcribe_enhanced_model.php @@ -41,7 +41,7 @@ // change these variables if necessary $encoding = AudioEncoding::LINEAR16; $sampleRateHertz = 8000; -$languageCode = 'en-US'; +$languageCode = 'ar-AR'; // get contents of a file into a string $content = file_get_contents($audioFile); diff --git a/speech/src/transcribe_model_selection.php b/speech/src/transcribe_model_selection.php index 39d1401855..de9a8ba8ac 100644 --- a/speech/src/transcribe_model_selection.php +++ b/speech/src/transcribe_model_selection.php @@ -41,8 +41,8 @@ // change these variables if necessary $encoding = AudioEncoding::LINEAR16; -$sampleRateHertz = 32000; -$languageCode = 'en-US'; +$sampleRateHertz = 8000; +$languageCode = 'ar-AR'; // get contents of a file into a string $content = file_get_contents($audioFile); diff --git a/speech/src/transcribe_sync.php b/speech/src/transcribe_sync.php index 0e01cbbe00..ad91d219cd 100644 --- a/speech/src/transcribe_sync.php +++ b/speech/src/transcribe_sync.php @@ -22,12 +22,13 @@ */ // Include Google Cloud dependendencies using Composer +header('Content-Type: application/json'); require_once __DIR__ . '/../vendor/autoload.php'; - -if (count($argv) != 2) { - return print("Usage: php transcribe_sync.php AUDIO_FILE\n"); -} -list($_, $audioFile) = $argv; +putenv('GOOGLE_APPLICATION_CREDENTIALS=/var/www/html/php-docs-samples/speech/src/cred.json'); +// if (count($argv) != 2) { +// return print("Usage: php transcribe_sync.php AUDIO_FILE\n"); +// } +// list($_, $audioFile) = $argv; # [START speech_transcribe_sync] use Google\Cloud\Speech\V1\SpeechClient; @@ -40,11 +41,12 @@ // change these variables if necessary $encoding = AudioEncoding::LINEAR16; -$sampleRateHertz = 32000; -$languageCode = 'en-US'; +$sampleRateHertz = 8000; +$languageCode = 'ar-EG'; // get contents of a file into a string -$content = file_get_contents($audioFile); +// $content = $_POST['file']; +$content = file_get_contents("php://input"); // set string as audio content $audio = (new RecognitionAudio()) @@ -58,18 +60,64 @@ // create the speech client $client = new SpeechClient(); - +/* + * [0] => stdClass Object + ( + [alternatives] => Array + ( + [0] => stdClass Object + ( + [confidence] => 0.63 + [transcript] => yes + ) + ) + [final] => 1 + ) +) + */ try { $response = $client->recognize($config, $audio); + // $results = array(); + foreach ($response->getResults() as $result) { $alternatives = $result->getAlternatives(); $mostLikely = $alternatives[0]; $transcript = $mostLikely->getTranscript(); $confidence = $mostLikely->getConfidence(); - printf('Transcript: %s' . PHP_EOL, $transcript); - printf('Confidence: %s' . PHP_EOL, $confidence); + $results = new stdClass(); + $results->alternatives = Array(); + $results->alternatives[0] = new StdClass(); + $results->alternatives[0]->confidence = $confidence; + $results->alternatives[0]->transcript = htmlentities((string)$transcript); + // $results[] = $resultx; + // printf('Transcript: %s' . PHP_EOL, $transcript); + //$result = Array(); + // $result = htmlentities((string)$transcript); + // echo '{result: "'.$result.'"}'; + // echo json_encode($result); + // break; + // printf('Confidence: %s' . PHP_EOL, $confidence); } } finally { $client->close(); } +// print_r($results); +// $resultsx = new StdClass(); +// $obj = new StdClass(); +// $obj->results[] = $results; +// $resultsx[] = $obj; + +$resultsx = array(); +$resultsx[] = $results; +$resultsy = new StdClass(); +$resultsy->results = $resultsx; + + +$json = json_encode($resultsy, JSON_UNESCAPED_UNICODE); +echo $json; +// echo html_entity_decode($json); +// echo html_entity_decode((string)$json); +// echo html_entity_decode(utf8_decode($json)); +// $output = preg_replace_callback("/(&#[0-9]+;)/", function($m) { return mb_convert_encoding($m[1], "UTF-8", "HTML-ENTITIES"); }, $json); +// echo "\n".$output; # [END speech_transcribe_sync]