Created
February 26, 2013 17:46
-
-
Save pschultz/5040460 to your computer and use it in GitHub Desktop.
Ngram tokenizer in php
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
<?php | |
class NgramTokenizer | |
{ | |
public static function tokenize($word, $n) | |
{ | |
if ($n === 1) { | |
return str_split($word); | |
} | |
$grams = array(); | |
for ($i = 0; $i <= strlen($word) - $n; ++$i) { | |
$grams[] = substr($word, $i, $n); | |
} | |
return $grams; | |
} | |
} |
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
<?php | |
class NgramTokenizerTest extends \PHPUnit_Framework_TestCase | |
{ | |
public function testUnigram() | |
{ | |
$tokens = NgramTokenizer::tokenize("abcd", 1); | |
$this->assertEquals(range('a', 'd'), $tokens); | |
} | |
public function testBigram() | |
{ | |
$tokens = NgramTokenizer::tokenize("abcd", 2); | |
$this->assertEquals(array('ab', 'bc', 'cd'), $tokens); | |
} | |
public function testTrigram() | |
{ | |
$tokens = NgramTokenizer::tokenize("abcdef", 3); | |
$this->assertEquals(array('abc', 'bcd', 'cde', 'def'), $tokens); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ASCII string only NGram tokenizer. No support for strings with unicode or breaking things up by certain characters like spaces.