コード例 #1
0
def write_result_to(file_path, true_keywords, pred_keywords):
    print("Writing true and predicted keywords to file {}...".format(file_path))
    with open(file_path, "w", encoding="utf-8") as file:
        for i in range(len(true_keywords)):
            true_keyword = utils.list_to_str(true_keywords[i]).lower() # 把labels中的英文字母全部换成小写
            pred_keyword = utils.list_to_str(pred_keywords[i])
            pairs = true_keyword + "," + pred_keyword + "\n"
            file.write(pairs)
    print("Done.")
コード例 #2
0
 def print_stats(self):
     logger.info("Training data stats: \n" +
                 "\t- intent examples: {} ({} distinct intents)\n".format(
                         len(self.intent_examples), len(self.intents)) +
                 "\t- Found intents: {}\n".format(
                         list_to_str(self.intents)) +
                 "\t- entity examples: {} ({} distinct entities)\n".format(
                         len(self.entity_examples), len(self.entities)) +
                 "\t- found entities: {}\n".format(
                         list_to_str(self.entities)))
コード例 #3
0
ファイル: mover_utils.py プロジェクト: ckrivacic/ab_mine
def choose_designable_residues(pose,
                               focus_residues,
                               include_focus=True,
                               dshell=8.0,
                               pshell=12.0):
    """
    Chooses a shell (for now, might make more sophisticated later) of residues
    to design around the motif residue.
    """

    focus_residue_selector =\
            rosetta.core.select.residue_selector.ResidueIndexSelector(list_to_str(focus_residues))
    designable_selector =\
            rosetta.core.select.residue_selector.NeighborhoodResidueSelector(
                    focus_residue_selector, dshell,
                    include_focus_in_subset=include_focus
            )
    designable_not_selector =\
            rosetta.core.select.residue_selector.NotResidueSelector(
                    designable_selector
                    )
    packable_selector =\
            rosetta.core.select.residue_selector.NeighborhoodResidueSelector(
                    focus_residue_selector, pshell,
                    include_focus_in_subset=include_focus
            )
    repack_only_selector =\
            rosetta.core.select.residue_selector.AndResidueSelector(
                    designable_not_selector, packable_selector
                    )

    design_residues = designable_selector.apply(pose)
    repack_residues = repack_only_selector.apply(pose)

    return design_residues, repack_residues
コード例 #4
0
ファイル: ircbot.py プロジェクト: PietPtr/btptr
    def cmd_afk(self, user, away_msg):
        """Marks a user afk

        user: username of the user who issued the command, string
        away_msg: away msg to be set, list of strings
        """
        away_msg = utils.list_to_str(away_msg)

        afk_users = []
        with open("afk_users.csv", 'r') as f:
            afk_users.extend(csv.reader(f))

        self.debug_print(afk_users)

        # Hacky fix for bug when no one is afk
        if afk_users == []:
            afk_users.append(['',''])
            self.debug_print("Added empty row to afk_users")

        set_afk = False
        for row in afk_users:
            if user == row[0]:
                row[1] = away_msg
                self.send_msg("You were already away, Your new afk message is: " + away_msg)
                set_afk = True
        if set_afk == False:
            afk_users.append([user, away_msg])
            self.send_msg("You are now afk.")

        self.debug_print(afk_users)

        with open("afk_users.csv", 'w') as f:
            csv.writer(f).writerows(afk_users)
コード例 #5
0
ファイル: linkify_it.py プロジェクト: veaba/pypackjs
    def compile(self):
        from linkify_it_re import linkifyItRe
        # todo 这个class 方法如何访问到类的私有属性
        re_dict = self.re = linkifyItRe(self._opts)
        tlds = self._tlds
        self.onCompile()
        if not self._tlds_replaced:
            tlds_2ch_src_re = 'a[cdefgilmnoqrstuwxz]|b[abdefghijmnorstvwyz]|c[acdfghiklmnoruvwxyz]|d[ejkmoz]|e[cegrstu]|f[ijkmor]|g[abdefghilmnpqrstuwy]|h[kmnrtu]|i[delmnoqrst]|j[emop]|k[eghimnprwyz]|l[abcikrstuvy]|m[acdeghklmnopqrstuvwxyz]|n[acefgilopruz]|om|p[aefghklmnrstwy]|qa|r[eosuw]|s[abcdeghijklmnortuvxyz]|t[cdfghjklmnortvwz]|u[agksyz]|v[aceginu]|w[fs]|y[et]|z[amw]'
            self._tlds.append(tlds_2ch_src_re)
        tlds.append(re_dict['src_xn'])
        re_dict['src_tlds'] = list_to_str(tlds, '|')

        def untpl(tpl):
            return tpl.replace('%TLDS%', re_dict['src_tlds'])

        # todo 编译为i
        re_dict['emial_fuzzy'] = untpl(re_dict['tpl_email_fuzzy'])
        re_dict['link_fuzzy'] = untpl(re_dict['tpl_link_fuzzy'])
        re_dict['link_no_ip_fuzzy'] = untpl(re_dict['tpl_link_no_ip_fuzzy'])
        re_dict['host_fuzzy_test'] = untpl(re_dict['tpl_host_fuzzy_test'])

        # Compile each schema

        aliased = []
        self._compiled = {}  # // Reset compiled data
