Last active
July 18, 2021 21:10
-
-
Save GlauberF/00d76398a997484fad6dde0583efd9fd to your computer and use it in GitHub Desktop.
- pt: Nos campos de linguística computacional e probabilidade, um n-grama é uma sequência contígua de n itens de uma determinada amostra de texto ou fala. Os itens podem ser fonemas, sílabas, letras, palavras ou pares de bases de acordo com a aplicação. Os n -gramas normalmente são coletados de um corpus de texto ou fala.[1] -en: In the fields o…
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