Skip to content

Commit beb1a91

Browse files
committed
Added google translate
1 parent caa17df commit beb1a91

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ A simple wrapper for PHP-NLP-Client and OpenCalais NLP tools.
33

44
## Installation
55

6+
```
7+
$ composer require web64/laravel-nlp
8+
```
9+
610
## Requirements
711

812
## Included Tools
@@ -18,13 +22,15 @@ A simple wrapper for PHP-NLP-Client and OpenCalais NLP tools.
1822
### Language Detection
1923
```php
2024
use Web64\LaravelNlp\Facades\NLP;
25+
2126
$lang = NLP::language("What language is this?");
2227
// 'en'
2328
```
2429

2530
### Article Extraction
2631
```php
2732
use Web64\LaravelNlp\Facades\NLP;
33+
2834
$article = NLP::article("https://medium.com/@taylorotwell/wildcard-letsencrypt-certificates-on-forge-d3bdec43692a");
2935
print_r($article);
3036
/*
@@ -54,6 +60,7 @@ print_r($article);
5460
For other Laravel NLP packages, check out:
5561
- https://github.com/AntoineAugusti/laravel-sentiment-analysis
5662
- https://github.com/michaeljhopkins/Laravel-Aylien-Wrapper
63+
- https://github.com/findbrok/laravel-personality-insights
5764

5865

5966
## Contribute

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"type": "library",
55
"require": {
66
"web64/php-nlp-client": "^0.30",
7-
"lsmonki/php-open-calais": "^2.1"
7+
"lsmonki/php-open-calais": "^2.1",
8+
"stichoza/google-translate-php": "~3.2"
89
},
910
"license": "MIT",
1011
"authors": [

src/LaravelNlp/LaravelNlp.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Web64\LaravelNlp;
44

5+
6+
57
class LaravelNlp extends \Web64\Nlp\NlpClient
68
{
79
private $opencalais_key;
@@ -24,6 +26,19 @@ public function article( $url )
2426
return $this->newspaperUrl( $url );
2527
}
2628

29+
public function translate($text, $source_lang = null, $target_lang = null)
30+
{
31+
$translator = new \Stichoza\GoogleTranslate\TranslateClient;
32+
33+
if ( $source_lang )
34+
$translator->setSource($source_lang);
35+
36+
if ( $target_lang )
37+
$translator->setTarget($target_lang);
38+
39+
return $translator->translate( $text );
40+
}
41+
2742
public function sentiment( $text, $language = 'en' )
2843
{
2944
$response = $this->polyglot($text, $language);

0 commit comments

Comments
 (0)