test.py 3.2 KB

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