impronta48/ibmwatson

PHP SDK to get translations from IBM Watson

v1.0.2 2022-04-07 16:36 UTC

This package is auto-updated.

Last update: 2024-04-24 09:24:27 UTC


README

This library allows easy interfacing with IBM Watson tranlsation services.

Requirements

  • PHP 7.1+
  • GuzzleHttp 6+

This library is well integrated in CakePHP

Getting Started (Installation)

  1. Create a (free) account on IBM Watson (https://www.ibm.com/watson/services/language-translator/)
  2. Get your API KEY and your URL
  3. Include the library in your project with composer
    composer require impronta48/IBMWatson
  1. Use the library in your project

Usage

This library offers 4 functionalities

Translate a set of sentences

<?php
declare(strict_types=1);

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use impronta48\IBMWatson;

  $w = new IBMWatson($this->apikey, $this->url);
  $resJson = $w->translateSentence('hello', 'en', 'it');
  $resJson = $w->translateSentence(['hello','goodbye'], 'en', 'it');

  //Convert the Json String in a Object
  $res = json_decode($resJson);

  echo $res->translations[0]->translation;
  // returns ciao
  echo $res->word_count;
  // returns 1
  echo $res->character_count;
  // returns 5

Identify the language of a string

  $w = new IBMWatson($this->apikey, $this->url);
  $lang = $w->identifyLanguage('Mi chiamo Massimo e scrivo molto bene in italiano');
  echo $lang;
  // returns 'it'

Tranlsate a full file

  $w = new impronta48\IBMWatson($this->apikey, $this->url);
  $inputFile = __DIR__ . '/test_it.docx';
  $outputFile = __DIR__ . '/test_en.docx';
  $translatedDoc = $w->translateDoc($inputFile, 'it', 'en');
  $dest = fopen($outputFile, 'w');
  stream_copy_to_stream($translatedDoc, $dest);

Support

Massimo INFUNTI http://impronta48.it/