好的,所以我的应用程序会在我的网站内的数据库中找到你在文本框中输入的单词..如果有超过1个“单词”,它会找到“单词”,它将通过每个单词并选择一个随机的“回复”
我怎么能这样做,如果它找不到“嘿”这个词,它会寻找像“他”等类似的东西?
这是我的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";
}
}
?>
这是我的java部分,它实际上得到了响应:
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);
}
}
答案 0 :(得分:1)
您可以使用名为metaphone的PHP函数。
还有一个名为soundex的函数,它返回表示其声音的单词的数字代码。听起来相似的单词将具有相同的soundex代码。您可以拥有一个包含单词及其soundex代码的表格,您可以使用它来查找类似的发音单词。然后你可以使用他们的levenshtein距离对它们进行排序
与soundex()类似,metaphone为类似的发声词创建相同的键。它比soundex()更准确,因为它知道英语发音的基本规则。 metaphone生成的密钥长度可变。
在MySQL中,您可以使用SOUNDEX功能。您只需要在
中更改查询中的where子句 ...WHERE `word` = '$text'...
到
...WHERE soundex(word) = soundex('$text')...
甚至更好,您可以使用SOUNDS LIKE运算符
...WHERE word sounds like '$text'...
希望这有帮助