def get_second_chain_image(): return get_chain_image( [ ValueCard("green", values["green"][3]).get_card(), FunctionCard("green", get_rules()["green"][2]).get_card(), FunctionCard("green", get_rules()["green"][7]).get_card(), ValueCard("green", values["green"][7]).get_card(), ], 11, 5, 2.5 )
def get_all_functions(): fns = [] for color in ["green", "yellow", "red", "white"]: for fn in get_rules()[color]: fns.append(TwoSidedCard(FunctionCard(color, fn), PlayingCardBack("yellow"))) return fns
def get_first_chain_image(): return get_chain_image( [ ValueCard("green", values["green"][3]).get_card(), FunctionCard("green", get_rules()["green"][5]).get_card(), ValueCard("green", values["green"][7]).get_card() ], 7, 5, 2.2 )
def _check_code(self, code): rules = get_rules() data = [] i = 1 for line in code.splitlines(): for key, rule in rules.items(): if rule.findall(line): data.append({"Line": i, "Code": line.strip(), "Rule": key}) i += 1 return data
def backup_account(url='', org='', key='', account='', backupdir='', **kwargs): # create directory structure backup_dir = create_dir(os.getcwd(), backupdir) org_dir = create_dir(backup_dir, org) account_dir = create_dir(org_dir, account) # backup agents agent_dir = create_dir(account_dir, 'agents') for agent_json in agents.get_agents(url=url, org=org, account=account, key=key): agent_path = os.path.join(agent_dir, str(agent_json['name']) + '.json') remove_keys = ['presence_state', 'created', 'modified', 'heartbeat'] for k in remove_keys: if k in agent_json: del agent_json[k] with open(agent_path, 'w') as f: f.write(json.dumps(agent_json, indent=4)) # backup dashboards dashboard_dir = create_dir(account_dir, 'dashboards') for d in dashboards.get_dashboards(url=url, org=org, account=account, key=key): dashboard_path = os.path.join(dashboard_dir, str(d['name']) + '.yaml') with open(dashboard_path, 'w') as f: f.write(yaml.safe_dump(d, default_flow_style=False, explicit_start=True)) # backup plugins plugin_dir = create_dir(account_dir, 'plugins') for p in plugins.get_plugins(url=url, org=org, account=account, key=key): plugin_path = os.path.join(plugin_dir, str(p['name']) + '.' + str(p['extension'])) with open(plugin_path, 'w') as f: f.write(plugins.export_plugin(plugin=p['name'], url=url, org=org, account=account, key=key)) # backup rules rule_dir = create_dir(account_dir, 'rules') for r in rules.get_rules(url=url, org=org, account=account, key=key): rule_path = os.path.join(rule_dir, str(r['name']) + '.yaml') with open(rule_path, 'w') as f: rule_content = yaml.safe_load(rules.export_rule(rule=r['id'], url=url, org=org, account=account, key=key)) if rule_content['actions']: action_count = len(rule_content['actions']) for i in range(action_count): try: del rule_content['actions'][i]['details']['status'] except KeyError: continue f.write(yaml.safe_dump(rule_content, default_flow_style=False, explicit_start=True)) # backup links link_dir = create_dir(account_dir, 'links') for l in links.get_links(url=url, org=org, account=account, key=key): link_path = os.path.join(link_dir, l['id'] + '.json') link_json = links.export_link(link_id=l['id'], url=url, org=org, account=account, key=key) with open(link_path, 'w') as f: f.write(json.dumps(link_json, indent=4))
def translate_functions(string): for color in ["green", "yellow", "red"]: for fn in get_rules()[color]: code = get_source_code(fn) if code.startswith("def "): fn_name = code[4:code.index("(")] string = string.replace(fn_name, translate("functions", fn_name)) for variable in variables: string = string.replace(variable, translate("variables", variable)) if variable == "string" and len(translate("variables", variable)) == len(variable) - 1: string = string.replace("# index: 012345678", "# index: 012345678") return string
def build_argument(conclusion, env): """ Builds an argument for the conclusion given. The conclusion should contain "filled" parameters, if it has any parameters. Arguments conclusion - A conclusion object to build the argument around Returns : An argument object """ env.new_scope() ruleset = rules.get_rules(conclusion) runset = [] for rule in ruleset: runset.append(rule.run(conclusion, env)) env.leave_scope() return arguments.Argument(conclusion, env, runset)
def quick_confidence(conclusion, working_env): """ runs 'quick' rules to do a first-pass over a conclusion to determine if it is worth pursuing further """ #TODO: does this need to memo-ize based on env too? and if so, how do #I make sure to limit the yuck? cached = working_env.quick_cached(conclusion) if cached: return cached ruleset = rules.get_rules(conclusion) runset = [] for rule in ruleset: runset.append(rule.quickrun(conclusion, working_env)) result = confidence.parse_conf(runset) if result: working_env.quick_results[conclusion] = result return result
def build_argument(conclusion, working_env): """ Builds an argument for the conclusion given. The conclusion should contain "filled" parameters, if it has any parameters. Arguments conclusion - A conclusion object to build the argument around Returns : An argument object """ cached = working_env.cached(conclusion) if cached: return cached ruleset = rules.get_rules(conclusion) runset = [] for rule in ruleset: runset.append(rule.run(conclusion, working_env)) result = arguments.Argument(conclusion, working_env, runset) if result: working_env.memoized_results[conclusion] = result return result
def view_results(): colors = ["green", "yellow", "red"] for num in range(2, 3): print("stats with %s cards" % (num + 1)) print() for c in range(len(colors)): # for c in [2]: print(colors[:c + 1]) color_values = [] color_rules = [] for color in colors[:c + 1]: color_values += values[color] for color in colors[:c + 1]: color_rules += get_rules()[color] color_values = [v[0] for v in color_values] color_values.sort() total = {} fn_total = {} s = [0] fn_s = {} value_color_fns = {} total_values = {} def account(_value, *rules): _rule = rules[0] original = _value time_start = time() for _rule in rules: _value = get_result(_rule, _value) if get_value_color(_value).split('-')[0] not in colors: break if time() - time_start > .01: print("\n", time() - time_start, original, [get_source_code_name(fn)[0] for fn in rules], _value) result_color = get_value_color(_value) if result_color not in total: total[result_color] = 0 total[result_color] += 1 s[0] += 1 if result_color not in value_color_fns: value_color_fns[result_color] = [] if _rule not in value_color_fns[result_color]: value_color_fns[result_color].append(_rule) if _rule not in fn_total: fn_total[_rule] = {} if _rule not in fn_s: fn_s[_rule] = 0 if _value not in fn_total[_rule]: fn_total[_rule][_value] = 0 fn_total[_rule][_value] += 1 fn_s[_rule] += 1 if _value not in total_values: total_values[_value] = 0 total_values[_value] += 1 for val_num, value in enumerate(color_values): for i, rule in enumerate(color_rules): for i2, rule2 in enumerate(color_rules): if num == 0: break if i == i2: continue for i3, rule3 in enumerate(color_rules): if num == 1: break if i == i3 or i2 == i3: continue for i4, rule4 in enumerate(color_rules): if num == 2: break if i == i4 or i2 == i4 or i3 == i4: continue account(value, rule, rule2, rule3, rule4) account(value, rule, rule2, rule3) account(value, rule, rule2) account(value, rule) print(int(val_num / len(color_values) * 100), end="% ") sys.stdout.flush() print() for color, count in sorted(total.items(), key=lambda w: (-w[1], w[0])): print('{:20}'.format(color), end=': ') print("{: 6.2f}%".format(100 * count / s[0]), end='') print('{}'.format("(abs count = "), end='') print('{:6}'.format(count), end=' out of ') print('{:6}'.format(s[0]), end=')\n') # if num == 0 and len(colors[:c + 1]) == 4: for rule, _ in sorted( fn_total.items(), key=lambda ww: -sorted(ww[1].items(), key=lambda w: -w[1])[ 0][1]): print("fn:", get_source_code_name(rule)[0]) for color, count in sorted(fn_total[rule].items(), key=lambda w: -w[1])[:5]: print('{:20}'.format(color), end=': ') print("{: 6.2f}%".format(100 * count / fn_s[rule]), end='') print('{}'.format("(abs count = "), end='') print('{:6}'.format(count), end=' out of ') print('{:6}'.format(fn_s[rule]), end=')\n') print() print("fns which produced certain result:") for value_color in value_color_fns: print("{:20}".format("color: " + value_color), [ get_source_code_name(fn)[0] for fn in value_color_fns[value_color] ]) print() print("most common values globally:") for value, count in sorted(total_values.items(), key=lambda w: -w[1])[:40]: print("{:20}".format("value: " + str(value)), count / s[0] * 100) print() print()
def __init__(self): self.rules = get_rules() self.paragraph = "" self.ouptut_para = ""
def backup_account(url='', org='', key='', account='', backup_dir='', **kwargs): # create directory structure backup_dir = create_dir(os.getcwd(), backup_dir) org_dir = create_dir(backup_dir, org) account_dir = create_dir(org_dir, account) # backup agents agent_dir = create_dir(account_dir, 'agents') for agent in agents.get_agents(url=url, org=org, account=account, key=key): logging.debug('Exporting JSON for agent "%s"', agent['name']) # some agents can have a name 'http://...' encode name before writing a dir agent_path = os.path.join(agent_dir, str(urllib.quote(agent['name'], safe='')) + '.json') remove_keys = ['presence_state', 'created', 'modified', 'heartbeat'] for k in remove_keys: if k in agent: del agent[k] with open(agent_path, 'w') as f: f.write(json.dumps(agent, indent=4)) # backup dashboards dashboard_dir = create_dir(account_dir, 'dashboards') for dash in dashboards.get_dashboards(url=url, org=org, account=account, key=key): logging.debug('Exporting YAML for dashboard "%s"', dash['name']) dashboard_path = os.path.join(dashboard_dir, str(dash['name']) + '.yaml') with open(dashboard_path, 'w') as f: f.write(yaml.safe_dump(dash, default_flow_style=False, explicit_start=True)) # backup plugins plugin_dir = create_dir(account_dir, 'plugins') for plugin in plugins.get_plugins(url=url, org=org, account=account, key=key): logging.debug('Exporting plugin "%s"', plugin['name']) plugin_path = os.path.join(plugin_dir, str(plugin['name']) + '.' + str(plugin['extension'])) with open(plugin_path, 'w') as f: f.write(plugins.export_plugin(plugin=plugin['name'], url=url, org=org, account=account, key=key)) # backup rules rule_dir = create_dir(account_dir, 'rules') for rule in rules.get_rules(url=url, org=org, account=account, key=key): logging.debug('Exporting YAML for rule "%s" with id %s', rule['name'], rule['id']) rule_path = os.path.join(rule_dir, str(rule['name']) + '.yaml') with open(rule_path, 'w') as f: rule_yaml = rules.export_rule(rule=rule['id'], url=url, org=org, account=account, key=key) try: rule_content = yaml.safe_load(rule_yaml) if rule_content['actions']: action_count = len(rule_content['actions']) for i in range(action_count): try: del rule_content['actions'][i]['details']['status'] except KeyError: continue f.write(yaml.safe_dump(rule_content, default_flow_style=False, explicit_start=True)) except yaml.YAMLError as e: logging.warn('Unable to parse YAML for rule %s: %s', rule['name'], e.problem) f.write(rule_yaml) # backup links link_dir = create_dir(account_dir, 'links') for link in links.get_links(url=url, org=org, account=account, key=key): logging.debug('Exporting JSON for pack "%s" with id %s', link['plugin'], link['id']) link_path = os.path.join(link_dir, link['id'] + '.json') link_json = links.export_link(link_id=link['id'], url=url, org=org, account=account, key=key) with open(link_path, 'w') as f: f.write(json.dumps(link_json, indent=4))
from rules import get_rules from transliteration import normalize_text, get_transliteration if __name__ == '__main__': rules = get_rules() # print(rules) text = input('Input some russian text: ') trans_text = get_transliteration(text, rules) print(trans_text) norm_text = normalize_text(trans_text, rules) print(norm_text)
def get_play( fns_count, values_count, input_value_index, output_value_index, used_fn_indexes, front_hand_count, right_hand_count, front_played_count=0, right_played_count=0, add_thumb=False ): background = ImageColor.getrgb(color_codes["lighter_black"]) base = Image.new("RGB", mm_to_px(87 - PictureHelpCard.DX, 87 - PictureHelpCard.DX), (*background,)) x1 = 30 x2 = 230 y1 = 30 y2 = 100 table = get_play_board() table.putalpha(100) table = transform_play_board(table, 0) base.paste(table, mask=table) blank = get_play_board() fn = get_play_board() fn_card = PlayingCardBack("yellow").get_card() fn.paste(fn_card, mm_to_px(x2, y1), fn_card) for i in range(fns_count): transformed = transform_play_board(fn, i - 0.5) base.paste(blank, mask=transformed) transformed = transform_play_board(fn, i) base.paste(transformed, mask=transformed) value = get_play_board() value_card = PlayingCardBack("blue").get_card() value.paste(value_card, mm_to_px(x1, y1), value_card) for i in range(values_count): transformed = transform_play_board(value, i - 0.5) base.paste(blank, mask=transformed) transformed = transform_play_board(value, i) base.paste(transformed, mask=transformed) used_value = get_play_board() used_value_card = ValueCard("green", values["green"][0]).get_card() used_value.paste(used_value_card, mm_to_px(x1, y2), used_value_card) for i in range(14 - values_count - 1): transformed = transform_play_board(used_value, i - 0.5) base.paste(blank, mask=transformed) transformed = transform_play_board(used_value, i) base.paste(transformed, mask=transformed) if input_value_index is not None: input_value = get_play_board() input_value_card = ValueCard("green", values["green"][input_value_index]).get_card() input_value.paste(input_value_card, mm_to_px(x1, y2), input_value_card) i = 14 - values_count - 1 transformed = transform_play_board(input_value, i - 0.5) base.paste(blank, mask=transformed) transformed = transform_play_board(input_value, i) base.paste(transformed, mask=transformed) if output_value_index is not None: output_value = get_play_board() output_value_card = ValueCard("green", values["green"][output_value_index]).get_card() output_value.paste(output_value_card, mm_to_px(x2, y2), output_value_card) i = 0 transformed = transform_play_board(output_value, i - 0.5) base.paste(blank, mask=transformed) transformed = transform_play_board(output_value, i) base.paste(transformed, mask=transformed) for i in range(right_hand_count): right_hand = Image.new("RGBA", mm_to_px(200, 200), (0, 0, 0, 0)) right_hand_tmp = Image.new("RGBA", mm_to_px(200, 200), (0, 0, 0, 0)) right_hand_card = PlayingCardBack("yellow").get_card() right_hand_tmp.paste(right_hand_card, mm_to_px(50, 50), right_hand_card) # right_hand_tmp = resize(right_hand_tmp, (2 * right_hand_tmp.size[0], 2 * right_hand_tmp.size[1])) right_hand_tmp = rotate(right_hand_tmp, 13 + (-i - 1) * 8, expand=False, center=mm_to_px((Card.base_width + 50, 50))) right_hand.paste(right_hand_tmp, mm_to_px(0, 80), right_hand_tmp) right_hand = rotate(right_hand, 180) coeffs = find_coefficients( [ (mm_to_px(140), mm_to_px(20)), (right_hand.size[0] - mm_to_px(20), mm_to_px(40)), (right_hand.size[0] - mm_to_px(20), right_hand.size[1] - mm_to_px(130)), (mm_to_px(160), right_hand.size[1] - mm_to_px(140)) ], [(0, 0), (right_hand.size[0], 0), (right_hand.size[0], right_hand.size[1]), (0, right_hand.size[1])] ) right_hand = transform(right_hand, right_hand.size, Image.PERSPECTIVE, coeffs) scale = 1.5 right_hand = resize(right_hand, (int(right_hand.width / scale), int(right_hand.height / scale))) base.paste(right_hand, mm_to_px(-110 / scale + 23, -30 / scale + 2), mask=right_hand) if add_thumb: thumb = Image.new("RGBA", mm_to_px(200, 200), (0, 0, 0, 0)) thumb_tmp = Image.new("RGBA", mm_to_px(200, 200), (0, 0, 0, 0)) thumb_card = Image.open("help/arrows/thumb.png").convert("RGBA") scale = 3. * 80 / ANTIALIASING / RESOLUTION_DPI thumb_card = resize(thumb_card, (int(thumb_card.width / scale), int(thumb_card.height / scale))) thumb_card = rotate(thumb_card, 180) color = getrgb("grey") for x in range(thumb_card.width): for y in range(thumb_card.height): thumb_card.putpixel((x, y), (*color, thumb_card.getpixel((x, y))[3])) thumb_tmp.paste(thumb_card, mm_to_px(70, 50), thumb_card) thumb_tmp = rotate(thumb_tmp, 13, expand=False, center=mm_to_px((Card.base_width + 50, 50))) thumb.paste(thumb_tmp, mm_to_px(0, 80), thumb_tmp) thumb = rotate(thumb, 180) coeffs = find_coefficients( [ (mm_to_px(140), mm_to_px(20)), (thumb.size[0] - mm_to_px(20), mm_to_px(40)), (thumb.size[0] - mm_to_px(20), thumb.size[1] - mm_to_px(130)), (mm_to_px(160), thumb.size[1] - mm_to_px(140)) ], [(0, 0), (thumb.size[0], 0), (thumb.size[0], thumb.size[1]), (0, thumb.size[1])] ) thumb = transform(thumb, thumb.size, Image.PERSPECTIVE, coeffs) scale = 1.5 thumb = resize(thumb, (int(thumb.width / scale), int(thumb.height / scale))) base.paste(thumb, mm_to_px(-110 / scale + 23, -30 / scale + 2), mask=thumb) for i in range(len(used_fn_indexes)): used_fn = get_play_board() used_fn_card = FunctionCard("green", get_rules()["green"][used_fn_indexes[i]]).get_card() used_fn_card = rotate(used_fn_card, randint(0, 360)) used_fn.paste(used_fn_card, mm_to_px(x1 / 2 + x2 / 2 - 5, y2 + 30), used_fn_card) transformed = transform_play_board(used_fn, i - 0.5) base.paste(blank, mask=transformed) transformed = transform_play_board(used_fn, i) base.paste(transformed, mask=transformed) for i in range(front_played_count): i += 1 front_player_fn = get_play_board() front_player_fn_card = PlayingCardBack("yellow").get_card() front_player_fn_card = rotate(front_player_fn_card, 97 + 10 * -i) front_player_fn.paste(front_player_fn_card, mm_to_px(x1 - 10 + 2 * i + randint(-3, 3), 200 + 20 * i + randint(-3, 3)), front_player_fn_card) transformed = transform_play_board(front_player_fn, i - 0.5) base.paste(blank, mask=transformed) transformed = transform_play_board(front_player_fn, i) base.paste(transformed, mask=transformed) for i in range(right_played_count): i += 1 right_player_fn = get_play_board() right_player_fn_card = PlayingCardBack("yellow").get_card() right_player_fn_card = rotate(right_player_fn_card, 51 + 8 * -i) right_player_fn.paste(right_player_fn_card, mm_to_px(x2 + 20 + 8 * i + randint(-3, 3), 300 - 20 * i + randint(-3, 3)), right_player_fn_card) transformed = transform_play_board(right_player_fn, i - 0.5) base.paste(blank, mask=transformed) transformed = transform_play_board(right_player_fn, i) base.paste(transformed, mask=transformed) for i in range(front_hand_count): front_hand = Image.new("RGBA", mm_to_px(200, 200), (0, 0, 0, 0)) front_hand_tmp = Image.new("RGBA", mm_to_px(200, 200), (0, 0, 0, 0)) front_hand_card = ValueCard("green", values["green"][0]).get_card() front_hand_tmp.paste(front_hand_card, mm_to_px(50, 50), front_hand_card) # front_hand_tmp = resize(front_hand_tmp, (2 * front_hand_tmp.size[0], 2 * front_hand_tmp.size[1])) front_hand_tmp = rotate(front_hand_tmp, -2 + (-i - 1) * 5, expand=False, center=mm_to_px((Card.base_width + 50, Card.base_height + 50))) front_hand.paste(front_hand_tmp, mm_to_px(0, 0), front_hand_tmp) coeffs = get_main_plane_coeffs(front_hand.size, .9, i) front_hand = transform(front_hand, front_hand.size, Image.PERSPECTIVE, coeffs) scale = 2 front_hand = resize(front_hand, (int(front_hand.width / scale), int(front_hand.height / scale))) base.paste(front_hand, mm_to_px(-12, 46), mask=front_hand) return base
import logging logging.basicConfig(level=logging.DEBUG) ### Constants # folder with tiff files TIF_FOLDER = './tif_files/' ROTATION_ANGLE_PEOPLE = 30 # Variables names BOOLEAN_STATES = ["true", "false"] debug = False ## Engine engine = InferenceEngine(rules.get_rules()) ## Read tiff files tif_files = glob.glob(TIF_FOLDER + "*.tif") tif_files.sort() evidences = [] # For each tif file for tif in tif_files[:]: ### Detectors people = PeopleEvidence() fire = FireEvidence() print "processing ", tif # Read the file