diff --git a/README.md b/README.md index d985113..a199a70 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,6 @@ ### Quick Guide - [Version Notes](#version-notes) - [Install Limelight](#install-limelight) - - [Initialize Limelight](#initialize-limelight) - [Parse Text](#parse-text) - [Get Results](#get-results) - [Full Documentation](#full-documentation) @@ -24,13 +23,13 @@ - April 11: php-mecab, the MeCab bindings Limelight uses, were updated to version 0.6.0 in Dec. 2015 for php 7 support. The pre-0.6.0 bindings no longer work with the master branch of Limelight. If you are using an older version of php-mecab, please update your bindings or use the [php-mecab_pre_0.6.0](https://github.com/nihongodera/limelight/tree/php-mecab_pre_0.6.0) version. ### Install Limelight -#### Requirements +##### Requirements - php > 5.6 -#### Dependencies +##### Dependencies Before installing Limelight, you must install both mecab and the php extension php-mecab on your system. -##### Linux Ubuntu Users +###### Linux Ubuntu Users Use the install script included in this repository. Download the script: ``` @@ -46,23 +45,22 @@ Execute the script: ``` For information about what the script does, see [here](https://github.com/nihongodera/limelight/wiki/Install-Script). -##### Other Systems +###### Other Systems Please see [this page](https://github.com/nihongodera/php-mecab-documentation) to learn more about installing on your system. -#### Install Limelight +##### Install Limelight Install Limelight through composer. ``` composer require nihongodera/limelight ``` -### Initialize Limelight +### Parse Text Make a new instance of Limelight\Limelight. Limelight takes no arguments. ```php $limelight = new Limelight(); ``` -### Parse Text Use the parse() method on the Limelight object to parse Japanese text. ```php $results = $limelight->parse('庭でライムを育てています。'); diff --git a/src/Classes/LimelightResults.php b/src/Classes/LimelightResults.php index bae3bc4..45bc9f0 100644 --- a/src/Classes/LimelightResults.php +++ b/src/Classes/LimelightResults.php @@ -66,12 +66,10 @@ public function __toString() */ public function string($value, $glue = null) { - $string = $this->map(function ($item, $key) use ($value, $glue) { - if ($value !== 'partOfSpeech' && $item->partOfSpeech === 'symbol' && preg_match('/\\s/', $glue)) { - return $item->$value; - } + $value = $this->makeSingular($value); - return $glue.$item->$value; + $string = $this->map(function ($item, $key) use ($value, $glue) { + return $this->buildString($item, $value, $glue); }); return $this->cutFirst(implode('', $string->all()), $glue); @@ -190,7 +188,25 @@ public function toKatakana() */ public function plugin($name) { - return $this->getPluginData($name); + return $this->getPluginData($name, 'self'); + } + + /** + * Build string for word. + * + * @param LimelightWord $item + * @param string $value + * @param string|null $glue + * + * @return string + */ + private function buildString($item, $value, $glue) + { + if ($value !== 'partOfSpeech' && $item->partOfSpeech === 'symbol' && preg_match('/\\s/', $glue)) { + return $item->$value; + } + + return $glue.$item->$value; } /** @@ -209,4 +225,20 @@ private function cutFirst($string, $divider) return $string; } + + /** + * Make value singular. + * + * @param string $value + * + * @return string + */ + private function makeSingular($value) + { + if (substr($value, -1) === 's') { + return substr($value, 0, -1); + } + + return $value; + } } diff --git a/src/Helpers/PluginHelper.php b/src/Helpers/PluginHelper.php index 79e2689..a07113c 100644 --- a/src/Helpers/PluginHelper.php +++ b/src/Helpers/PluginHelper.php @@ -13,12 +13,13 @@ trait PluginHelper /** * Get data from pluginData. * - * @param string $type [romaji, furigana] + * @param string $type [romaji, furigana] + * @param string $target [self, child] * * @return static * @throws PluginNotFoundException */ - protected function getPluginData($type) + protected function getPluginData($type, $target = 'child') { $type = ucfirst($type); @@ -28,7 +29,7 @@ protected function getPluginData($type) throw new PluginNotFoundException("Plugin data for {$type} can not be found. Is the {$type} plugin registered in config?"); } - if ($this instanceof Collection) { + if ($this instanceof Collection && $target === 'child') { return $this->pluck('pluginData')->pluck($type); } else { return $this->pluginData[$type]; diff --git a/tests/Integration/LimelightResultsTest.php b/tests/Integration/LimelightResultsTest.php index 585673d..84aa87e 100644 --- a/tests/Integration/LimelightResultsTest.php +++ b/tests/Integration/LimelightResultsTest.php @@ -272,14 +272,11 @@ public function it_throws_exception_when_plugin_not_registered() */ public function it_can_get_plugin_data() { - $furigana = $this->getResults()->plugin('Furigana')->all(); + $furigana = $this->getResults()->plugin('Furigana'); - $this->assertEquals([ - '音楽(おんがく)', - 'を', - '()きます', - '。' - ], $furigana); + $this->assertEquals( + '音楽(おんがく)()きます。', + $furigana); } /** @@ -292,6 +289,16 @@ public function it_puts_a_space_before_symbol_when_partofspeech() $this->assertEquals('noun postposition verb symbol', $string); } + /** + * @test + */ + public function it_accepts_plural_string_values() + { + $string = $this->getResults()->string('words'); + + $this->assertEquals('音楽を聴きます。', $string); + } + /** * Parse test phrase and return LimelightResults. * diff --git a/tests/Integration/Plugins/Furigana/FuriganaTest.php b/tests/Integration/Plugins/Furigana/FuriganaTest.php index 7e17170..634fb0d 100644 --- a/tests/Integration/Plugins/Furigana/FuriganaTest.php +++ b/tests/Integration/Plugins/Furigana/FuriganaTest.php @@ -287,18 +287,11 @@ public function it_can_get_furigana_off_results_object() { $results = self::$limelight->parse('アッ、太郎!久しぶり!元気?'); - $furigana = $results->plugin('Furigana')->all(); - - $this->assertEquals([ - 'アッ', - '、', - '太郎(たろう)', - '!', - '(ひさ)しぶり', - '!', - '元気(げんき)', - '?' - ], $furigana); + $furigana = $results->plugin('Furigana'); + + $this->assertEquals( + 'アッ、太郎(たろう)(ひさ)しぶり!元気(げんき)?', + $furigana); } /** @@ -308,12 +301,9 @@ public function it_doesnt_make_furigana_for_half_width_numbers() { $results = self::$limelight->parse('7時'); - $furigana = $results->plugin('Furigana')->all(); + $furigana = $results->plugin('Furigana'); - $this->assertEquals([ - '7', - '()' - ], $furigana); + $this->assertEquals('7()', $furigana); } /** @@ -323,11 +313,8 @@ public function it_doesnt_make_furigana_for_full_width_numbers() { $results = self::$limelight->parse('7時'); - $furigana = $results->plugin('Furigana')->all(); + $furigana = $results->plugin('Furigana'); - $this->assertEquals([ - '7', - '()' - ], $furigana); + $this->assertEquals('7()', $furigana); } } diff --git a/tests/Integration/Plugins/Romaji/RomajiTest.php b/tests/Integration/Plugins/Romaji/RomajiTest.php index 819d2bd..9b7659f 100644 --- a/tests/Integration/Plugins/Romaji/RomajiTest.php +++ b/tests/Integration/Plugins/Romaji/RomajiTest.php @@ -13,15 +13,9 @@ public function it_stores_space_seperated_strings_on_object() { $results = self::$limelight->parse('今週末山を登ります!'); - $conversion = $results->plugin('Romaji')->all(); - - $this->assertEquals([ - 'konshūmatsu', - 'yama', - 'o', - 'noborimasu', - '!' - ], $conversion); + $conversion = $results->plugin('Romaji'); + + $this->assertEquals('Konshūmatsu yama o noborimasu!', $conversion); } /** @@ -31,9 +25,9 @@ public function it_converts_multibyte_chars_to_uppercase() { $results = self::$limelight->parse('大阪'); - $conversion = $results->plugin('Romaji')->all(); + $conversion = $results->plugin('Romaji'); - $this->assertEquals(['Ōsaka'], $conversion); + $this->assertEquals('Ōsaka', $conversion); } /** @@ -43,8 +37,8 @@ public function it_allows_english_punctuation_to_remain_when_noparse() { $results = self::$limelight->noParse('うれ.しい'); - $conversion = $results->plugin('Romaji')->all(); + $conversion = $results->plugin('Romaji'); - $this->assertEquals(['ure.shii'], $conversion); + $this->assertEquals('Ure.shii', $conversion); } }