-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add noParse method for better kana romanji.
- Loading branch information
Showing
7 changed files
with
239 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace Limelight\Helpers; | ||
|
||
use Limelight\Config\Config; | ||
use Limelight\Plugins\Plugin; | ||
|
||
trait PluginHelper | ||
{ | ||
/** | ||
* Run all registered plugins. | ||
* | ||
* @param string $text | ||
* @param Node $node | ||
* @param array $tokens | ||
* @param array $words | ||
* | ||
* @return array | ||
*/ | ||
protected function runPlugins($text, $node, $tokens, $words) | ||
{ | ||
$pluginResults = []; | ||
|
||
$config = Config::getInstance(); | ||
|
||
$plugins = $config->getPlugins(); | ||
|
||
foreach ($plugins as $plugin => $namespace) { | ||
$this->validatePlugin($namespace); | ||
|
||
$pluginClass = new $namespace($text, $node, $tokens, $words); | ||
|
||
$pluginResults[$plugin] = $this->firePlugin($pluginClass); | ||
} | ||
|
||
return $pluginResults; | ||
} | ||
|
||
/** | ||
* Validate plugin class exists. | ||
* | ||
* @param string $namespace | ||
*/ | ||
private function validatePlugin($namespace) | ||
{ | ||
if (!class_exists($namespace)) { | ||
throw new PluginNotFoundException("Plugin {$namespace} not found."); | ||
} | ||
} | ||
|
||
/** | ||
* Fire the plugin. | ||
* | ||
* @param Plugin $plugin | ||
* | ||
* @return mixed | ||
*/ | ||
private function firePlugin(Plugin $plugin) | ||
{ | ||
return $plugin->handle(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace Limelight\Parse; | ||
|
||
use Limelight\Limelight; | ||
use Limelight\Helpers\Converter; | ||
use Limelight\Helpers\PluginHelper; | ||
use Limelight\Classes\LimelightWord; | ||
use Limelight\Helpers\JapaneseHelpers; | ||
use Limelight\Classes\LimelightResults; | ||
use Limelight\Exceptions\InvalidInputException; | ||
|
||
class NoParser | ||
{ | ||
use PluginHelper; | ||
use JapaneseHelpers; | ||
|
||
/** | ||
* Handle the no-parse for given text. | ||
* | ||
* @param string $text | ||
* | ||
* @return LimelightResults | ||
*/ | ||
public function handle($text) | ||
{ | ||
if ($this->hasKanji($text)) { | ||
throw new InvalidInputException('Text must not contain kanji.'); | ||
} | ||
|
||
$limelight = new Limelight(); | ||
|
||
$converter = new Converter($limelight); | ||
|
||
$token = $this->buildToken($text); | ||
|
||
$properties = $this->buildProperties(); | ||
|
||
$words = [new LimelightWord($token, $properties, $converter)]; | ||
|
||
$pluginResults = $this->runPlugins($text, null, $token, $words); | ||
|
||
return new LimelightResults($text, $words, $pluginResults); | ||
} | ||
|
||
/** | ||
* Build token using raw text for all properties. | ||
* | ||
* @param string $text | ||
* | ||
* @return array | ||
*/ | ||
private function buildToken($text) | ||
{ | ||
return [ | ||
'literal' => $text, | ||
'lemma' => $text, | ||
'reading' => $text, | ||
'pronunciation' => $text, | ||
]; | ||
} | ||
|
||
/** | ||
* Build array on full properties. | ||
* | ||
* @return array | ||
*/ | ||
private function buildProperties() | ||
{ | ||
return [ | ||
'partOfSpeech' => null, | ||
'grammar' => null, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace Limelight\Tests\Classes; | ||
|
||
use Limelight\Limelight; | ||
use Limelight\Tests\TestCase; | ||
|
||
class NoParseTest extends TestCase | ||
{ | ||
/** | ||
* @var Limelight\Limelight | ||
*/ | ||
protected static $limelight; | ||
|
||
/** | ||
* Set static limelight on object. | ||
*/ | ||
public static function setUpBeforeClass() | ||
{ | ||
self::$limelight = new Limelight(); | ||
} | ||
|
||
/** | ||
* It parses kana text. | ||
* | ||
* @test | ||
*/ | ||
public function it_parses_kana_text() | ||
{ | ||
$results = self::$limelight->noParse('できるかな。。。'); | ||
|
||
$this->assertEquals('できるかな。。。', $results->words()); | ||
} | ||
|
||
/** | ||
* It gets romanji for kana text. | ||
* | ||
* @test | ||
*/ | ||
public function it_gets_romanji_for_kana_text() | ||
{ | ||
$results = self::$limelight->noParse('ねんがっぴ'); | ||
|
||
$this->assertEquals('Nengappi', $results->plugin('romanji')); | ||
} | ||
|
||
/** | ||
* It throws exception for kanji input. | ||
* | ||
* @test | ||
* @expectedException Limelight\Exceptions\InvalidInputException | ||
* @expectedExceptionMessage Text must not contain kanji. | ||
*/ | ||
public function it_throws_exception_for_kanji_text() | ||
{ | ||
$results = self::$limelight->noParse('今日'); | ||
} | ||
} |