# *_* coding:UTF-8 *_*
from copy import deepcopy
import os


class MyDic(object):

    my_dc = {}
    my_ls = []
    cnt = 0

    def __init__(self, n_gram):
        data_base = os.getcwd()
        name = str(n_gram) + r'_gram.csv'
        self.file = open(data_base + '\\' + name, "a")
        for i in MyDic.my_ls:  # 对my_dc进行初始化
            MyDic.my_dc[i] = 0

    def make_dic(self, feature, count, isVirus):
        index = 0
        for ls in feature:
            temp = deepcopy(MyDic.my_dc)  # 不进行深拷贝的话修改的是原字典内容
            for i in ls:
                # temp[i] = 1  # 将下面一句注释替换为这句则提取0-1值的特征
                temp[i] = count[index][i]
            self.write2file(temp, isVirus)
            MyDic.cnt += 1
            index += 1
        self.close()

    def write2file(self, temp, isVirus):
        flag = False
        for key in temp:
            if flag:
                self.file.write(',' + str(temp[key]))
                continue
            flag = True
            self.file.write(str(temp[key]))
        self.file.write(',' + str(isVirus[MyDic.cnt]))
        self.file.write('\n')

    def close(self):
        self.file.close()
