from sklearn.metrics import precision_recall_fscore_support as acc
from hyperparams import Hyperparams as params
from sklearn.utils import shuffle
from collections import Counter
from string import punctuation
from nltk import word_tokenize
import pandas as pd
import csv
import re
import torch
import codecs
import copy
from sklearn.metrics import precision_recall_fscore_support

punct_dict = {';':'.', ':':',', '!' : '.'}
special_char_dict_v2 = {',': 'COMMA', '.': 'PERIOD', '?': 'QUESTION'}

def make_vocab(fpath, fname):
    text = open(fpath, 'r').read()
    if params.is_lower:
        words = text.lower().split()
    else:
        words = text.split()
    word2cnt = Counter(words)
    # if not os.path.exists('vocab'):
    #     os.mkdir('vocab')
    with open('{}'.format(fname), 'w') as fout:
        fout.write("{}\t1000000\n{}\t1000000\n{}\t1000000\n{}\t1000000\n".format("<pad>", "<unk>", "<s>", "</s>"))
        for word, cnt in word2cnt.most_common(len(word2cnt)):
            fout.write("{}\t{}\n".format(word, cnt))
        # fout.write("{}\t{}\n".format('_', '100'))

def process_g2p_english(f_file):
    graphes, phonemes = list(), list()
    fo = open(f_file, 'r')
    count = 0

    for line in fo:
        count += 1
        if count % 1000:
            print('count = %d' % count)
        graph = line.split('')[0]
        phoneme = ' '.join(line.split('')[1:])

        if graph[0] in punctuation:
            graph = graph[1:]

        graph = re.sub('\(1\)', '', graph)
        graph = re.sub('\(2\)', '', graph)
        graph = re.sub('\(3\)', '', graph)
        if graph not in graphes:
            graphes.append(graph)
            phonemes.append(phoneme)

    return graphes, phonemes

def process_g2p_english_anhKhoa(f_file):
    graphes, phonemes = list(), list()
    fo = open(f_file, 'r')
    count = 0

    for line in fo:
        count += 1
        if count % 1000:
            print('count = %d' % count)
        graph = ' '.join(line.split('\t')[0])
        phoneme = line.split('\t')[1]
        phoneme = phoneme.replace(' ', ' $ ').replace('|', ' | ')

        if graph not in graphes:
            graphes.append(graph)
            phonemes.append(phoneme)

    return graphes, phonemes

def process_g2p_vnmese(f_file):
    graphes, phonemes = list(), list()
    fo = open(f_file, 'r')
    count = 0
    for line in fo:
        count += 1
        if count % 1000:
            print('count = %d' % count)
        graph = line.split(' ', 1)[0].strip()
        phoneme = line.split(' ', 1)[1].strip()
        phoneme = phoneme.replace(' ', ' $ ').replace('|', ' | ')

        if graph in punctuation:
            continue

        graph = re.sub('\(1\)', '', graph)
        graph = re.sub('\(2\)', '', graph)
        graph = re.sub('\(3\)', '', graph)

        # graph_ch = list()
        # for c in list(graph):
        #     if c in punctuation:
        #         graph_ch.append(' ')
        #     else: graph_ch.append(c)
        # graph = ''.join(graph_ch)

        if graph not in graphes:
            graphes.append(graph)
            phonemes.append(phoneme)
    return graphes, phonemes

def save_list(l, file):
    fo = open(file, 'w')
    for e in l:
        fo.write(str(e).strip() + '\n')

def preprocess(text):
    vnese_lower = 'aáàảãạăắằẳẵặâấầẩẫậeéèẻẽẹêếềểễệiíìỉĩịoóòỏõọôốồổỗộơớờởỡợuúùủũụưứừửữựyýỳỷỹỵdđ'
    vnese_upper = 'AÁÀẢÃẠĂẮẰẲẴẶÂẤẦẨẪẬEÉÈẺẼẸÊẾỀỂỄỆIÍÌỈĨỊOÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢUÚÙỦŨỤƯỨỪỬỮỰYÝỲỶỸỴDĐ'
    text = re.sub('\([a-zA-Z_' +vnese_lower +vnese_upper + ']*\)', '', text)
    return text

def load_g2p(f_g2p):
    fo = open(f_g2p, 'r')
    lines = list()
    for line in fo:
        input_text = ' '.join(list(line.split()[0]))
        target_text = ' '.join(line.split()[1:])
        lines.append(input_text + '\t' + target_text)

    return lines

def load_g2p_english(graph_file, phoneme_file):
    lines = list()
    f_graph = open(graph_file, 'r')
    f_phoneme = open(phoneme_file, 'r')
    graphes = f_graph.readlines()
    phonemes = f_phoneme.readlines()
    for i in range(len(graphes)):
        line = graphes[i] + '\t' + phonemes[i]
        lines.append(line)

    return lines

def load_foreign_words(f_foreign):
    csv_writer = csv.writer(open('sample_synthesize.csv', 'w'), delimiter='\t')
    csv_writer.writerow(['written', 'spoken'])
    df = pd.read_csv(f_foreign)
    words_foreign = df.word.values.tolist()
    trans_foreign = df.transcription.values.tolist()
    lines = list()
    for i in range(len(words_foreign)):
        input_text = str(words_foreign[i]).lower()
        target_text = str(trans_foreign[i]).lower()
        target_text = target_text.replace('-','_')
        target_text = target_text.replace(' ', '$')
        target_text = target_text.replace('_', ' ')
        target_text = target_text.replace('$', ' _ ')
        input_text = ' '.join(preprocess(input_text))
        target_text = preprocess(target_text)
        # input_words = input_text.split()
        # target_words = target_text.split()
        # if len(input_words) == len(target_words):
        line = input_text + '\t' + target_text
        lines.append(line)

    return lines

def load_foreign_words_v2(f_foreign):
    df = pd.read_csv(f_foreign)
    words_foreign = df.word.values.tolist()
    trans_foreign = df.transcription.values.tolist()
    lines = list()
    line_errors = list()
    for i in range(len(words_foreign)):
        input_text = str(words_foreign[i]).lower()
        target_text = str(trans_foreign[i]).lower()
        target_text = target_text.replace('-','_')
        # target_text = target_text.replace(' ', '$')
        # target_text = target_text.replace('_', ' ')
        # target_text = target_text.replace('$', ' _ ')
        input_text = preprocess(input_text)
        target_text = preprocess(target_text)
        input_words = input_text.split()
        target_words = target_text.split()
        if len(input_words) == len(target_words):
            for j in range(len(input_words)):
                target_words[j] = target_words[j].replace('_', ' ')
                line = ' '.join(list(input_words[j])) + '\t' + target_words[j]
                if line not in lines:
                    lines.append(line)
        else:
            target_text = target_text.replace('-','_')
            line_errors.append(input_text + '\t' + target_text)
            lines.append(' '.join(list(input_text)) + '\t' + target_text)

    return lines, line_errors

def load_csv_2_cols(csv_file, col1, col2):
    df = pd.read_csv(csv_file, delimiter='\t')
    col1_data = df[col1].values.tolist()
    col2_data = df[col2].values.tolist()
    lines = list()
    for i in range(len(col1_data)):
        lines.append(str(col1_data[i]) + '\t' + str(col2_data[i]))
    return lines

def load_csv_3_cols(csv_file, col1, col2, col3):
    df = pd.read_csv(csv_file, names=[col1, col2, col3], delimiter='\t')
    line_col1 = df[col1].values.tolist()
    line_col2 = df[col2].values.tolist()
    line_col3 = df[col3].values.tolist()
    lines = list()
    for i in range(len(line_col1)):
        lines.append(str(line_col1[i]) + ' ' + str(line_col2[i]) + '\t' + str(line_col3[i]))
    return lines

def load_seq2seq(csv_file, tag_col, src_col, tgt_col):
    df = pd.read_csv(csv_file, delimiter='\t')
    tag = df[tag_col].values.tolist()
    src = df[src_col].values.tolist()
    tgt = df[tgt_col].values.tolist()
    lines = list()
    for i in range(len(src)):
        # lines.append(str(tag[i].strip()) + ' ' + str(src[i]) + '\t' + str(tgt[i]))
        lines.append(str(tag[i].strip()) + ' ' + str(tgt[i]) + '\t' + str(src[i]))
    return lines

def split_train_test(lines, path, train_ratio=0.8, val_ratio=0.1):
    lines = shuffle(lines)
    train_size = int(len(lines) * train_ratio)
    val_size = int(len(lines) * val_ratio)
    print('train size: ', train_size)
    print('val size: ', val_size)
    train_data = lines[:train_size]
    val_data = lines[train_size:train_size + val_size]
    test_data = lines[train_size + val_size:]
    save_data(train_data, 'train', path)
    save_data(val_data, 'val', path)
    save_data(test_data, 'test', path)

def save_data(lines, type, path):
    src = list()
    tgt = list()
    f_src = open(path + type + '.src.txt', 'w')
    f_tgt = open(path + type + '.tgt.txt', 'w')
    for line in lines:
        pair = line.split('\t')
        f_src.write(pair[0].strip() + '\n')
        f_tgt.write(pair[1].strip() + '\n')
        src.append(pair[0])
        tgt.append(pair[1])

def read_data(data_path):
    data = torch.load(data_path)
    dict = data['dict']
    tgt_word2idx = dict['tgt']
    print(tgt_word2idx)

def save_file(lines, path, f_name):
    f_out = open(path + f_name, 'w')
    for line in lines:
        f_out.write(line + '\n')

def word2char(f_word, f_char):
    fo = open(f_word, 'r')
    fw = open(f_char, 'w')
    for line in fo:
        fw.write(' '.join(list(line)))

def save_csv(l1, l2, csv_file):
    csv_writer = csv.writer(open(csv_file, 'w'), delimiter='\t')
    csv_writer.writerow(['graph_word', 'graph_char', 'phoneme'])
    l1_char = list()
    for e in l1:
        l1_char.append(' '.join(list(e.strip())))
    for i in range(len(l1)):
        csv_writer.writerow([l1[i], l1_char[i], l2[i].strip()])

def revert_norm_word(str):
    words = str.split()
    for i in range(1, len(words)):
        if words[i] in special_char_dict_v2.keys():
            words[i - 1] = words[i - 1] + words[i]
            words[i] = ''

    return ' '.join(words)

# remove punctuation exclude .,?
def remove_puntuation(str):
    tokens = list()
    for token in str.split():
        if token in punct_dict.keys():
            tokens.append(punct_dict[token])
        elif token in special_char_dict_v2.keys():
            tokens.append(token)
        elif token in punctuation:
            tokens.append('')
        else:
            tokens.append(token)

    input_str = ' '.join(tokens)
    input_str = re.sub('\.+\s*[\.*|,*|\?*]', '.', input_str)
    input_str = re.sub(',+\s*[\.*|,*|\?*]', ',', input_str)
    input_str = re.sub('\?+\s*[\.*|,*|\?*]', ',', input_str)
    return input_str

# manually tokenize
def norm_word(word):
    char = list(word)
    pre_is_word = False
    pre_is_punct = False
    for i in range(len(char)):
        if char[i] in punctuation:
            if pre_is_word:
                char[i] = ' ' + char[i]
            else:
                char[i] = char[i] + ' '
            pre_is_word = False
            pre_is_punct = True
        else:
            if pre_is_punct:
                char[i] = ' ' + char[i]
            pre_is_word = True
            pre_is_punct = False

    return ''.join(char)

def replace_multi_space(str):
    str = re.sub(' +', ' ', str)
    return str

def tokenize(input_str):
    input_str = re.sub('^,', '', input_str)
    input_str = re.sub('\.+\s*\.', '.', input_str) # replace multiple period with one period
    tokens = word_tokenize(input_str)
    for i in range(len(tokens)):
        tokens[i] = norm_word(tokens[i])
        # if i != len(tokens) - 1:
        #     tokens[i] = tokens[i].replace('.','')

    input_str = " ".join(tokens)
    input_str = remove_puntuation(input_str)
    input_str = revert_norm_word(input_str)

    return replace_multi_space(input_str)

def save_csv_v2(src, tgt, csv_file):
    assert len(src) == len(tgt), '[Warning] The training instance count is not equal.'
    csv_writer = csv.writer(open(csv_file, 'w'), delimiter='\t')
    csv_writer.writerow(['src', 'tgt'])
    for i in range(len(src)):
        csv_writer.writerow([src[i], tgt[i]])

