Okay, so my app will find the word you type into the text box from a database inside my website.. and it will find the “word” if there is more than 1 “word” it will go through each and pick a random “reply”
How can I do it so that if it can’t find the word “hey” it will look for similar things like “he” etc?
Here’s my PHP:
<?php
// Connect to database
mysql_connect("server", "db username", "db password");
mysql_select_db("db405677000");
// If something is received
if($_POST)
{
if($_POST['action'] == "ask")
{
// Filter it to prevent SQL injections
$text = mysql_real_escape_string($_POST['stringdata']);
// Search it on the database
$q = mysql_query("SELECT `reply` FROM `poka` WHERE `word` = '$text' ORDER BY RAND()");
// Show result
if($r = mysql_fetch_assoc($q))
echo $r['reply'];
else
echo "Cannot find a reply";
}
elseif($_POST['action'] == "teach")
{
// Filter it to prevent SQL injections
$word = mysql_real_escape_string($_POST['word']);
$answer = mysql_real_escape_string($_POST['answer']);
// Insert it to the database
if( mysql_query("INSERT INTO `poka` VALUES(NULL, '$word', '$answer')") )
echo "ok";
else
echo "fail";
}
}
?>
And here is my java part where it actually gets the response:
public void responseGet(String textRequest)
{
EditText textbox = (EditText)findViewById(R.id.chat);
textbox.setText("");
TableLayout chatbox = (TableLayout)findViewById(R.id.chatbox);
if(canReply == false)
{
TableRow tr1 = new TableRow(this);
tr1.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
TextView textview = new TextView(this);
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://monaiz.net/get.php");
String responseStr = "";
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("stringdata", textRequest));
nameValuePairs.add(new BasicNameValuePair("action", "ask"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity( );
responseStr = EntityUtils.toString( entity ).replace("\\", "");
} catch (Exception e) {
// TODO Auto-generated catch block
}
textview.setText(responseStr);
// textview.getTextColors(R.color.)
textview.setTextColor(Color.BLACK);
textview.setGravity(Gravity.LEFT);
tr1.addView(textview);
// Convert dp to px
int padding_in_dp = 7;
final float scale = getResources().getDisplayMetrics().density;
int padding_in_px = (int) (padding_in_dp * scale + 0.5f);
tr1.setPadding(padding_in_px, 0, padding_in_px, 0);
tr1.setBackgroundResource(R.drawable.pokaspeak);
tr1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent startNewActivityOpen = new Intent(main.this, TeachmeDialog.class);
startActivityForResult(startNewActivityOpen, 0);
}
});
chatbox.addView(tr1, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
canReply = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
ScrollView s = (ScrollView) findViewById(R.id.SV);
s.fullScroll(View.FOCUS_DOWN);
}
},100);
}
}
You can use PHP function called metaphone.
There is also a function called soundex that return a numerical code for a word that represents its sound. Words that sound similar will have the same soundex code. You could have a table with words and their soundex codes that you could use to look up similar sounding words. You could then sort them using their levenshtein distance
Similar to soundex() metaphone creates the same key for similar sounding words. It’s more accurate than soundex() as it knows the basic rules of English pronunciation. The metaphone generated keys are of variable length.
In MySQL you can make use of the SOUNDEX function. You just need to change your where clause in the query from
to
or even better you can use SOUNDS LIKE operator as
Hope this helps