Detect keywords in text

<div style="width:550px"> <form method="POST"> <textarea name="text" style="width:100%" rows="20"><?=($_POST['text'])?$_POST['text']:''?></textarea> <input type="submit"/> <pre> <?php function keywords1($text){ $text = preg_replace('/[\!-@\[-\x60\x7b\x7e]/',' ',mb_strtolower($text,'UTF-8')); $text = preg_replace('/\s+/',' ',$text); var_dump($text); foreach(explode(' ',$text) as $key)$count[$key]++; foreach($count as $key=>$value)$length[$key] = mb_strlen($key,'UTF-8'); asort($count,1); arsort($length); $count = array_slice($count, 0, round(count($count)/2)); $length = array_slice($length, 0, round(count($length)/2)); // $count = array_slice($count, round(count($count)/3), round(count($count)/3) - round(count($count)/2)); // $length = array_slice($length, round(count($length)/3), round(count($length)/3) - round(count($length)/2)); //var_dump($count); //var_dump($length); $keywords = array_intersect(array_keys($count),array_keys($length)); $keywords = array_slice($keywords, 0, 5); // $keywords = array_slice($keywords, round(count($keywords)/3), round(count($keywords)/3) - round(count($keywords)/2)); var_dump($keywords); return $keywords; } function keywords($text, $max=5){ $text = preg_replace('/[\!-@\[-\x60\x7b\x7e]/',' ',mb_strtolower($text,'UTF-8')); $text = preg_replace('/\s+/',' ',$text); foreach(explode(' ',$text) as $key)$count[$key]++; foreach($count as $key=>$value)$length[$key] = mb_strlen($key,'UTF-8'); asort($count,1); arsort($length); $count = array_slice($count, 0, round(count($count)/2)); $length = array_slice($length, 0, round(count($length)/2)); $keywords = array_intersect(array_keys($count),array_keys($length)); $keywords = array_slice($keywords, 0, $max); return $keywords; } $keywords = ($_POST['max'])?keywords($_POST['text'],$_POST['max']):keywords($_POST['text']); echo '</pre><input value="'.implode(', ',$keywords).'" style="width:90%"/><input type="number" name="max" style="width:9%" value="'.$_POST['max'].'" step="1"/></form></div>';

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.