def prepare_seq2seq(lines):
    src_all = list()
    tgt_all = list()
    for line in lines:
        line = tokenize(line)
        src = line.replace(',','')
        src = src.replace('.','')
        src = src.replace('?','')
        src = src.lower()
        src_all.append(src.strip())
        tgt_all.append(line.strip())

    return src_all, tgt_all

def read_lines(f_file):
    fo = open(f_file, 'r')
    return fo.readlines()

def read_csv(f_csv, cols):
    df = pd.read_csv(f_csv, delimiter='\t')
    lines = list()
    for i in range(len(cols)):
        lines.append(df[cols[i]].values.tolist())

    return lines

def norm_vnmese_accent(str):
    words = str.split(' ')
    for i in range(len(words)):
        if len(words[i]) <= 3:
            if not words[i].startswith('qu'):
                words[i] = words[i].replace("uỳ", "ùy")
                words[i] = words[i].replace("uý", "úy")
                words[i] = words[i].replace("uỷ", "ủy")
                words[i] = words[i].replace("uỹ", "ũy")
                words[i] = words[i].replace("uỵ", "ụy")
            else:
                words[i] = words[i].replace("ùy", "uỳ")
                words[i] = words[i].replace("úy", "uý")
                words[i] = words[i].replace("ủy", "uỷ")
                words[i] = words[i].replace("ũy", "uỹ")
                words[i] = words[i].replace("ụy", "uỵ")

            words[i] = words[i].replace("oà", "òa")
            words[i] = words[i].replace("oá", "óa")
            words[i] = words[i].replace("oả", "ỏa")
            words[i] = words[i].replace("oã", "õa")
            words[i] = words[i].replace("oạ", "ọa")
            words[i] = words[i].replace("oè", "òe")
            words[i] = words[i].replace("oé", "óe")
            words[i] = words[i].replace("oẻ", "ỏe")
            words[i] = words[i].replace("oẽ", "õe")
            words[i] = words[i].replace("oẹ", "ọe")
        else:
            words[i] = words[i].replace("òa", "oà")
            words[i] = words[i].replace("óa", "oá")
            words[i] = words[i].replace("ỏa", "oả")
            words[i] = words[i].replace("õa", "oã")
            words[i] = words[i].replace("ọa", "oạ")
            words[i] = words[i].replace("òe", "oè")
            words[i] = words[i].replace("óe", "oé")
            words[i] = words[i].replace("ỏe", "oẻ")
            words[i] = words[i].replace("õe", "oẽ")
            words[i] = words[i].replace("ọe", "oẹ")

    return ' '.join(words)

def load_lexicon(full_path, assert2fields=False, value_processor=None):
    phones = set()
    if value_processor is None:
        value_processor = lambda x: x[0]
    lex = {}
    for line in codecs.open(full_path, mode='r', encoding='utf-8'):
        line = norm_vnmese_accent(line)
        # line = unicodedata.normalize("NFC", line)
        parts = line.strip().split()
        if assert2fields:
            assert (len(parts) == 2)
        lex[parts[0]] = value_processor(parts[1:])
        for p in parts[1:]:
            phones.add(p)

    return lex, phones

def build_phone_vocab(vi_lexicon_file, foreign_lexicon_file):
    viLex, viPhones = load_lexicon(vi_lexicon_file, value_processor = lambda x: " ".join(x))
    foreignLex, foreignPhones = load_lexicon(foreign_lexicon_file, value_processor = lambda x: " ".join(x))

    phones = set(list(viPhones) + list(foreignPhones))
    vocab = list(phones)

    symbol_to_id = {s: i for i, s in enumerate(vocab)}
    id_to_symbol = {i: s for i, s in enumerate(vocab)}

    lexicon = (viLex, foreignLex)

    return symbol_to_id, id_to_symbol, lexicon

_pad = '_'
_punctuation = '!\'(),.:;? '
_special = '-'

def G2P(text, viLex, foreignLex):
    text = norm_vnmese_accent(text)
    # text = unicodedata.normalize("NFC", text)
    # chars = re.escape(string.punctuation)
    # line = text.strip().lower()
    # tmp = re.sub(' +',' ',line).split()
    tmp_string = ""

    for nword in text.split():
        if nword in viLex:  # found in vietnamese lexicon
            lexout = viLex[nword]
            tmp_string = tmp_string + ' ' + re.sub(' ', '|', ' '.join(lexout.split()))
        elif nword in foreignLex:
            lexout = foreignLex[nword]
            tmp_string = tmp_string + ' ' + re.sub(' ', '|', ' '.join(lexout.split()))
        else:
            if nword not in _punctuation:
                tmp_string = tmp_string + ' _'  # _ for UNK
            else:
                tmp_string = tmp_string + ' ' + nword

    # print(text)
    # print(tmp_string.strip())

    return tmp_string.strip()

def process_vin_list(f_vin, g2p_csv):
    symbol_to_id, id_to_symbol, lexicon = build_phone_vocab(
        'resources/all-vietnamese-syllables_17k9.XSAMPA.Mien-BAC.lex', 'resources/Foreign-Lexicon-6k.lex')
    fo = open(f_vin, 'r')
    f_error = open('resources/vin_errors.txt', 'w')
    df = pd.read_csv(g2p_csv, delimiter='\t')
    graphes_ = df.graph_word.values.tolist()
    phoneme_ = df.phoneme.values.tolist()
    g2p = dict(zip(graphes_, phoneme_))

    for line in fo:
        pairs = line.split('\t')
        graphes = pairs[0].lower()
        phonemes = pairs[1]
        graph_tokens = graphes.split()
        phoneme_tokens = phonemes.split()
        if len(graph_tokens) != len(phoneme_tokens):
            f_error.write(line)
        else:
            for i in range(len(graph_tokens)):
                phoneme_tokens[i] = phoneme_tokens[i].replace('_', ' ')
                phoneme_token_converted = G2P(phoneme_tokens[i], lexicon[0], lexicon[1])
                phoneme_token_converted = phoneme_token_converted.replace(' ', ' $ ').replace('|', ' | ')
                g2p[graph_tokens[i]] = phoneme_token_converted

    out_file = g2p_csv.replace('.csv', '_v2.csv')
    csv_writer = csv.writer(open(out_file, 'w'), delimiter='\t')
    csv_writer.writerow(['graph_word', 'graph_char', 'phoneme'])
    for key, value in g2p.items():
        csv_writer.writerow([key, ' '.join(list(str(key))), value])

def load_g2p_csv(f_csv):
    df = pd.read_csv(f_csv, delimiter='\t')
    graph = df.graph_word.values.tolist()
    graph_char = df.graph_char.values.tolist()
    phonemes = df.phoneme.values.tolist()
    save_list(graph, 'task_g2p_vnmese_withtone_localization_sampa_v3/dataset/graph_vnmese_withtone_sampa_v2.txt')
    save_list(graph_char, 'task_g2p_vnmese_withtone_localization_sampa_v3/dataset/graph_vnmese_withtone_sampa_char_v2.txt')
    save_list(phonemes, 'task_g2p_vnmese_withtone_localization_sampa_v3/dataset/phonemes_vnmese_withtone_sampa_v2.txt')

def is_all_upper(word):
    if is_special_char(word): return False
    char = list(word)
    for c in char:
        if c.lower() == c and c not in punctuation:
            return False

    return True

def is_number(word):
    try:
        word = int(word)
        word = float(word)
        return True
    except:
        return False

def is_number_first(word):
    try:
        first_char = int(word[0])
        return True
    except:
        return False

def is_special_char(word):
    if word == '.' or word == ',' or word == '?':
        return True
    return False

def is_first_upper(word):
    if is_number_first(word): return False
    if is_special_char(word): return False
    char = list(word)
    if char[0] == char[0].upper():
        return True

    return False

def is_period(word):
    return '.' in word

def is_comma(word):
    return ',' in word

def is_question(word):
    return '?' in word

def tag_sample(words):
    label = list()
    for i in range(len(words)):
        if is_all_upper(words[i]) and is_period(words[i]):
            label.append('B-UPALL-PERIOD')
        elif is_all_upper(words[i]) and is_comma(words[i]):
            label.append('B-UPALL-COMMA')
        elif is_all_upper(words[i]) and is_question(words[i]):
            label.append('B-UPALL-QUESTION')
        elif is_first_upper(words[i]) and is_period(words[i]):
            label.append('B-UPFIRST-PERIOD')
        elif is_first_upper(words[i]) and is_comma(words[i]):
            label.append('B-UPFIRST-COMMA')
        elif is_first_upper(words[i]) and is_question(words[i]):
            label.append('B-UPFIRST-QUESTION')
        elif is_period(words[i]):
            label.append('B-PERIOD')
        elif is_comma(words[i]):
            label.append('B-COMMA')
        elif is_question(words[i]):
            label.append('B-QUESTION')
        elif is_all_upper(words[i]):
            label.append('B-UPALL')
        elif is_first_upper(words[i]):
            label.append('B-UPFIRST')
        else:
            label.append('O')

    return label

def filter_tag(real, predict, real_tag, predict_tag):
    real_filter, predict_filter = list(), list()
    if len(real_tag) != len(predict_tag):
        # print('real: ', real)
        # print('predict: ', predict)
        # print('len real: ', len(real))
        # print('len predict: ', len(predict))
        # print('---------------------------------------------------------------')
        print('')
    else:
        for i in range(len(real_tag)):
            if real_tag[i] != 'O' and predict_tag[i] != 'O':
                real_filter.append(real_tag[i])
                predict_filter.append(predict_tag[i])

    return real_filter, predict_filter

def cal_tag_acc(real, predict):
    pre, rec, f1, support = precision_recall_fscore_support(real, predict, average='weighted')
    return pre, rec, f1

def prepare_label_data(src_path, tgt_path, label_path):
    tgt_path_filter = tgt_path.replace('.tgt.txt', '.filter.tgt.txt')
    src_path_filter = src_path.replace('.src.txt', '.filter.src.txt')
    fo_src = open(src_path, 'r')
    fo_tgt = open(tgt_path, 'r')
    fo_label = open(label_path, 'w')
    fo_src_filter = open(src_path_filter, 'w')
    fo_tgt_filter = open(tgt_path_filter, 'w')
    target_labels = list()
    count = 0
    for line_src, line_tgt in zip(fo_src, fo_tgt):
        source_text = copy.deepcopy(line_tgt)
        line_tgt = remove_multi_punct(line_tgt)
        source_words = line_src.split()
        target_words = line_tgt.split()
        if target_words[0].strip() == '.' or target_words[0].strip() == ',' or target_words[0].strip() == '?':
            del target_words[0]
        if len(source_words) == len(target_words):
            count += 1
            print('\r count = ', count, end='\r')
            target_label = tag_sample(target_words)
            target_labels.append(target_label)
            fo_label.write(' '.join(target_label) + '\n')
            fo_src_filter.write(' '.join(source_words).strip() + '\n')
            fo_tgt_filter.write(' '.join(target_words).strip() + '\n')
        # else:
        #     print(line_src.strip() + '\n')
        #     print(source_text.strip() + '\n')
        #     print(line_tgt.strip() + '\n')
        #     print('--------------------------------------')

    return target_labels

def remove_multi_punct(sentence):
    sentence = re.sub('(\.\s)+\.', '.', sentence)
    sentence = re.sub('(,\s)+,', ',', sentence)
    sentence = re.sub('(\?\s)+\?', '?', sentence)
    sentence = re.sub(',\s\.', '.', sentence)
    sentence = re.sub(',\s\?', '?', sentence)
    sentence = re.sub('\.\s,', ',', sentence)
    sentence = re.sub('\.\s\?', '?', sentence)
    sentence = re.sub('\?\s\.', '.', sentence)
    sentence = re.sub('\?\s,', ',', sentence)
    return sentence

