def _guess_item(self, text): text_ = text.casefold() if starts_with(text_, ['вопросы для самопроверки к ', 'вопросы для самопроверки по ']): item = text_[nfind(text_, ' ', 4):].strip() for (name, (module_id, module_name)) in self.get_items(): if not starts_with(name.casefold(), [item + ' ', item + '.']): continue logging.info('Guessed "%s": module "%s"', text, module_name) return (module_id, module_name) if starts_with(text_, ['контрольный тест к ', 'контрольный тест по ']): item = (text_[nfind(text_, ' ', 3):].strip() .replace('разделу', 'раздел')) for (_, (module_id, module_name)) in self.get_items(): if not starts_with(module_name.casefold(), [item + ' ', item + '.']): continue logging.info('Guessed "%s": module "%s"', text, module_name) return (module_id, module_name) return None
def pre_suggestions(self): """ Return a list of commands that the user can select by pressing <up> """ result = [] for register in self.registers: for m in register.contents: s = str(messages.strip_prefix(m)) if utils.starts_with("A", m.text[0]): result.append("reply " + s) elif utils.starts_with("Q", m.text[0]): result.append("ask " + s) return result
def add_kink_fb(self, functions, cp): res = [] for f in functions: f_l = f.to_list() for i in range(1, len(f_l)): f_left = f_l[:i] f_right = f_l[i:] if self.forward_state.is_end(): corrects = [] for bp in self.backward_state.backward_paths: if len(f_left) == len(f_right) + bp.length() and \ bp.direction == "b": corrects.append(bp) if len(corrects) != 1: continue forward_path = " ".join(f_left) backward_path = corrects[0].backward_path + " " + \ " ".join(f_right[::-1]) else: # find ending backward found = [] # One path at least needs to start if len(self.backward_state.backward_paths) != 0: for bp in self.backward_state.backward_paths: if bp.is_end() and bp.direction == "b": found.append(bp) if len(found) != 1 or (len(f_right) != len(f_left) +\ self.forward_state.length()): continue elif (len(f_right) != len(f_left) +\ self.forward_state.length()): continue forward_path = self.forward_state.forward_path + " " +\ " ".join(f_left) backward_path = " ".join(f_right[::-1]) backward_inv = inverse_path(backward_path) if starts_with(forward_path, cp) and\ starts_with(backward_inv, cp) and \ starts_with(forward_path, backward_inv): clean_bp = [] for bp in self.backward_state.backward_paths: if not bp.is_end(): if corrects and bp != corrects[0]: clean_bp.append(bp) res.append( State( ForwardState(forward_path), BackwardState(clean_bp + [BackwardPath(backward_path, "b")]))) return res
def get_next_states(self, functions, max_size): if not self.is_valid(): return [] if not self.forward_state.is_end() and \ self.backward_state.total_length() >= max_size: return [self.remove_one_relation()] function_list = [] cp = self.get_constraint_path() for f in functions: f_str = " ".join(f.to_list()) f_str_inv = " ".join(f.get_inverse_function()) if starts_with(f_str, cp) or \ starts_with(f_str_inv, cp): function_list.append(f) res = [] ends_f, ends_b = self.backward_state.number_ends() if self.forward_state.is_end(): for f in functions: f_str = " ".join(f.to_list()) if starts_with(f_str, cp): next_state = State(ForwardState(f), self.backward_state) cp_n = next_state.get_constraint_path() res.append(next_state) res += self.add_kink_fb(functions, cp) elif ends_f > 0 or ends_b > 0: res_temp = self.backward_state.add_one(function_list, cp) for bs in res_temp: res.append(State(self.forward_state, bs)) res += self.add_kink_fb(functions, cp) else: if self.backward_state.total_length() == 0: res += self.add_kink_fb(functions, cp) res.append(self) complete_res = [] for state in res: complete_res += generate_all_states(state, functions, max_size) complete_res = list( filter( lambda x: x.is_valid() and x.backward_state.total_length() != 0, complete_res)) complete_res = map(lambda x: x.remove_one_relation(), complete_res) return list(set(complete_res))
def check_received_message(new_message_data): print(json.dumps(new_message_data, indent=4, sort_keys=True)) message_text = utils.get_message_text(new_message_data) repo.insert(new_message_data) chat_id = utils.get_chat_id(new_message_data) if utils.starts_with(message_text, "/banter"): send_audio(chat_id, message_text) if utils.starts_with(message_text, "/"): if message_text == '/subit': send_message_with_styles(chat_id, cmds.subway()) elif message_text == '/inside': send_message_with_styles(chat_id, cmds.inside()) elif message_text == '/matti': send_message_with_styles(chat_id, cmds.matti()) elif message_text == '/donald': send_message_with_styles(chat_id, cmds.donald()) elif message_text == '/help' or message_text == '/apua': send_message_with_styles(chat_id, cmds.help())
def generate_all_states_rec(bs, functions, n_added=0): if bs.total_length() >= max_size or len( functions) == 0 or n_added >= 2: res.append(bs) return cp_temp = bs.get_constraint_path() function = functions[0] f_l = function.to_list() f_str = " ".join(f_l) if starts_with(f_str, cp) and\ starts_with(f_str, cp_temp): generate_all_states_rec( BackwardState(bs.backward_paths[:] + [BackwardPath(f_str, "f")]), functions[1:], n_added + 1) f_str = " ".join(function.get_inverse_function()) if starts_with(f_str, cp) and \ starts_with(f_str, cp_temp): generate_all_states_rec( BackwardState(bs.backward_paths[:] + [BackwardPath(function, "b")]), functions[1:], n_added + 1) # kinks for i in range(1, len(f_l)): f_left = f_l[:i] f_right = f_l[i:] f_str_right = " ".join(f_right) f_left_inv = " ".join(map(lambda x: inverse(x, "-"), f_left[::-1])) if starts_with(f_str_right, cp) and \ starts_with(f_str_right, cp_temp) and \ starts_with(f_left_inv, cp) and \ starts_with(f_left_inv, cp_temp) and \ starts_with(f_str_right, f_left_inv): generate_all_states_rec( BackwardState(bs.backward_paths[:] + [ BackwardPath(f_left[::-1], "b"), BackwardPath(f_right, "f") ]), functions, n_added) generate_all_states_rec(bs, functions[1:], n_added)
def read_simulation_report(file_path): """Given a file path, it read a data into a dictionary """ assert os.path.exists(file_path), "No file exists in %s" % file_path result = {} keys = ["Received", "Sent", "Correct average","Correct values","Estimated average","Estimated values","Identified values","% precision","Identified rate","Average number of cohorts"] with open(file_path, "r") as f: no_input = False while True: l = f.readline() if not l: break if l.startswith("## INPUT"): l = eval(f.readline()) if l == {}: no_input = True else: no_input = False elif l.startswith("## OUTPUT"): l = eval(f.readline()) if l == {}: no_output = True else: no_output = False if no_input and no_output: result["null IO"] = True else: result["null IO"] = False else: key = starts_with(l, keys) if key: try: result[key] = eval(l.split(":")[1]) except SyntaxError: # Identified values will raise an error because of its error format # Identified values: [1.00, ?(2), ?(3), ?(4), ?(5), ?(6), ?(7), ?(8)] # [1.00, ?(2), ?(3), ?(4), ?(5), ?(6), ?(7), ?(8)] # ^ # SyntaxError: invalid syntax result[key] = recover_to_list((l.split(":")[1]).rstrip()) return result
def standardize_response(r): subs = { "A:": "reply", "A ": "reply ", "return": "reply", "Q:": "ask", "Q ": "ask ", "say": "note", "ask@": "resume ", "reply:": "reply", "ask:": "ask", } for i in range(8): subs["reply {}".format(i)] = "resume {}".format(i) subs["Q{} ".format(10**i)] = "ask{} ".format(10**i) for k, v in subs.items(): if starts_with(k, r): return v + r[len(k):] return r
# ============================================================================== wb = load_workbook(excel_filepath, read_only=True) series_ws_rows = wb['Series'].rows data_ws_rows = wb['Data'].rows country_ws_rows = wb['Country'].rows series_headers = get_row_values(next(series_ws_rows)) data_headers = get_row_values(next(data_ws_rows)) country_headers = get_row_values(next(country_ws_rows)) # NOTE at this point, the rows generators have yielded once to retrieve the # headers, so they will yield a data row next. if not starts_with(series_headers, SERIES_EXPECTED_HEADERS): terminate("Headers mismatch on 'Series' worksheet") if not starts_with(data_headers, DATA_EXPECTED_HEADERS): terminate("Headers mismatch on 'Data' worksheet") if not starts_with(country_headers, COUNTRY_EXPECTED_HEADERS): terminate("Headers mismatch on 'Country' worksheet") # ============================================================================== # Initialise the data structures to track the state of the import. # ============================================================================== last_available_year = int(data_headers[-1]) timespan = str(FIRST_YEAR) + "-" + str(last_available_year) indicators = [indicator_from_row(row) for row in series_ws_rows]
def add_one(self, functions, cp): forward = True bp_ending = [] # Add forward or backward ? for bp in self.backward_paths: if bp.is_end(): bp_ending.append(bp) if bp.is_end() and bp.direction == "b": forward = False if len(bp_ending) != 1: return [] res_f = [] for f in functions: if forward: f_str = " ".join(f.to_list()) else: f_str = " ".join(f.get_inverse_function()) if starts_with(f_str, cp): res_f.append(f) clean_bp = [] for bp in self.backward_paths: if not bp.is_end(): clean_bp.append(bp) res = [] if forward: direction = "f" else: direction = "b" for f in res_f: res.append( BackwardState(clean_bp[:] + [BackwardPath(f, direction)])) # Deal with kinks # For now, we do a simplify version, which is INCOMPLETE for f in functions: f_l = f.to_list() for i in range(1, len(f_l)): f_left = f_l[:i] f_right = f_l[i:] if forward: # We need to find a backward path which has the correct # length corrects = [] for bp in clean_bp: if len(f_left) == len(f_right) + bp.length() and\ bp.direction == "b": corrects.append(bp) if len(corrects) != 1: continue forward_p = " ".join(f_left) backward_p = corrects[0].backward_path + " " + \ " ".join(f_right[::-1]) else: corrects = [] for bp in clean_bp: if len(f_right) == len(f_left) + bp.length() and \ bp.direction == "f": corrects.append(bp) if len(corrects) != 1: continue forward_p = corrects[0].backward_path + " " + \ " ".join(f_left) backward_p = f_right[::-1] backward_inv = inverse_path(backward_p) if starts_with(forward_p, cp) and\ starts_with(backward_inv, cp) and\ stars_with(forward_p, backward_inv): res.append( BackwardState(clean_bp[:] + [ BackwardPath(forward_p, "f"), BackwardPath(backward_p, "b") ])) return res