コード例 #6
0
ファイル: ircbot.py プロジェクト: PietPtr/btptr
    def cmd_remind(self, args, user):
        """Sets a reminder

        args[0]: user to be reminded
        args[1]: relative time
        args[2]: time unit (s/m/h/dD/wW/M/yY/)
        """

        if len(args) < 3:
            self.send_msg("You did not provide me with enough information :/ " +\
                "Syntax: !remind [user] [number] [time unit (s/m/h/dD/wW/M/yY/)] [msg]")
            return

        user_to_remind = args[0]
        rel_time = args[1]
        time_unit = args[2]
        msg = args[3:]

        if user_to_remind == "me":
            user_to_remind = user

        # Get time in seconds since epoch
        cur_time = time.time()
        reminder_time = None

        if utils.represents_int(rel_time) is False:
            self.send_msg("I don't know when to remind you :( "+\
                "Syntax: !remind [user] [number] [time unit (s/m/h/d/w/M/y/)] [msg]")
            return
        if time_unit not in "smhdDwWMyYD":
            self.send_msg("Oof, I don't know how to interpret that time unit :S Use one of these: /m/h/d/w/M/y/")
            return

        if time_unit == 's':
            reminder_time = cur_time + int(rel_time)
        elif time_unit == 'm':
            reminder_time = cur_time + (int(rel_time) * 60)
        elif time_unit == 'h':
            reminder_time = cur_time + (int(rel_time) * 3600)
        elif time_unit == 'd' or time_unit == 'D':
            reminder_time = cur_time + (int(rel_time) * 86400)
        elif time_unit == 'w' or time_unit == 'W':
            reminder_time = cur_time + (int(rel_time) * 604800)
        elif time_unit == 'M':
            reminder_time = cur_time + (int(rel_time) * 2592000)
        elif time_unit == 'y' or time_unit == 'Y':
            reminder_time = cur_time + (int(rel_time) * 31536000)

        self.send_msg("Okay, I will remind " + user_to_remind + " on " + time.strftime("%A %d %B %H:%M:%S %Y", time.localtime(reminder_time)))
        del args[1:2]

        t = Timer(reminder_time - cur_time, self.remind_user, [user_to_remind, utils.list_to_str(msg)])
        t.start();
コード例 #7
0
ファイル: ircbot.py プロジェクト: PietPtr/btptr
    def parse_recv_data(self, data):
        if data.startswith("PING"):
            self.sock_send_str(data.replace("PING", "PONG"))
        if data[0] != ':':
            pass
        if (self.nickname + " :End of /MOTD") in data:
            self.join_channel()
            self.get_online_users()
        if data.split(' ')[1] == "353":
            self.debug_print("Parsing userlist!")
            self.parse_userlist(data)
        elif data.split(' ')[1] == "PRIVMSG":
            user = data.split(':')[1].split('!')[0] # potiental crash point?
            msg = data.split(' ')[3]
            if msg.startswith(":!"):
                command_and_args = self.parse_arguments(data)
                command = command_and_args[0]
                args = command_and_args[1]
                self.debug_print(command_and_args)

                if command in self.preset_text_cmds:
                    self.send_msg(self.preset_text_cmds[command])

                if command == "!say":
                    self.cmd_say(args)
                elif command == "!choose":
                    self.cmd_choose(args)
                elif command == "!ascii":
                    self.cmd_ascii(args)
                elif command == "!afk":
                    self.cmd_afk(self.get_sender(data), args)
                elif command == "!back" or command == "!rug" or command == "!brak":
                    self.cmd_back(self.get_sender(data))
                elif command == "!where":
                    self.cmd_where(args)
                elif command == "!remind":
                    self.cmd_remind(args, user)

                # Debug-only commands
                if self.DEBUG:
                    if command == "!stop":
                        if self.get_sender(data) == "MrTijn":
                            print("Stopping...")
                            sys.exit(0)
                    elif command == "!send_raw":
                        print("!send_raw")
                        print(self.send_raw(utils.list_to_str(args)))
コード例 #8
0
ファイル: ircbot.py プロジェクト: PietPtr/btptr
    def cmd_ascii(self, msg):
        """Print msg in big ascii art letters

        msg - list of strings
        """
        # Convert msg to string
        msg = utils.list_to_str(msg)

        line1 = ""
        line2 = ""
        line3 = ""

        for char in msg:
            char = char.lower()
            line1 += AsciiArt.characters[char][0]
            line2 += AsciiArt.characters[char][1]
            line3 += AsciiArt.characters[char][2]

        self.send_msg(line1)
        self.send_msg(line2)
        self.send_msg(line3)
コード例 #9
0
ファイル: mover_utils.py プロジェクト: ckrivacic/ab_mine
def setup_task_factory(pose,
                       designable_residue_selector,
                       repackable_residue_selector,
                       motif_dict=None,
                       extra_rotamers=True,
                       limit_aro_chi2=True,
                       layered_design=True,
                       designable_aa_types=None,
                       prepare_focus=False):
    """
    Adapted from XingJie Pan's code at
    [email protected]:Kortemme-Lab/local_protein_sequence_design.git:
    local_protein_sequence_design/basic.py

    motif_dict should have the resnum as the key for the single-letter restype,
    ex. {38:'E'}

    If you want to set up a task factory to only design the 'focus' residue,
    pass it a motif dict and prepare_focus=True
    """
    def list_to_str(l):
        return ','.join(list(str(i) for i in l))

    task_factory = rosetta.core.pack.task.TaskFactory()

    if len(designable_residue_selector) > 0 and not prepare_focus:
        racaa = rosetta.core.pack.task.operation.RestrictAbsentCanonicalAASRLT(
        )

        if designable_aa_types is None or\
                len(list(compress(xrange(len(designable_residue_selector)),\
                    designable_residue_selector))) != len(designable_aa_types):
            racaa.aas_to_keep('GAPVILMFYWSTKRDENQ')  # No Cys or His
        else:
            racaa.aas_to_keep(designable_aa_types[i])

        designable_operation = rosetta.core.pack.task.operation.OperateOnResidueSubset(
            racaa, designable_residue_selector)
        task_factory.push_back(designable_operation)

    if motif_dict:
        for resnum in motif_dict:
            selector = rosetta.core.select.residue_selector.ResidueIndexSelector(
                str(resnum))
            print(selector)
            racaa = rosetta.core.pack.task.operation.RestrictAbsentCanonicalAASRLT(
            )
            racaa.aas_to_keep(motif_dict[resnum])

            motif_operation = rosetta.core.pack.task.operation.OperateOnResidueSubset(
                racaa, selector)
            task_factory.push_back(motif_operation)

    if len(repackable_residue_selector) > 0:  # This is always going to be true
        # given a res selector; should alter this logic at some point
        repackable_operation = rosetta.core.pack.task.operation.OperateOnResidueSubset(
            rosetta.core.pack.task.operation.RestrictToRepackingRLT(),
            repackable_residue_selector)
        task_factory.push_back(repackable_operation)
    if prepare_focus and len(designable_residue_selector) > 0:
        repackable_operation = rosetta.core.pack.task.operation.OperateOnResidueSubset(
            rosetta.core.pack.task.operation.RestrictToRepackingRLT(),
            designable_residue_selector)
        task_factory.push_back(repackable_operation)

    natro_residues = [
        i for i in range(1,
                         pose.size() + 1)
        if (not (designable_residue_selector[i]
                 or repackable_residue_selector[i]) and i not in motif_dict)
    ]
    if len(natro_residues) > 0:
        natro_selector =\
            rosetta.core.select.residue_selector.ResidueIndexSelector(list_to_str(natro_residues))
        natro_operation = rosetta.core.pack.task.operation.OperateOnResidueSubset(
            rosetta.core.pack.task.operation.PreventRepackingRLT(),
            natro_selector)
        task_factory.push_back(natro_operation)

    if extra_rotamers:
        ers = rosetta.core.pack.task.operation.ExtraRotamersGeneric()
        ers.ex1(True)
        ers.ex2(True)
        ers.extrachi_cutoff(18)
        task_factory.push_back(ers)

    if limit_aro_chi2:
        lac = rosetta.protocols.task_operations.LimitAromaChi2Operation()
        task_factory.push_back(lac)

    if layered_design:
        ld = rosetta.protocols.rosetta_scripts.XmlObjects.static_get_task_operation(
            '''<LayerDesign name="layer_all" layer="core_boundary_surface_Nterm_Cterm" use_sidechain_neighbors="True">
    		<Nterm>
    			<all append="DEGHKNQRST" />
    			<all exclude="CAFILMPVWY" />
    		</Nterm>
    		<Cterm>
    			<all append="DEGHKNQRST" />
    			<all exclude="CAFILMPVWY" />
    		</Cterm>
        </LayerDesign>''')
        task_factory.push_back(ld)

    return task_factory