if __name__ == '__main__':
    '''Step 3: build vocab'''
    # concat train and val before building
    make_vocab(params.source_train, params.src_vocab)
    make_vocab(params.target_train, params.tgt_vocab)

    ex = 'ex4'
    file_name = '04_10kForeign_27kForeign_125kEnglish_17kVNSylable.lex'
    '''Step 1: save graphes, phonemes to file'''
    # graphes, phonemes = process_g2p_english('task_g2p_anhKhoa/dataset/ex1/01_baseline_vnsylable_only_17k.txt')
    # graphes, phonemes = process_g2p_vnmese('task_g2p_vnmese_withtone_localization_sampa_v2/dataset/Foreign-Lexicon-13k-6k-27k.lex')
    # graphes, phonemes = process_g2p_english_anhKhoa('task_g2p_anhKhoa_v2/dataset/' + ex + '/' + file_name)
    # save_list(graphes, 'task_g2p_anhKhoa_v2/dataset/' + ex + '/' + file_name + '_graph.txt')
    # save_list(phonemes, 'task_g2p_anhKhoa_v2/dataset/' + ex + '/' + file_name + '_phonemes.txt')
    # save_csv(graphes, phonemes, 'task_g2p_anhKhoa/dataset/ex1/01_baseline_vnsylable_only_17k.csv')
    # word2char('task_spoken2written/dataset/val.tgt.txt', 'task_spoken2written/dataset/val_char.tgt.txt')

    '''Step 2: Split train test'''
    # lines = load_tone_data('corpora/addtone/train_tokenizer.csv')
    # lines = load_foreign_words('dataset/norm_foreign/foreign.csv')
    # lines, line_errors = load_foreign_words_v2('dataset/norm_foreign/foreign.csv')
    # save_file(line_errors, 'dataset/norm_foreign_v2/', 'line_errors.txt')
    # lines = load_seq2seq('dataset/auto_upper_punct_seq2seq/shard_0000_auto_upper_punct_seq2seq.csv', 'src', 'tgt')
    # lines = load_seq2seq('dataset/spoken2written/norm_seq2seq.csv', 'tag', 'written', 'spoken')
    # lines = load_g2p('dataset/g2p/foreign-lexicon-13k_one_type.lex')
    # lines = load_g2p_english('task_g2p_vnmese_withtone_localization_sampa_v3/dataset/graph_vnmese_withtone_sampa_char_v2.txt',
    #                          'task_g2p_vnmese_withtone_localization_sampa_v3/dataset/phonemes_vnmese_withtone_sampa_v2.txt')
    # lines = load_g2p_english('task_g2p_anhKhoa_v2/dataset/' + ex + '/' + file_name + '_graph.txt',
    #                          'task_g2p_anhKhoa_v2/dataset/' + ex + '/' + file_name + '_phonemes.txt')
    # split_train_test(lines, 'task_g2p_anhKhoa_v2/dataset/' + ex + '/')
    # read_data('dataset/norm_foreign/norm_foreign.pkl')

    # lines = read_csv('task_diacritics_restoration/dataset/train_tokenizer.csv', ['no_tone', 'tone'])
    # src_all, tgt_all = prepare_seq2seq(lines[1])
    # save_csv_v2(src_all, tgt_all, 'task_punct_restoration/dataset/dataset.csv')
    # lines = load_csv_2_cols('task_punct_restoration/dataset/dataset.csv', 'src', 'tgt')
    # split_train_test(lines, 'task_punct_restoration/dataset/', train_ratio=0.95, val_ratio=0.025)

    # lines = load_csv_2_cols('task_spoken2written/dataset/norm_seq2seq.csv', 'src', 'tgt')
    # lines = load_csv_3_cols('task_written2spoken/dataset/norm_seq2seq.csv', 'tag', 'src', 'tgt')
    # split_train_test(lines, 'task_written2spoken/dataset/', train_ratio=0.9, val_ratio=0.05)

    # convert graph2sampa
    # symbol_to_id, id_to_symbol, lexicon = build_phone_vocab('resources/all-vietnamese-syllables_17k9.XSAMPA.Mien-BAC.lex', 'resources/Foreign-Lexicon-6k.lex')
    # output = G2P('vin e co', lexicon[0], lexicon[1])
    # print(output)

    # process_vin_list('resources/vinGroup_list.txt', 'task_g2p_vnmese_withtone_localization_sampa_v3/dataset/g2p_vnmese_withtone_sampa.csv')
    # load_g2p_csv('task_g2p_vnmese_withtone_localization_sampa_v3/dataset/g2p_vnmese_withtone_sampa_v2.csv')

    # prepare_label_data(params.source_train, params.target_train, params.target_train_label)
    # prepare_label_data(params.source_test, params.target_test, params.target_test_label)
    # prepare_label_data(params.source_val, params.target_val, params.target_val_label)
    # print(remove_multi_punct('Băng đô, mũ len, balo, Bình sữa Hàn Quốc, Mỹ cực HOT. . . .'))


from appium import webdriver
import pytest

@pytest.fixture(scope='class')
def setup(request):
    driver = webdriver.Remote(command_executor='http://127.0.0.1:4723/wd/hub',
                                   desired_capabilities={
                                       "platformName": "iOS",
                                       "platformVersion": "13.5",
                                       "deviceName": "iPhone 11 Pro MT",
                                       "automationName": "XCUITest",
                                       "app": "/Users/username/Library/Developer/Xcode/DerivedData/WordPress-bgx/Build/Products/Debug-iphonesimulator/WordPress.app",
                                       "noReset": True,
                                       "autoAcceptAlerts": False
                                   })
    request.cls.driver = driver
    yield
    driver.quit()


from odoo import api, fields, models, _

class TranslationSource(models.Model):
    _name = 'formio.translation.source'
    _description = 'formio.js Version Translation Source'
    _rec_name = 'source'

    property = fields.Text(string='Property', required=True)
    source = fields.Text(string='Source Term', required=True)


#!/usr/bin/python3

"""
takes in a URL and an email address, sends a POST request to the passed URL
"""

import requests
import sys

if __name__ == '__main__':
    url = sys.argv[1]
    r = requests.get(url)
    if r.status_code >= 400:
        print("Error code: {}".format(r.status_code))
    else:
        print(r.text)


import pytest
import django.utils.timezone
from learn_islam.quizzes import models

pytestmark = pytest.mark.django_db

def test_quizzes_models_creation():
    before = django.utils.timezone.now()
    models.Quiz.objects.create(
        difficulty=1
    )

    quizzes = models.Quiz.objects.all()
    assert quizzes.count() == 1
    assert quizzes[0].difficulty == 1
    assert quizzes[0].pub_date >= before
    assert quizzes[0].pub_date <= django.utils.timezone.now()


import logging

from WDL import values_to_json
from WDL.runtime.task import run_local_task, _eval_task_inputs, InputStdLib
from WDL.runtime import task_container

from utils import unmarshall_task, unmarshall_input, load_config

from cluster import app

def _eval_command(logger, cfg, task, inputs, run_id = None, run_dir = None, logger_prefix = None,
    _run_id_stack = None, _cache = None, _plugins = None,):
    container =  task_container.new(cfg, logger, run_id, run_dir)
    container_env=_eval_task_inputs(logger, task, inputs, container)
    stdlib = InputStdLib(task.effective_wdl_version, logger, container)
    cmd = task.command.eval(container_env, stdlib).value

@app.task
def run_task(
    task_str,
    task_digest,
    task_inputs,
    run_id = None,
    run_dir = None,
    logger_prefix = None,
    _run_id_stack = None,
    _cache = None,
    _plugins = None,
):

    cfg = load_config()

    task = unmarshall_task(task_str, task_digest)
    input_env = unmarshall_input(task, task_inputs)

    sub_args = (cfg, task, input_env)
    sub_kwargs = {
        "run_id": run_id,
        "run_dir": run_dir,
        "logger_prefix": logger_prefix,
        "_run_id_stack": _run_id_stack,
        "_cache": _cache,
    }
    #_eval_command(logger, *sub_args, **sub_kwargs)

    output_dir, output_env = run_local_task(*sub_args, **sub_kwargs)
    output_json = values_to_json(output_env, namespace=task.name)
    output = (output_dir, output_json)

    return output


import getopt
import json
import os
import sys
import pandas as pd
from operator import itemgetter
from sv_portal_reader import SVPortalParser
from jcg_scraper import JCGScraper

def analyzer(argv):

    try:
        if not os.path.exists('jcg-data'):
            os.makedirs('jcg-data')

        format = ""
        jcg_code = ""
        opts, args = getopt.getopt(argv, 'c:f:')

        for opt, arg in opts:
            if opt == "-c":
                jcg_code = arg

            if opt == "-f":
                format = arg

        f = open(format)
        data = json.load(f)
        sv_portal = SVPortalParser(format_data=data)
        scraper = JCGScraper(jcg_code, parser=sv_portal)
        scraper.scrape_entries()
        scraper.scrape_results()

        jcg_data = scraper.entry_decks.values()
        sorted_data = sorted(jcg_data, key=itemgetter("top"), reverse=True)

        jcg_decks_data = []
        players_data = []

        for data in jcg_data:
            player = {
                "id": data["id"],
                "name": data["name"],
                "top": data["top"]
            }

            for (idx, deck) in enumerate(data["decks"]):
                result = (deck["craft"], deck["archetype"], deck["link"], data["top"], data["name"], data["id"])
                player[f'deck_{idx+1}'] = deck["archetype"]
                player[f'link_{idx+1}'] = deck["link"]
                jcg_decks_data.append(result)

            players_data.append(player)

        players_data_df = pd.DataFrame(players_data)
        decks_data_df = pd.DataFrame(jcg_decks_data,
            columns=[
                "craft",
                "archetype",
                "deck_link",
                "score",
                "player_name",
                "player_id"
            ]
        )
        outdir = './jcg-data'

        archetype_df = decks_data_df['archetype'].value_counts()
        craft_df = decks_data_df['craft'].value_counts()

        players_data_df.to_csv(os.path.join(outdir, f'{scraper.jcg_date}-lineups.csv'), index=False)
        decks_data_df.to_csv(os.path.join(outdir, f"{scraper.jcg_date}-classified-decks.csv"), index=False)
        archetype_df.to_csv(os.path.join(outdir, f"{scraper.jcg_date}-archetypes-data.csv"))
        craft_df.to_csv(os.path.join(outdir, f"{scraper.jcg_date}-crafts-data.csv"))
    except getopt.GetoptError:
        print('analyzer.py -c <jcg-code> -f <format-file>')
        sys.exit(2)

if __name__ == "__main__":
    analyzer(sys.argv[1:])


# 42840 (모의고사)
# 시간 복잡도: O(n)
def solution(answers):
    one = [1,2,3,4,5]
    two = [2,1,2,3,2,4,2,5]
    thr = [3,3,1,1,2,2,4,4,5,5]

    cnt = [0,0,0]
    right = []

    # answer의 각 요소를 순회하며 각 학생이 맞춘 문제 수를 계산
    for i in range(len(answers)):
        if answers[i] == one[(i%5)]:
            cnt[0] += 1
        if answers[i] == two[(i%8)]:
            cnt[1] += 1
        if answers[i] == thr[(i%10)]:
            cnt[2] += 1

    # 각 학생이 맞춘 문제 수를 토대로 최고 점수를 받은 학생을 찾아 리스트에 추가하고 반환
    for j in range(3):
        if cnt[j] == max(cnt):
            right.append(j+1)

    return right


import json
import os
from pathlib import Path

"""Utility script to remove a certain language from the translations mapping json"""
PATH_TO_TRANSLATIONS_MAPPING_JSON = '/Users/sunyitao/Documents/Projects/GitHub/PsychicLearners/data_utils/translations_mapping.json'
LANGUAGE_TO_REMOVE = ''

#Below can be unchanged
PATH_TO_WORD_2_LANG_JSON = Path.cwd() / 'word_to_lang.json'
OUTPUT_JSON = Path(PATH_TO_TRANSLATIONS_MAPPING_JSON).parent / 'translations_mapping_output.json'

with open(PATH_TO_TRANSLATIONS_MAPPING_JSON, 'r') as f:
    translations_mapping = json.load(f)
with open(PATH_TO_WORD_2_LANG_JSON, 'r') as f:
    word2lang = json.load(f)

new_mapping = {}
for word, translated_word in translations_mapping.items():
    if word in word2lang.keys():
        language = word2lang[word]
        if language == LANGUAGE_TO_REMOVE:
            print(f'Word:{word}, Translation: {translated_word} will be removed')
            continue
        else:
            new_mapping[word] = translated_word
    else:
        raise LookupError(f'Unrecognised word {word}, shouldnt be happening')

with open(OUTPUT_JSON, 'w') as f:
    f.write(json.dumps(new_mapping))


from pathlib import Path
from stat import S_IMODE

import pytest

from omdlib.certs import CertificateAuthority  # pylint: disable=wrong-import-order

from cmk.utils.certs import root_cert_path, RootCA
from cmk.utils.crypto.certificate import (
    Certificate,
    CertificatePEM,
    PlaintextPrivateKeyPEM,
    RsaPrivateKey,
)

