def calc_intents(self, query): """ Tests all the intents against the query and returns data on how well each one matched against the query Args: query (str): Input sentence to test against intents Returns: list<MatchData>: List of intent matches See calc_intent() for a description of the returned MatchData """ if self.must_train: self.train() intents = {} if self.train_thread and self.train_thread.is_alive( ) else { i.name: i for i in self.intents.calc_intents(query, self.entities) } sent = tokenize(query) for perfect_match in self.padaos.calc_intents(query): name = perfect_match['name'] intents[name] = MatchData(name, sent, matches=perfect_match['entities'], conf=1.0) return list(intents.values())
def calc_intents(self, query, entity_manager): sent = tokenize(query) matches = [] for i in self.objects: match = i.match(sent, entity_manager) match.detokenize() matches.append(match) return matches
def expand_keywords(sentece): kwords = {"required": [], "optional": []} in_optional = False for exp in expand_parentheses(tokenize(sentece)): if "[" in exp: in_optional = True required = exp[:exp.index("[")] kwords["required"].append(" ".join(required)) optional = exp[exp.index("[") + 1:] kwords["optional"].append(" ".join(optional).replace("]", "").strip()) elif in_optional: optional = exp kwords["optional"].append(" ".join(optional).replace("]", "").strip()) if "]" in exp: in_optional = False else: kwords["required"].append(" ".join(exp)) return kwords
def add_lines(self, name, lines): lines = remove_comments(lines) self.sent_lists[name] = sum([expand_parentheses(tokenize(line)) for line in lines], []) self.sent_lists[name] = [i for i in self.sent_lists[name] if i]
def xp(s): return {''.join(sent) for sent in expand_parentheses(tokenize(s))}
def test_tokenize(): assert tokenize('one two three') == ['one', 'two', 'three'] assert tokenize('one1 two2') == ['one', '1', 'two', '2'] assert tokenize('word {ent}') == ['word', '{ent}'] assert tokenize('test:') == ['test', ':']
def expand_options(sentece): sentences = [] sentece = sentece.replace("[", "(").replace("]", "| )") for exp in expand_parentheses(tokenize(sentece)): sentences.append(" ".join(exp).replace("(", "[").replace(")", "]")) return sentences