コード例 #10
0
 def prepare_row(row: str) -> str:
     prepared_row = list(map(lambda char: f"{char} ", f"|{row}|"))
     return list_to_str(prepared_row)
コード例 #11
0
ファイル: wrapper.py プロジェクト: jgombac/web_extraction
    def stringify(self, depth=0):
        if self.tag == "[document]":
            return self.children[0].stringify()

        tabs = ""
        for i in range(depth):
            tabs += "\t"

        if self.tag == "text":
            if self.value is None:
                return ""
            string = self.value
            for c in self.children:
                if c.removed or c.is_empty_text():
                    continue
                string += c.stringify(depth + 1)

            regex_bl = ""
            regex_br = ""
            regex_symbol = ""
            if self.optional:
                regex_bl = "("
                regex_br = ")"
                regex_symbol = "?"
            if self.repeating:
                regex_bl = "("
                regex_br = ")"
                regex_symbol = "+"
            string = f"{tabs}{regex_bl}{string}{regex_br}{regex_symbol}"

        else:
            attrs_list = ""
            for attr_name in self.attrs:
                if type(self.attrs[attr_name]) is list:
                    attr_string = list_to_str(self.attrs[attr_name])
                else:
                    attr_string = self.attrs[attr_name]
                attrs_list += f' {attr_name}="{attr_string}"'
            opening_tag = f"<{self.tag}{attrs_list}>"
            if self.tag != "br":
                closing_tag = f"</{self.tag}>"
            else:
                closing_tag = ""
            children_string = ""
            regex_bl = ""
            regex_br = ""
            regex_symbol = ""
            if self.optional:
                regex_bl = "("
                regex_br = ")"
                regex_symbol = "?"
            if self.repeating:
                regex_bl = "("
                regex_br = ")"
                regex_symbol = "+"

            if len(self.children) > 0:
                if len(self.children) == 1 and self.children[
                        0].tag == "text" and not self.children[
                            0].is_empty_text():
                    string = f"{tabs}{regex_bl}{opening_tag}{self.children[0].stringify()}{closing_tag}{regex_br}{regex_symbol}"
                else:
                    for c in self.children:
                        if c.removed or c.is_empty_text():
                            continue
                        children_string += c.stringify(depth + 1) + "\n"
                    string = f"{tabs}{regex_bl}{opening_tag}\n{children_string}{tabs}{closing_tag}{regex_br}{regex_symbol}"
            else:
                string = f"{tabs}{regex_bl}{opening_tag}{closing_tag}{regex_br}{regex_symbol}"

        return string
コード例 #12
0
ファイル: ircbot.py プロジェクト: PietPtr/btptr
    def cmd_say(self, msg):
        """Say what the user told us to say

        msg - list of strings
        """
        return self.send_msg(utils.list_to_str(msg))
コード例 #13
0
        self.p = p

    def encrypt(self, m):
        res = []
        for i in range(len(m)):
            j = i % len(self.k)
            res.append((m[i] + self.k[j]) % self.p)
        return res

    def decrypt(self, c):
        res = []
        for i in range(len(c)):
            j = i % len(self.k)
            res.append((c[i] - self.k[j]) % self.p)
        return res


if __name__ == "__main__":
    enc = str_to_list("wegvrjepqnwezbdzqetgzbigvpta")
    affine = Affine(exeu(5, 26), 13)
    res = affine.decrypt(enc)
    res = list_to_str(res)
    print(res)

    key = intstr_to_list("20170317")
    vigenere = Vigenere(key, 10)
    m = intstr_to_list("314159265358979323846264")
    res = vigenere.encrypt(m)
    res = list_to_intstr(res)
    print(res)
コード例 #14
0
latin_chars.add("œ")
# remove some spurious characters
latin_chars.remove("<")
latin_chars.remove(">")
latin_chars.remove(",")
print(latin_chars)

char_embeddings = {}
char2index = {char: i for i, char in enumerate(latin_chars)}
for char, one_hot_vector in zip(
        char2index.keys(),
        keras.utils.to_categorical(list(char2index.values()),
                                   num_classes=len(char2index))):
    char_embeddings[char] = one_hot_vector

assert len(char_embeddings) == len(latin_chars), "Size of embedding and character set mismatch, expected {}, got {}" \
    .format(len(latin_chars), len(char_embeddings))
print("Extracted char embeddings")

latin_path = Path("../data/alphabets/latin.csv")
latin_path.touch()
out = latin_path.open('w', encoding='utf-16')

# empty header
out.write("\n")

for char, embedding in char_embeddings.items():
    s = char + "," + list_to_str(embedding) + "\n"
    out.write(s)
out.close()