1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GinoPane\PHPolyglot; |
4
|
|
|
|
5
|
|
|
use GinoPane\PHPolyglot\API\Response\TTS\TtsResponse; |
6
|
|
|
use GinoPane\PHPolyglot\Supplemental\GetApiInstancesTrait; |
7
|
|
|
use GinoPane\PHPolyglot\Supplemental\Language\Language; |
8
|
|
|
use GinoPane\PHPolyglot\API\Supplemental\TTS\TtsAudioFormat; |
9
|
|
|
use GinoPane\PHPolyglot\API\Response\Translate\TranslateResponse; |
10
|
|
|
use GinoPane\PHPolyglot\API\Response\Dictionary\DictionaryResponse; |
11
|
|
|
use GinoPane\PHPolyglot\API\Response\SpellCheck\SpellCheckResponse; |
12
|
|
|
|
13
|
|
|
define(__NAMESPACE__ . '\ROOT_DIRECTORY', dirname(__FILE__)); |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* PHPolyglot |
17
|
|
|
* Easily translate, do spell check and speak-out texts in different languages |
18
|
|
|
* |
19
|
|
|
* @author Sergey <Gino Pane> Karavay |
20
|
|
|
*/ |
21
|
|
|
class PHPolyglot |
22
|
|
|
{ |
23
|
|
|
use GetApiInstancesTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $text |
27
|
|
|
* @param string $languageTo |
28
|
|
|
* @param string $languageFrom |
29
|
|
|
* |
30
|
|
|
* @return TranslateResponse |
31
|
|
|
*/ |
32
|
|
|
public function translate(string $text, string $languageTo, string $languageFrom = ''): TranslateResponse |
33
|
|
|
{ |
34
|
|
|
return $this->getTranslateApi()->translate($text, new Language($languageTo), new Language($languageFrom)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array $text |
39
|
|
|
* @param string $languageTo |
40
|
|
|
* @param string $languageFrom |
41
|
|
|
* |
42
|
|
|
* @return TranslateResponse |
43
|
|
|
*/ |
44
|
|
|
public function translateBulk(array $text, string $languageTo, string $languageFrom = ''): TranslateResponse |
45
|
|
|
{ |
46
|
|
|
return $this->getTranslateApi()->translateBulk($text, new Language($languageTo), new Language($languageFrom)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The most common use of `lookup` is look up of the word in the same language, that's |
51
|
|
|
* why the first language parameter of `lookup` method is language-from, language-to is optional, |
52
|
|
|
* which differs from the language parameters order for translation |
53
|
|
|
* |
54
|
|
|
* @param string $text |
55
|
|
|
* @param string $languageFrom |
56
|
|
|
* @param string $languageTo |
57
|
|
|
* |
58
|
|
|
* @return DictionaryResponse |
59
|
|
|
*/ |
60
|
|
|
public function lookup(string $text, string $languageFrom, string $languageTo = ''): DictionaryResponse |
61
|
|
|
{ |
62
|
|
|
if ($languageTo) { |
63
|
|
|
$response = $this->getDictionaryApi()->getTranslateAlternatives( |
64
|
|
|
$text, //@codeCoverageIgnore |
65
|
|
|
new Language($languageTo), |
66
|
|
|
new Language($languageFrom) |
67
|
|
|
); |
68
|
|
|
} else { |
69
|
|
|
$response = $this->getDictionaryApi()->getTextAlternatives($text, new Language($languageFrom)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $response; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $text |
77
|
|
|
* @param string $languageFrom |
78
|
|
|
* @param string $audioFormat |
79
|
|
|
* @param array $additionalData |
80
|
|
|
* |
81
|
|
|
* @return TtsResponse |
82
|
|
|
*/ |
83
|
|
|
public function speak( |
84
|
|
|
string $text, |
85
|
|
|
string $languageFrom, |
86
|
|
|
string $audioFormat = TtsAudioFormat::AUDIO_MP3, |
87
|
|
|
array $additionalData = [] |
88
|
|
|
): TtsResponse { |
89
|
|
|
$languageFrom = new Language($languageFrom); |
90
|
|
|
|
91
|
|
|
return $this |
92
|
|
|
->getTtsApi($additionalData) |
93
|
|
|
->textToSpeech($text, new Language($languageFrom), new TtsAudioFormat($audioFormat), $additionalData); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $text |
98
|
|
|
* @param string $languageFrom |
99
|
|
|
* |
100
|
|
|
* @return SpellCheckResponse |
101
|
|
|
*/ |
102
|
|
|
public function spellCheck(string $text, string $languageFrom = ''): SpellCheckResponse |
103
|
|
|
{ |
104
|
|
|
return $this->getSpellCheckApi()->checkTexts([$text], new Language($languageFrom)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param array $texts |
109
|
|
|
* @param string $languageFrom |
110
|
|
|
* |
111
|
|
|
* @return SpellCheckResponse |
112
|
|
|
*/ |
113
|
|
|
public function spellCheckBulk(array $texts, string $languageFrom = ''): SpellCheckResponse |
114
|
|
|
{ |
115
|
|
|
return $this->getSpellCheckApi()->checkTexts($texts, new Language($languageFrom)); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|