import jsgf class NaturalLanguageAnalyzer: # def process(self, text): # user_act = None # if ("imie" in text or "imiÄ™" in text) and "?" in text: # user_act = "request(firstname)" # return user_act def process(self, text): with open('grammar.jsgf', 'r', encoding='utf-8') as f: content = f.read() book_grammar = jsgf.parse_grammar_string(content) matched = book_grammar.find_matching_rules(text) if matched: return self.get_dialog_act(matched[0]) else: return {'act': 'null', 'slots': []} def get_slots(self, expansion, slots): if expansion.tag != '': slots.append((expansion.tag, expansion.current_match)) return for child in expansion.children: self.get_slots(child, slots) if not expansion.children and isinstance(expansion, jsgf.NamedRuleRef): self.get_slots(expansion.referenced_rule.expansion, slots) def get_dialog_act(self, rule): slots = [] self.get_slots(rule.expansion, slots) return {'act': rule.grammar.name, 'slots': slots}