Answers.Body: <p>To compare the strings you can use Fuzzy string matching. <a href="https://github.com/seatgeek/fuzzywuzzy" rel="nofollow noreferrer">FuzzyWuzzy</a> is a python package that does this using the <a href="https://en.wikipedia.org/wiki/Levenshtein_distance" rel="nofollow noreferrer">Levenshtein distance</a>, which calculates the difference between two strings by counting the number of single character edits (insert, delete, substitute) that have to be done to make them equal.</p>
  
  <hr>
  
  <p>To install the package you can use <code>pip</code> for example:</p>
  
  <pre><code>pip install fuzzywuzzy
  </code></pre>
  
  <p>Then you can use it to check the similarity between two strings (100 is the highest score; higher score means more similar):</p>
  
  <pre><code>&gt;&gt;&gt; from fuzzywuzzy import fuzz
  &gt;&gt;&gt; fuzz.ratio("How are you?", "How are you?")
  100
  &gt;&gt;&gt; fuzz.ratio("How are you?", "How are you doing?")
  80
  </code></pre>
  
  <p>Or, which might be most interesting to you, find the most similar string from a list of strings:</p>
  
  <pre><code>from fuzzywuzzy import process
  choices = ['HI', 'HELLO', 'HEY', 'GOOD MORNING','TELL ME A JOKE', 'JOKE', 'FUNNY','GIVE ME A TIP', 'TIP', 'LESSON','WHAT IS YOUR NAME', 'WHO ARE YOU', 'HOW ARE YOU?']
  </code></pre>
  
  <p>You can get a list of the phrases with the highest score (parameter limit, 2 in this example):</p>
  
  <pre><code>&gt;&gt;&gt; process.extract("hello there", choices, limit=2)
  [('HELLO', 90), ('HEY', 60)]
  &gt;&gt;&gt; process.extract("please make a joke", choices, limit=2)
  [('JOKE', 90), ('TELL ME A JOKE', 69)]
  </code></pre>
  
  <p>Or just get the one with the highest score:</p>
  
  <pre><code>&gt;&gt;&gt; process.extractOne("whoe are you?", choices)
  ('WHO ARE YOU', 96)
  &gt;&gt;&gt; process.extractOne("I want a lesson", choices)
  ('LESSON', 90)
  </code></pre>
  
  <p><em>Note</em>:</p>
  
  <p>There are cases where another phrase might give a relatively high score also:</p>
  
  <pre><code>&gt;&gt;&gt; fuzz.ratio("How are you?", "Who are you?")
  83
  </code></pre>
  
  <p>But as long as you also have "Who are you" and "How are you" in the list, they will be detected correctly:</p>
  
  <pre><code>&gt;&gt;&gt; process.extract("Who are you?", choices, limit=2)
  [('WHO ARE YOU', 100), ('HOW ARE YOU?', 91)]
  &gt;&gt;&gt; process.extract("How are you?", choices, limit=2)
  [('HOW ARE YOU?', 100), ('WHO ARE YOU', 91)]
  </code></pre>
  
  <hr>
  
  <p>Also see: <a href="http://chairnerd.seatgeek.com/fuzzywuzzy-fuzzy-string-matching-in-python/" rel="nofollow noreferrer">FuzzyWuzzy: Fuzzy String Matching in Python</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-11T16:00:00.973Z
