test_Cosine_Similarity.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # -*- coding: utf-8 -*-
  2. import re, math
  3. from collections import Counter
  4. # user_said = "maman je voudrais ecouter ACDC"
  5. # order = "je voudrais ecouter {{ artist_name }}"
  6. user_said = "s'il te plait regle le reveil pour dix huit heures et dix neuf minutes trente trois secondes cent quatre vingt dix "
  7. user_said_list = [" regle le reveil pour neuf heures et quinze minutes trente trois secondes ",
  8. "s'il te plait regle le reveil pour dix huit huf minutes trente trois secondes cent quatre vingt dix ",
  9. "regle pour dix huit heures et trente trois secondes cent quatre vingt dix ",
  10. "s'il te plait regle le reveil poutes trente trois secondes cent quatre vingt dix ",
  11. "RIEN A VOIR",
  12. " minutes neuf trente reveil regle secondes quinze le heures et trois pour "
  13. ]
  14. order = "{{ politesse }} regle le reveil pour {{ hour}} heures et {{minute }} minutes {{ seconde }} secondes {{mili}}"
  15. order_list = ["regle le reveil pour heures et minutes secondes ",
  16. "{{ politesse }} regle le reveil pour {{ hour}} heures et {{minute }} minutes {{ seconde }} secondes {{mili}}",
  17. "politesse regle le reveil pour hour heures et minute minutes seconde secondes mili",
  18. " reveil pour {{ hour}} et {{minute }} minutes secondes {{mili}}",
  19. "{{ politesse }} regle le reveil pour "
  20. ]
  21. # take a look to each order
  22. WORD = re.compile(r'\w+')
  23. def get_cosine(vec1, vec2):
  24. intersection = set(vec1.keys()) & set(vec2.keys())
  25. numerator = sum([vec1[x] * vec2[x] for x in intersection])
  26. sum1 = sum([vec1[x]**2 for x in vec1.keys()])
  27. sum2 = sum([vec2[x]**2 for x in vec2.keys()])
  28. denominator = math.sqrt(sum1) * math.sqrt(sum2)
  29. if not denominator:
  30. return 0.0
  31. else:
  32. return float(numerator) / denominator
  33. def text_to_vector(text):
  34. words = WORD.findall(text)
  35. return Counter(words)
  36. def _is_containing_bracket(sentence):
  37. # print "sentence to test %s" % sentence
  38. pattern = r"{{|}}"
  39. # prog = re.compile(pattern)
  40. bool = re.search(pattern, sentence)
  41. if bool is not None:
  42. return True
  43. return False
  44. def _get_next_value_list(list):
  45. ite = list.__iter__()
  46. next(ite, None)
  47. return next(ite, None)
  48. # check if the order contain bracket
  49. if _is_containing_bracket(order):
  50. # remove white space between {{ and }}
  51. # get a table of word said
  52. list_word_in_order = re.sub('\s+(?=[^\{\{\}\}]*\}\})', '',order).split()
  53. print "order matched: %s" % list_word_in_order
  54. # get the order, defined by the first words before {{
  55. the_order = order[:order.find('{{')]
  56. print "the order catched %s" % the_order
  57. # remove sentence before order
  58. nb = user_said[user_said.find(the_order):]
  59. truncate_list_word_said = nb.split()
  60. print "truncate_list_word_said : %s" % truncate_list_word_said
  61. # make dict var:value
  62. dictVar = {}
  63. for idx, ow in enumerate(list_word_in_order):
  64. if _is_containing_bracket(ow):
  65. # remove bracket et key dict
  66. varname = ow.replace("{{","").replace("}}", "")
  67. stopValue = _get_next_value_list(list_word_in_order[idx:])
  68. if stopValue is None:
  69. dictVar[varname] = " ".join(truncate_list_word_said)
  70. break
  71. for word_said in truncate_list_word_said:
  72. if word_said == stopValue: break
  73. if varname in dictVar:
  74. dictVar[varname] += " " + word_said
  75. truncate_list_word_said = truncate_list_word_said[1:]
  76. else:
  77. dictVar[varname] = word_said
  78. truncate_list_word_said = truncate_list_word_said[1:]
  79. print "The dict Var : %s" % dictVar
  80. for us in user_said_list:
  81. for od in order_list:
  82. vector1 = text_to_vector(us)
  83. vector2 = text_to_vector(od)
  84. cosine = get_cosine(vector1, vector2)
  85. print "Cosine -> ", cosine, " for usersaid: ",us, " ,order:", od