读取文件,加载数据
(file_name)
| 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 | """ |
no test coverage detected