CA_NAME = "test-ca"

@pytest.fixture(name="ca")
def fixture_ca(tmp_path: Path) -> CertificateAuthority:
    ca_path = tmp_path / "ca"
    return CertificateAuthority(
        root_ca=RootCA.load_or_create(root_cert_path(ca_path), CA_NAME),
        ca_path=ca_path,
    )

def test_initialize(ca: CertificateAuthority) -> None:
    assert ca.root_ca.certificate.common_name == CA_NAME
    assert ca.root_ca.certificate.public_key == ca.root_ca.private_key.public_key

def _file_permissions_is_660(path: Path) -> bool:
    return oct(S_IMODE(path.stat().st_mode)) == "0o660"

def test_create_site_certificate(ca: CertificateAuthority) -> None:
    site_id = "xyz"
    assert not ca.site_certificate_exists(site_id)

    ca.create_site_certificate(site_id, key_size=1024)
    assert ca.site_certificate_exists(site_id)
    assert _file_permissions_is_660(ca._site_certificate_path(site_id))

    mixed_pem = ca._site_certificate_path(site_id).read_bytes()
    certificate = Certificate.load_pem(CertificatePEM(mixed_pem))
    private_key = RsaPrivateKey.load_pem(PlaintextPrivateKeyPEM(mixed_pem), None)

    assert certificate.common_name == site_id
    assert certificate.public_key == private_key.public_key
    certificate.verify_is_signed_by(ca.root_ca.certificate)


# This Python 3 environment comes with many helpful analytics libraries installed

# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python

# For example, here's several helpful packages to load in

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the "../input/" directory.

# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os

for dirname, _, filenames in os.walk('/kaggle/input'):

    for filename in filenames:

        print(os.path.join(dirname, filename))

# Any results you write to the current directory are saved as output.
import glob

import cv2

from matplotlib import pyplot as plt

X_train_path = glob.glob("/kaggle/input/denoising-dirty-documents/train/*.png")

y_train_path = glob.glob("/kaggle/input/denoising-dirty-documents/train_cleaned/*.png")

X_test_path = glob.glob("/kaggle/input/denoising-dirty-documents/test/*.png")

input_shape = (258, 540, 1)
# img = cv2.imread(X_train_path[0], cv2.IMREAD_GRAYSCALE)

# plt.imshow(img)

# plt.show()
def load_images(path):

    image_list = []

    for pth in path:

        img = cv2.imread(pth, 0) # read grayscale image

        img = cv2.resize(img, (input_shape[1], input_shape[0]))

        img = img / 255.

        img = np.expand_dims(img, axis=-1)

        image_list.append(img)

    return image_list
X_train_all = load_images(X_train_path)

y_train_all = load_images(y_train_path)

X_test = load_images(X_test_path)

# convert list of images to np array

X_train_all = np.array(X_train_all)

y_train_all = np.array(y_train_all)

X_test = np.array(X_test)

print(X_train_all.shape)

print(y_train_all.shape)

print(X_test.shape)
# print(X_train_all[0].shape)

# plt.imshow(X_train_all[44])

# plt.show()
# train val split

from sklearn.model_selection import train_test_split

X_train, X_val, y_train, y_val = train_test_split(X_train_all, y_train_all, test_size=0.3, random_state=0)

print(X_train.shape)

print(y_train.shape)

print(X_val.shape)

print(y_val.shape)
from __future__ import absolute_import, division, print_function, unicode_literals

import tensorflow as tf

from tensorflow.keras import layers

tf.keras.backend.clear_session()  # For easy reset of notebook state.
model = tf.keras.Sequential()

# add convolutional layer to the model with relu activation

# 32 convolution filters used each of size 3x3

model.add(layers.Conv2D(32, kernel_size=(3, 3), activation='relu', padding='same', input_shape=input_shape))

model.add(layers.MaxPooling2D(2, padding='same'))

model.add(layers.Conv2D(64, (3,3), activation='relu', padding='same'))

model.add(layers.UpSampling2D((2,2)))

model.add(layers.Conv2D(1, (3,3), activation='sigmoid', padding='same'))

model.summary()
model.compile(loss='mean_squared_error',

              optimizer='adam',

              metrics=['accuracy'])
num_epochs = 100

# batch size -Total number of training examples present in a single batch.

batch_size = 8

history = model.fit(X_train, y_train, epochs=num_epochs,

                    batch_size=batch_size,

                    verbose=1,

                    validation_data=(X_val, y_val))
score = model.evaluate(X_val, y_val, verbose=0)

print('Test loss:', score[0]) #Test loss: 0.0296396646054

print('Test accuracy:', score[1]) #Test accuracy: 0.9904

final_predictions = model.predict(X_test)
preds_0 = final_predictions[10] * 255.0

preds_0 = preds_0.reshape(258, 540)

x_test_0 = X_test[10] * 255.0

x_test_0 = x_test_0.reshape(258, 540)

plt.imshow(x_test_0, cmap='gray')
plt.imshow(preds_0, cmap='gray')
final_predictions = final_predictions.reshape(-1, 258, 540)

ids = []

vals = []

for i, f in enumerate(X_test_path):

    file = os.path.basename(f)

    imgid = int(file[:-4])

    test_img = cv2.imread(f, 0)

    img_shape = test_img.shape

    print('processing: {}'.format(imgid))

    print(img_shape)

    preds_reshaped = cv2.resize(final_predictions[i], (img_shape[1], img_shape[0]))

    for r in range(img_shape[0]):

        for c in range(img_shape[1]):

            ids.append(str(imgid)+'_'+str(r + 1)+'_'+str(c + 1))

            vals.append(preds_reshaped[r, c])

print('Writing to csv file')

pd.DataFrame({'id': ids, 'value': vals}).to_csv('submission.csv', index=False)


from decimal import Decimal
from django.conf import settings
from django.core.urlresolvers import reverse
from django.shortcuts import render, get_object_or_404
from paypal.standard.forms import PayPalPaymentsForm
from orders.models import Order
from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def payment_done(request):
    return render(request, 'payment/done.html')

@csrf_exempt
def payment_canceled(request):
    return render(request, 'payment/canceled.html')

def payment_process(request):
    order_id = request.session.get('order_id')
    order = get_object_or_404(Order, id=order_id)
    host = request.get_host()

    paypal_dict = {
        'business': settings.PAYPAL_RECEIVER_EMAIL,
        'amount': '%.2f' % order.get_total_cost().quantize(Decimal('.01')),
        'item_name': 'Order {}'.format(order.id),
        'invoice': str(order.id),
        'currency_code': 'USD',
        'notify_url': 'http://{}{}'.format(host, reverse('paypal-ipn')),
        'return_url': 'http://{}{}'.format(host, reverse('payment:done')),
        'cancel_return': 'http://{}{}'.format(host, reverse('payment:canceled')),
    }
    form = PayPalPaymentsForm(initial=paypal_dict)
    return render(request, 'payment/process.html', {'order': order,
                                                    'form':form})


horizontal = 0
aim = 0
depth = 0
with open('input.txt') as file:
    for move in file:
        move = move.split()
        if move[0] == 'forward':
            horizontal += int(move[1])
            depth += aim * int(move[1])
        elif move[0] == 'up':
            aim -= int(move[1])
        elif move[0] == 'down':
            aim += int(move[1])
print(horizontal)
print(depth)
print(horizontal * depth)


import numpy as np
import copy
import time
import copy
from abc import ABCMeta, abstractmethod
from scipy.special import expit

from ..utility import _get_inv_logdet_cholesky
from ..optim.utility import check_gradient
from ..gpres import GPRes
from ..elbo import ELBO

class JJbase(ELBO):
	"""
	Abstract base class for several methods, based on Jaakkola-Jordan lower bound
	"""
	__metaclass__ = ABCMeta

	def __init__(self, X, y, inputs, cov, name):
		"""
		:param X: data points (possibly a batch for some methods)
		:param y: target values
		:param inputs: inducing inputs (positions)
		:param cov: covariance function
		"""
		ELBO.__init__(self, X, y, name)
		self.cov = cov
		self.inputs = inputs
		self.xi = None
		self.mu = None
		self.Sigma = None

	def _recompute_xi(self, K_mm, K_mm_inv, K_nm, K_ii):
		"""
		Computes the optimal values of xi, given mu and Sigma
		"""
		K_mn = K_nm.T
		means = K_nm.dot(K_mm_inv.dot(self.mu))
		vars = K_ii + np.einsum('ij,ji->i', K_nm,
								K_mm_inv.dot((self.Sigma - K_mm).dot(K_mm_inv.dot(K_mn))))[:, None]
		self.xi = np.sqrt(means**2 + vars)

	def _recompute_var_parameters(self, K_mm_inv, K_nm):
		"""
		Computes optimal mu and Sigma, given xi
		"""
		K_mn = K_nm.T
		Lambda_xi = self._lambda(self.xi)
		self.Sigma = np.linalg.inv(2 * K_mm_inv.dot(K_mn.dot((Lambda_xi * K_nm).dot(K_mm_inv))) + K_mm_inv)
		self.mu = self.Sigma.dot(K_mm_inv.dot(K_mn.dot(self.y))) / 2

	def recompute_parameters(self, n_upd=5):
		"""
		Updates xi, mu, Sigma
		:n_upd: number of updates for each parameter
		"""
		K_nm = self.cov(self.X, self.inputs)
		K_mm = self.cov(self.inputs, self.inputs)
		K_mm_inv, K_log_det = _get_inv_logdet_cholesky(K_mm)
		K_ii = self.cov(self.X[:1, :], self.X[:1, :])
		for i in range(n_upd):
			self._recompute_xi(K_mm, K_mm_inv, K_nm, K_ii)
			self._recompute_var_parameters(K_mm_inv, K_nm)

	@staticmethod
	def _lambda(xi):
		xi = xi.astype(float)
		return np.tanh(xi / 2) / (4 * xi)

	@staticmethod
	def _log_g(xi):
		return np.log(expit(xi))

	@staticmethod
	def _dlambda_dxi(xi):
		return (xi - np.sinh(xi)) / (4 * xi**2 * (np.cosh(xi) + 1))

	def _elbo(self, params_vec, with_xi):
		"""
		The evidence lower bound, used in the vi method.
		:param params: a vector (kernel hyper-parameters, xi)
		:param with_xi: wether or not to compute the derivatives wrt xi
		:return: the value and the gradient of the lower bound
		"""
		params_num = self.cov.get_params().size
		params = params_vec[:params_num]
		xi = params_vec[params_num:][:, None]

		y = self.y
		n = self.X.shape[0]
		m = self.inputs.shape[0]
		cov_fun = copy.deepcopy(self.cov)
		cov_fun.set_params(params)
		lambda_xi = self._lambda(xi)
		K_mm = cov_fun(self.inputs, self.inputs)
		K_mm_inv, K_mm_log_det = _get_inv_logdet_cholesky(K_mm)
		K_nm = cov_fun(self.X, self.inputs)
		K_mn = K_nm.T
		K_mnLambdaK_nm = K_mn.dot(lambda_xi*K_nm)
		K_ii = cov_fun(self.X[:1, :], self.X[:1, :])

		B = 2 * K_mnLambdaK_nm + K_mm

		B_inv, B_log_det = _get_inv_logdet_cholesky(B)

		fun = ((y.T.dot(K_nm.dot(B_inv.dot(K_mn.dot(y))))/8)[0, 0] + K_mm_log_det/2 - B_log_det/2
			   - np.sum(K_ii * lambda_xi) + np.einsum('ij,ji->', K_mm_inv, K_mnLambdaK_nm))
		if with_xi:
			fun -= np.sum(xi) / 2
			fun += np.sum(lambda_xi * xi**2)
			fun += np.sum(self._log_g(xi))

		gradient = []
		derivative_matrix_list = cov_fun.get_derivative_function_list(params)

		for param in range(len(params)):
			if param != len(params) - 1:
				func = derivative_matrix_list[param]
			else:
				func = lambda x, y: cov_fun.get_noise_derivative(1)
			if param != len(params) - 1:
				dK_mm = func(self.inputs, self.inputs)
				dK_nm = func(self.X, self.inputs)
				dK_mn = dK_nm.T
				dB = 4 * dK_mn.dot(lambda_xi*K_nm) + dK_mm
			else:
				dK_mm = np.eye(m) * func(self.inputs, self.inputs)
				dK_mn = np.zeros_like(K_mn)
				dK_nm = dK_mn.T
				dB = dK_mm
			dK_nn = func(np.array([[0]]), np.array([[0]]))
			derivative = np.array([[0]], dtype=float)
			derivative += y.T.dot(dK_nm.dot(B_inv.dot(K_mn.dot(y))))/4
			derivative -= y.T.dot(K_nm.dot(B_inv.dot(dB.dot(B_inv.dot(K_mn.dot(y))))))/8
			derivative += np.trace(K_mm_inv.dot(dK_mm))/2
			derivative -= np.trace(B_inv.dot(dB))/2
			derivative -= np.sum(lambda_xi * dK_nn)
			derivative += np.trace(2 * K_mm_inv.dot(K_mn.dot(lambda_xi*dK_nm)) -
								   K_mm_inv.dot(dK_mm.dot(K_mm_inv.dot(K_mnLambdaK_nm))))
			gradient.append(derivative[0, 0])

		if with_xi:
			xi_gradient = np.zeros((n, 1))
			dlambdaxi_dxi = self._dlambda_dxi(xi)
			anc_vec = B_inv.dot(K_mn.dot(y))
			xi_gradient += - 2 * np.einsum('ij,jk->jik', anc_vec.T.dot(K_mn),
											dlambdaxi_dxi*K_nm.dot(anc_vec)).reshape(-1)[:, None] / 8

			xi_gradient += - np.einsum('ij,ji->j', B_inv.dot(K_mn), dlambdaxi_dxi*K_nm)[:, None]

			xi_gradient += - K_ii * dlambdaxi_dxi
			xi_gradient += (np.einsum('ij,ji->i', K_nm, K_mm_inv.dot(K_mn))[:, None] * dlambdaxi_dxi)
			xi_gradient += -1/2
			xi_gradient += dlambdaxi_dxi * xi**2 + lambda_xi * xi * 2
			xi_gradient += expit(-xi)

			gradient = gradient + xi_gradient.reshape(-1).tolist()

		return fun, np.array(gradient).reshape(-1)

	def get_inputs(self):
		"""
		Returns the inducing input positions, mean and covariance matrix
		of the process at these points
		"""
		if self.mu is None or self.Sigma is None:
			if self.xi is None:
				raise ValueError('Model is not fitted')
			K_mm = self.cov(self.inputs, self.inputs)
			K_nm = self.cov(self.X, self.inputs)

			K_mm_inv, _ = _get_inv_logdet_cholesky(K_mm)
			self._recompute_var_parameters(K_mm_inv, K_nm, self.y)

		return self.inputs, self.mu, self.Sigma


import json
import numpy as np

"""
* Pre and Post Config Templates
"""
POST_CONFIG_TEMPLATE_BW = "WS_PSEUDOWIRE_BANDWIDTH"
PRE_CONFIG_TEMPLATE_CTR_PARAMS = "SET_CEM_CONTROLLER_PARAMETERS"

"""
* Template module for service end-points
"""
def electrical_end_point_template(service_sub_type,
                                  end_point,
                                  end_point_controller_type,
                                  end_point_controller_name,
                                  end_point_clock_source,
                                  end_point_lower_channel):

    """ Parameters' name are self descriptive
    :param service_sub_type:
    :param end_point:
    :param end_point_controller_type:
    :param end_point_controller_name:
    :param end_point_clock_source:
    :param end_point_lower_channel:
    :return: template
    """

    template = {
        "sa.tp-ref": "MD=CISCO_EPNM!ND={}!PTP=name={} {};lr=lr-{}".format(end_point, end_point_controller_type.upper(), end_point_controller_name, end_point_controller_type.lower()),
        "sa.cem-data": {
            "sa.clock-source": end_point_clock_source,
            "sa.working-path": {
                "sa.auto-allocate-path": False
            }
        }
    }
    if (service_sub_type.upper() == 'T1' and end_point_controller_type.upper() == 'T3'):
        template["sa.cem-data"]["sa.working-path"]["sa.lower-order-path"] = {
            "sa.available-path-ref": "T1 {}".format(end_point_lower_channel)
        }

    return template

"""
* Template module for adding forwarding path for the target service.
  - In this case, it is MPLS FlexLSP
"""
def forwarding_path_template(preferred_path_name, is_create_new_path=False):

    """ Parameters' name are self descriptive
    :param preferred_path_name:
    :param is_create_new_path:
    :return: template
    """

    if is_create_new_path == False:
        template = {
            "sa.mpls-te-data": {
                "sa.preferred-path-ref": "MD=CISCO_EPNM!VC={}".format(preferred_path_name)
            },
            "sa.pseudowire-settings": {
                "sa.enable-control-word": "true",
                "sa.fallback-to-ldp": "true"
            }
        }
        return template
    else:
        pass  # Will implement later
        #########
        ### Code to be added
        #########

"""
* Template for adding EPNM-Templates
  - This is a post-config template for adjusting service (pseudowire) bandwidth
"""
def post_config_pw_bw_template(template_name, pw_bandwidth):

    """ Parameters' name are self descriptive
    :param template_name:
    :param pw_bandwidth:
    :return: template
    """

    template = {
        "sa.service-template": {
            "sa.type": "postconfig",
            "sa.name": template_name,
            "sa.usage": "Service Create Modify",
            "sa.variables": {
                "sa.variable": [
                    {
                        "sa.name": "Bandwidth",
                        "sa.value": pw_bandwidth
                    }
                ]
            }
        }
    }

    return template

"""
* This is a pre-config template for adjusing electrical (T1/T3) controller parameters
*** Will add later.
"""
#########
### Code to be added
#########

"""
Combining different Template modules defined above to create a final template for the target service
"""
def Create_Electrical_T3_T1_Service_Commands(pw_service_name,
                                   frame_type,
                                   service_sub_type,
                                   A_end,
                                   A_controller_type,
                                   A_controller_name,
                                   A_clock_source,
                                   A_T1_channel,
                                   Z_end,
                                   Z_controller_type,
                                   Z_controller_name,
                                   Z_clock_source,
                                   Z_T1_channel,
                                   preferred_path_name = None,
                                   is_create_new_path = False,
                                   pw_bandwidth = None):

    """ Parameters' name are self descriptive
    :param pw_service_name:
    :param frame_type:
    :param service_sub_type:
    :param A_end:
    :param A_controller_type:
    :param A_controller_name:
    :param A_clock_source:
    :param A_T1_channel:
    :param Z_end:
    :param Z_controller_type:
    :param Z_controller_name:
    :param Z_clock_source:
    :param Z_T1_channel:
    :param preferred_path_name:
    :param is_create_new_path:
    :param pw_bandwidth:
    :return: serialized form of json template
    """

    """
    * Electrical_T3_T1_Template; defining with common key-values
    *
    """
    Electrical_T3_T1_Template = {
        "sa.service-order-data": {
            "sa.customer-ref": "MD=CISCO_EPNM!CUSTOMER=Infrastructure",
            "sa.service-name": pw_service_name,
            "sa.service-description": pw_service_name,
            "sa.service-type": "tdm-cem",
            "sa.service-subtype": service_sub_type.lower(),
            "sa.service-activate": True,
            "sa.cem-data": {
                "sa.transport-settings": {
                    "sa.frame-type": frame_type
#                    "sa.payload-size":"192",
#                    "sa.dejitter-buffer-size":"6"

                }
            }
        }
    }

    """
    * Adding A and Z end points in the Template
    """
    termination_ends = list()
    termination_ends.append(
        electrical_end_point_template(service_sub_type,
                                      A_end,
                                      A_controller_type,
                                      A_controller_name,
                                      A_clock_source,
                                      A_T1_channel)
    )
    termination_ends.append(
        electrical_end_point_template(service_sub_type,
                                      Z_end,
                                      Z_controller_type,
                                      Z_controller_name,
                                      Z_clock_source,
                                      Z_T1_channel)
    )

    termination_ends_config_list = {}
    termination_ends_config_list["sa.termination-point-config"] = termination_ends
    Electrical_T3_T1_Template["sa.service-order-data"]["sa.termination-point-list"] = termination_ends_config_list

    """
    * Adding forwarding path section in the template
    """
    if (preferred_path_name != None and preferred_path_name != '' and preferred_path_name != ""):
        path_data = forwarding_path_template(preferred_path_name, is_create_new_path)
        if path_data != None: Electrical_T3_T1_Template["sa.service-order-data"]["sa.forwarding-path"] = path_data

    """
    * Adding EPNM-templates (post-config) section in the template
    """
    if (pw_bandwidth != None and pw_bandwidth != '' and pw_bandwidth != ""):
        Electrical_T3_T1_Template["sa.service-order-data"]["sa.service-templates"] = post_config_pw_bw_template(POST_CONFIG_TEMPLATE_BW, pw_bandwidth)

    """
    * Adding EPNM-templates (pre-config) section in the template
    """
    #########
    ### Code to be added
    #########

    """
    * Returning serialized template.
    """
    return json.dumps(Electrical_T3_T1_Template)


from django.core.paginator import Paginator
from django.shortcuts import render, get_object_or_404
from django.shortcuts import redirect
from .models import Post, Group, User, Comment, Follow
from .forms import PostForm, CommentForm
from django.contrib.auth.decorators import login_required

def index(request):
    post_list = Post.objects.order_by("-pub_date").all()
    paginator = Paginator(post_list, 10) # показывать по 10 записей на странице.
    page_number = request.GET.get('page') # переменная в URL с номером запрошенной страницы
    page = paginator.get_page(page_number) # получить записи с нужным смещением
    return render(request, 'posts/index.html', {'page': page, 'paginator': paginator})

def group_posts(request, slug):
    group = get_object_or_404(Group, slug=slug)
    posts = Post.objects.filter(group=group).order_by("-pub_date")[:12]
    paginator = Paginator(posts, 10)
    page_number = request.GET.get('page')
    page = paginator.get_page(page_number)
    return render(request, "group.html", {"group": group, 'page': page, 'paginator': paginator})

def new_post(request):
    user = request.user
    form = PostForm(request.POST or None, files=request.FILES or None)
    if not request.user.is_authenticated:
        return redirect(index)
    if request.method == 'POST':
        if form.is_valid():
            n_post = form.save(commit=False)
            n_post.author = user
            n_post.save()
            return redirect('index')
        return render(request, 'posts/new_post.html', {'form': form})
    form = PostForm()
    return render(request, 'posts/new_post.html', {'form': form})

def profile(request, username):
    author = get_object_or_404(User, username = username)
    post_list = Post.objects.filter(author = author).order_by('-pub_date')
    paginator = Paginator(post_list, 10) # показывать по 10 записей на странице.
    page_number = request.GET.get('page') # переменная в URL с номером запрошенной страницы
    page = paginator.get_page(page_number) # получить записи с нужным смещением
    first_post = Post.objects.filter(author = author).order_by('-pub_date')[:1]
    followers = Follow.objects.filter()
    count_followers = Follow.objects.filter(author=author).count()
    count_followering = Follow.objects.filter(user=author).count()
    if request.user.is_authenticated:
        following = Follow.objects.filter(author=author, user=request.user)
    else:
        following = []
    count = post_list.count
    return render(request, "posts/profile.html", {'count':count, 'author':author, 'page': page,
                  'paginator': paginator, 'count_followers': count_followers,
                  "following": following, 'count_followering': count_followering, 'first_post': first_post})

def post_view(request, username, post_id):
    profile = get_object_or_404(User, username=username)
    post = get_object_or_404(Post, author=profile.pk, id=post_id)
    count = Post.objects.filter(author = profile).count
    form = CommentForm()
    comments = Comment.objects.filter(post=post_id)
    count_followers = Follow.objects.filter(author=profile).count()
    count_followering = Follow.objects.filter(user=profile).count()
    return render(request, "posts/post.html", {"profile":profile, 'post':post, "count":count, 'form': form, 'comments' : comments, "count_followers": count_followers, "count_followering": count_followering})

def post_edit(request, username, post_id):
    post = get_object_or_404(Post, id = post_id)
    if request.user != post.author:
        return redirect("post", username, post_id)
    form = PostForm(request.POST or None, files=request.FILES or None, instance=post)
    if form.is_valid():
        form.save()
        return redirect("post", username, post_id)
    form = PostForm()
    return render(request, "posts/post_edit.html", {"form": form, 'post':post})

