Skip to content

Commit

Permalink
Merge pull request #33 from heiglandreas/feature/addCustomTokenizer
Browse files Browse the repository at this point in the history
Feature/add custom tokenizer
  • Loading branch information
heiglandreas committed Mar 30, 2017
2 parents c5a25c6 + 39977c9 commit 70c6b7e
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 8 deletions.
146 changes: 146 additions & 0 deletions src/Tokenizer/CustomHyphenationTokenizer.php
@@ -0,0 +1,146 @@
<?php
/**
* Copyright (c) 2008-2011 Andreas Heigl<andreas@heigl.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @category Hyphenation
* @package Org_Heigl_Hyphenator
* @subpackage Tokenizer
* @author Andreas Heigl <andreas@heigl.org>
* @copyright 2008-2011 Andreas Heigl<andreas@heigl.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT-License
* @version 2.0.1
* @link http://github.com/heiglandreas/Hyphenator
* @since 11.11.2011
*/

namespace Org\Heigl\Hyphenator\Tokenizer;

use Org\Heigl\Hyphenator\Options;

/**
* Use Punktuation to split any input into tokens
*
* @category Hyphenation
* @package Org_Heigl_Hyphenator
* @subpackage Tokenizer
* @author Andreas Heigl <andreas@heigl.org>
* @copyright 2008-2011 Andreas Heigl<andreas@heigl.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT-License
* @version 2.0.1
* @link http://github.com/heiglandreas/Hyphenator
* @since 04.11.2011
*/
class CustomHyphenationTokenizer implements Tokenizer
{

private $options;

public function __construct(Options $options)
{
$this->options = $options;
}

/**
* Split the given input into tokens using punktuation marks as splitter
*
* The input can be a string or a tokenRegistry. If the input is a
* TokenRegistry, each item will be tokenized.
*
* @param string|\Org\Heigl\Hyphenator\Tokenizer\TokenRegistry $input The
* input to be tokenized
*
* @return \Org\Heigl\Hyphenator\Tokenizer\TokenRegistry
*/
public function run($input)
{
if ($input instanceof TokenRegistry) {
// Tokenize a TokenRegistry
$f = clone($input);
foreach ($input as $token) {
if (! $token instanceof WordToken) {
continue;
}
$newTokens = $this->tokenize($token->get());
$f->replace($token, $newTokens);
}

return $f ;
}

// Tokenize a simple string.
$array = $this->tokenize($input);
$registry = new TokenRegistry();
foreach ($array as $item) {
$registry->add($item);
}

return $registry;
}

/**
* Split the given string into tokens using whitespace.
*
* Each whitespace is placed in a WhitespaceToken and everything else is
* placed in a WordToken-Object
*
* @param \string $input The String to tokenize
*
* @return Token
*/
private function tokenize($input)
{
$tokens = [];

$splits = preg_split(sprintf(
'/((?:(?<=\W)%1$s|\b\w+%2$s)\w+?\b)/u',
$this->options->getNoHyphenateString(),
$this->options->getCustomHyphen()
), $input, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach ($splits as $split) {
if ('' == $split) {
continue;
}
if (0 === mb_strpos($split, $this->options->getNoHyphenateString())) {

$tokens[] = new ExcludedWordToken(str_replace(
$this->options->getNoHyphenateString(),
'',
$split
));
continue;
}

if (false !== mb_strpos($split, $this->options->getCustomHyphen())) {

$tokens[] = new ExcludedWordToken(str_replace(
$this->options->getCustomHyphen(),
$this->options->getHyphen(),
$split
));
continue;
}

$tokens[] = new WordToken($split);
}

return $tokens;
}
}
52 changes: 52 additions & 0 deletions src/Tokenizer/ExcludedWordToken.php
@@ -0,0 +1,52 @@
<?php
/**
* Copyright (c) 2008-2011 Andreas Heigl<andreas@heigl.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @category Hyphenation
* @package Org_Heigl_Hyphenator
* @subpackage Tokenizer
* @author Andreas Heigl <andreas@heigl.org>
* @copyright 2008-2011 Andreas Heigl<andreas@heigl.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT-License
* @version 2.0.beta
* @link http://github.com/heiglandreas/Hyphenator
* @since 11.11.2011
*/

namespace Org\Heigl\Hyphenator\Tokenizer;

/**
* This Class describes a Token represeonting something that is not a word.
*
* @category Hyphenation
* @package Org_Heigl_Hyphenator
* @subpackage Tokenizer
* @author Andreas Heigl <andreas@heigl.org>
* @copyright 2008-2011 Andreas Heigl<andreas@heigl.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT-License
* @version 2.0.beta
* @link http://github.com/heiglandreas/Hyphenator
* @since 04.11.2011
*/
class ExcludedWordToken extends Token
{
//
}
5 changes: 1 addition & 4 deletions src/Tokenizer/PunctuationTokenizer.php
Expand Up @@ -112,10 +112,7 @@ public function run($input)
// Tokenize a TokenRegistry
$f = clone($input);
foreach ($input as $token) {
if ($token instanceof WhitespaceToken) {
continue;
}
if ($token instanceof NonWordToken) {
if (! $token instanceof WordToken) {
continue;
}
$newTokens = $this->_tokenize($token->get());
Expand Down
5 changes: 1 addition & 4 deletions src/Tokenizer/WhitespaceTokenizer.php
Expand Up @@ -70,10 +70,7 @@ public function run($input)
if ($input instanceof TokenRegistry) {
// Tokenize a TokenRegistry
foreach ($input as $token) {
if ($token instanceof WhitespaceToken) {
continue;
}
if ($token instanceof NonWordToken) {
if (! $token instanceof WordToken) {
continue;
}
$newTokens = $this->_tokenize($token->get());
Expand Down
70 changes: 70 additions & 0 deletions tests/Tokenizer/CustomHyphenationTokenizerTest.php
@@ -0,0 +1,70 @@
<?php
/**
* Copyright (c) 2008-2011 Andreas Heigl<andreas@heigl.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @category Hyphenator
* @package Org\Heigl\Hyphenator
* @author Andreas Heigl <andreas@heigl.org>
* @copyright 2008-2011 Andreas Heigl<andreas@heigl.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT-License
* @version 2.0.1
* @since 02.11.2011
*/

namespace Org\Heigl\HyphenatorTest\Tokenizer;

use Org\Heigl\Hyphenator\Options;
use \Org\Heigl\Hyphenator\Tokenizer as t;
use Mockery as M;

/**
* This class tests the functionality of the class PunctuationTokenizer
*
* @category Hyphenator
* @package Org\Heigl\Hyphenator
* @author Andreas Heigl <andreas@heigl.org>
* @copyright 2008-2011 Andreas Heigl<andreas@heigl.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT-License
* @version 2.0.1
* @since 02.11.2011
*/
class CustomHyphenationTokenizerTest extends \PHPUnit_Framework_TestCase
{
public function testTokenizingString()
{
$options = M::mock(Options::class);
$options->shouldReceive('getNoHyphenateString')->andReturn('==');
$options->shouldReceive('getCustomHyphen')->andReturn('--');
$options->shouldReceive('getHyphen')->andReturn('^^');


$tReg = new t\TokenRegistry();
$tReg->add(new t\WordToken('Das ist '))
->add(new t\ExcludedWordToken('nicht'))
->add(new t\WordToken(' getrennt und das hat eine '))
->add(new t\ExcludedWordToken('kunden^^spezifische'))
->add(new t\WordToken(' Trennung!'));

$tokenizer = new t\CustomHyphenationTokenizer($options);
$registry = $tokenizer->run('Das ist ==nicht getrennt und das hat eine kunden--spezifische Trennung!');
$this->assertEquals($tReg, $registry);
}
}

0 comments on commit 70c6b7e

Please sign in to comment.