Skip to content

Instantly share code, notes, and snippets.

@panfeng
Last active October 7, 2019 11:53
Show Gist options
  • Save panfeng/5361411 to your computer and use it in GitHub Desktop.
Save panfeng/5361411 to your computer and use it in GitHub Desktop.
<?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