Здравствуйте. мне нужно реализовать метод шинглов на javascript. в поисковиках находил только php и питоне. помогите конвертировать код из php в js. в долгу не останусь.
PHP код:
function get_shingle($text,$n=3) {
$shingles = array();
$text = clean_text($text);
$elements = explode(" ",$text);
for ($i=0;$i<(count($elements)-$n+1);$i++) {
$shingle = '';
for ($j=0;$j<$n;$j++){
$shingle .= mb_strtolower(trim($elements[$i+$j]), 'UTF-8')." ";
}
if(strlen(trim($shingle)))
$shingles[$i] = trim($shingle, ' -');
}
return $shingles;
}
function clean_text($text) {
$new_text = eregi_replace("[\,|\.|\'|\"|\\|\/]","",$text);
$new_text = eregi_replace("[\n|\t]"," ",$new_text);
$new_text = preg_replace('/(\s\s+)/', ' ', trim($new_text));
return $new_text;
}
function check_it($first, $second) {
if (!$first || !$second) {
echo "Отсутствуют оба или один из текстов!";
return 0;
}
for ($i=1;$i<5;$i++) {
$first_shingles = array_unique(get_shingle($first,$i));
$second_shingles = array_unique(get_shingle($second,$i));
if(count($first_shingles) < $i-1 || count($second_shingles) < $i-1) {
echo "Количество слов в тексте меньше чем длинна шинглы<br />";
continue;
}
$intersect = array_intersect($first_shingles,$second_shingles);
$merge = array_unique(array_merge($first_shingles,$second_shingles));
$diff = (count($intersect)/count($merge))/0.01;
echo "Количество слов в шингле - $i. Процент схожести - ".round($diff, 2)."%<br />";
}
}
if (isset($_POST['text1']) && isset($_POST['text2'])) {
check_it(strip_tags($_POST['text1']), strip_tags($_POST['text2']));
}