Пришёл к тому что надо собирать свою базу запросов, грабить и можернизировать тексты, и на основе своей базы делать дорвеи.
Во время поиска полезной информации о дорвеях, набрёл на скрипт который генерирует текст по алгоритму цепей Маркова.
Вот он:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| Class MarkovChains {
var $prepared = array();
function MarkovChains ($source){
$source = strtolower($source);
$source = str_replace(array ("? ", "! "), ".", $source);
$source = str_replace(array (" -", "- ", "\t", "\r", "\n", "|", "&", '\', '/', " :", " ;", "©", "•"), ' ', $source);
$source = str_replace(array (")", "(", "]", "[", "'", "\"", '*', '•', '~', '{', '}'), '', $source);
$source = str_replace(" ,", ",", $source);
$source = preg_replace("~(\s+\d{1,2}\s+)|(\w*\.\w+)~", " ", $source);
$source = preg_replace("~\s+~", " ", $source);$sentens = explode('. ', $source);
$count_sentens = count($sentens);
for ($j=0; $j< ;$count_sentens; ++$j){
$sentens[$j] = explode(' ', $sentens[$j]);
$count_words = count($sentens[$j]) - 1;
for ($i=0; $i < ; $count_words; ++$i){
$prefix = $sentens[$j][$i];
$this-> ;prepared [$prefix][] = $sentens[$j][$i+1];
}
}
$keys = array_keys($this-> ;prepared );
foreach ($keys as $key){
$this-> ;prepared [$key] = array_unique($this-> ;prepared [$key]);
}
}
function GenerateText ($size){
$result_count = 0;
for ($j=0; $result_count < ; $size; ++$j){
$prev = array_rand($this-> ;prepared );
$num = mt_rand(5, 12);
for ($i=0; $i< ;$num; ++$i){
$sents[$j][$i] = $prev;
++$result_count;
$p = $this-> ;prepared [$prev][mt_rand(0, count($this-> ;prepared [$prev]) - 1)];
if ($p == '') $p = array_rand($this-> ;prepared );
$prev = $p;
if ($prev == '') break 2;
}
}
foreach ($sents as $sent){
$count_word=count($sent);
if ($count_word< ;=2) continue;
if (strlen($sent[$count_word-1]) < ; 4) unset($sent[$count_word-1]);
$sent[$count_word-2] = rtrim($sent[$count_word-2], ",:;");
$sent[$count_word-1] = rtrim($sent[$count_word-1], ",:;");
$output .= ucfirst(implode(' ', $sent)).'. ';
}
$output = str_replace(' .', '.', $output);
return $output;
}
}
? > ; |
Скачать скрипт на php для генерирования текста по правилам цепей Маркова
Цепи Маркова (782 байт, Скачено: 1,354)
Пример использования
1 2 3 4 5
| $source = "тут текст";
//file_get_contents("text.txt");
$markov = new MarkovChains ($source);
$result = $markov-> ;GenerateText (500);
echo $result; |
|