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"
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'
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"
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'
def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[Any]): doc = nlp(ngrams.text()) entities = set() for ent in doc.ents: if not args or ent.label_.lower() in {x.lower() for x in args}: entities.add(ent.text) return entities
def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[Any]): results = self.analyzer.polarity_scores(ngrams.text()) print(results) if "pos" in args[0].lower(): return results["pos"] > results["neu"] and results["pos"] > results["neg"] elif "neg" in args[0].lower(): return results["neg"] > results["neu"] and results["neg"] > results["pos"] elif "neu" in args[0].lower(): return results["neu"] > results["pos"] and results["neu"] > results["neg"] else: return results["pos"] > results["neu"] or results["neg"] > results["neu"]
def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[Any]): results = self.analyzer.polarity_scores(ngrams.text()) print(results) if 'pos' in args[0].lower(): return results['pos'] > results['neu'] and results['pos'] > results['neg'] elif 'neg' in args[0].lower(): return results['neg'] > results['neu'] and results['neg'] > results['pos'] elif 'neu' in args[0].lower(): return results['neu'] > results['pos'] and results['neu'] > results['neg'] else: return results['pos'] > results['neu'] or results['neg'] > results['neu']
def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[Any]): statement_only = 's' in args or 'state' in args or 'statement' in args vars['__score__'] = 0.0 if '__previous_unx_response__' not in vars: vars['__previous_unx_response__'] = 'Gotcha.' if '__previous_unx_answer__' not in vars: vars['__previous_unx_answer__'] = 'None' is_question = self.question_natex.match(ngrams.text()) if is_question and statement_only: return False elif is_question: if '_explained_stupidity_' in vars and vars[ '_explained_stupidity_'] == 'True': options = { 'I\'m not sure.', 'I don\'t know.', 'I\'m not sure about that.', '' } - {vars['__previous_unx_response__']} question_response = random.choice(list(options)) vars['__previous_unx_answer__'] = question_response vars['__response_prefix__'] = question_response else: vars['_explained_stupidity_'] = 'True' vars['__response_prefix__'] = 'I\'m not sure.' elif len(ngrams.text().split()) < 3 and len(args) == 0: vars['__response_prefix__'] = '' return True else: options = {'Yeah.', 'For sure.', 'Right.', 'Uh-huh.' } - {vars['__previous_unx_response__']} statement_response = random.choice(list(options)) if len(args) > 0: statement_response = ', '.join([ arg for arg in args if arg not in {'s', 'state', 'statement'} ]) + ', ' if args[0] == 'None': statement_response = '' vars['__previous_unx_response__'] = statement_response vars['__response_prefix__'] = statement_response return True
def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[Any]): statement_only = "s" in args or "state" in args or "statement" in args vars["__score__"] = 0.0 if "__previous_unx_response__" not in vars: vars["__previous_unx_response__"] = "Gotcha." if "__previous_unx_answer__" not in vars: vars["__previous_unx_answer__"] = "None" is_question = self.question_natex.match(ngrams.text()) if is_question and statement_only: return False elif is_question: if "_explained_stupidity_" in vars and vars[ "_explained_stupidity_"] == "True": options = { "I'm not sure.", "I don't know.", "I'm not sure about that.", "" } - {vars["__previous_unx_response__"]} question_response = random.choice(list(options)) vars["__previous_unx_answer__"] = question_response vars["__response_prefix__"] = question_response else: vars["_explained_stupidity_"] = "True" vars["__response_prefix__"] = "I'm not sure." elif len(ngrams.text().split()) < 3 and len(args) == 0: vars["__response_prefix__"] = "" return True else: options = {"Yeah.", "For sure.", "Right.", "Uh-huh." } - {vars["__previous_unx_response__"]} statement_response = random.choice(list(options)) if len(args) > 0: statement_response = ", ".join([ arg for arg in args if arg not in {"s", "state", "statement"} ]) + ", " if args[0] == "None": statement_response = "" vars["__previous_unx_response__"] = statement_response vars["__response_prefix__"] = statement_response return True
def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[Any]): for i, arg in enumerate(args): if isinstance(arg, str) and arg.isnumeric(): threshold = float(arg) del args[i] break else: threshold = 0.0 user = nlp(ngrams.text()) dev = [nlp(arg) for arg in args] similarity = max([self._similarity(user, x) for x in dev]) vars["__score__"] = similarity if similarity < threshold: return False else: return True
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]))
def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[Any]): match = self.natex.match(ngrams.text(), vars=vars) return bool(match)
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
def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[Any]): if ngrams: if len(ngrams.text().split()) <= int(args[0]): return True else: return False
def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[Any]): doc = nlp(ngrams.text()) return {token.text for token in doc if token.pos_.lower() in {x.lower() for x in args}}