-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessLanguageDetection.php
73 lines (63 loc) · 2.01 KB
/
ProcessLanguageDetection.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
set_time_limit(0);
ob_implicit_flush(true);
ob_end_flush();
/**
* Created by PhpStorm.
* User: enteng
* Date: 10/13/2018
* Time: 7:14 PM
*/
class ProcessLanguageDetection
{
public static function detectLanguage()
{
$execDirectory = 'text_files';
if (is_dir($execDirectory)) {
if ($dh = opendir($execDirectory)) {
while (($execFile = readdir($dh)) !== false) {
$fileType = substr(strrchr($execDirectory . "/" . $execFile, "."), 1);
if ($fileType === "txt") {
$lines = file($execDirectory . "/" . $execFile, FILE_IGNORE_NEW_LINES);
$file = fopen("translated_csv/" . str_replace($fileType, 'csv', $execFile),"w");
foreach ($lines as $line) {
try {
echo 'Word: ' . $line;
$ch = curl_init("https://cxl-services.appspot.com/proxy?url=https://translation.googleapis.com/language/translate/v2/detect?" . http_build_query(array('q'=>$line)));
if ($ch === false) {
throw new Exception('failed to initialize');
}
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
// Check the return value of curl_exec(), too
if ($output === false) {
throw new Exception(curl_error($ch), curl_errno($ch));
} else {
if ($output === 'Service Unavailable') {
//do sleep here
sleep(320);
//do request again
$output = curl_exec($ch);
}
}
curl_close($ch);
$decoded = json_decode($output, true);
$language = $decoded['data']['detections'][0][0]['language'];
echo ' Language: ' . $language . '<br>';
$lineToWrite = '"' . $line . '","' . $language .'"' . PHP_EOL;
fwrite($file,$lineToWrite);
sleep(rand(.5,3));
} catch (Exception $e) {
echo 'Exception: ' . $e->getMessage();
}
}
fclose($file);
}
}
}
closedir($dh);
}
}
} //ProcessLanguageDetection
ProcessLanguageDetection::detectLanguage();