Last active
October 7, 2019 11:53
-
-
Save panfeng/5361411 to your computer and use it in GitHub Desktop.
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 | |
function pre_print_r($var){ | |
echo "<pre>"; | |
print_r($var); | |
echo "</pre>"; | |
} | |
function Bigrams($word){ | |
$ngrams = array(); | |
$len = strlen($word); | |
for($i=0;$i+1<$len;$i++){ | |
$ngrams[$i]=$word[$i].$word[$i+1]; | |
} | |
return $ngrams; | |
} | |
pre_print_r(Bigrams("abcdefg")); | |
function Trigrams($word){ | |
$ngrams = array(); | |
$len = strlen($word); | |
for($i=0;$i+2<$len;$i++){ | |
$ngrams[$i]=$word[$i].$word[$i+1].$word[$i+2]; | |
} | |
return $ngrams; | |
} | |
pre_print_r(Trigrams("abcdefg")); | |
function Ngrams($word,$n=3){ | |
$len=strlen($word); | |
$ngram=array(); | |
for($i=0;$i+$n<=$len;$i++){ | |
$string=""; | |
for($j=0;$j<$n;$j++){ | |
$string.=$word[$j+$i]; | |
} | |
$ngram[$i]=$string; | |
} | |
return $ngram; | |
} | |
pre_print_r(Ngrams("abcdefg",1)); | |
//http://phpir.com/language-detection-with-n-grams/ | |
function getNgrams($word, $n = 3) { | |
$ngrams = array(); | |
$len = strlen($word); | |
for($i = 0; $i < $len; $i++) { | |
if($i > ($n - 2)) { | |
$ng = ''; | |
for($j = $n-1; $j >= 0; $j--) { | |
$ng .= $word[$i-$j]; | |
} | |
$ngrams[] = $ng; | |
} | |
} | |
return $ngrams; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment