Example #1
0
    def __repr__(self):
        s = "{0:02d}. {1}".format(self.id+1, self.newname) + "\n"
        command_names = []
        for c in self.command_objs:
            if c is not None:
                command_names.append(c.name.lower())
        s += "Commands: "
        s += ", ".join(command_names) + "\n"
        if self.original_appearance and self.new_appearance:
            s += "Looks like: %s\n" % self.new_appearance
            s += "Originally: %s\n" % self.original_appearance

        from utils import make_table
        statblurbs = {}
        for name in CHARSTATNAMES:
            blurb = "{0:8} {1}".format(name.upper() + ":", self.stats[name])
            statblurbs[name] = blurb
        column1 = [statblurbs[n] for n in ["hp", "mp", "evade", "mblock"]]
        column2 = [statblurbs[n] for n in ["vigor", "m.power", "speed", "stamina"]]
        column3 = [statblurbs[n] for n in ["attack", "defense", "m.def"]]
        s += make_table([column1, column2, column3]) + "\n"
        if self.id < 14:
            s += "Notable equipment: "
            s += ", ".join([n.name for n in self.get_notable_equips()])
            s += "\n"
        if self.wor_location is not None:
            s += "World of Ruin location: %s\n" % self.wor_location
        if self.natural_magic is not None:
            s += "Has natural magic.\n"
            for level, spell in self.natural_magic:
                s += "  LV %s - %s\n" % (level, spell.name)
        return s.strip()
Example #2
0
def test_search_results(word, num):
    byte_word = word.encode('utf-8')
    table = make_table(byte_word)
    qs_table = make_qs_table(byte_word)
    with read_hyogo() as blob:
        bfs = brute_force_search(blob, byte_word)
        sbm = simplified_boyer_moore_search(blob, byte_word, table)
        bmh = boyer_moore_horspool_search(blob, byte_word, table)
        bms = boyer_moore_sunday_search(blob, byte_word, qs_table)
        assert num == len(bfs) == len(sbm) == len(bmh) == len(bms)
        assert bfs == sbm == bmh == bms
Example #3
0
def get_stat_table_HH(languages: list,
                      params: dict,
                      title='HeadHunter') -> str:
    p_fetch_records_sj = partial(fetch_records_hh, params=params)

    languages_stat_list = get_languages_stat_list(
        languages,
        p_fetch_records_sj,
        get_predict_rub_salary_hh,
        desc=fetch_records_hh.__name__)
    table = make_table(languages_stat_list, title)

    return table
Example #4
0
def get_stat_table_SJ(languages: list,
                      params: dict,
                      headers: dict,
                      title='SuperJob') -> str:
    p_fetch_records_sj = partial(fetch_records_sj,
                                 params=params,
                                 headers=headers)

    languages_stat_list = get_languages_stat_list(
        languages,
        p_fetch_records_sj,
        get_predict_rub_salary_sj,
        desc=fetch_records_sj.__name__)
    table = make_table(languages_stat_list, title)

    return table
def main():
    args = parse_argument()
    byte_word = args.word.encode('utf-8')
    log.info('検索語: %s' % args.word)
    log.info('検索語のバイト長: %d' % len(byte_word))

    if args.measure:
        log.setLevel(logging.ERROR)

    with args.data() as blob:
        table = make_table(byte_word)
        results = simplified_boyer_moore_search(blob, byte_word, table)

    for result in results:
        log.debug(result.decode('utf-8').strip())
    log.info('検索結果: %d 件' % len(results))
Example #6
0
def decode(vocab_file, model_file, input_file, no_prog):
    d = pickle.load(open(vocab_file, "rb"))
    wv = WordVocab.from_dump(d["vocab"]["word"])
    tv = {k: TableVocab.from_dump(v) for k, v in d["vocab"]["table"].items()}
    writer = d["author"] if "writer" in model_file else None
    model = Reporter.parse_config(tv=tv,
                                  wv=wv,
                                  writer=writer,
                                  model_file=model_file)

    inputs = json.load(open(input_file))
    for ins in tqdm(inputs, total=len(inputs), ncols=80, disable=no_prog):
        print(
            model.decode(
                make_table(ins),
                writer=writer.get(ins.get("author"), 0) if writer else None))
def prep(dir_path, annotation_path, out_path):
    desc = str(datetime.datetime.now(
    )) + " Overwrite the preprocessed data: {}? Y/n (default: n)".format(
        out_path)
    if os.path.exists(out_path) and input(desc) != "Y":
        print(str(datetime.datetime.now()) + " Exit.")
        exit()

    print(str(datetime.datetime.now()) + " Building dataset from " + dir_path)

    train = json.load(open(os.path.join(dir_path, "train.json")))
    tables, texts = [make_table(ins)
                     for ins in train], list(make_text(train, annotation_path))
    authors = {ins.get("author", "UNK") for ins in train}
    assert len(tables) == len(texts)

    tv = {}
    for k in ("team", "player"):
        tv[k] = TableVocab([t[k] for t in tables], key=k)
    wv = WordVocab({w for doc in texts for sent, _ in doc for w in sent})

    print(str(datetime.datetime.now()) + " Saving dataset from " + out_path)
    pickle.dump(
        {
            "data": {
                "text": texts,
                "table": tables
            },
            "vocab": {
                "word": wv.__dict__,
                "table": {k: v.__dict__
                          for k, v in tv.items()}
            },
            "author": {k: i
                       for i, k in enumerate(authors)}
        }, open(out_path, "wb"))
Example #8
0
File: plist.py Project: 0x0d/hijack
 def make_table(self, *args, **kargs):
     """Prints a table using a function that returs for each packet its head column value, head row value and displayed value
     ex: p.make_table(lambda x:(x[IP].dst, x[TCP].dport, x[TCP].sprintf("%flags%")) """
     return make_table(self.res, *args, **kargs)
Example #9
0
 def make_table(self, *args, **kargs):
     """Prints a table using a function that returs for each packet its head column value, head row value and displayed value
     ex: p.make_table(lambda x:(x[IP].dst, x[TCP].dport, x[TCP].sprintf("%flags%")) """
     return make_table(self.res, *args, **kargs)
Example #10
0
def train(vocab_file, valid_file, nh_vocab, nh_rnn, writer, learning_rate,
          lr_decay, batch_size, n_epoch, log_dir):
    log_dir = os.path.join(log_dir, str(int(time.time())))

    # Initialize...
    print(str(datetime.datetime.now()) + " Log dir at {}".format(log_dir))
    os.mkdir(log_dir)
    print(str(datetime.datetime.now()) + " Loading dataset...")
    d = pickle.load(open(vocab_file, "rb"))
    texts, tables = d["data"]["text"], d["data"]["table"]
    wv = WordVocab.from_dump(d["vocab"]["word"])
    tv = {k: TableVocab.from_dump(v) for k, v in d["vocab"]["table"].items()}
    writer = d["author"] if writer else None

    print(str(datetime.datetime.now()) + " Vectorizing...")
    data = list(vectorize(texts, tables, wv, tv, writer))

    valid = json.load(open(valid_file)) if valid_file else None

    # Model
    model = Reporter(tv=tv,
                     wv=wv,
                     nh_vocab=nh_vocab,
                     nh_rnn=nh_rnn,
                     writer=writer)
    print(str(datetime.datetime.now()) + " Model configurations...")
    print(str(datetime.datetime.now()) + " " + str(model))

    # Trainer
    trainer = Trainer(model,
                      lr=learning_rate,
                      decay=lr_decay,
                      batch_size=batch_size)
    print(str(datetime.datetime.now()) + " Trainer configurations...")
    print(str(datetime.datetime.now()) + " " + str(trainer))

    try:
        best = 0.
        print(str(datetime.datetime.now()) + " Start training...")
        for _ in range(n_epoch):
            trainer.fit_partial(data)
            pc_name = str(model) + "_{}.dy".format(trainer.iter)
            model.pc.save(os.path.join(log_dir, pc_name))

            if valid and trainer.iter >= 5:
                pred = []
                prog = tqdm(
                    desc="Evaluation: ",
                    total=len(valid) + 1,
                    ncols=80,
                )
                for ins in valid:
                    p = model.decode(make_table(ins),
                                     writer=writer.get(ins.get("author"))
                                     if writer else None)
                    pred.append(p.split())
                    prog.update()

                bleu = nltk.translate.bleu_score.corpus_bleu(
                    [[nltk.word_tokenize(' '.join(v["summary"]))]
                     for v in valid], pred)
                prog.set_postfix(BLEU=bleu)
                prog.update()
                prog.close()
                if bleu > best:
                    best = bleu
                    print(str(datetime.datetime.now()) + " Save best model...")
                    model.pc.save(
                        os.path.join(log_dir,
                                     str(model) + "_best.dy"))

    except KeyboardInterrupt:
        print("KeyboardInterrupted...")
        first_condition_constraint=first_condition_constraint,
        second_condition_constraint=second_condition_constraint)
    #import ipdb
    # ipdb.set_trace()
    c_mat, G_mat, h_mat, b_mat, A_mat = matrix(c), matrix(G), matrix(
        h), matrix(b), matrix(A)

    sol = solvers.lp(c_mat, G_mat, h_mat, A_mat, b_mat, solver='glpk')
    return np.array(sol['x']).T[0], c


if __name__ == '__main__':
    stuff_timeline = list(zip(range(9, 19), range(10, 20)))
    stuff_values = [16, 30, 31, 45, 66, 72, 61, 34, 16, 10]

    stuff_table = make_table(stuff_timeline, stuff_values)

    cost_timeline = [[9, 17], [11, 19], [9, 13], [10, 14], [11, 15], [12, 16],
                     [13, 17], [14, 18], [15, 19]]
    cost_values = [8, 8, 6, 7, 9, 10, 8, 6, 6]

    cost_table = make_table(cost_timeline, cost_values)
    sol, c = solve(stuff_table, cost_table)
    print("=" * 80)
    print("COST MATRIX = ")
    print(c.reshape(9, 10))
    print("=" * 80)
    print("OPTIMAL SOLUTION = ")
    print(sol.reshape(9, 10))
    print("=" * 80)
    print("COST = ")
Example #12
0
                "new_vol",
                "old_rating",
                "room_placed",
                "round_id",
            )
        )
        rounds = cur.execute("SELECT %s FROM results WHERE coder_id = ? AND rated_flag = 1" % fields, [cid]).fetchall()
        num_rounds = len(rounds)
        round_ids = [row["round_id"] for row in rounds]
        round_names = cur.execute(
            "SELECT short_name FROM rounds WHERE round_id IN (" + ", ".join("?" * len(rounds)) + ")", round_ids
        ).fetchall()
        for i in range(len(rounds)):
            rounds[i] = list(round_names[i]) + list(rounds[i])[1:-1]
        rounds = utils.make_table(
            rounds, titles=["Match", "Div", "Rank", "New Rating", "New Volatility", "Old Rating", "Room Rank"]
        )
    except Exception, e:
        error_msg = "Failed to read round history for %s: %s" % (handle, e)
        app.logger.error(error_msg)
        flash(error_msg)

    try:
        pvpetr_data = rating_functions.pvpetr(conn, cid)
        pvpetr_summary = "<p>Wins: %d / Losses: %d</p>" % (len(pvpetr_data[0]), len(pvpetr_data[1]))
        for dataset in pvpetr_data:
            for i, row in enumerate(dataset):
                dataset[i] = list(row)
                dataset[i][1] = rating_functions.pname[row[1]]
        pvpetr_wins_table = utils.make_table(
            pvpetr_data[0],