@login_required
def add_comment(request, username, post_id):
    post = get_object_or_404(Post, pk=post_id)
    comments = Comment.objects.filter(post=post).all()
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = post
            comment.author = request.user
            comment.save()
            return redirect('post', username=post.author.username, post_id=post_id,)
        return render(request, "posts/post.html", {'form': form, 'post':post})
    form = CommentForm()
    return render(request, "posts/comment.html", {"post": post, "form": form, 'comments': comments})

def page_not_found(request, exception):
    # Переменная exception содержит отладочную информацию,
    # выводить её в шаблон пользователской страницы 404 мы не станем
    return render(request, "misc/404.html", {"path": request.path}, status=404)

def server_error(request):
    return render(request, "misc/500.html", status=500)

@login_required
def follow_index(request):
    authors = Follow.objects.filter(user=request.user).values_list('author', flat=True)
    post_list = Post.objects.filter(author__in=authors).order_by("-pub_date").all()
    paginator = Paginator(post_list, 10) # показывать по 10 записей на странице.
    page_number = request.GET.get('page') # переменная в URL с номером запрошенной страницы
    page = paginator.get_page(page_number) # получить записи с нужным смещением
    return render(request, 'posts/follow.html', {'page': page, 'paginator': paginator})

@login_required
def profile_follow(request, username):
    author = get_object_or_404(User, username = username)
    user = request.user
    following = Follow.objects.filter(user=user).filter(author=author)
    if not following:
        if user != author:
            Follow.objects.create(user=user, author=author)
            return redirect("profile", username=username)
        else:
            return redirect("profile", username=username)
    else:
        return redirect("profile", username=username)

@login_required
def profile_unfollow(request, username):
    author = get_object_or_404(User, username = username)
    user = request.user
    Follow.objects.filter(user=user, author=author).delete()
    return redirect("profile", username=username)


# s3pkg_util - various utility functions used by s3pkg

import boto3
from os import path

'''
get_matching generators sourced from
https://alexwlchan.net/2019/07/listing-s3-keys/
'''

def get_matching_s3_objects(bucket, prefix="", suffix=""):
    s3 = boto3.client("s3")
    paginator = s3.get_paginator("list_objects_v2")
    kwargs = {'Bucket': bucket}

    if isinstance(prefix, str):
        prefixes = (prefix, )
    else:
        prefixes = prefix

    for key_prefix in prefixes:
        kwargs["Prefix"] = key_prefix

        for page in paginator.paginate(**kwargs):
            try:
                contents = page["Contents"]
            except KeyError:
                break

            for obj in contents:
                key = obj["Key"]
                if key.endswith(suffix):
                    yield obj

def get_matching_s3_keys(bucket, prefix="", suffix=""):
    for obj in get_matching_s3_objects(bucket, prefix, suffix):
        yield obj["Key"]

def get_package_data(bucket, prefix, suffix):
    '''
    retreives either package .tar.gz or manifest file names
    '''
    filename = []
    files = get_matching_s3_keys(bucket, prefix, suffix)
    for i in files:
        filename += [i]

    if len(filename) > 1:
        raise Exception("Package name too ambiguous or suffix not specified")

    return str(filename[0])

def retrieve_manifest(package_name, destination):
    '''
    retrieves manifest file from remote repository.
    by default, download to cache_dir/package_name.
    '''
    remote_manifest = prefix + '/' + package_name + '.manifest'
    s3 = boto3.client('s3')
    s3.download_file(bucket, remote_manifest, destination)

def retrieve_package(package_name, destination):
    '''
    retrieves package .tar.gz from remote repository.
    by default, download to cache_dir/package_name.
    '''
    remote_package = get_package_data(bucket, prefix, 'tar.gz')
    s3 = boto3.client('s3')
    s3.download_file(bucket, remote_package, destination)

def parse_manifest(packagename):
    '''
    parse manifest into dict to create folders and install paths
    '''
    # assign manifest_file to path of manifest file
    # if file does not exist, raise exception
    manifest

    # read manifest into new data structure
    '''
    info_regex finds info lines such as:
        name: package_name
        version: 2.3
        description: this is a package i like
    and splits them into [0]: [1] tuples.
    '''
    info_regex = re.compile(r'^(.*): (.*)$')

    '''
    file_regex finds file/directory lines such as:
        <dir> /path/to/dir 0775
        filename /path/to/dir 0755
    and splits them into [0] [1] [2] tuples:
    [0] is either a filename or an indication that
    this line should be a directory.
    [1] is the path to the file, and
    [2] is the permissions (3 or 4 digits) that should
    should be applied to the directory or file upon install.
    '''
    file_regex = re.compile(r'^(.*) (.*) (\d{3,4})')

def install_package(package_name):
    '''
    install_package installs a package.
    first checks to see if the most recent version exists
    in the cachedir. if so, install from local. if not, retrieve
    package and continue with installation.
    '''


# Name: Syed Faysel Ahammad Rajo
# Date: November 23, 2021
# CodeForces: 1385A Pairwise Maximus

t = int(input()) # test Case
for i in range(t):
    x,y,z = [int(j) for j in input().split()] # given integer
    listOfint = [x,y,z]

    #1 if x = y = z, then a,b,c are equals

    if x==y and y==z:
        a,b,c= [x,y,z]
        print('YES')
        print(a,b,c)

    #2 If the maximum number comes twice among them, then the three numbers are the max number itself, and the min number, common 1 as given in Problem 1<=a,b,c

    elif listOfint.count(max(listOfint))==2:
        a = max(listOfint)
        b = min(listOfint)
        c = 1
        print('YES')
        print(a,b,c)

    # if x!=y and x!=z and y!=z:
    else:
        print("No")

    #3 If all three numbers are different or max number doesn't come twice then a,b,c are not possible to determine

    # Codes runs successfully :(


from neutron.extensions import securitygroup as sg_ext
from neutron import quota
from neutron_lib import exceptions as n_exc
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import uuidutils

from quark.db import api as db_api
from quark import environment as env
from quark import exceptions as q_exc
from quark.plugin_modules import jobs as job_api
from quark import plugin_views as v
from quark import protocols

from quark.worker_plugins import sg_update_worker as sg_rpc_api

CONF = cfg.CONF
LOG = logging.getLogger(__name__)
DEFAULT_SG_UUID = "00000000-0000-0000-0000-000000000000"
GROUP_NAME_MAX_LENGTH = 255
GROUP_DESCRIPTION_MAX_LENGTH = 255
RULE_CREATE = 'create'
RULE_DELETE = 'delete'

def _validate_security_group_rule(context, rule):
    # TODO(mdietz): As per RM8615, Remote groups are not currently supported
    if rule.get("remote_group_id"):
        raise n_exc.InvalidInput(
            error_message="Remote groups are not currently supported")

    direction = rule.get("direction")
    if direction == env.Capabilities.EGRESS:
        if env.Capabilities.EGRESS not in CONF.QUARK.environment_capabilities:
            raise q_exc.EgressSecurityGroupRulesNotEnabled()

    protocol = rule.pop('protocol')
    # NOTE(roaet): these are not required by spec
    port_range_min = rule.get('port_range_min')
    port_range_max = rule.get('port_range_max')
    # TODO(anyone): this will error as None, so defaulting to ipv4
    et = rule.get('ethertype', 'IPv4')
    ethertype = protocols.translate_ethertype(et)

    if protocol:
        protocol = protocols.translate_protocol(protocol, et)
        protocols.validate_protocol_with_port_ranges(ethertype,
                                                     protocol,
                                                     port_range_min,
                                                     port_range_max)
        rule['protocol'] = protocol
    else:
        if port_range_min is not None or port_range_max is not None:
            raise sg_ext.SecurityGroupProtocolRequiredWithPorts()

    rule["ethertype"] = ethertype

    protocols.validate_remote_ip_prefix(ethertype,
                                        rule.get("remote_ip_prefix"))

    return rule

def _validate_security_group(security_group):
    if "name" in security_group:
        if len(security_group["name"]) > GROUP_NAME_MAX_LENGTH:
            raise n_exc.InvalidInput(
                error_message="Group name must be 255 characters or less")

        if security_group["name"] == "default":
            raise sg_ext.SecurityGroupDefaultAlreadyExists()

    if ("description" in security_group and
            len(security_group["description"]) > GROUP_DESCRIPTION_MAX_LENGTH):
        raise n_exc.InvalidInput(
            error_message="Group description must be 255 characters or less")

def create_security_group(context, security_group):
    LOG.info("create_security_group for tenant %s" %
             (context.tenant_id))
    group = security_group["security_group"]
    _validate_security_group(group)

    group_name = group.get('name', '')
    group_id = uuidutils.generate_uuid()

    with context.session.begin():
        group["id"] = group_id
        group["name"] = group_name
        group["tenant_id"] = context.tenant_id
        dbgroup = db_api.security_group_create(context, **group)
    return v._make_security_group_dict(dbgroup)

def update_security_group(context, id, security_group):
    if id == DEFAULT_SG_UUID:
        raise sg_ext.SecurityGroupCannotUpdateDefault()
    new_group = security_group["security_group"]
    _validate_security_group(new_group)

    with context.session.begin():
        group = db_api.security_group_find(context, id=id, scope=db_api.ONE)
        db_group = db_api.security_group_update(context, group, **new_group)

    return v._make_security_group_dict(db_group)

def delete_security_group(context, id):
    LOG.info("delete_security_group %s for tenant %s" %
             (id, context.tenant_id))

    with context.session.begin():
        group = db_api.security_group_find(context, id=id, scope=db_api.ONE)

        # TODO(anyone): name and ports are lazy-loaded. Could be good op later
        if not group:
            raise sg_ext.SecurityGroupNotFound(id=id)
        if id == DEFAULT_SG_UUID or group.name == "default":
            raise sg_ext.SecurityGroupCannotRemoveDefault()
        if group.ports:
            raise sg_ext.SecurityGroupInUse(id=id)
        db_api.security_group_delete(context, group)

def get_security_group(context, id, fields=None):
    LOG.info("get_security_group %s for tenant %s" %
             (id, context.tenant_id))
    group = db_api.security_group_find(context, id=id, scope=db_api.ONE)
    if not group:
        raise sg_ext.SecurityGroupNotFound(id=id)
    return v._make_security_group_dict(group, fields)

def get_security_groups(context, filters=None, fields=None,
                        sorts=None, limit=None, marker=None,
                        page_reverse=False):
    LOG.info("get_security_groups for tenant %s" %
             (context.tenant_id))
    groups = db_api.security_group_find(context, **filters)
    return [v._make_security_group_dict(group) for group in groups]

@env.has_capability(env.Capabilities.SG_UPDATE_ASYNC)
def _perform_async_update_rule(context, id, db_sg_group, rule_id, action):
    """Updates a SG rule async and return the job information.

    Only happens if the security group has associated ports. If the async
    connection fails the update continues (legacy mode).
    """
    rpc_reply = None
    sg_rpc = sg_rpc_api.QuarkSGAsyncProcessClient()
    ports = db_api.sg_gather_associated_ports(context, db_sg_group)
    if len(ports) > 0:
        rpc_reply = sg_rpc.start_update(context, id, rule_id, action)
        if rpc_reply:
            job_id = rpc_reply['job_id']
            job_api.add_job_to_context(context, job_id)
        else:
            LOG.error("Async update failed. Is the worker running?")

def create_security_group_rule(context, security_group_rule):
    """Creates a rule and updates the ports (async) if enabled."""
    LOG.info("create_security_group for tenant %s" %
             (context.tenant_id))
    with context.session.begin():
        rule = _validate_security_group_rule(
            context, security_group_rule["security_group_rule"])
        rule["id"] = uuidutils.generate_uuid()

        group_id = rule["security_group_id"]
        group = db_api.security_group_find(context, id=group_id,
                                           scope=db_api.ONE)
        if not group:
            raise sg_ext.SecurityGroupNotFound(id=group_id)

        quota.QUOTAS.limit_check(
            context, context.tenant_id,
            security_rules_per_group=len(group.get("rules", [])) + 1)

        new_rule = db_api.security_group_rule_create(context, **rule)
    if group:
        _perform_async_update_rule(context, group_id, group, new_rule.id,
                                   RULE_CREATE)
    return v._make_security_group_rule_dict(new_rule)

def get_security_group_rule(context, id, fields=None):
    LOG.info("get_security_group_rule %s for tenant %s" %
             (id, context.tenant_id))
    rule = db_api.security_group_rule_find(context, id=id,
                                           scope=db_api.ONE)
    if not rule:
        raise sg_ext.SecurityGroupRuleNotFound(id=id)
    return v._make_security_group_rule_dict(rule, fields)

