def process_content(idChat, msg, content): '''Process content for cases when we want to filter the content :param: id chat :param: user message :param: content to filter ''' globals.redis_db.delete("content" + str(idChat)) m = clean_msg(msg) if not re.search(r'^\s*0\s*$', m) and not re.search( r'\bnenhuma das hipoteses\b', m) and not re.search( r'\bnenhuma?\b', m): num = re.search(r'^\s*([0-9]+)\s*$', msg) if num: num = num.group(1) process_content_num(idChat, content, int(num)) else: i = 0 n = len(content['keys']) found = False while i < n and not found: if re.search(r'\b' + content['keys'][i]['match'] + r'\b', msg): found = True process_content_num(idChat, content, content['keys'][i]['choice']) i += 1 if not found: process_content_num(idChat, content, n + 1) else: send_msg(idChat, prefab_msgs["request"][0])
def ntp_answer(idChat, msg, silent): '''Get solution and parse it :param: id chat :param: user message :param: silent mode? ''' msg = clean_msg(msg) answer = get_solver(idChat, msg) if not silent: if answer: send_msg(idChat, answer['msg']) if answer['chat_id'] == -1: globals.redis_db.delete(idChat) elif answer['chat_id'] == -2: globals.redis_db.delete(idChat) linhas_apoio = get_content("/fs_scrapper/linhas_apoio", [], {}) if linhas_apoio: if 'assunto' in answer: linhas_apoio = process_linhas_apoio( linhas_apoio, answer['assunto']) pretty_print(idChat, "/fs_scrapper/linhas_apoio", linhas_apoio, True) else: send_msg(idChat, prefab_msgs["failed"][3]) else: send_msg(idChat, prefab_msgs["failed"][2])
def ask_cinema(idChat, content, cat): '''Ask user to select a cinema :param: id chat :param: content to filter :param: category detected ''' n = 1 m = 'Escolha um dos cinemas:\n' m += " 0. Nenhuma das hipóteses\n" cinemas = [] for c in content: m += " " + str(n) + ". " + c + "\n" cl = clean_msg(c) words = cl.split() l = len(words) s = set() while l > 0: s.add(" ".join(words[0:l])) l -= 1 s.add(words[l]) for w in list(s): cinemas.append({'choice': n, 'match': w}) n += 1 m += "Indique o número ou o nome do cinema." globals.redis_db.set( "content" + str(idChat), json.dumps({ 'cat': cat, 'value': content, 'keys': cinemas })) send_msg(idChat, m)
def detect_entities_regex(msg): '''Detects de entities of a message ''' entities = detect_address(msg) msg = clean_msg(msg) for f in detect_functions: entities = entities + f(msg) return entities
def get_response(idChat, idUser, msg, name, timestamp, location): ''' For a given user message answer him :param: id chat :param: id user :param: user message :param: user name :param: message timestamp :param: user location ''' #save for periodic message save_chat_timestamp(idChat, idUser, timestamp) if re.match(r'^/start\s*$', msg): send_msg(idChat, prefab_msgs["about"][0]) elif re.match(r'^/help\s*$', msg): send_msg(idChat, prefab_msgs["about"][1]) elif re.match(r'^/reset(\s)*$', msg): reset(idChat, idUser, False) else: contentAux = globals.redis_db.get("content" + str(idChat)) content = json.loads(contentAux) if contentAux else None if content: process_content(idChat, msg, content) else: chatData = fetchChatMetadata(idChat) if location: if 'previous_state' in chatData: chatData["status"] = "modo regras" else: chatData["status"] = "" chatData["locationParam"] = location if chatData["status"] == "modo regras": forward_to( idChat, chatData, get_response_rules(idChat, idUser, msg, name, chatData)) elif chatData["status"] == "modo problemas": globals.redis_db.set(idChat, json.dumps(chatData)) ntp_answer(idChat, msg, False) else: m = clean_msg(msg) if re.match(r'\bmodo (de )?regras\b', m) or re.match( r'^/interativo$', msg): chatData["status"] = "modo regras" globals.redis_db.set(idChat, json.dumps(chatData)) forward_to( idChat, chatData, get_response_rules(idChat, idUser, msg, name, chatData)) elif re.match(r'\bver mais\b', m) or re.match(r'^/mais$', msg): ver_mais(idChat) else: get_response_default(idChat, idUser, msg, name, chatData)
def detect(words, t, msg): ''' Detects the entities of type t of a clean message (w/o accents & capitals) where the possible entities values list are 'words' ''' ents = [] for w in words: if re.search(r'\b' + clean_msg(w) + r'\b', msg): ents.append({'entity': w, 'type': t}) return ents
def process_linhas_apoio(linhas_apoio, assunto): '''Process support lines according to assunto of ntp problem :param: list of support lines :param: assunto ''' regex = r'\btelevisao\b' if assunto == 'voz': regex += r'|\btele\w+' elif assunto == 'internet': regex += r'|\binternet\b' las = [] for la in linhas_apoio: if re.search(regex, clean_msg(la['categoria'])): las.append(la) return las
def detect_location(msg): ''' Detects the entities of type GPE of a message ''' ents = {} for m in municipies: cm = clean_msg(m) if re.search(r'\b' + cm + r'\b', msg): ents[m] = len(m.split()) else: for st in municipies_stopwords: cm = re.sub(r'\b' + st + r'\b', "", cm) cm = re.sub(r'\s+', ' ', cm) words = cm.split() n = len(words) found = False i = 0 while i < n and not found: word = " ".join(words[i:n + 1]) if word != "sao" and re.search(r'\b' + word + r'\b', msg): ents[m] = n - i found = True i += 1 i = 0 while i < n and not found: word = " ".join(words[0:i + 1]) if word != "sao" and re.search(r'\b' + word + r'\b', msg): ents[m] = i + 1 i += 1 i = 0 while i < n and not found: if words[i] != "sao" and re.search(r'\b' + words[i] + r'\b', msg): ents[m] = 1 i += 1 if len(ents) > 0: ent = max(ents, key=ents.get) return [{'entity': ent, 'type': 'GPE'}] else: return []