test.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import re
  4. import logging
  5. # user_said = "maman je voudrais ecouter ACDC"
  6. # order = "je voudrais ecouter {{ artist_name }}"
  7. user_said = "s'il te plait regle le reveil pour dix huit heures et dix neuf minutes trente trois secondes cent quatre vingt dix "
  8. order = "regle le reveil pour {{ hour}} heures et {{minute }} minutes {{ seconde }} secondes {{mili}}"
  9. # take a look to each order
  10. def _is_containing_bracket(sentence):
  11. # print "sentence to test %s" % sentence
  12. pattern = r"{{|}}"
  13. # prog = re.compile(pattern)
  14. bool = re.search(pattern, sentence)
  15. if bool is not None:
  16. return True
  17. return False
  18. def _get_next_value_list(list):
  19. ite = list.__iter__()
  20. next(ite, None)
  21. return next(ite, None)
  22. # check if the order contain bracket
  23. if _is_containing_bracket(order):
  24. # remove white space between {{ and }}
  25. # get a table of word said
  26. list_word_in_order = re.sub('\s+(?=[^\{\{\}\}]*\}\})', '',order).split()
  27. print "order matched: %s" % list_word_in_order
  28. # get the order, defined by the first words before {{
  29. the_order = order[:order.find('{{')]
  30. print "the order catched %s" % the_order
  31. # remove sentence before order
  32. nb = user_said[user_said.find(the_order):]
  33. truncate_list_word_said = nb.split()
  34. print "truncate_list_word_said : %s" % truncate_list_word_said
  35. # make dict var:value
  36. dictVar = {}
  37. for idx, ow in enumerate(list_word_in_order):
  38. if _is_containing_bracket(ow):
  39. # remove bracket et key dict
  40. varname = ow.replace("{{","").replace("}}", "")
  41. stopValue = _get_next_value_list(list_word_in_order[idx:])
  42. if stopValue is None:
  43. dictVar[varname] = " ".join(truncate_list_word_said)
  44. break
  45. for word_said in truncate_list_word_said:
  46. if word_said == stopValue: break
  47. if varname in dictVar:
  48. dictVar[varname] += " " + word_said
  49. truncate_list_word_said = truncate_list_word_said[1:]
  50. else:
  51. dictVar[varname] = word_said
  52. truncate_list_word_said = truncate_list_word_said[1:]
  53. print "The dict Var : %s" % dictVar