"""

"""
import json
from collections import Counter
import numpy as np


def data_trans(input_path, output_path):
    ct = Counter()
    positive = []
    negative = []
    with open(input_path, "r", encoding="utf-8") as f, open(output_path, "w", encoding="utf-8") as f_o:
        for line in f.readlines():
            line = json.loads(line)
            cv_rt = line.get("cv_rt")
            jd_rt = line.get("jd_rt")
            cv_rt_v = list(cv_rt.values())
            jd_rt_v = list(jd_rt.values())
            ct[str(line.get("label"))] += 1
            tmp = "\t".join(["##".join(x) for x in cv_rt_v]) + "@" + "\t".join(
                ["##".join(x) for x in jd_rt_v]) + "@" + str(line.get("label"))
            if str(line.get("label")) == "1":
                positive.append(tmp)
            elif str(line.get("label")) == "0":
                negative.append(tmp)
        sample_total = positive + list(np.random.choice(negative, len(positive) * 5, replace=False))
        np.random.shuffle(sample_total)
        for neg_sample in sample_total:
            f_o.write(neg_sample + "\n")
    print(ct.items())


def gen_vocab(input_path, output_path):
    with open(input_path, "r", encoding="utf-8") as f, open(output_path, "w", encoding="utf-8") as f_o:
        x, y = f.readline().strip("\n").split(" ")
        print(x, y)
        f_o.write("UNK" + "\n")
        for line in f.readlines():
            line = line.strip("\n").split(" ")
            f_o.write(line[0] + "\n")


def filter_embedding_nocompany(input_path, output_path):
    embedding = []
    with open(input_path, "r", encoding="utf-8") as f, open(output_path, "w", encoding="utf-8") as f_o:
        x, y = f.readline().strip("\n").split(" ")
        print(x, y)
        for line in f.readlines():
            _line = line.strip("\n").split(" ")
            name_type = _line[0].split("|$|")[0]
            if name_type != "company":
                embedding.append(line)
        f_o.write(" ".join([str(len(embedding)), str(y)]) + "\n")
        for item in embedding:
            f_o.write(item)


def sample(input_path, output_path, pos_num, neg_num):
    ct = Counter()
    positive = []
    negative = []
    with open(input_path, "r", encoding="utf-8") as f, open(output_path, "w", encoding="utf-8") as f_o:
        for line in f.readlines():
            line = json.loads(line)
            uuid_key = line.get("uuid_key")
            cv_rt = line.get("cv_rt")
            jd_rt = line.get("jd_rt")
            cv_rt_v = list(cv_rt.values())
            jd_rt_v = list(jd_rt.values())
            ct[str(line.get("label"))] += 1
            tmp = "\t".join(["##".join(x) for x in cv_rt_v]) + "@" + "\t".join(
                ["##".join(x) for x in jd_rt_v]) + "@" + str(line.get("label")) + "@" + uuid_key
            if str(line.get("label")) == "1":
                positive.append(tmp)
            elif str(line.get("label")) == "0":
                negative.append(tmp)
        sample_total = list(np.random.choice(positive, pos_num, replace=False)) + list(np.random.choice(negative, neg_num, replace=False))
        np.random.shuffle(sample_total)
        for _sample in sample_total:
            f_o.write(_sample + "\n")
    print(ct.items())


if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser()

    subparsers = parser.add_subparsers(dest='command')

    data_trans_parser = subparsers.add_parser('data_trans')
    data_trans_parser.add_argument('--input_path', required=True)
    data_trans_parser.add_argument('--output_path', required=True)

    gen_vocab_parser = subparsers.add_parser('gen_vocab')
    gen_vocab_parser.add_argument('--input_path', required=True)
    gen_vocab_parser.add_argument('--output_path', required=True)

    filter_embedding_parser = subparsers.add_parser('filter_embedding')
    filter_embedding_parser.add_argument('--input_path', required=True)
    filter_embedding_parser.add_argument('--output_path', required=True)

    data_predict_parser = subparsers.add_parser('sample')
    data_predict_parser.add_argument('--input_path', required=True)
    data_predict_parser.add_argument('--output_path', required=True)
    data_predict_parser.add_argument('--pos_num', required=True, type=int)
    data_predict_parser.add_argument('--neg_num', required=True, type=int)

    args = parser.parse_args()

    if args.command == 'data_trans':
        data_trans(args.input_path, args.output_path)
    elif args.command == 'gen_vocab':
        gen_vocab(args.input_path, args.output_path)
    elif args.command == 'filter_embedding':
        filter_embedding_nocompany(args.input_path, args.output_path)
    elif args.command == 'sample':
        sample(args.input_path, args.output_path, args.pos_num, args.neg_num)

"""

"""
import json
from collections import Counter
import numpy as np