def delete_security_group_rule(context, id):
    """Deletes a rule and updates the ports (async) if enabled."""
    LOG.info("delete_security_group %s for tenant %s" %
             (id, context.tenant_id))
    with context.session.begin():
        rule = db_api.security_group_rule_find(context, id=id,
                                               scope=db_api.ONE)
        if not rule:
            raise sg_ext.SecurityGroupRuleNotFound(id=id)

        group = db_api.security_group_find(context, id=rule["group_id"],
                                           scope=db_api.ONE)
        if not group:
            raise sg_ext.SecurityGroupNotFound(id=id)

        rule["id"] = id
        db_api.security_group_rule_delete(context, rule)
    if group:
        _perform_async_update_rule(context, group.id, group, id, RULE_DELETE)

def get_security_group_rules(context, filters=None, fields=None,
                             sorts=None, limit=None, marker=None,
                             page_reverse=False):
    LOG.info("get_security_group_rules for tenant %s" %
             (context.tenant_id))
    rules = db_api.security_group_rule_find(context, **filters)
    return [v._make_security_group_rule_dict(rule) for rule in rules]


from setuptools import setup
import os

# stole this from ImSim:
def all_files_from(dir, ext=''):
    """Quick function to get all files from directory and all subdirectories
    """
    files = []
    for root, dirnames, filenames in os.walk(dir):
        for filename in filenames:
            if filename.endswith(ext) and not filename.startswith('.'):
                files.append(os.path.join(root, filename))
    return files

data_files = all_files_from('data')

setup(
    name='psfws',
    version='0.1',
    description='generate realistic wind and turbulence parameters for atmospheric point-spread function simulations',
    url='https://github.com/LSSTDESC/psf-weather-station',
    author='Claire-Alice Hebert',
    author_email='chebert@stanford.edu',
    license='MIT',
    packages=['psfws'],
    package_data={'psfws': data_files},
    python_requires='>=3.5'
)


import sys
input = sys.stdin.readline

T = int(input())
for tc in range(T):
    N = int(input())
    clothes = {}

    for _ in range(N):
        a, b = input().split()
        if clothes.get(b) == None:
            clothes[b] = set()
        clothes[b].add(a)

    cnt = 1
    for key in clothes.keys():
        cnt *= len(clothes[key]) + 1

    print(cnt-1)


# -*- coding: utf-8 -*-
"""
Created on Tue Jun 21 11:12:52 2016

@author: Anni
"""

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

eingangsbild = r"092.png" #raw strings (verhindern Formatierung)

def solve(eingangsbild=eingangsbild):
    bild = np.array(Image.open(eingangsbild))
    bildBW = np.array(Image.open(eingangsbild).convert("L"))
    ermittelSpieler(inhalt(bild, bildBW), bild)
    plt.imshow(bild)

#Erster Schritt: Felder analysieren. Symbole einheitlich färben
def inhalt(bild, bildBW):
    height, width = bildBW.shape
    drittelh = int(1/3*height)
    drittelb = int(1/3*width)
    #Einzelne Felder voneinander trennen.
    L=[[0, 0], [0, drittelb], [0, 2*drittelb], [drittelh, 0],[drittelh, drittelb], [drittelh, 2*drittelb], [2*drittelh, 0],[2*drittelh, drittelb], [2*drittelh, 2*drittelb]]
    J=[]
    for i in range(len(L)):
        I=[]
        h,b=L[i]
        anzahlpixel=0
        #Analyse der Farbe
        for y in range(h+1,h + drittelh-1):             # +1 und -1 um die Linien auszuschließen
            for x in range(b+1, b + int(1/3*width)-1):
                if(bildBW[y,x] <= 240):
                    farbe = bild[y,x]
                    pixel = farbe[0], farbe[1], farbe[2]
                    I=I + [pixel]
        np.sort(I)
        f= (int)(len(I)/2)
        if(f != 0):
            t= I[f]
            #Mit neuer Farbe ausfüllen
            for y in range(h+1,h + drittelh-1):             # +1 und -1 um die Linien auszuschließen
                for x in range(b+1, b + int(1/3*width)-1):
                    if(bildBW[y,x] <= 240):
                        anzahlpixel= anzahlpixel + 1
                        bild[y,x] = t[0], t[1], t[2], 255
                        pixel = t[0], t[1], t[2]
                    else:
                        bild[y,x] = 255,255,255,255
        J=J+[[i+1, anzahlpixel, pixel]]
    return J

#2. Schritt: Felder den Spielern zuordnen. Dabei Farbe uns Symbol beachten
def ermittelSpieler(I, bild):
    spielerA = []   #Alle Steine von A
    spielerB = []   #Alle Steine von B
    felderA = []    #Nur die Felder
    felderB = []    #Nur die Felder
    leer=[]
    aSymbol=[]
    a=0
    for i in range(len(I)):
        feld, symbol, farbe = I[i]
        if(symbol == 0):
            leer = leer + [feld]
        else:
            if((a==0)):
                spielerA = spielerA +I[i]
                aSymbol = spielerA[1]
                r, g, b = spielerA[2]
                felderA = felderA + [feld]
                a = a + 1
            else:
                if((a!=0)&(aSymbol==symbol)&((farbe[0]==r)&(farbe[1]==g)&(farbe[2]==b))):
                    spielerA = spielerA + I[i]
                    felderA = felderA + [feld]
                else:
                    if(a != 0):
                        spielerB = spielerB +I[i]
                        felderB = felderB + [feld]
    zielkoord=gewinne(felderA, felderB, leer)
    print(zielkoord)

