Commit b5111c9f authored by Milan Bartky's avatar Milan Bartky

Updated code

parent ea3088ea
......@@ -56,20 +56,6 @@ class Helper
}
return $list;
}
/**
* Shuffles all lines in a given text.
* @param string $text
* @return string
*/
public static function shuffleLines(string $text) : string
{
$texts = explode("\n", $text);
shuffle($texts);
$text = implode("\n", $texts);
return $text;
}
}
/**
......
<?php
define('SOURCE_FILE', 'data.txt');
include_once "functions.php";
include "markov.php";
......@@ -45,15 +47,31 @@ function prepareTextForFrontend(string $text) : string
$text = $nText;
return $text;
}
/**
* Removes lines that were already used
* @param string $text
*/
function removeUsedLines(string &$text)
{
$lines = explode("\n", $text);
$newLines = [];
foreach($lines as $line) {
if(!exec('grep '.escapeshellarg(trim($line)).' ./steve/steve.txt')) {
$newLines[] = $line;
}
}
$text = implode("\n", $newLines);
}
// Refresh cache when GET parameter "g" is set or the cache file does'nt exist
if (isset($_GET["g"]) || !file_exists("data.txt"))
if (isset($_GET["g"]) || !file_exists(SOURCE_FILE))
{
// Including vichans config (for database credentials)
require_once("../../www/inc/instance-config.php");
// Making sure the cache file is deleted
unlink("data.txt");
unlink(SOURCE_FILE);
$text = "";
......@@ -139,7 +157,7 @@ if (isset($_GET["g"]) || !file_exists("data.txt"))
$text = NormalizeHelper::quickCleanup($text);
// Finally saving that text to a file (so we dont have to regenerate it every time)
file_put_contents("data.txt", $text);
file_put_contents(SOURCE_FILE, $text);
// Redirecting to the same site without the "g" get parameter so we dont cache on every reload
header("Location: generator.php");
......@@ -147,13 +165,10 @@ if (isset($_GET["g"]) || !file_exists("data.txt"))
}
// Getting cached data from data.txt
$origtext = file_get_contents("data.txt");
// Shuffling lines
$text = Helper::shuffleLines($origtext);
$origtext = file_get_contents(SOURCE_FILE);
// Removing unicode to avoid utf-8 errors
$text = preg_replace('/[\x10-\x1F\x80-\xFF]/', '', $text);
$text = preg_replace('/[\x10-\x1F\x80-\xFF]/', '', $origtext);
// Building markov table and then generating text with a length of 200 characters
......@@ -174,6 +189,8 @@ NormalizeHelper::advancedCleanUp($generated);
$generated = NormalizeHelper::removeDirt($generated);
removeUsedLines($generated);
/*
* Lets get at least 5 lines of generated text
*/
......@@ -200,6 +217,8 @@ while(sizeof(explode("\n", $generated)) < 5)
$generated .= "\n";
}
$generated = trim($generated . $generated2);
removeUsedLines($generated);
}
/*
......@@ -261,6 +280,8 @@ if (isset($_GET["w"]) && Helper::includes($text, $_GET["w"]))
<title>Steve</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<?php include 'meta.html'; ?>
<script src="./rita-full.min.js"></script>
<script src="./compromise.min.js"></script>
<style>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment