Archived
1
0
This repository has been archived on 2020-12-10. You can view files and clone it, but cannot push or open issues or pull requests.
old/application/models/NicknameModel.php

106 lines
3.0 KiB
PHP
Raw Permalink Normal View History

2018-10-16 16:28:42 +00:00
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class NicknameModel extends CI_Model {
public function __construct() {
parent::__construct();
}
function generateMarkovTable($names = [])
{
$table = [];
foreach ($names as $name) {
$letters = str_split($name);
foreach ($letters as $key => $letter) {
$letter = strtolower($letter);
if (!array_key_exists($key + 1, $letters)) {
continue;
}
$nextLetter = strtolower($letters[$key + 1]);
if (!(array_key_exists($letter, $table) && array_key_exists($nextLetter, $table[$letter]))) {
$table[$letter][$nextLetter] = 1;
continue;
}
$table[$letter][$nextLetter] = $table[$letter][$nextLetter] + 1;
}
}
return $table;
}
function generateMarkovName($table, $minLength = 3, $maxLength = 10)
{
$currentLetter = array_rand($table);
$text = [$currentLetter];
$length = mt_rand($minLength, $maxLength);
for ($i = 0; $i < $length; $i++) {
$nextLetter = $this->getRandomWeightedElement($currentLetter, $table);
if (count($text) > 1) {
$previousLetter = $text[$i - 1];
while ($previousLetter == $nextLetter) {
$nextLetter = $this->getRandomWeightedElement($currentLetter, $table);
}
}
$text[] = $nextLetter;
$currentLetter = $nextLetter;
}
return ucfirst(implode('', $text));
}
function getRandomWeightedElement($key, array $table)
{
if (array_key_exists($key, $table)) {
$weightedValues = $table[$key];
$rand = mt_rand(1, (int) array_sum($weightedValues));
foreach ($weightedValues as $key => $value) {
$rand -= $value;
if ($rand <= 0) {
return $key;
}
}
}
return array_rand($table);
}
public function generateName() {
$names = [
'Gronkh',
'Currywurst',
'KingOfDog',
'Zeybe',
'unge',
'Dner',
'Arazhul',
'Hopson',
'Carykh',
'Benx',
'Demaster',
'Manucraft',
'Minecraft',
'ProPlayer91',
'Green',
'Blue',
'Red',
'Ultra',
'Pro',
'Racing',
'Shooter',
'King',
'Yellow',
'Gold',
'xXXx',
'iTz_',
'Running',
'YT',
'TV',
'HD',
'Huge',
'Best',
'imFlying',
'Not'
];
$table = $this->generateMarkovTable($names);
echo str_pad($this->generateMarkovName($table, 4, 7), 12);
}
}