Example #1
0
def test_parameterized_constructor():
    ng = Ngrams("this is a test", 3)
    grams1 = {"this", "is", "a", "test"}
    grams2 = {"this is", "is a", "a test"}
    grams3 = {"this is a", "is a test"}
    assert ng == set.union(grams1, grams2, grams3)
    assert ng[1] == grams1
    assert ng[2] == grams2
    assert ng[3] == grams3
    assert ng.text() == "this is a test"
Example #2
0
def test_parameterized_constructor():
    ng = Ngrams('this is a test', 3)
    grams1 = {'this', 'is', 'a', 'test'}
    grams2 = {'this is', 'is a', 'a test'}
    grams3 = {'this is a', 'is a test'}
    assert ng == set.union(grams1, grams2, grams3)
    assert ng[1] == grams1
    assert ng[2] == grams2
    assert ng[3] == grams3
    assert ng.text() == 'this is a test'
Example #3
0
def test_default_constructor():
    ng = Ngrams("this is a test sentence")
    grams1 = {"this", "is", "a", "test", "sentence"}
    grams2 = {"this is", "is a", "a test", "test sentence"}
    grams3 = {"this is a", "is a test", "a test sentence"}
    grams4 = {"this is a test", "is a test sentence"}
    grams5 = {"this is a test sentence"}
    assert ng == set.union(grams1, grams2, grams3, grams4, grams5)
    assert ng[1] == grams1
    assert ng[2] == grams2
    assert ng[3] == grams3
    assert ng[4] == grams4
    assert ng[5] == grams5
    assert ng.text() == "this is a test sentence"
Example #4
0
def test_default_constructor():
    ng = Ngrams("this is a test sentence")
    grams1 = {'this', 'is', 'a', 'test', 'sentence'}
    grams2 = {'this is', 'is a', 'a test', 'test sentence'}
    grams3 = {'this is a', 'is a test', 'a test sentence'}
    grams4 = {'this is a test', 'is a test sentence'}
    grams5 = {'this is a test sentence'}
    assert ng == set.union(grams1, grams2, grams3, grams4, grams5)
    assert ng[1] == grams1
    assert ng[2] == grams2
    assert ng[3] == grams3
    assert ng[4] == grams4
    assert ng[5] == grams5
    assert ng.text() == 'this is a test sentence'
Example #5
0
 def match(self,
           natural_language,
           vars=None,
           macros=None,
           ngrams=None,
           debugging=False):
     natural_language += ' _END_'
     if vars is None:
         vars = HashableDict()
     original_vars = vars
     vars = HashableDict(vars)
     if ngrams is None:
         ngrams = Ngrams(natural_language)
     self.compile(ngrams, vars, macros, debugging)
     match = regex.fullmatch(self._regex, natural_language)
     self._regex = None
     if match:
         vars.update(
             {k: v
              for k, v in match.groupdict().items() if v is not None})
         original_vars.update({k: vars[k] for k in vars})
     return match
from emora_stdm.state_transition_dialogue_manager.macros_common import WN
from emora_stdm.state_transition_dialogue_manager.ngrams import Ngrams

wn = WN()

if __name__ == '__main__':

    while True:
        i = input('Enter "<WN ARG>", or "<PHRASE> in <WN ARG>": ')
        if ' in ' in i:
            i = [x.strip() for x in i.split(' in ')]
            print(i[0] in wn.run(Ngrams(i[0]), {}, [i[1]]))
        else:
            print(wn.run(None, {}, [i]))
