Skip to content

Commit

Permalink
Fix bug in LimelightResults plugin() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleigh committed Apr 26, 2016
1 parent 7723d8a commit 411f9ec
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 59 deletions.
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
```
Expand All @@ -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('庭でライムを育てています。');
Expand Down
44 changes: 38 additions & 6 deletions src/Classes/LimelightResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
}
}
7 changes: 4 additions & 3 deletions src/Helpers/PluginHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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];
Expand Down
21 changes: 14 additions & 7 deletions tests/Integration/LimelightResultsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
'<ruby><rb>音楽</rb><rp>(</rp><rt>おんがく</rt><rp>)</rp></ruby>',
'',
'<ruby><rb>聴</rb><rp>(</rp><rt>き</rt><rp>)</rp></ruby>きます',
''
], $furigana);
$this->assertEquals(
'<ruby><rb>音楽</rb><rp>(</rp><rt>おんがく</rt><rp>)</rp></ruby>を<ruby><rb>聴</rb><rp>(</rp><rt>き</rt><rp>)</rp></ruby>きます。',
$furigana);
}

/**
Expand All @@ -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.
*
Expand Down
31 changes: 9 additions & 22 deletions tests/Integration/Plugins/Furigana/FuriganaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,18 +287,11 @@ public function it_can_get_furigana_off_results_object()
{
$results = self::$limelight->parse('アッ、太郎!久しぶり!元気?');

$furigana = $results->plugin('Furigana')->all();

$this->assertEquals([
'アッ',
'',
'<ruby><rb>太郎</rb><rp>(</rp><rt>たろう</rt><rp>)</rp></ruby>',
'',
'<ruby><rb>久</rb><rp>(</rp><rt>ひさ</rt><rp>)</rp></ruby>しぶり',
'',
'<ruby><rb>元気</rb><rp>(</rp><rt>げんき</rt><rp>)</rp></ruby>',
''
], $furigana);
$furigana = $results->plugin('Furigana');

$this->assertEquals(
'アッ、<ruby><rb>太郎</rb><rp>(</rp><rt>たろう</rt><rp>)</rp></ruby>!<ruby><rb>久</rb><rp>(</rp><rt>ひさ</rt><rp>)</rp></ruby>しぶり!<ruby><rb>元気</rb><rp>(</rp><rt>げんき</rt><rp>)</rp></ruby>?',
$furigana);
}

/**
Expand All @@ -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',
'<ruby><rb>時</rb><rp>(</rp><rt>じ</rt><rp>)</rp></ruby>'
], $furigana);
$this->assertEquals('7<ruby><rb>時</rb><rp>(</rp><rt>じ</rt><rp>)</rp></ruby>', $furigana);
}

/**
Expand All @@ -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([
'',
'<ruby><rb>時</rb><rp>(</rp><rt>じ</rt><rp>)</rp></ruby>'
], $furigana);
$this->assertEquals('7<ruby><rb>時</rb><rp>(</rp><rt>じ</rt><rp>)</rp></ruby>', $furigana);
}
}
20 changes: 7 additions & 13 deletions tests/Integration/Plugins/Romaji/RomajiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}
}

0 comments on commit 411f9ec

Please sign in to comment.