test.py 3.8 KB

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