In levenstein how are you
, hw r u
, how are u
, and hw ar you
can be compare as same,
Is there anyway i can achieved this
if i have a phrase like.
phrase
hi, my name is john doe. I live in new york. What is your name?
phrase
My name is Bruce. wht's your name
key phrase
What is your name
response
my name is batman.
im getting the input from user.I have a table with a list of possible request with response. for example the user will ask about 'its name', is there a way i can check if a sentence has a key phrase like What is your name
and if its found it will return the possible response
like
phrase = ' hi, my name is john doe. I live in new york. What is your name?'
//I know this one will work
if (strpos($phrase,"What is your name") !== false) {
return $response;
}
//but what if the user mistype it
if (strpos($phrase,"Wht's your name") !== false) {
return $response;
}
is there i way to achieve this. levenstein works perfect only if the lenght of strings are not that long with the compared string.
like
hi,wht's your name
my name is batman.
but if it so long
hi, my name is john doe. I live in new york. What is your name?
its not working well. if there are shorter phrase, it will identify the shorter phrase that have a shorter distance and return a wrong response
i was thinking another way around is to check some key phrase. so any idea to achieve this one?
i was working on something like this but maybe there is a better and proper way i think
$samplePhrase = 'hi, im spongebob, i work at krabby patty. i love patties. Whts your name my friend';
$keyPhrase = 'What is your name';
- get first character of
keyPhrase
. That would be 'W' iterate through $samplePhrase
characters and compare to first character ofkeyPhrase
h,i, ,i,m, ,s,p
etc. . .- if
keyPhrase.char = samplePhrase.currentChar
- get keyPhrase.length
- get samplePhrase.currentChar index
- get substring of samplePhrase base on the currentChar index to keyPhrase.length
- the first it will get would be
work at krabby pa
- compare
work at krabby pa
to $keyPhrase ('What is your name') using levenstiens distance - and to check it better use semilar_text. 11.if not equal and distance is to big repeat process.