|
| 1 | +""" |
| 2 | +样本加载 |
| 3 | +""" |
| 4 | +# coding=utf8 |
| 5 | + |
| 6 | +import sys |
| 7 | +import random |
| 8 | +import jieba |
| 9 | +import numpy as np |
| 10 | +from word_vectors_loader import get_words_sizes, load_vectors |
| 11 | + |
| 12 | +VECTORS_BIN = 'data/wordvec/vectors.bin' |
| 13 | +TEST_COUNT = 5 |
| 14 | + |
| 15 | + |
| 16 | +class DimInfo(object): |
| 17 | + """ |
| 18 | + 维度信息 |
| 19 | + """ |
| 20 | + |
| 21 | + def __init__(self): |
| 22 | + # 词向量有多少维 |
| 23 | + self.vec_dim = 0 |
| 24 | + # 样本输入的x有多少维 |
| 25 | + self.x_dim = 0 |
| 26 | + # 当前最大的词编号是多大 |
| 27 | + self.max_word_id = -1 |
| 28 | + |
| 29 | + def get_vec_dim(self): |
| 30 | + """ |
| 31 | + get_vec_dim |
| 32 | + """ |
| 33 | + return self.vec_dim |
| 34 | + |
| 35 | + def get_x_dim(self): |
| 36 | + """ |
| 37 | + get_x_dim |
| 38 | + """ |
| 39 | + return self.x_dim |
| 40 | + |
| 41 | + |
| 42 | +class Maps(object): |
| 43 | + """ |
| 44 | + 各种映射表 |
| 45 | + """ |
| 46 | + |
| 47 | + def __init__(self): |
| 48 | + self.local_word_id_map = {} |
| 49 | + self.local_group_id_map = {"1":0, "2":1, "3":2, "4":3, "5":4} |
| 50 | + self.real_group_id_map = {} |
| 51 | + for key in self.local_group_id_map: |
| 52 | + value = str(self.local_group_id_map[key]) |
| 53 | + self.real_group_id_map[value] = int(key) |
| 54 | + |
| 55 | + def get_local_word_id_map(self): |
| 56 | + """ |
| 57 | + get_local_word_id_map |
| 58 | + """ |
| 59 | + return self.local_word_id_map |
| 60 | + |
| 61 | + def get_local_group_id_map(self): |
| 62 | + """ |
| 63 | + get_local_group_id_map |
| 64 | + """ |
| 65 | + return self.local_group_id_map |
| 66 | + |
| 67 | + def group_id_size(self): |
| 68 | + """ |
| 69 | + 获取local_group的数量 |
| 70 | + """ |
| 71 | + return len(self.local_group_id_map) |
| 72 | + |
| 73 | + |
| 74 | +class InputData(object): |
| 75 | + """ |
| 76 | + 样本加载类 |
| 77 | + """ |
| 78 | + |
| 79 | + def __init__(self): |
| 80 | + self.data = [] |
| 81 | + self.test_data = [] |
| 82 | + self.pos = 0 |
| 83 | + self.word_vector_dict, self.word_id_dict = load_vectors(VECTORS_BIN) |
| 84 | + self.dim_info = DimInfo() |
| 85 | + self.maps = Maps() |
| 86 | + _, self.dim_info.vec_dim = get_words_sizes(VECTORS_BIN) |
| 87 | + self.dim_info.x_dim = len(self.word_vector_dict) * self.dim_info.vec_dim |
| 88 | + self.maps.local_word_id_map = {} |
| 89 | + |
| 90 | + def clear_word_vector(self): |
| 91 | + """ |
| 92 | + 清理点内存 |
| 93 | + """ |
| 94 | + self.word_vector_dict.clear() |
| 95 | + self.word_id_dict.clear() |
| 96 | + |
| 97 | + @staticmethod |
| 98 | + def read_data_sets(file_name): |
| 99 | + """ |
| 100 | + 读取文件,加载数据 |
| 101 | + """ |
| 102 | + instance = InputData() |
| 103 | + file_object = open(file_name, 'r') |
| 104 | + while True: |
| 105 | + line = file_object.readline(1024) |
| 106 | + if line: |
| 107 | + line = line.strip() |
| 108 | + if len(line) == 0: |
| 109 | + continue |
| 110 | + split = line.split(' ') |
| 111 | + group_id = 0 |
| 112 | + try: |
| 113 | + group_id = int(split[0]) |
| 114 | + except ValueError: |
| 115 | + continue |
| 116 | + txt = ' '.join(split[1:]) |
| 117 | + txt = txt.replace('None', '').strip() |
| 118 | + if len(txt) == 0: |
| 119 | + continue |
| 120 | + |
| 121 | + vectors = {} |
| 122 | + seg_list = jieba.cut(txt) |
| 123 | + for seg in seg_list: |
| 124 | + seg_unicode = seg.encode('utf-8') |
| 125 | + if seg_unicode in instance.word_vector_dict: |
| 126 | + word_id = instance.word_id_dict[seg_unicode] |
| 127 | + if word_id in instance.maps.local_word_id_map: |
| 128 | + local_word_id = instance.maps.local_word_id_map[word_id] |
| 129 | + vectors[local_word_id] = instance.word_vector_dict[seg_unicode] |
| 130 | + else: |
| 131 | + local_word_id = instance.dim_info.max_word_id |
| 132 | + instance.maps.local_word_id_map[word_id] = local_word_id |
| 133 | + vectors[local_word_id] = instance.word_vector_dict[seg_unicode] |
| 134 | + instance.dim_info.max_word_id = instance.dim_info.max_word_id + 1 |
| 135 | + |
| 136 | + # 稀疏向量 |
| 137 | + item = {'vectors':vectors, |
| 138 | + 'local_group_id':instance.maps.local_group_id_map[str(group_id)]} |
| 139 | + instance.data.append(item) |
| 140 | + else: |
| 141 | + break |
| 142 | + file_object.close() |
| 143 | + |
| 144 | + random.shuffle(instance.data) |
| 145 | + for _ in range(TEST_COUNT): |
| 146 | + instance.test_data.append(instance.data.pop()) |
| 147 | + instance.dim_info.x_dim = instance.dim_info.max_word_id * instance.dim_info.vec_dim |
| 148 | + print("max_word_id=", instance.dim_info.max_word_id) |
| 149 | + print("x_dim=", instance.dim_info.x_dim) |
| 150 | + return instance |
| 151 | + |
| 152 | + def generate_xs(self, txt): |
| 153 | + """ |
| 154 | + 根据文本生成输入向量 |
| 155 | + """ |
| 156 | + x_s = [] |
| 157 | + vectors = {} |
| 158 | + seg_list = jieba.cut(txt) |
| 159 | + for seg in seg_list: |
| 160 | + seg_unicode = seg.encode('utf-8') |
| 161 | + if seg_unicode in self.word_vector_dict: |
| 162 | + word_id = self.word_id_dict[seg_unicode] |
| 163 | + if word_id in self.maps.local_word_id_map: |
| 164 | + local_word_id = self.maps.local_word_id_map[word_id] |
| 165 | + vectors[local_word_id] = self.word_vector_dict[seg_unicode] |
| 166 | + |
| 167 | + x_array = np.zeros([self.dim_info.x_dim], dtype=np.float) |
| 168 | + for word_id in vectors: |
| 169 | + vector = vectors[word_id] |
| 170 | + for index, weight in enumerate(vector): |
| 171 | + x_array[word_id*self.dim_info.vec_dim+index] = weight |
| 172 | + x_s.append(x_array) |
| 173 | + return x_s |
| 174 | + |
| 175 | + |
| 176 | + def next_batch(self, count): |
| 177 | + """ |
| 178 | + 获取一批样本数据 |
| 179 | + """ |
| 180 | + x_s = [] |
| 181 | + y_s = [] |
| 182 | + if self.pos >= len(self.data): |
| 183 | + print("error") |
| 184 | + sys.exit(1) |
| 185 | + while count > 0: |
| 186 | + item = self.data[self.pos] |
| 187 | + vectors = item['vectors'] |
| 188 | + local_group_id = item['local_group_id'] |
| 189 | + x_array = np.zeros([self.dim_info.x_dim], dtype=np.float) |
| 190 | + y_array = np.zeros(self.maps.group_id_size(), dtype=np.float) |
| 191 | + y_array[local_group_id] = 1 |
| 192 | + for word_id in vectors: |
| 193 | + vector = vectors[word_id] |
| 194 | + for index, weight in enumerate(vector): |
| 195 | + x_array[word_id*self.dim_info.vec_dim+index] = weight |
| 196 | + x_s.append(x_array) |
| 197 | + y_s.append(y_array) |
| 198 | + self.pos = (self.pos + 1) % len(self.data) |
| 199 | + count = count - 1 |
| 200 | + return x_s, y_s |
| 201 | + |
| 202 | + def test_sets(self): |
| 203 | + """ |
| 204 | + 获取测试样本集 |
| 205 | + """ |
| 206 | + x_s = [] |
| 207 | + y_s = [] |
| 208 | + for item in self.test_data: |
| 209 | + vectors = item['vectors'] |
| 210 | + local_group_id = item['local_group_id'] |
| 211 | + x_array = np.zeros([self.dim_info.x_dim], dtype=np.float) |
| 212 | + y_array = np.zeros(self.maps.group_id_size(), dtype=np.float) |
| 213 | + y_array[local_group_id] = 1 |
| 214 | + for word_id in vectors: |
| 215 | + vector = vectors[word_id] |
| 216 | + for index, weight in enumerate(vector): |
| 217 | + x_array[word_id*self.dim_info.vec_dim+index] = weight |
| 218 | + x_s.append(x_array) |
| 219 | + y_s.append(y_array) |
| 220 | + return x_s, y_s |
| 221 | + |
| 222 | + |
| 223 | +if __name__ == '__main__': |
| 224 | + CLUES = InputData.read_data_sets('./data/sample/samples') |
| 225 | + XS, YS = CLUES.next_batch(2) |
| 226 | + print(XS) |
| 227 | + print(XS[0].shape) |
| 228 | + print(YS) |
0 commit comments