| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_help().
|
| 6 |
*/
|
| 7 |
function markov_help($section) {
|
| 8 |
switch ($section) {
|
| 9 |
case 'admin/modules#description':
|
| 10 |
return t('Provides functions to generate pseudo-text using markov chains.');
|
| 11 |
}
|
| 12 |
}
|
| 13 |
|
| 14 |
/**
|
| 15 |
* Implementation of hook_filter_tips().
|
| 16 |
*
|
| 17 |
* This hook allows filters to provide help text to users during the content
|
| 18 |
* editing process. Short tips are provided on the content editing screen, while
|
| 19 |
* long tips are provided on a separate linked page. Short tips are optional,
|
| 20 |
* but long tips are highly recommended.
|
| 21 |
*/
|
| 22 |
function markov_filter_tips($delta, $format, $long = FALSE) {
|
| 23 |
return t('Content will be turned into pseudo-english gibberish.');
|
| 24 |
}
|
| 25 |
|
| 26 |
/**
|
| 27 |
* Implementation of hook_filter().
|
| 28 |
*
|
| 29 |
* The bulk of filtering work is done here. This hook is quite complicated, so
|
| 30 |
* we'll discuss each operation it defines.
|
| 31 |
*/
|
| 32 |
function markov_filter($op, $delta = 0, $format = -1, $text = '') {
|
| 33 |
// The "list" operation provides the module an opportunity to declare both how
|
| 34 |
// many filters it defines and a human-readable name for each filter. Note that
|
| 35 |
// the returned name should be passed through t() for translation.
|
| 36 |
if ($op == 'list') {
|
| 37 |
return array(
|
| 38 |
0 => t('Markovize'));
|
| 39 |
}
|
| 40 |
|
| 41 |
// All operations besides "list" provide a $delta argument so we know which
|
| 42 |
// filter they refer to. We'll switch on that argument now so that we can
|
| 43 |
// discuss each filter in turn.
|
| 44 |
switch ($delta) {
|
| 45 |
|
| 46 |
// First we define the simple string substitution filter.
|
| 47 |
case 0:
|
| 48 |
switch ($op) {
|
| 49 |
// This description is shown in the administrative interface, unlike the
|
| 50 |
// filter tips which are shown in the content editing interface.
|
| 51 |
case 'description':
|
| 52 |
return t('Turns content into pseudo-english gibberish.');
|
| 53 |
|
| 54 |
// We don't need the "prepare" operation for this filter, but it's required
|
| 55 |
// to at least return the input text as-is.
|
| 56 |
case 'prepare':
|
| 57 |
return $text;
|
| 58 |
|
| 59 |
// The actual filtering is performed here. The supplied text should be
|
| 60 |
// returned, once any necessary substitutions have taken place.
|
| 61 |
case 'process':
|
| 62 |
return markov_generate_text($text);
|
| 63 |
}
|
| 64 |
break;
|
| 65 |
}
|
| 66 |
}
|
| 67 |
|
| 68 |
function markov_generate_gibberish_from_file($filename = '', $length = 50) {
|
| 69 |
$filename = drupal_get_path('module', 'markov') . '/' . $filename;
|
| 70 |
$handle = fopen($filename, "r");
|
| 71 |
$contents = fread($handle, filesize($filename));
|
| 72 |
fclose($handle);
|
| 73 |
|
| 74 |
return markov_generate_text($contents, $length);
|
| 75 |
}
|
| 76 |
|
| 77 |
function markov_generate_user_gibberish($uid = 0, $limit = 10, $length = 50) {
|
| 78 |
$sql = "SELECT body FROM {node_revisions} nr WHERE uid = %d ORDER BY RAND() ASC";
|
| 79 |
$result = db_query($sql, $uid);
|
| 80 |
|
| 81 |
while ($node_text = db_fetch_object($result)) {
|
| 82 |
$text .= _markov_strip_tags_in_big_string($node_text->body);
|
| 83 |
$count++;
|
| 84 |
if ($count > $limit) {
|
| 85 |
break;
|
| 86 |
}
|
| 87 |
}
|
| 88 |
|
| 89 |
return markov_generate_text($text);
|
| 90 |
}
|
| 91 |
|
| 92 |
function markov_generate_text($incoming = '', $sentences = 10) {
|
| 93 |
if ($incoming == '') {
|
| 94 |
return t('Not enough text was provided.');
|
| 95 |
}
|
| 96 |
|
| 97 |
$words = explode(' ', $incoming);
|
| 98 |
|
| 99 |
$end_sentence = array();
|
| 100 |
$dict = array();
|
| 101 |
$prev1 = '';
|
| 102 |
$prev2 = '';
|
| 103 |
foreach ($words as $word) {
|
| 104 |
if ($prev1 != '' && $prev2 != '') {
|
| 105 |
$key = ($prev2 . ' ' . $prev1);
|
| 106 |
if (array_key_exists($key, $dict)) {
|
| 107 |
$dict[$key][] = $word;
|
| 108 |
}
|
| 109 |
else {
|
| 110 |
$dict[$key] = array($word);
|
| 111 |
if (_markov_string_is_end($prev1)) {
|
| 112 |
$end_sentence[] = $key;
|
| 113 |
}
|
| 114 |
}
|
| 115 |
}
|
| 116 |
$prev2 = $prev1;
|
| 117 |
$prev1 = $word;
|
| 118 |
}
|
| 119 |
|
| 120 |
if (count($end_sentence) == 0 || count($dict) == 0) {
|
| 121 |
return t('Sorry, there are no sentences in the text.');
|
| 122 |
}
|
| 123 |
|
| 124 |
$key = '';
|
| 125 |
|
| 126 |
while ($sentences > 0) {
|
| 127 |
if (array_key_exists($key, $dict)) {
|
| 128 |
$word = $dict[$key][rand(0, count($dict[$key]))];
|
| 129 |
$output .= $word . ' ';
|
| 130 |
$old_key = explode(' ', $key);
|
| 131 |
$key = $old_key[1] . ' ' . $word;
|
| 132 |
if (in_array($key, $end_sentence)) {
|
| 133 |
$output .= $key . ' ';
|
| 134 |
if (rand(1,5) == 1) {
|
| 135 |
$output .= "\n\n";
|
| 136 |
}
|
| 137 |
$sentences--;
|
| 138 |
}
|
| 139 |
}
|
| 140 |
else {
|
| 141 |
$key = $end_sentence[rand(0, count($end_sentence))];
|
| 142 |
}
|
| 143 |
}
|
| 144 |
|
| 145 |
return $output;
|
| 146 |
}
|
| 147 |
|
| 148 |
function _markov_string_is_end($string) {
|
| 149 |
if (strstr($string, '.') || strstr($string, '!') || strstr($string, '?') || strstr($string, '...')) {
|
| 150 |
return TRUE;
|
| 151 |
}
|
| 152 |
else {
|
| 153 |
return FALSE;
|
| 154 |
}
|
| 155 |
}
|
| 156 |
|
| 157 |
function _markov_strip_tags_in_big_string($textstring){
|
| 158 |
while (strlen($textstring) != 0) {
|
| 159 |
$temptext = strip_tags(substr($textstring,0,1024));
|
| 160 |
$safetext .= $temptext;
|
| 161 |
$textstring = substr_replace($textstring,'',0,1024);
|
| 162 |
}
|
| 163 |
return $safetext;
|
| 164 |
}
|