impronta48 / ibmwatson
从 IBM Watson 获取翻译的 PHP SDK
v1.0.3
2024-09-10 16:52 UTC
Requires
- php: >=7.1
- guzzlehttp/guzzle: >6.5
Requires (Dev)
- cakephp/cakephp-codesniffer: ^3.0
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^8.0
This package is auto-updated.
Last update: 2024-09-10 16:53:27 UTC
README
此库允许轻松与 IBM Watson 翻译服务进行交互。
要求
- PHP 7.1+
- GuzzleHttp 6+
此库与 CakePHP 集成良好
入门(安装)
- 在 IBM Watson(https://www.ibm.com/watson/services/language-translator/)上创建(免费)账户
- 获取您的 API 密钥 和您的 URL
- 使用 composer 将库包含到您的项目中
composer require impronta48/IBMWatson
- 在您的项目中使用库
用法
此库提供 4 种功能
- 翻译一组句子
- 识别文本的语言
- 翻译完整文档(如 Word、ODT、Excel、PowerPoint、PDF 等)- 有关完整列表,请参阅此处 https://cloud.ibm.com/docs/services/language-translator/translating-documents.html
翻译一组句子
<?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
识别字符串的语言
$w = new IBMWatson($this->apikey, $this->url); $lang = $w->identifyLanguage('Mi chiamo Massimo e scrivo molto bene in italiano'); echo $lang; // returns 'it'
翻译完整文件
$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);
支持
Massimo INFUNTI http://impronta48.it/