#Schritt 2.1: Einmittlung welcher Spieler am Zug ist
def gewinne(felderA, felderB, leer):
    if((len(felderA))<(len(felderB))):
        zug = felderA
    else:
        zug = felderB
    feld = loesung(zug, leer)
    spalte = int((feld-1) % 3)
    zeile = int((feld-1) // 3)
    return(zeile+1, spalte+1)

#Schritt 2.1.1: Gewinnmöglichkeiten werden geprüft
def loesung(zug, leer):

    diagonal = [[1,5,9],[3,5,7]]
    horizontal = [[1,2,3],[4,5,6],[7,8,9]]
    vertikal = [[1,4,7],[2,5,8],[3,6,9]]

    moeglichkeiten = diagonal + horizontal + vertikal
    for j in range(len(leer)):
        pruef = leer[j]
        zuga = []
        zuga = zug + [pruef]
        for i in range(len(moeglichkeiten)):
            pruef=moeglichkeiten[i]
            if((pruef[0] in zuga) & (pruef[1] in zuga) & (pruef[2] in zuga)):
                return(leer[j])
                break

solve(eingangsbild)


#!/usr/bin/env python

from tasks.pnp_sim import PickAndPlace
import baxter_interface
import signal
import sys

def signal_handler(sig, frame):
    print('You pressed Ctrl+C!')
    rs = baxter_interface.RobotEnable(baxter_interface.CHECK_VERSION)
    if rs.state().enabled:
        print("Disabling Baxter.")
        rs.disable()

    sys.exit(0)

if __name__ == '__main__':
    signal.signal(signal.SIGINT, signal_handler)
    print('Press Ctrl+C')

    pnp = PickAndPlace(
        #place_position=[0.7, -0.5, -0.2],
        place_position=[0.6, 0.6, 0.05],
    )
    pnp.run()


import os
import glob
import sys

dir_path = os.path.dirname(os.path.realpath(__file__))
config_dir_path = os.path.abspath(os.path.join(dir_path, '..'))
sys.path.append(config_dir_path)

import config as c
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point
from tqdm import tqdm
from ormgap import Crop, Group, Accession, Country

def read_file( df = None, name_file = '', columns = [] ):
    file = []
    if df is not None:
        file = df
    else:
        file_path = os.path.join(c.path_inputs, name_file)
        if os.path.isfile(file_path):
            file = pd.read_csv(file_path, encoding='utf8', keep_default_na=False)
        else:
            return None

    # Verify that the required columns exist in the DataFrame
    missing_cols = set(columns) - set(file.columns)

    if len(missing_cols) > 0:
        raise ValueError(f"Missing required columns in {name_file} file: {', '.join(missing_cols)}")

    return file

def import_accessions(data = None):
    print('The process of importing accessions has begun')

    #accession_files = glob.glob(os.path.join(c.path_inputs, "*_accession.csv"))

    accession_files = glob.glob(c.path_inputs + '/*accession.csv')
    accession_file_names = [os.path.basename(file) for file in accession_files]

    # load country shapefile
    countries = gpd.read_file(os.path.join(c.path_data, "Countries.zip"))

    countries = countries.to_crs("EPSG:4326")

    # select only the necessary columns (ISO2 and geometry)
    countries = countries[["ISO2", "geometry"]]

    if not len(accession_files) == 0:

        for file in accession_file_names:

            print(f'{file} file found')

            try:
                accessions = read_file( data, file, c.cols_accession)
            except ValueError as e:
                print(f"Error reading {file} file: {str(e)}")
                continue

            #accessions = read_file( data, file, c.cols_accession)

            # Create a data frame to store errors
            errores = pd.DataFrame(columns=['ext_id', 'species_name', 'row', 'error'])

            # Initialize counters
            success_count = 0
            error_count = 0

            # creates a GeoDataFrame with the points corresponding to accessions
            points = [Point(xy) for xy in zip(accessions["longitude"], accessions["latitude"])]
            accessions_gdf = gpd.GeoDataFrame(accessions, geometry=points, crs="EPSG:4326")

            # join GeoDataFrames using the sjoin method
            accessions_by_country = gpd.sjoin(accessions_gdf, countries, how="left", predicate="within")

            accessions.fillna('', inplace=True)

            # adds the column with the ISO2 for the corresponding country
            accessions["country"] = accessions_by_country["ISO2"]

            # Traverse each row of the DataFrame
            for index, row in tqdm(accessions.iterrows(), total=len(accessions)):

                try:

                    crop = Crop.objects.get(ext_id=str(row['crop']))

                    if crop is not None:

                        group = Group.objects.get(ext_id=str(row['landrace_group']))

                        if group is not None:

                            country = Country.objects.get(iso_2=str(row['country']))

                            if country is not None:

                                # Create a Accession object with the row data
                                accession = Accession(
                                    ext_id=str(row['ext_id']).strip(),
                                    species_name=str(row['species_name']).strip(),
                                    crop=crop,
                                    country=country,
                                    landrace_group=group,
                                    institution_name=str(row['institution_name']).strip(),
                                    source_database=str(row['source_database']).strip(),
                                    latitude=row['latitude'],
                                    longitude=row['longitude'],
                                    accession_id=str(row['accession_id']).strip()
                                )

                                # Save the object in the database
                                accession.save()

                                # Increment success counter
                                success_count += 1
                            else:
                                errores = pd.concat([errores, pd.DataFrame(data={'ext_id': [str(row['ext_id'])], 'species_name': [str(row['species_name'])], 'row': [str(index + 2)],
                                    'error': ['no country was found with that iso_2: ' + str(row['country'])]})], ignore_index=True)

                                # Increment error counter
                                error_count += 1
                        else:
                            errores = pd.concat([errores, pd.DataFrame(data={'ext_id': [str(row['ext_id'])], 'species_name': [str(row['species_name'])], 'row': [str(index + 2)],
                                'error': ['no group was found with that ext_id: ' + str(row['landrace_group'])]})], ignore_index=True)

                            # Increment error counter
                            error_count += 1
                    else:
                        errores = pd.concat([errores, pd.DataFrame(data={'ext_id': [str(row['ext_id'])], 'species_name': [str(row['species_name'])], 'row': [str(index + 2)],
                            'error': ['no crop was found with that ext_id: ' + str(row['crop'])]})], ignore_index=True)

                        # Increment error counter
                        error_count += 1

                except Exception as e:
                    #print('Errors found in row #' + str(index + 2) + ', ext_id:' + str(row['ext_id']) + ', name:' + str(row['name']))

                    # Add the row with the error to the error dataframe
                    errores = pd.concat([errores, pd.DataFrame(data={'ext_id': [str(row['ext_id'])], 'species_name': [str(row['species_name'])],
                        'row': [str(index + 2)], 'error': [str(e)]})], ignore_index=True)

                    # Increment error counter
                    error_count += 1

            # Print results
            print(f"Processed {success_count + error_count} rows.")
            print(f"Successfully saved {success_count} rows.")
            print(f"Failed to save {error_count} rows.")

            # Saving the error dataframe in an Excel file
            if not errores.empty:
                file_name = str(file).replace(".csv", "")
                try:
                    errores.to_excel(os.path.join(c.path_outputs, f"{file_name}_logs.xlsx"), index=False)
                    print(f"Errors found, {file_name}_logs.xlsx file saved")
                except Exception as e:
                    print(f"Errors found, but the file {file_name}_logs.xlsx could not be saved. Error:")
                    print(e)
            else:
                print("No errors found")
    else:
        print('No file ending in accession.csv has been found.')

def import_attributes(data = None):
    attributes_files = glob.glob(c.path_inputs + '/*attribute.csv')
    attributes_file_names = [os.path.basename(file) for file in attributes_files]

    print('The process of importing attributes of accessions has begun')

    if not len(attributes_files) == 0:

        for file in attributes_file_names:

            print(f'{file} file found')

            try:
                attributes = read_file( data, file, c.cols_attribute)
            except ValueError as e:
                print(f"Error reading {file} file: {str(e)}")
                continue

            # Create a dataframe to store errors
            errores = pd.DataFrame(columns=['key', 'value', 'accession', 'row', 'error'])

            # Initialize counters
            success_count = 0
            error_count = 0

            # Traverse each row of the DataFrame
            for index, row in tqdm(attributes.iterrows(), total=len(attributes)):

                try:

                    accession = Accession.objects.get(ext_id=str(row['accession']))

                    if accession is not None:
                        # add attribute to the accession found
                        accession.other_attributes[str(row['key']).strip()] = str(row['value']).strip()

                        # Save the object in the database
                        accession.save()

                        # Increment success counter
                        success_count += 1
                    else:
                        errores = pd.concat([errores, pd.DataFrame(data={'key': [str(row['key'])], 'value': [str(row['value'])], 'accession': [str(row['accession'])], 'row': [str(index + 2)],
                            'error': ['no accession was found with that ext_id: ' + str(row['accession'])]})], ignore_index=True)

                        # Increment error counter
                        error_count += 1

                except Exception as e:
                    #print('Errors found in row #' + str(index + 2) + ', ext_id:' + str(row['ext_id']) + ', name:' + str(row['name']))

                    # Add the row with the error to the error dataframe
                    errores = pd.concat([errores, pd.DataFrame(data={'key': [str(row['key'])], 'value': [str(row['value'])], 'accession': [str(row['accession'])],
                        'row': [str(index + 2)], 'error': [str(e)]})], ignore_index=True)

                    # Increment error counter
                    error_count += 1

            # Print results
            print(f"Processed {success_count + error_count} rows.")
            print(f"Successfully saved {success_count} rows.")
            print(f"Failed to save {error_count} rows.")

            # Saving the error dataframe in an Excel file
            if not errores.empty:
                file_name = str(file).replace(".csv", "")
                try:
                    errores.to_excel(os.path.join(c.path_outputs, f"{file_name}_logs.xlsx"), index=False)
                    print(f"Errors found, {file_name}_logs.xlsx file saved")
                except Exception as e:
                    print(f"Errors found, but the file {file_name}_logs.xlsx could not be saved. Error:")
                    print(e)
            else:
                print("No errors found")
    else:
        print('Could not find attribute.csv file')

if __name__ == '__main__':
    c.connect_db()
    import_accessions()
    import_attributes()


# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        dummy_node = ListNode(-1)
        cur_node = dummy_node
        s = 0

        # 只要二者之一非空，就加下去
        while l1 or l2:
            if l1:
                s += l1.val
                # 向后挪一位
                l1 = l1.next
            if l2:
                s += l2.val
                # 向后挪一位
                l2 = l2.next
            cur_node.next = ListNode(s % 10)
            cur_node = cur_node.next
            s //= 10
        if s == 1:
            cur_node.next = ListNode(1)
        return dummy_node.next


import os
import sys
import argparse
import pandas as pd
import numpy as np

### Version 2, created 27 August 2020 by Nicole S Paulat ###

### Reformats concatenated, unique-hit, headerless MELT vcf files, to return SPLIT hits to single base point insertions for accurate downstream analysis
### Also removes any hits that are less than 10kb from a scaffold end to limit any effects of misassembly on scaffold edges

def get_args():
	#What this script does
	parser = argparse.ArgumentParser(description="General removal of most MELT duplicate calls and overlapping calls", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
	required = parser.add_argument_group('required arguments')
	#Give input coordinate file of MELT hits (CHROM# + POS)
	parser.add_argument('-i', '--input', help='raw concatenated MELT output file', required=True)
	#Argument of directory containing the list of formatted MELT hits (need full path)
	parser.add_argument('-d', '--directory', type=str, help='Path to the directory of the input file', default=".")
	#Argument of the output directory (need full path)
	parser.add_argument('-od', '--outdir', type=str, help='Location of directory for the output file', default=".")
	parser.add_argument('-in', '--index', type=str, help='Path to reference genome index file (.fai)', required=True)
	args = parser.parse_args()
	CAT = args.input
	DIR = args.directory
	OUTDIR = args.outdir
	INDEX = args.index

	return CAT, DIR, OUTDIR, INDEX

CAT, DIR, OUTDIR, INDEX = get_args()

if DIR == ".":
	DIR = os.getcwd()
if OUTDIR == ".":
	OUTDIR = os.getcwd()

BASENAME = os.path.basename(CAT).split("_dupsless")[0]
INPUT = os.path.join(DIR, CAT)
OUTBASE = os.path.join(OUTDIR, BASENAME)

HEADERS = ['#CHROM', 'POS', 'END', 'ASSESS', 'SVTYPE', 'SVLENGTH', 'ORIENTATION', 'Austroriparius', 'Brandtii', 'Ciliolabrum', 'Davidii', 'Myotis', 'Occultus', 'Sept_TTU', 'Thysanodes', 'Velifer', 'Vivesi', 'Yumanensis', 'FAMILY', 'MODULE']

DF = pd.read_csv(INPUT, sep='\t', names=HEADERS)
DF['END'] = np.where((DF['MODULE'] == 'SPLIT'), DF['POS'], DF['END'])
DF = DF[DF['POS'] >= 10000]
with open(INDEX, "r") as i:
	SCAFFOLD_DICT = {}
	for LINE in i:
		RECORD = LINE.split('\t')
		SCAFFOLD = RECORD[0]
		SCAFF_LEN = int(RECORD[1])
		try:
			SCAFFOLD_DICT[RECORD[0]].append(SCAFF_LEN)
		except KeyError:
			SCAFFOLD_DICT[SCAFFOLD] = [SCAFF_LEN]
for SCAFF_INFO in SCAFFOLD_DICT:
	DF['END'] = np.where(((DF['#CHROM'] == SCAFF_INFO) & (DF['END'] >= (SCAFFOLD_DICT[SCAFF_INFO][0] - 10000))), 0, DF['END'])
DF = DF[DF['END'] != 0]
DF = DF.sort_values(by=['#CHROM', 'POS'], ascending=[True, True])
DF.drop(columns=['ASSESS', 'FAMILY'], inplace=True)
DF.to_csv(OUTBASE+"_final_tosort.bed", sep='\t', header=True, index=False)
DF.to_csv(OUTBASE+"_split_point.bed", sep='\t', header=False, index=False)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Oct  7 14:53:52 2021

@author: ml
"""

import ast
from code.feature_extraction.feature_extractor import FeatureExtractor
from code.util import COLUMN_CONTAINED_EMOJI
import nltk
import numpy as np
import re

class HasMostCommonEmojis(FeatureExtractor):

    _n=5
    _n_most_common_emojis=None
    _emojis=[]
    _emoji_re_masks=[]
    def __init__(self, input_column: str,n:int=20):
        super().__init__([input_column], "#{0}_occurs".format(input_column))
        self._n=n

    def get_feature_name(self) ->list:
        """
        returns n feature names for the n most common emojis

        Returns
        -------
        names : TYPE
            list.

        """
        suffixes=self._emojis
        names=[]
        for i in range(0,self._n):
            names+=[COLUMN_CONTAINED_EMOJI.format(suffixes[i])]
        return names

    def _set_variables(self, inputs: list):
        """
        find out the most common emojis

        Parameters
        ----------
        inputs : list
            List of emojis in tweet.

        Returns
        -------
        None.

        """

        #collect all emojis from every tweet
        all_emojis = []
        c=0
        for tweet in np.array(inputs[0]):
            emojis_in_tweet=ast.literal_eval(tweet)
            #remove empty objects
            if(len(emojis_in_tweet)!=0):
                all_emojis+=emojis_in_tweet
            else:
                c+=1
        all_emojis =[i for i in all_emojis if i != ""]
        all_emojis =[i for i in all_emojis if i != '']
        all_emojis =[i for i in all_emojis if i != "''"]
        all_emojis =[i for i in all_emojis if i != '""']
        all_emojis =[i for i in all_emojis if i != "''"]
        print("We have got "+str(c)+" tweets without emojis")

        #compute most common emojis
        all_emojis_freqdist = nltk.FreqDist(all_emojis)
        self._n_most_common_emojis=all_emojis_freqdist.most_common(self._n)

        self._emojis=[emoji for (emoji,count) in self._n_most_common_emojis]

        #generate mask for each emoji
        for i in self._emojis:
            mask=re.compile(i)
            self._emoji_re_masks+=[mask]

        print("the most common n emojis are: "+str(self._emojis))

    def _get_values(self, inputs: list) -> np.ndarray :
        """
        returns the amount of occurences of the emoji in the tweet for every emoji.

        Parameters
        ----------
        inputs : list
            DESCRIPTION.

        Returns
        -------
        result : TYPE
            DESCRIPTION.

        """

        list_of_most_common_emojis=[]

        #splitting up the input
        tweets=inputs[0]

        for tweet in tweets:
            ohe_emojis_used=self._n*[0]
            for i in range(self._n):
                current_emoji=self._emojis[i][0]
                emojis_in_tweet=ast.literal_eval(tweet)

                ohe_emojis_used[i]=0
                if current_emoji in emojis_in_tweet:
                    findings=[]
                    findings+=re.findall(self._emoji_re_masks[i],tweet)

                    ohe_emojis_used[i]=len(findings)

            #add to list
            list_of_most_common_emojis+=[ohe_emojis_used]

        #saving it in an array
        result = np.array(list_of_most_common_emojis)
        return result


'''
用__next__和yield和__getitem__三种方式表示斐波那契
'''
class Fib(object):
    def __init__(self):
        self.a,self.b=0,1#初始化两个计数器a,b
    def __iter__(self):
        return self#实例本身就是迭代对象，故返回自己
    def __next__(self):
        self.a,self.b=self.b,self.a+self.b#计算下一个值
        if self.a>100000:#退出循环的条件
            raise StopIteration()
        return self.a#返回下一个值
for i in Fib():
    print(i,end=" ")
print()

class Fib2(object):
    def __init__(self,end):
        self.a, self.b = 0, 1
        self.end=end
    def __iter__(self):
        for i in range(self.end):
            self.a, self.b = self.b, self.a + self.b
            yield self.a
for i in Fib2(30):
    print(i,end=" ")
print()

class Fib3(object):
    def __getitem__(self, item):
        a,b=1,1
        for i in range(item):
            a,b=b,a+b
        return a
f=Fib3()
for i in range(30):
    print(f[i],end=" ")


import time
import bluetooth
from mindwavemobile.MindwaveDataPoints import RawDataPoint
from mindwavemobile.MindwaveDataPointReader import MindwaveDataPointReader
import textwrap
import RPi.GPIO as GPIO
from mindwavemobile.MindwaveDataPoints import AttentionDataPoint

#led_section
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.OUT)

if __name__ == '__main__':
    mindwaveDataPointReader = MindwaveDataPointReader()
    mindwaveDataPointReader.start()
    if (mindwaveDataPointReader.isConnected()):

        while(True):
            dataPoint = mindwaveDataPointReader.readNextDataPoint()
            if (not dataPoint.__class__ is RawDataPoint):
                print(dataPoint)
                GPIO.output(17, True)
                time.sleep(1)
                GPIO.output(17, False)
                time.sleep(1)
    else:
        print((textwrap.dedent("""\
            Exiting because the program could not connect
            to the Mindwave Mobile device.""").replace("\n", " ")))
        GPIO.out(17, False)