Answers.LastActivityDate: 2016-12-11T16:00:00.973Z
Answers.UserDisplayName: agold
Body: <p>I have started to make a Python AI, and thee beginning of its code looks something like this:</p>
  
  <pre><code>print "ARTEMIS starting. . ."
  
  import random
  import math
  import os
  
  greet = ['HI', 'HELLO', 'HEY', 'GOOD MORNING', 'GOOD DAY', 'GOOD AFTERNOON',               'GOOD EVENING', 'GREETINGS', 'GREETING']
  joke = ['TELL ME A JOKE', 'JOKE', 'FUNNY', 'TELL ME SOMETHING FUNNY']
  insult = ['YOURE A LOSER', 'YOU ARE A LOSER', 'YOU STINK', 'IDIOT', 'JERK',     'FOOL', 'DUMMY', 'HOOLIGAN', 'YOURE DUMB', 'YOURE STUPID', 'YOU ARE DUMB', 'YOU     ARE STUPID']
  maker = ['WHO MADE YOU', 'WHO PROGRAMMED YOU', 'PLEASE TELL ME WHO MADE     YOU', 'PLEASE TELL ME WHO PROGRAMMED YOU']
  name = ['ARTEMIS', 'A.R.T.E.M.I.S.', 'HEY ARTEMIS', 'HEY A.R.T.E.M.I.S.',     'ARTIE', 'HEY ARTIE', 'HELLO ARTEMIS', 'HELLO A.R.T.E.M.I.S.', 'HELLO ARTIE']
  myAge = ['HOW OLD AM I', 'WHAT IS MY AGE', 'MY AGE']
  tip = ['GIVE ME A TIP', 'TIP', 'LESSON', 'GIVE ME A LIFE LESSON', 'LIFE     LESSON', 'DO YOU HAVE A LIFE LESSON TO SHARE']
  language = ['WHAT PROGRAMMING LANGUAGE WAS USED TO MAKE YOU', 'WHAT     PROGRAMMING LANGUAGE DO YOU USE', 'PROGRAMMING LANGUAGE']
  compliment = ['COOL', 'AWESOME', 'I LIKE YOU', 'EXCELLENT', 'YOURE COOL',     'YOURE AWESOME', 'YOU ARE COOL', 'YOU ARE AWESOME']
  maths = ['LETS DO MATH', 'CALCULATE', 'CALCULATOR','DO MATH', 'MATH',      'PLEASE DO MATH', 'DO ARITHMETIC', 'ARITHMETIC', 'PLEASE DO ARITHMETIC']
  game = ['GAME', 'LETS PLAY A GAME', 'LETS HAVE FUN', 'WANT TO PLAY A GAME']
  gender = ['WHAT GENDER ARE YOU', 'ARE YOU A BOY OR A GIRL', 'ARE YOU MALE OR     FEMALE', 'BOY OR GIRL', 'MALE OR FEMALE', 'GENDER', 'ARE YOU A BOY OR GIRL']
  guessWhat = ['GUESS WHAT', 'GUESS WHAT ARTEMIS', 'GUESS WHAT     A.R.T.E.M.I.S.', 'GUESS WHAT ARTIE', 'YOU WONT BELIEVE IT', 'YOU WILL NOT     BELIEVE IT', 'YOU WONT BELIEVE IT ARTEMIS', 'YOU WILL NOT BELIEVE IT ARTEMIS',     'YOU WONT BELIEVE IT A.R.T.E.M.I.S.', 'YOU WILL NOT BELIEVE IT A.R.T.E.M.I.S.',     'YOU WONT BELIEVE IT ARTIE', 'YOU WILL NOT BELIEVE IT ARTIE']
  cls = ['CLEAR SCREEN', 'CLEARSCREEN', 'CLS', 'BLANK']
  lawsOfRobotics = ['WHAT ARE THE LAWS OF ROBOTICS', 'WHAT ARE THE THREE LAWS     OF ROBOTICS', 'LAWS OF ROBOTICS', 'THREE LAWS OF ROBOTICS']
  itsName = ['WHATS YOUR NAME', 'WHAT IS YOUR NAME', 'WHO ARE YOU']
  </code></pre>
  
  <p>However, I would like to know if I could make it detect "similar" phrases instead of trying to come up with every possible phrase someone would type. How can I do this?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-12-11T11:36:12.317Z
