From 687a0d17b3953a05c6f94a98d0e15ee1778bf509 Mon Sep 17 00:00:00 2001 From: Olav Date: Tue, 10 Apr 2018 19:21:15 +0200 Subject: [PATCH] re-config --- examples/embeddings.php | 2 +- examples/language_detection.php | 2 +- examples/newspaper.php | 2 +- examples/polyglot.php | 2 +- src/NlpClient.php | 12 ++++++------ tests/Unit/EmbeddingsTest.php | 2 +- tests/Unit/LanguageDetectonTest.php | 13 ++++++------- tests/Unit/NewspaperTest.php | 2 +- tests/Unit/PolyglotTest.php | 2 +- 9 files changed, 19 insertions(+), 20 deletions(-) diff --git a/examples/embeddings.php b/examples/embeddings.php index 9582cb4..8f09784 100644 --- a/examples/embeddings.php +++ b/examples/embeddings.php @@ -10,7 +10,7 @@ 'debug' => true, ]; -$nlp = new \Web64\Nlp\NlpClient( $nlpserver_config ); +$nlp = new \Web64\Nlp\NlpClient( $nlpserver_config['hosts'], $nlpserver_config['debug'] ); $neighbours = $nlp->embeddings('obama', 'no'); diff --git a/examples/language_detection.php b/examples/language_detection.php index 3a5e189..0cc9fe6 100644 --- a/examples/language_detection.php +++ b/examples/language_detection.php @@ -10,7 +10,7 @@ 'debug' => false, ]; -$nlp = new \Web64\Nlp\NlpClient( $nlpserver_config ); +$nlp = new \Web64\Nlp\NlpClient( $nlpserver_config['hosts'], $nlpserver_config['debug'] ); $texts = [ 'en' => "The quick brown fox jumps over the lazy dog", diff --git a/examples/newspaper.php b/examples/newspaper.php index 2863869..68f30ba 100644 --- a/examples/newspaper.php +++ b/examples/newspaper.php @@ -10,7 +10,7 @@ 'debug' => true, ]; -$nlp = new \Web64\Nlp\NlpClient( $nlpserver_config ); +$nlp = new \Web64\Nlp\NlpClient( $nlpserver_config['hosts'], $nlpserver_config['debug'] ); $newspaper = $nlp->newspaperUrl('http://www.bbc.com/news/science-environment-43710766'); diff --git a/examples/polyglot.php b/examples/polyglot.php index d8edd57..1e4b2b4 100644 --- a/examples/polyglot.php +++ b/examples/polyglot.php @@ -9,7 +9,7 @@ 'debug' => false, ]; -$nlp = new \Web64\Nlp\NlpClient( $nlpserver_config ); +$nlp = new \Web64\Nlp\NlpClient( $nlpserver_config['hosts'], $nlpserver_config['debug'] ); $text = "Den har en tykk, sukkulent stamme som blir 1,5–8 m høy. Stammen er bare forgrenet i toppen på eldre planter. Stammen og eventuelle greiner er dekket av lange torner, som er omdannede akselblad. I toppen av planten sitter en rosett med avlange blad. Bladene er olivengrønne på oversiden og lyse under. Etterhvert som planten vokser, faller bladene av nedover stammen, men tornene står parvis igjen i de gamle bladfestene. Blomstene er femtallige og hvite. Arten vokser i torneskogene på sørlige og sørvestlige Madagaskar opptil 750 moh. diff --git a/src/NlpClient.php b/src/NlpClient.php index cc0a9f9..f515068 100644 --- a/src/NlpClient.php +++ b/src/NlpClient.php @@ -14,18 +14,18 @@ class NlpClient{ public $debug = false; private $max_retry_count = 3; - function __construct( $config ) + function __construct( $hosts, $debug = false ) { - if ( is_array($config['hosts']) ) + $this->debug = (bool)$debug; + + if ( is_array($hosts) ) { - foreach( $config['hosts'] as $host ) + foreach( $hosts as $host ) $this->addHost( $host ); } else - $this->addHost( $config['hosts'] ); + $this->addHost( $hosts ); - if ( isset($config['debug']) && $config['debug'] ) - $this->debug = true; // pick random host as default $this->api_url = $this->api_hosts[ diff --git a/tests/Unit/EmbeddingsTest.php b/tests/Unit/EmbeddingsTest.php index cdc8546..3e3d2e4 100644 --- a/tests/Unit/EmbeddingsTest.php +++ b/tests/Unit/EmbeddingsTest.php @@ -9,7 +9,7 @@ class EmbeddingsTest extends TestCase /** @test */ public function get_neighbours() { - $nlp = new \Web64\Nlp\NlpClient( $this->nlpserver_config ); + $nlp = new \Web64\Nlp\NlpClient( $this->nlpserver_config['hosts'], $this->nlpserver_config['debug'] ); $neighbours = $nlp->embeddings('obama', 'no'); diff --git a/tests/Unit/LanguageDetectonTest.php b/tests/Unit/LanguageDetectonTest.php index e55dfcd..cca7b94 100644 --- a/tests/Unit/LanguageDetectonTest.php +++ b/tests/Unit/LanguageDetectonTest.php @@ -25,7 +25,7 @@ public function setUp() /** @test */ public function detect_languages() { - $nlp = new \Web64\Nlp\NlpClient( $this->nlpserver_config ); + $nlp = new \Web64\Nlp\NlpClient( $this->nlpserver_config['hosts'], $this->nlpserver_config['debug'] ); $tests = [ 'en' => "The quick brown fox jumps over the lazy dog", @@ -40,7 +40,6 @@ public function detect_languages() foreach( $tests as $lang => $text ) { $detected_lang = $nlp->language( $text ); - $this->assertEquals($lang, $detected_lang); } } @@ -50,7 +49,7 @@ public function single_host() { $this->nlpserver_config['hosts'] = 'http://localhost:6400/'; - $nlp = new \Web64\Nlp\NlpClient( $this->nlpserver_config ); + $nlp = new \Web64\Nlp\NlpClient( $this->nlpserver_config['hosts'], $this->nlpserver_config['debug'] ); $detected_lang = $nlp->language( "The quick brown fox jumps over the lazy dog" ); @@ -61,19 +60,19 @@ public function single_host() /** @test */ public function not_enough_text() { - $nlp = new \Web64\Nlp\NlpClient( $this->nlpserver_config ); + $nlp = new \Web64\Nlp\NlpClient( $this->nlpserver_config['hosts'], $this->nlpserver_config['debug'] ); $detected_lang = $nlp->language( "?" ); - echo "Detected: lang:". $detected_lang . PHP_EOL; + $this->msg( "Detected: lang:". $detected_lang ); $this->assertEquals('en', $detected_lang);; } /** @test */ public function fail_first_then_retry() { - $nlp = new \Web64\Nlp\NlpClient( $this->nlpserver_config ); + $nlp = new \Web64\Nlp\NlpClient( $this->nlpserver_config['hosts'], $this->nlpserver_config['debug'] ); $nlp->api_url = 'http://localhost:6666/'; // <-- wrong port - print_r( $nlp ); + //print_r( $nlp ); $detected_lang = $nlp->language( "The quick brown fox jumps over the lazy dog" ); diff --git a/tests/Unit/NewspaperTest.php b/tests/Unit/NewspaperTest.php index b2fd044..2b38ae1 100644 --- a/tests/Unit/NewspaperTest.php +++ b/tests/Unit/NewspaperTest.php @@ -9,7 +9,7 @@ class NewspaperTest extends TestCase /** @test */ public function url_article_extraction() { - $nlp = new \Web64\Nlp\NlpClient( $this->nlpserver_config ); + $nlp = new \Web64\Nlp\NlpClient( $this->nlpserver_config['hosts'], $this->nlpserver_config['debug'] ); $newspaper = $nlp->newspaperUrl('http://www.bbc.com/news/science-environment-43710766'); diff --git a/tests/Unit/PolyglotTest.php b/tests/Unit/PolyglotTest.php index 2e24348..f019169 100644 --- a/tests/Unit/PolyglotTest.php +++ b/tests/Unit/PolyglotTest.php @@ -10,7 +10,7 @@ class PolyglotTest extends TestCase /** @test */ public function entity_extraction() { - $nlp = new \Web64\Nlp\NlpClient( $this->nlpserver_config ); + $nlp = new \Web64\Nlp\NlpClient( $this->nlpserver_config['hosts'], $this->nlpserver_config['debug'] ); $text = "Big Ben is the largest of five bells and weighs 13.7 tonnes. It was the largest bell in the United Kingdom for 23 years. The origin of the bell's nickname is open to question; it may be named after Sir Benjamin Hall, who oversaw its installation, or heavyweight boxing champion Benjamin Caunt.