PHP Classes
elePHPant
Icontem

PHPolyglot: Translate, correct spelling and speak a given text

Recommend this page to a friend!
  Info   View files Example   View files View files (95)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2018-02-14 (1 hour ago) RSS 2.0 feedNot yet rated by the usersTotal: 72 This week: 72All time: 9,005 This week: 8Up
Version License PHP version Categories
phpolyglot 1.1.0MIT/X Consortium ...7.1Localization, Text processing, Web se..., P...
Description Author

This package can translate, correct spelling and speak a given text.

It sends HTTP request to different API Web servers to perform requests to services that can take a text and process it in several ways. Currently it can:

- Translate it to another language
- Check the spelling looking at a dictionary
- Suggest corrections to the text
- Convert the text to speech (TTS)

Recommendations

Happy Birthday Song of your name with background music
I want to make a mp3 file which will wish you for your birthday.

  Performance   Level  
Name: Sergey Karavay <contact>
Classes: 3 packages by
Country: Belarus Belarus
Innovation award
Innovation award
Nominee: 1x

Details

PHPolyglot

Latest Stable Version Build Status Maintainability Test Coverage Scrutinizer Code Quality License Total Downloads

Combining and featuring different APIs for language translation, dictionary lookup, spelling correction and speech synthesis (TTS) in an easy to use and extend way.

Table of Contents

