Cleverbot Clone - Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
iKevz
Forum Newbie
Posts: 4
Joined: Wed Aug 17, 2011 3:13 pm

Cleverbot Clone - Help

Post by iKevz »

Hey guys, i've got a slight problem with a code i've been writing. Dont worry about the horrible mess it is atm, basically i've been trying to create a clone of the website CleverBot.

This is how i believe it works

1. person asks a question.
2. Bot looks at database for the question and spits out the answer if found.
3. If the answer was not found, just respond "I don't know" or something similar. Store the question in the database.
4. Spit out one of the unanswered questions from the database. Record the answer for future person

Make this basic system and add a few hundred "stock" questions and answers, a few features to increase humanishness (like store multiple possible answers for each question) and a few hard-coded answers to "stumpers" like "What time is it?" and swear words.


1. User A says "Where do you live?".
2. Cleverbot says "Huh?"
3. Cleverbot asks User B, later, "Where do you live?".
4. User B responds "I live in Devon."
5. Later, User C asks Cleverbot, "Where do you live?".
6. Cleverbot responds "I live in Devon.", echoing User B.
7. User C corrects Cleverbot with "They say I live in Devon."
8. Cleverbot stores that response in its database.
9. I could say "I live in Devon.", and Cleverbot spits out the correction. Without even knowing it's making a correction. As far as it knows it's just another question with a stored answer.

and this is what i've wrote so far,

Code: Select all

<?php
error_reporting(0);

// Establish A Connection, And Select Database
$conn = mysql_connect("******","****","********");
$db = mysql_select_db("****", $conn);

if (empty($_POST['question'])) //// If the question hasn't been set yet
{
$question = $_POST['question'] = "<br /><font color='#0000FF'>Please ask me a question...</font>"; // set with dummy value
echo $question;
}else{
$question = $_POST['question'];
		$question = ucfirst(substr_replace($question, '?', 1000, 0));  /// Make the question have the first letter uppercased, add a question mark to the end of the sentance.
        $sql = mysql_query("SELECT * FROM qanda WHERE question='$question'");
		if(mysql_num_rows($sql) <= 0){ //// If no reply is found, add dummy reply
		$answer = "I don't know."; 
			if (!empty($answer)){
			$f = mysql_query("INSERT INTO qanda (id, question) VALUES ('NULL', '$question')") or die (mysql_error());
		///	$s = mysql_query("UPDATE qanda SET answer='$answer' WHERE (question='$question')") or die (mysql_error());
		}
	}
echo '<br /><font color="#800080">You:</font> '.$question;
while($row = mysql_fetch_array($sql)) {
 	$answer = $row["answer"];
 	$reply = $row["reply"];
	}
		print '<br /><font color="#0000FF">iThink:</font> '.$answer;
		if (!empty($reply)){
		print '<br /><font color="#0000FF">iThink:</font> '.$reply;
		}
		}
	///	if question is unkown, print 'i dont know', insert question as a reply to a question.
?>
<br />
<center>
<form name="question" action="index.php" method="post">
<input type="text" name="question" maxlength="2048" name="question2" size="41" title="Question" value="" />
<input type="submit" value="Question me!" />
</form> 
</center>
Keep questions simple!<br /> Don't add a '?', it will be added automatically.
I'm looking for a way to go about making the bot add a unknown question to the database, as a response to the question that was asked before,

This is how the database works

Code: Select all

CREATE TABLE IF NOT EXISTS `qanda` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `question` longtext NOT NULL,
  `answer` longtext NOT NULL,
  `reply` mediumtext NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

--
-- Dumping data for table `qanda`
--

INSERT INTO `qanda` (`id`, `question`, `answer`, `reply`) VALUES
(1, 'What is the date?', 'The date today is botdate();', ''),
(2, 'What is your name?', 'My name is Bot.', 'What about your name?'),
(3, 'Who created you?', 'I was created by, Kevin Cornish. On 15/08/11 at 01:20 AM.', ''),
(4, 'How are you?', 'I''m very well thankyou.', 'What about yourself?'),
(5, 'Hey.', 'Hello.', ''),
(6, 'Hello.', 'Hi.', ''),
(7, 'Good, thankyou.', 'You''re welcome.', '');
Dorin85
Forum Newbie
Posts: 20
Joined: Tue Aug 16, 2011 3:16 pm

Re: Cleverbot Clone - Help

Post by Dorin85 »

I think it's far more complex than that. It has some unknown fitness function that determines which answer is better than another. It probably involves punctuation, grammar and meaning detecting etc.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Cleverbot Clone - Help

Post by califdon »

First of all, congratulations on being interested in this area of computer programming. But instead of just trying to clone a particular website, I recommend that you read up a little on what's behind it (a whole lot more than you realize!). This field is a part of Artificial Intelligence, or "AI". If you google that, you will learn more in a half hour than you would by spending hours and hours in forums and trying to copy the logic of somebody else's approach to it, plus avoiding sidetracks that are dead-ends.
Dorin85
Forum Newbie
Posts: 20
Joined: Tue Aug 16, 2011 3:16 pm

Re: Cleverbot Clone - Help

Post by Dorin85 »

This field is a part of Artificial Intelligence, or "AI". If you google that, you will learn more in a half hour than you would by spending hours and hours in forums and trying to copy the logic of somebody else's approach to it, plus avoiding sidetracks that are dead-ends.
More specifically, look up "fitness functions" and try to imagine how it could relate to your current project.
Post Reply