def data_trans(input_path, output_path):
    ct = Counter()
    positive = []
    negative = []
    with open(input_path, "r", encoding="utf-8") as f, open(output_path, "w", encoding="utf-8") as f_o:
        for line in f.readlines():
            line = json.loads(line)
            cv_rt = line.get("cv_rt")
            jd_rt = line.get("jd_rt")
            cv_rt_v = list(cv_rt.values())
            jd_rt_v = list(jd_rt.values())
            ct[str(line.get("label"))] += 1
            tmp = "\t".join(["##".join(x) for x in cv_rt_v]) + "@" + "\t".join(
                ["##".join(x) for x in jd_rt_v]) + "@" + str(line.get("label"))
            if str(line.get("label")) == "1":
                positive.append(tmp)
            elif str(line.get("label")) == "0":
                negative.append(tmp)
        sample_total = positive + list(np.random.choice(negative, len(positive) * 5, replace=False))
        np.random.shuffle(sample_total)
        for neg_sample in sample_total:
            f_o.write(neg_sample + "\n")
    print(ct.items())


def gen_vocab(input_path, output_path):
    with open(input_path, "r", encoding="utf-8") as f, open(output_path, "w", encoding="utf-8") as f_o:
        x, y = f.readline().strip("\n").split(" ")
        print(x, y)
        f_o.write("UNK" + "\n")
        for line in f.readlines():
            line = line.strip("\n").split(" ")
            f_o.write(line[0] + "\n")


def filter_embedding_nocompany(input_path, output_path):
    embedding = []
    with open(input_path, "r", encoding="utf-8") as f, open(output_path, "w", encoding="utf-8") as f_o:
        x, y = f.readline().strip("\n").split(" ")
        print(x, y)
        for line in f.readlines():
            _line = line.strip("\n").split(" ")
            name_type = _line[0].split("|$|")[0]
            if name_type != "company":
                embedding.append(line)
        f_o.write(" ".join([str(len(embedding)), str(y)]) + "\n")
        for item in embedding:
            f_o.write(item)


def sample(input_path, output_path, pos_num, neg_num):
    ct = Counter()
    positive = []
    negative = []
    with open(input_path, "r", encoding="utf-8") as f, open(output_path, "w", encoding="utf-8") as f_o:
        for line in f.readlines():
            line = json.loads(line)
            uuid_key = line.get("uuid_key")
            cv_rt = line.get("cv_rt")
            jd_rt = line.get("jd_rt")
            cv_rt_v = list(cv_rt.values())
            jd_rt_v = list(jd_rt.values())
            ct[str(line.get("label"))] += 1
            tmp = "\t".join(["##".join(x) for x in cv_rt_v]) + "@" + "\t".join(
                ["##".join(x) for x in jd_rt_v]) + "@" + str(line.get("label")) + "@" + uuid_key
            if str(line.get("label")) == "1":
                positive.append(tmp)
            elif str(line.get("label")) == "0":
                negative.append(tmp)
        sample_total = list(np.random.choice(positive, pos_num, replace=False)) + list(np.random.choice(negative, neg_num, replace=False))
        np.random.shuffle(sample_total)
        for _sample in sample_total:
            f_o.write(_sample + "\n")
    print(ct.items())


if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser()

    subparsers = parser.add_subparsers(dest='command')

    data_trans_parser = subparsers.add_parser('data_trans')
    data_trans_parser.add_argument('--input_path', required=True)
    data_trans_parser.add_argument('--output_path', required=True)

    gen_vocab_parser = subparsers.add_parser('gen_vocab')
    gen_vocab_parser.add_argument('--input_path', required=True)
    gen_vocab_parser.add_argument('--output_path', required=True)

    filter_embedding_parser = subparsers.add_parser('filter_embedding')
    filter_embedding_parser.add_argument('--input_path', required=True)
    filter_embedding_parser.add_argument('--output_path', required=True)

    data_predict_parser = subparsers.add_parser('sample')
    data_predict_parser.add_argument('--input_path', required=True)
    data_predict_parser.add_argument('--output_path', required=True)
    data_predict_parser.add_argument('--pos_num', required=True, type=int)
    data_predict_parser.add_argument('--neg_num', required=True, type=int)

    args = parser.parse_args()

    if args.command == 'data_trans':
        data_trans(args.input_path, args.output_path)
    elif args.command == 'gen_vocab':
        gen_vocab(args.input_path, args.output_path)
    elif args.command == 'filter_embedding':
        filter_embedding_nocompany(args.input_path, args.output_path)
    elif args.command == 'sample':
        sample(args.input_path, args.output_path, args.pos_num, args.neg_num)

#".join(x) for x in cv_rt_v]) + "@" + "\t".join( #".join(x) for x in jd_rt_v]) + "@" + str(line.get("label")) #".join(x) for x in cv_rt_v]) + "@" + "\t".join( #".join(x) for x in jd_rt_v]) + "@" + str(line.get("label")) + "@" + uuid_key