Comments.Text: To let you know. The programming questions should be asked at [Stack Overflow](http://stackoverflow.com/).
Comments.UserDisplayName: kenorb
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-01-10T17:11:46.163Z
Comments.Text: I would also look at the NLTK Natural Language Toolkit for Python. It is intended for Natural Language teaching, but can also be used for small scale production work.
Comments.UserDisplayName: winwaed
Tags: <algorithm>
Title: How to efficiently interpret phrases in a Python AI?
UserDisplayName: anonymous
ThreadText: # How to efficiently interpret phrases in a Python AI?
  
  (Asked by: username_0 on 2016-12-11)
  
  I have started to make a Python AI, and thee beginning of its code looks something like this:
  print "ARTEMIS starting. . ."
  
  import random
  import math
  import os
  
  greet = ['HI', 'HELLO', 'HEY', 'GOOD MORNING', 'GOOD DAY', 'GOOD AFTERNOON',               'GOOD EVENING', 'GREETINGS', 'GREETING']
  joke = ['TELL ME A JOKE', 'JOKE', 'FUNNY', 'TELL ME SOMETHING FUNNY']
  insult = ['YOURE A LOSER', 'YOU ARE A LOSER', 'YOU STINK', 'IDIOT', 'JERK',     'FOOL', 'DUMMY', 'HOOLIGAN', 'YOURE DUMB', 'YOURE STUPID', 'YOU ARE DUMB', 'YOU     ARE STUPID']
  maker = ['WHO MADE YOU', 'WHO PROGRAMMED YOU', 'PLEASE TELL ME WHO MADE     YOU', 'PLEASE TELL ME WHO PROGRAMMED YOU']
  name = ['ARTEMIS', 'A.R.T.E.M.I.S.', 'HEY ARTEMIS', 'HEY A.R.T.E.M.I.S.',     'ARTIE', 'HEY ARTIE', 'HELLO ARTEMIS', 'HELLO A.R.T.E.M.I.S.', 'HELLO ARTIE']
  myAge = ['HOW OLD AM I', 'WHAT IS MY AGE', 'MY AGE']
  tip = ['GIVE ME A TIP', 'TIP', 'LESSON', 'GIVE ME A LIFE LESSON', 'LIFE     LESSON', 'DO YOU HAVE A LIFE LESSON TO SHARE']
  language = ['WHAT PROGRAMMING LANGUAGE WAS USED TO MAKE YOU', 'WHAT     PROGRAMMING LANGUAGE DO YOU USE', 'PROGRAMMING LANGUAGE']
  compliment = ['COOL', 'AWESOME', 'I LIKE YOU', 'EXCELLENT', 'YOURE COOL',     'YOURE AWESOME', 'YOU ARE COOL', 'YOU ARE AWESOME']
  maths = ['LETS DO MATH', 'CALCULATE', 'CALCULATOR','DO MATH', 'MATH',      'PLEASE DO MATH', 'DO ARITHMETIC', 'ARITHMETIC', 'PLEASE DO ARITHMETIC']
  game = ['GAME', 'LETS PLAY A GAME', 'LETS HAVE FUN', 'WANT TO PLAY A GAME']
  gender = ['WHAT GENDER ARE YOU', 'ARE YOU A BOY OR A GIRL', 'ARE YOU MALE OR     FEMALE', 'BOY OR GIRL', 'MALE OR FEMALE', 'GENDER', 'ARE YOU A BOY OR GIRL']
  guessWhat = ['GUESS WHAT', 'GUESS WHAT ARTEMIS', 'GUESS WHAT     A.R.T.E.M.I.S.', 'GUESS WHAT ARTIE', 'YOU WONT BELIEVE IT', 'YOU WILL NOT     BELIEVE IT', 'YOU WONT BELIEVE IT ARTEMIS', 'YOU WILL NOT BELIEVE IT ARTEMIS',     'YOU WONT BELIEVE IT A.R.T.E.M.I.S.', 'YOU WILL NOT BELIEVE IT A.R.T.E.M.I.S.',     'YOU WONT BELIEVE IT ARTIE', 'YOU WILL NOT BELIEVE IT ARTIE']
  cls = ['CLEAR SCREEN', 'CLEARSCREEN', 'CLS', 'BLANK']
  lawsOfRobotics = ['WHAT ARE THE LAWS OF ROBOTICS', 'WHAT ARE THE THREE LAWS     OF ROBOTICS', 'LAWS OF ROBOTICS', 'THREE LAWS OF ROBOTICS']
  itsName = ['WHATS YOUR NAME', 'WHAT IS YOUR NAME', 'WHO ARE YOU']
  
  However, I would like to know if I could make it detect "similar" phrases instead of trying to come up with every possible phrase someone would type. How can I do this?
  
  **Tags:** algorithm
  > **Comments:**
  > **username_1**: To let you know. The programming questions should be asked at [Stack Overflow](http://stackoverflow.com/).  
  > **username_2**: I would also look at the NLTK Natural Language Toolkit for Python. It is intended for Natural Language teaching, but can also be used for small scale production work.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_3 on 2016-12-11. Score: 2
  
  To compare the strings you can use Fuzzy string matching. FuzzyWuzzy is a python package that does this using the Levenshtein distance, which calculates the difference between two strings by counting the number of single character edits (insert, delete, substitute) that have to be done to make them equal.
  
  To install the package you can use pip for example:
  pip install fuzzywuzzy
  
  Then you can use it to check the similarity between two strings (100 is the highest score; higher score means more similar):
  >>> from fuzzywuzzy import fuzz
  >>> fuzz.ratio("How are you?", "How are you?")
  100
  >>> fuzz.ratio("How are you?", "How are you doing?")
  80
  
  Or, which might be most interesting to you, find the most similar string from a list of strings:
  from fuzzywuzzy import process
  choices = ['HI', 'HELLO', 'HEY', 'GOOD MORNING','TELL ME A JOKE', 'JOKE', 'FUNNY','GIVE ME A TIP', 'TIP', 'LESSON','WHAT IS YOUR NAME', 'WHO ARE YOU', 'HOW ARE YOU?']
  
  You can get a list of the phrases with the highest score (parameter limit, 2 in this example):
  >>> process.extract("hello there", choices, limit=2)
  [('HELLO', 90), ('HEY', 60)]
  >>> process.extract("please make a joke", choices, limit=2)
  [('JOKE', 90), ('TELL ME A JOKE', 69)]
  
  Or just get the one with the highest score:
  >>> process.extractOne("whoe are you?", choices)
  ('WHO ARE YOU', 96)
  >>> process.extractOne("I want a lesson", choices)
  ('LESSON', 90)
  
  Note:
  There are cases where another phrase might give a relatively high score also:
  >>> fuzz.ratio("How are you?", "Who are you?")
  83
  
  But as long as you also have "Who are you" and "How are you" in the list, they will be detected correctly:
  >>> process.extract("Who are you?", choices, limit=2)
  [('WHO ARE YOU', 100), ('HOW ARE YOU?', 91)]
  >>> process.extract("How are you?", choices, limit=2)
  [('HOW ARE YOU?', 100), ('WHO ARE YOU', 91)]
  
  
  Also see: FuzzyWuzzy: Fuzzy String Matching in Python
  
  
  ---
  
  