Файл: adultscript-2.0.3-pro/files/modules/video/helpers/synonyms.php
Строк: 105
<?php
defined('_VALID') or die('Restricted Access!');
define('PROCESS_METHOD', 'array');
class VHelper_video_synonyms
{
public static $words;
public static function rebuild()
{
$words = array();
$db = VF::factory('database');
$db->query("SELECT word FROM #__words");
if (!$db->affected_rows()) {
return;
}
foreach ($db->fetch_rows() as $row) {
$words[$row['word']] = 1;
}
$file = TMP_DIR.'/cache/config/words_'.md5(VF::cfg_item('secret')).'.php';
file_put_contents($file, "<?phpnreturn ".var_export($words, true).";n");
}
public static function load()
{
$file = TMP_DIR.'/cache/config/words_'.md5(VF::cfg_item('secret')).'.php';
if (!file_exists($file)) {
self::rebuild();
}
return require $file;
}
public static function process($title)
{
if (!self::$words) {
self::$words = self::load();
}
if (PROCESS_METHOD == 'array') {
return self::process_array($title);
} elseif (PROCESS_METHOD == 'replace') {
return self::process_replace($title);
}
}
public static function process_array($title)
{
$strings = explode(' ', $title);
$processed = array();
$count = count($strings);
$two = false;
foreach ($strings as $index => $string) {
if ($two) {
$two = false;
continue;
}
$string = trim($string);
$processed[$index] = $string;
//reset 2 words;
$two = false;
if (utf8_strlen($string) < 3) {
continue;
}
$upper = false;
$lower = utf8_strtolower($string);
if ($string != $lower) {
$upper = true;
}
$next = $index+1;
if (isset($strings[$next])) {
$tstring = $string.' '.$strings[$next];
if (isset(self::$words[$tstring])) {
$synonym = self::synonym($tstring);
$processed[$index] = ($upper) ? utf8_ucwords($synonym) : $synonym;
$two = true;
continue;
}
}
if (isset(self::$words[$lower])) {
$synonym = self::synonym($lower);
$processed[$index] = ($upper) ? utf8_ucwords($synonym) : $synonym;
}
}
return implode(' ', $processed);
}
public static function process_replace($title)
{
foreach (self::$words as $word => $value) {
if (stripos($title, $word) !== false) {
$title = str_ireplace($word, self::synonym($word), $title);
}
}
return $title;
}
public static function synonym($word)
{
$db = VF::factory('database');
$db->query("
SELECT GROUP_CONCAT(ws.word) AS synonyms
FROM #__words AS w
INNER JOIN #__synonyms AS s ON (s.word_id = w.word_id AND s.synonym_id != w.word_id)
INNER JOIN #__words AS ws ON (ws.word_id = s.synonym_id)
WHERE w.word = '".$db->escape($word)."'
GROUP BY w.word_id
");
if ($db->affected_rows()) {
$synonyms = explode(',', $db->fetch_field('synonyms'));
$index = rand(0, count($synonyms)-1);
return $synonyms[$index];
}
return false;
}
}