Example #7
0
 def user_transition(self, natural_language: str, state: Union[Enum, str, tuple], debugging=False):
     """
     :param state:
     :param natural_language:
     :param debugging:
     :return: the successor state representing the highest score user transition
              that matches natural_language, or None if none match
     """
     if '__gate__' in self._vars:
         del self._vars['__gate__']
     if '__user_utterance__' in self.vars() and self.vars()['__user_utterance__'] is not None:
         natural_language = self.vars()['__user_utterance__']
     else:
         natural_language = ''.join([c.lower() for c in natural_language if c.isalpha() or c == ' '])
     state = module_state(state)
     self._error_transitioned = False
     ti = time()
     if state is None:
         state = self.state()
     else:
         state = State(state)
     transition_options = []
     transition_items = []
     for transition in self.transitions(state, Speaker.USER):
         natex = self.transition_natex(*transition)
         score = self.transition_settings(*transition).score
         transition_items.append((natex, transition, score))
     while self._transitions:
         natex, transition, score = self._transitions.pop()
         transition_items.append((natex, transition, score))
     ngrams = Ngrams(natural_language, n=10)
     for natex, transition, score in transition_items:
         self._potential_transition = transition
         if not self.is_module() and isinstance(transition[1], tuple):
             continue
         t1 = time()
         if debugging:
             print('Evaluating transition {}'.format(transition[:2]))
         vars = HashableDict(self._vars)
         try:
             match = natex.match(natural_language, vars, self._macros, ngrams, debugging)
         except Exception as e:
             print()
             print('Transition {}: {} failed'.format(str(transition), natex))
             traceback.print_exc(file=sys.stdout)
             print()
             match = None
         source, target, speaker = transition
         if '__source__' in vars:
             source = State(module_state(vars['__source__']))
             del vars['__source__']
         if '__target__' in vars:
             target = State(module_state(vars['__target__']))
             del vars['__target__']
         transition = source, target, speaker
         if self.is_module() and isinstance(target, tuple):
             enter_natex = self.composite_dialogue_flow().state_settings(*target).enter
         else:
             enter_natex = self.state_settings(target).enter
         enter_natex_pass = True
         if enter_natex is not None:
             try:
                 enter_natex_pass = enter_natex.generate(vars=vars, macros=self._macros, debugging=debugging)
             except Exception as e:
                 print()
                 print(e)
                 print('Enter Natex {}: {} failed'.format(str(target), enter_natex))
                 print()
                 enter_natex_pass = None
         if match and enter_natex_pass is not None:
             if debugging:
                 print('Transition {} matched "{}"'.format(transition[:2], natural_language))
             if '__score__' in vars:
                 score = vars['__score__']
                 del vars['__score__']
             gate_closed = False
             gate_var_config = None
             gate_target_id = None
             if '__gate__' in vars:
                 gate_var_config = vars['__gate__']
                 gate_target_id = (self.namespace(), target) if (
                             not isinstance(target, tuple) and self.is_module()) else target
                 for vc in self.gates()[gate_target_id]:
                     if gate_var_config == vc:
                         gate_closed = True
                 del vars['__gate__']
             if not gate_closed:
                 transition_options.append((score, natex, transition, vars, gate_var_config, gate_target_id))
         t2 = time()
         if debugging:
             print('Transition {} evaluated in {:.5f}'.format(transition, t2-t1))
         while self._transitions:
             natex, transition, score = self._transitions.pop()
             transition_items.append((natex, transition, score))
     self._transitions.clear()
     if transition_options:
         if debugging:
             print('Transition options: ------------')
             for option in transition_options:
                 print('{} {}: {}'.format(option[0], option[2][1], option[1]))
             print('--------------------------------')
         score, natex, transition, vars, gate_var_config, gate_target_id = random_max(transition_options, key=lambda x: x[0])
         if gate_var_config is not None:
             self.gates()[gate_target_id].append(gate_var_config)
         if debugging:
             updates = {}
             for k, v in vars.items():
                 if k not in self._vars or v != self._vars[k]:
                     updates[k] = v
             if updates:
                 print('Updating vars:')
                 for k, v in updates.items():
                     if k in self._vars:
                         print('  {} = {} -> {}'.format(k, self._vars[k], v))
                     else:
                         print('  {} = None -> {}'.format(k, v))
         self.update_vars(vars)
         next_state = transition[1]
         if debugging:
             print('User transition in {:.5f}'.format(time() - ti))
             print('Transitioning {} -> {}'.format(self.state(), next_state))
         return next_state
     else:
         self._error_transitioned = True
         next_state = self.error_successor(self.state())
         if debugging:
             print('User transition in {:.5f}'.format(time() - ti))
             print('Error transition {} -> {}'.format(self.state(), next_state))
         return next_state