Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My suggestion would be to generate a list of n-grams from the key phrase and calculate the edit distance between each n-gram and the key phrase.</p> <p>Example:</p> <pre><code>key phrase: "What is your name" phrase 1: "hi, my name is john doe. I live in new york. What is your name?" phrase 2: "My name is Bruce. wht's your name" </code></pre> <p>A possible matching n-gram would be between 3 and 4 words long, therefore we create all 3-grams and 4-grams for each phrase, we should also normalize the string by removing punctuation and lowercasing everything.</p> <pre><code>phrase 1 3-grams: "hi my name", "my name is", "name is john", "is john doe", "john doe I", "doe I live"... "what is your", "is your name" phrase 1 4-grams: "hi my name is", "my name is john doe", "name is john doe I", "is john doe I live"... "what is your name" phrase 2 3-grams: "my name is", "name is bruce", "is bruce wht's", "bruce wht's your", "wht's your name" phrase 2 4-grmas: "my name is bruce", "name is bruce wht's", "is bruce wht's your", "bruce wht's your name" </code></pre> <p>Next you can do levenstein distance on each n-gram this should solve the use case you presented above. if you need to further normalize each word you can use phonetic encoders such as Double Metaphone or NYSIIS, however, I did a test with all the "common" phonetic encoders and in your case it didn't show significant improvement, phonetic encoders are more suitable for names.</p> <p>I have limited experience with PHP but here is a code example:</p> <pre><code>&lt;?php function extract_ngrams($phrase, $min_words, $max_words) { echo "Calculating N-Grams for phrase: $phrase\n"; $ngrams = array(); $words = str_word_count(strtolower($phrase), 1); $word_count = count($words); for ($i = 0; $i &lt;= $word_count - $min_words; $i++) { for ($j = $min_words; $j &lt;= $max_words &amp;&amp; ($j + $i) &lt;= $word_count; $j++) { $ngrams[] = implode(' ',array_slice($words, $i, $j)); } } return array_unique($ngrams); } function contains_key_phrase($ngrams, $key) { foreach ($ngrams as $ngram) { if (levenshtein($key, $ngram) &lt; 5) { echo "found match: $ngram\n"; return true; } } return false; } $key_phrase = "what is your name"; $phrases = array( "hi, my name is john doe. I live in new york. What is your name?", "My name is Bruce. wht's your name" ); $min_words = 3; $max_words = 4; foreach ($phrases as $phrase) { $ngrams = extract_ngrams($phrase, $min_words, $max_words); if (contains_key_phrase($ngrams,$key_phrase)) { echo "Phrase [$phrase] contains the key phrase [$key_phrase]\n"; } } ?&gt; </code></pre> <p>And the output is something like this:</p> <pre> Calculating N-Grams for phrase: hi, my name is john doe. I live in new york. What is your name? found match: what is your name Phrase [hi, my name is john doe. I live in new york. What is your name?] contains the key phrase [what is your name] Calculating N-Grams for phrase: My name is Bruce. wht's your name found match: wht's your name Phrase [My name is Bruce. wht's your name] contains the key phrase [what is your name] </pre> <p><strong>EDIT</strong>: I noticed some suggestions to add phonetic encoding to each word in the generated n-gram. I'm not sure phonetic encoding is the best answer to this problem as they are mostly tuned to stemming names (american, german or french depending on the algorithm) and are not very good at stemming plain words.</p> <p>I actually wrote a test to validate this in Java (as the encoders are more readily available) here is the output:</p> <pre> =========================== Created new phonetic matcher Engine: Caverphone2 Key Phrase: what is your name Encoded Key Phrase: WT11111111 AS11111111 YA11111111 NM11111111 Found match: [What is your name?] Encoded: WT11111111 AS11111111 YA11111111 NM11111111 Phrase: [hi, my name is john doe. I live in new york. What is your name?] MATCH: true Phrase: [My name is Bruce. wht's your name] MATCH: false =========================== Created new phonetic matcher Engine: DoubleMetaphone Key Phrase: what is your name Encoded Key Phrase: AT AS AR NM Found match: [What is your] Encoded: AT AS AR Phrase: [hi, my name is john doe. I live in new york. What is your name?] MATCH: true Found match: [wht's your name] Encoded: ATS AR NM Phrase: [My name is Bruce. wht's your name] MATCH: true =========================== Created new phonetic matcher Engine: Nysiis Key Phrase: what is your name Encoded Key Phrase: WAT I YAR NAN Found match: [What is your name?] Encoded: WAT I YAR NAN Phrase: [hi, my name is john doe. I live in new york. What is your name?] MATCH: true Found match: [wht's your name] Encoded: WT YAR NAN Phrase: [My name is Bruce. wht's your name] MATCH: true =========================== Created new phonetic matcher Engine: Soundex Key Phrase: what is your name Encoded Key Phrase: W300 I200 Y600 N500 Found match: [What is your name?] Encoded: W300 I200 Y600 N500 Phrase: [hi, my name is john doe. I live in new york. What is your name?] MATCH: true Phrase: [My name is Bruce. wht's your name] MATCH: false =========================== Created new phonetic matcher Engine: RefinedSoundex Key Phrase: what is your name Encoded Key Phrase: W06 I03 Y09 N8080 Found match: [What is your name?] Encoded: W06 I03 Y09 N8080 Phrase: [hi, my name is john doe. I live in new york. What is your name?] MATCH: true Found match: [wht's your name] Encoded: W063 Y09 N8080 Phrase: [My name is Bruce. wht's your name] MATCH: true </pre> <p>I used a levenshtein distance of 4 when running these tests, but I am pretty sure you can find multiple edge cases where using the phonetic encoder will fail to match correctly. by looking at the example you can see that because of the stemming done by the encoders you are actually more likely to have false positives when using them in this way. keep in mind that these algorithms are originally intended to find those people in the population census that have the same name and not really which english words 'sound' the same.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload