Dustin Gibson

Note: As of 7/2/2019, I moved to digitalocean VPS. Application will not be functional yet. I need some time to migrate MySQL DB.

Markov Project (PHP, Javascript, Python, SQL)

Sentences can be generated using Markov Chains using sample text. In this project, I gathered debate transcripts from four most popular 2016 presidential candidates. I parsed the transcripts, generated insert queries, and used the speech data to generate text. Below is an application where users can guess which generated text belong to which candidate.

  • Text Processing - Python
  • Server Processing - PHP, SQL
  • Client - Javascript


Source code is posted below

Your browser does not support the HTML5 canvas tag.
Code Details

Server (PHP)

			
				
				
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);


function connect() {
	$servername = "XXXX";
	$username = "XXXX";
	$password = "XXXX";
	$dbname = "XXXX";
	$conn = new mysqli($servername, $username, $password, $dbname);
	if ($conn->connect_error) {
		die("Connection failed: " . $conn->connect_error);
		return NULL;
	} 
	return $conn;
}

function test($conn) {
	$sql = "SELECT name FROM candidate";
	$result = $conn->query($sql);
	if ($result->num_rows > 0) {
		// output data of each row
		while($row = $result->fetch_assoc()) {
			echo "name: " . $row["name"];
		}
	} else {
		echo "0 results";
	}
}

function get_single_result($conn,$sql,$row_name) {
	$result = $conn->query($sql);
	while($row = $result->fetch_assoc()) {
		return $row[$row_name];
	}
	return "";
}

function get_rand($probabilities) {
	$n = 100;
	$rand_list = array();
	foreach( $probabilities as $value) {
		array_push($rand_list, round($n*$value,0));
	}
	$choice = rand(1,$n);
	$sum = 0;
	for( $i = 0; $i < count($rand_list); $i++) {
		$sum += 1;
		if( $choice >= $sum and $choice <= $sum + $rand_list[$i]) {
			return $i;
		}
		$sum += $rand_list[$i];
	}
	return 0;
}

function iterate_text($conn, $name, $id, $value, $length, $capitalize) {
	$should_capitalize = False;
	if($length <= 0) {
		return $value;
	}
	$prob_list = array();
	$val_list = array();
	$prob_sql = "SELECT to_state, frequency FROM chain ch 
	JOIN candidate ca ON ca.id = ch.candidate
	WHERE ca.name='" . $name . "' and from_state='" . $id ."'";
	$result = $conn->query($prob_sql);
	while($row = $result->fetch_assoc()) {
		array_push($val_list, $row["to_state"]);
		array_push($prob_list, $row["frequency"]);
	}
	$state_index = get_rand($prob_list);
	$next_state = $val_list[$state_index];
	$new_value = $value . " " . $next_state;
	return iterate_text($conn, $name,  $new_value, $next_state, $length - 1, $should_capitalize);
}

function make_text($conn,$name,$length) {
	$rand_sql = "SELECT ch.from_state from_state FROM chain ch JOIN candidate ca ON ca.id = ch.candidate WHERE ca.name='" . $name . "' ORDER BY RAND() LIMIT 1";
	$rand_id = get_single_result($conn, $rand_sql, "from_sate");
	echo iterate_text($conn, $name, $rand_id, $rand_id, $length, True);
}

function disconnect($conn) {
	$conn->close();
}

$conn = connect();
make_text($conn,$_GET["name"],5);
disconnect($conn);