Features

  • provides an easy-to-use way to utilise different language-related APIs for translation, grammar correction, TTS, etc.;
  • custom APIs can be easily added, because the package heavily relies on implementation of different interfaces, therefore it is easy to plug-in (pull requests are appreciated);
  • open or free (possibly with limitations) APIs are preferred;
  • language codes must be ISO-639 compatible (alpha-2 or alpha-3 if there's no alpha-2);
  • third-party APIs may contain their own limitations or licensing requirements (see License)

Requirements

  • PHP >= 7.1;
  • credentials for Yandex Translate API, Yandex Dictionary API and IBM Watson API (depending on what you are going to use).

Installation

composer require gino-pane/phpolyglot

Create a copy of .env.example file, name it .env and put your own API credentials in it. File contains links to pages which may be related to required credentials.

> In order to run examples from examples directory you have to specify your own valid API credentials.


Basic Usage

The package contains a plenty of ready-to-use examples in examples directory. All endpoints either return a valid response or throws a relevant exception. All APIs are configured through config.php file which contains the default API classes mapping. Support of dynamic configs was added in 1.1.0 update:

$phpolyglot = new PHPolyglot($config, $env);

This allows you to pass your own configuration values if you don't want to rely on those that are stored in configuration files.

Translation

There are two endpoints. For a single string:

function translate(string $text, string $languageTo, string $languageFrom = ''): TranslateResponse

and for multiple strings:

function translateBulk(array $text, string $languageTo, string $languageFrom = ''): TranslateResponse

As a minimum example you can pass text and language to translate into (source language will be detected by API):

$response = (new PHPolyglot())->translate('Hello world', 'it')->getTranslations(); // [ 0 => Ciao mondo ]

TranslateResponse has getTranslations method which returns an array of translations.

Supported languages may vary depending on third-party API.

Yandex Translate API

Please check the list of supported languages. Yandex Translate API is free to use with limitations (1000 000 characters per day, up to 10 000 000 per month). If you want you can get a paid plan of course. The API won't let you to get into paid plan automatically, it will simply return an error when the limit is reached. In order to use the API you need to get the valid API key.

Dictionary Lookup

There is a single endpoint, which can be used in two different forms.

For a lookup within the same language (get word forms):

function lookup(string $text, string $languageFrom): DictionaryResponse

and for translation-with-lookup (get multiple translations and additional information including word forms, examples, meanings, synonyms, transcription, etc.):

function lookup(string $text, string $languageFrom, string $languageTo): DictionaryResponse

As a minimum example you can pass text and its source language:

$response = (new PHPolyglot)->lookup('Hello', 'en)->getEntries();

$synonyms = implode(", ", $response[0]->getSynonyms());

$output = <<<TEXT
Initial word: {$response[0]->getTextFrom()}

Part of speech: {$response[0]->getPosFrom()}
Transcription: {$response[0]->getTranscription()}

Main alternative: {$response[0]->getTextTo()}
Synonyms: {$synonyms}
TEXT

echo $output

/
Initial word: hello
  
Part of speech: noun
Transcription: ?he?l??

Main alternative: hi
Synonyms: hallo, salut
*/

Supported languages may vary depending on third-party API.

Yandex Dictionary API

Please check the list of supported languages. Yandex Dictionary API is free to use with limitations (up to 10 000 references per day). In order to use the API you need to get the valid API key.

Spelling Check

There are two endpoints. For a single string:

function spellCheck(string $text, string $languageFrom = ''): SpellCheckResponse

and for multiple strings:

function spellCheckBulk(array $texts, string $languageFrom = ''): SpellCheckResponse

As a minimum example you can pass only a text to check:

$corrections = $phpolyglot->spellCheckText('Helo werld', $languageFrom)->getCorrections();

/
array(1) {
  [0] =>
  array(2) {
    'Helo' =>
    array(1) {
      [0] =>
      string(5) "Hello"
    }
    'werld' =>
    array(1) {
      [0] =>
      string(5) "world"
    }
  }
}
*/

Supported languages may vary depending on third-party API.

Yandex Speller API

Please check the list of supported languages (basically, only English, Russian and Ukrainian are supported at the moment). Yandex Speller API is free to use with limitations (up to 10 000 calls/10 000 000 characters per day). No keys are required.

Speech Synthesis

The main endpoint is PHPolyglot's speak method:

public function speak(
    string $text,
    string $languageFrom,
    string $audioFormat = TtsAudioFormat::AUDIO_MP3,
    array $additionalData = []
): TtsResponse

Only two parameters are required - text for synthesis $text and its source language $languageFrom.

Optional parameters $audioFormat and $additionalData may be omitted. Audio format allows to explicitly specify the required audio format of returned audio. Additional data allows to set API specific parameters for more precise results (voice, pitch, speed, etc.).

The list of audio formats which are currently recognized:

  • TtsAudioFormat::AUDIO_BASIC
  • TtsAudioFormat::AUDIO_FLAC
  • TtsAudioFormat::AUDIO_L16
  • TtsAudioFormat::AUDIO_MP3
  • TtsAudioFormat::AUDIO_MPEG
  • TtsAudioFormat::AUDIO_MULAW
  • TtsAudioFormat::AUDIO_OGG
  • TtsAudioFormat::AUDIO_WAV
  • TtsAudioFormat::AUDIO_WEBM

Please note that not all of them may be supported by your API of choice.

The TTS method returns TtsResponse which has storeFile method to store generated file with required name and extension into the specified directory (or by using default values):

function storeFile(string $fileName = '', string $extension = '', string $directory = ''): string

By default the file name is a simple md5 hash of $text that was used for TTS, $extension is being populated based on content-type header (at least, for IBM Watson API), $directory is based on config setting.

(new PHPolyglot())->speak('Hello world', 'en')->storeFile(); // stores 3e25960a79dbc69b674cd4ec67a72c62.mp3

IBM Watson Text-to-Speech

Please check the list of supported languages and voices. IBM Watson TTS requires API credentials for authorization. Create your TTS project there and get your API-specific credentials. API is free to use with limitations (up to 10 000 characters per month).

Possible ToDos

  • transcribe words;
  • get synonyms, antonyms, derivatives;
  • detect text language;
  • add more configuration flexibility (choose API based on config constraints, like different APIs for different languages).

Useful Tools

Running Tests:

php vendor/bin/phpunit

or

composer test

Code Sniffer Tool:

php vendor/bin/phpcs --standard=PSR2 src/

or

composer psr2check

Code Auto-fixer:

php vendor/bin/phpcbf --standard=PSR2 src/ 

or

composer psr2autofix

Building Docs:

php vendor/bin/phpdoc -d "src" -t "docs"

or

composer docs

Changelog

To keep track, please refer to CHANGELOG.md.

Contributing

  1. Fork it;
  2. Create your feature branch (git checkout -b my-new-feature);
  3. Make your changes;
  4. Run the tests, adding new ones for your own code if necessary (phpunit);
  5. Commit your changes (git commit -am 'Added some feature');
  6. Push to the branch (git push origin my-new-feature);
  7. Create new pull request.

Also please refer to CONTRIBUTING.md.

License

Please refer to LICENSE. > The PHPolyglot does not own any of results that APIs may return. Also, APIs may have their own rules about data usage, so beware of them when you use them.

Notes

Powered by composer-package-template and PHP Nano Rest.

  Files folder image Files  
File Role Description
Files folder imageexamples (6 files)
Files folder imagesrc (1 file, 3 directories)
Files folder imagetests (2 files, 2 directories)
Accessible without login Plain text file .env.example Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file CODE_OF_CONDUCT.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Plain text file config.php Class Class source
Accessible without login Plain text file CONTRIBUTING.md Data Auxiliary data
Accessible without login Plain text file issue_template.md Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file PULL_REQUEST_TEMPLATE.md Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  examples  
File Role Description
  Accessible without login Plain text file lookup-translate.php Example Example script
  Accessible without login Plain text file lookup.php Example Example script
  Accessible without login Plain text file speak.php Example Example script
  Accessible without login Plain text file spellcheck.php Example Example script
  Accessible without login Plain text file translate-bulk.php Example Example script
  Accessible without login Plain text file translate.php Example Example script

  Files folder image Files  /  src  
File Role Description
Files folder imageAPI (4 directories)
Files folder imageException (16 files)
Files folder imageSupplemental (2 files, 1 directory)
  Plain text file PHPolyglot.php Class Class source

  Files folder image Files  /  src  /  API  
File Role Description
Files folder imageFactory (2 files, 4 directories)
Files folder imageImplementation (1 file, 4 directories)
Files folder imageResponse (2 files, 4 directories)
Files folder imageSupplemental (2 directories)

  Files folder image Files  /  src  /  API  /  Factory  
File Role Description
Files folder imageDictionary (1 file)
Files folder imageSpellCheck (1 file)
Files folder imageTranslate (1 file)
Files folder imageTTS (1 file)
  Plain text file ApiFactoryAbstract.php Class Class source
  Plain text file ApiFactoryInterface.php Class Class source

  Files folder image Files  /  src  /  API  /  Factory  /  Dictionary  
File Role Description
  Plain text file DictionaryApiFactory.php Class Class source

  Files folder image Files  /  src  /  API  /  Factory  /  SpellCheck  
File Role Description
  Plain text file SpellCheckApiFactory.php Class Class source

  Files folder image Files  /  src  /  API  /  Factory  /  Translate  
File Role Description
  Plain text file TranslateApiFactory.php Class Class source

  Files folder image Files  /  src  /  API  /  Factory  /  TTS  
File Role Description
  Plain text file TtsApiFactory.php Class Class source

  Files folder image Files  /  src  /  API  /  Implementation  
File Role Description
Files folder imageDictionary (2 files, 1 directory)
Files folder imageSpellCheck (2 files, 1 directory)
Files folder imageTranslate (2 files, 1 directory)
Files folder imageTTS (2 files, 1 directory)
  Plain text file ApiAbstract.php Class Class source

  Files folder image Files  /  src  /  API  /  Implementation  /  Dictionary  
File Role Description
Files folder imageYandex (1 file)
  Plain text file DictionaryApiAbstract.php Class Class source
  Plain text file DictionaryApiInterface.php Class Class source

  Files folder image Files  /  src  /  API  /  Implementation  /  Dictionary  /  Yandex  
File Role Description
  Plain text file YandexDictionaryApi.php Class Class source

  Files folder image Files  /  src  /  API  /  Implementation  /  SpellCheck  
File Role Description
Files folder imageYandex (1 file)
  Plain text file SpellCheckApiAbstract.php Class Class source
  Plain text file SpellCheckApiInterface.php Class Class source

  Files folder image Files  /  src  /  API  /  Implementation  /  SpellCheck  /  Yandex  
File Role Description
  Plain text file YandexSpellCheckApi.php Class Class source

  Files folder image Files  /  src  /  API  /  Implementation  /  Translate  
File Role Description
Files folder imageYandex (1 file)
  Plain text file TranslateApiAbstract.php Class Class source
  Plain text file TranslateApiInterface.php Class Class source

  Files folder image Files  /  src  /  API  /  Implementation  /  Translate  /  Yandex  
File Role Description
  Plain text file YandexTranslateApi.php Class Class source

  Files folder image Files  /  src  /  API  /  Implementation  /  TTS  
File Role Description
Files folder imageIbmWatson (1 file, 2 directories)
  Plain text file TtsApiAbstract.php Class Class source
  Plain text file TtsApiInterface.php Class Class source

  Files folder image Files  /  src  /  API  /  Implementation  /  TTS  /  IbmWatson  
File Role Description
Files folder imageAudioFormat (2 files)
Files folder imageVoice (2 files)
  Plain text file IbmWatsonTtsApi.php Class Class source

  Files folder image Files  /  src  /  API  /  Implementation  /  TTS  /  IbmWatson  /  AudioFormat  
File Role Description
  Plain text file IbmWatsonAudioFormatsInterface.php Class Class source
  Plain text file IbmWatsonAudioFormatsTrait.php Class Class source

  Files folder image Files  /  src  /  API  /  Implementation  /  TTS  /  IbmWatson  /  Voice  
File Role Description
  Plain text file IbmWatsonVoicesInterface.php Class Class source
  Plain text file IbmWatsonVoicesTrait.php Class Class source

  Files folder image Files  /  src  /  API  /  Response  
File Role Description
Files folder imageDictionary (1 file, 1 directory)
Files folder imageSpellCheck (1 file)
Files folder imageTranslate (1 file)
Files folder imageTTS (1 file)
  Plain text file ApiResponseAbstract.php Class Class source
  Plain text file ApiResponseInterface.php Class Class source

  Files folder image Files  /  src  /  API  /  Response  /  Dictionary  
File Role Description
Files folder imageEntry (1 file, 1 directory)
  Plain text file DictionaryResponse.php Class Class source

  Files folder image Files  /  src  /  API  /  Response  /  Dictionary  /  Entry  
File Role Description
Files folder imagePOS (1 file)
  Plain text file DictionaryEntry.php Class Class source

  Files folder image Files  /  src  /  API  /  Response  /  Dictionary  /  Entry  /  POS  
File Role Description
  Plain text file DictionaryEntryPos.php Class Class source

  Files folder image Files  /  src  /  API  /  Response  /  SpellCheck  
File Role Description
  Plain text file SpellCheckResponse.php Class Class source

  Files folder image Files  /  src  /  API  /  Response  /  Translate  
File Role Description
  Plain text file TranslateResponse.php Class Class source

  Files folder image Files  /  src  /  API  /  Response  /  TTS  
File Role Description
  Plain text file TtsResponse.php Class Class source

  Files folder image Files  /  src  /  API  /  Supplemental  
File Role Description
Files folder imageTTS (2 files)
Files folder imageYandex (2 files)

  Files folder image Files  /  src  /  API  /  Supplemental  /  TTS  
File Role Description
  Plain text file TtsAudioFormat.php Class Class source
  Plain text file TtsVoiceFormat.php Class Class source

  Files folder image Files  /  src  /  API  /  Supplemental  /  Yandex  
File Role Description
  Plain text file YandexApiStatusesInterface.php Class Class source
  Plain text file YandexApiTrait.php Class Class source

  Files folder image Files  /  src  /  Exception  
File Role Description
  Plain text file BadResponseContextException.php Class Class source
  Plain text file InvalidApiClassException.php Class Class source
  Plain text file InvalidAudioFormatCodeException.php Class Class source
  Plain text file InvalidAudioFormatParameterException.php Class Class source
  Plain text file InvalidConfigException.php Class Class source
  Plain text file InvalidContentTypeException.php Class Class source
  Plain text file InvalidEnvironmentException.php Class Class source
  Plain text file InvalidGenderCodeException.php Class Class source
  Plain text file InvalidIoException.php Class Class source
  Plain text file InvalidLanguageCodeException.php Class Class source
  Plain text file InvalidPathException.php Class Class source
  Plain text file InvalidPropertyException.php Class Class source
  Plain text file InvalidResponseContent.php Class Class source
  Plain text file InvalidVoiceCodeException.php Class Class source
  Plain text file InvalidVoiceParametersException.php Class Class source
  Plain text file MethodDoesNotExistException.php Class Class source

  Files folder image Files  /  src  /  Supplemental  
File Role Description
Files folder imageLanguage (2 files)
  Plain text file GetApiInstancesTrait.php Class Class source
  Plain text file GetConstantsTrait.php Class Class source

  Files folder image Files  /  src  /  Supplemental  /  Language  
File Role Description
  Plain text file Language.php Class Class source
  Plain text file LanguageInterface.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageconfigs (5 files)
Files folder imagesuites (3 directories)
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script
  Plain text file PHPolyglotTestCase.php Class Class source

  Files folder image Files  /  tests  /  configs  
File Role Description
  Accessible without login Plain text file invalid1.config.php Aux. Auxiliary script
  Plain text file invalid2.config.php Class Class source
  Plain text file invalid3.config.php Class Class source
  Plain text file test.config.php Class Class source
  Accessible without login Plain text file test.env Data Auxiliary data

  Files folder image Files  /  tests  /  suites  
File Role Description
Files folder imagegeneral (1 file)
Files folder imagespecific (7 files)
Files folder imagesupplemental (5 files)

  Files folder image Files  /  tests  /  suites  /  general  
File Role Description
  Plain text file PHPolyglotTest.php Class Class source

  Files folder image Files  /  tests  /  suites  /  specific  
File Role Description
  Plain text file DictionaryApiFactoryTest.php Class Class source
  Plain text file IbmWatsonTtsApiTest.php Class Class source
  Plain text file SpellCheckApiTest.php Class Class source
  Plain text file TranslateApiFactoryTest.php Class Class source
  Plain text file TtsApiFactoryTest.php Class Class source
  Plain text file YandexDictionaryApiTest.php Class Class source
  Plain text file YandexTranslateApiTest.php Class Class source

  Files folder image Files  /  tests  /  suites  /  supplemental  
File Role Description
  Plain text file DictionaryEntryPosTest.php Class Class source
  Plain text file DictionaryEntryTest.php Class Class source
  Plain text file LanguageTest.php Class Class source
  Plain text file TtsAudioFormatTest.php Class Class source
  Plain text file TtsVoiceFormatTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:72
This week:72
All time:9,005
This week:8Up