Skip to content

Instantly share code, notes, and snippets.

@iegik
Last active May 19, 2016 12:31
Show Gist options
  • Select an option

  • Save iegik/32b6146065b058d1657d to your computer and use it in GitHub Desktop.

Select an option

Save iegik/32b6146065b058d1657d to your computer and use it in GitHub Desktop.
Text tool to get useful keywords to describe it
<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>';
<?php
if(@$a = @$_POST?:$_GET){
$a = strip_tags(trim(@current($a)?:key($a)));
echo $a.'<br/>';
echo 'Кол. символов: '.mb_strlen($a,'UTF-8').'<br/>';
}else{
echo '<form method="post"><textarea name="a"></textarea><input type="submit"/></form>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment