在做项目的时候离线下载了200w+的文本数据,提取了mean_w2v特征,如果保存在一个文件中,内存原地爆炸-_-.!!
于是我打算分批存储,然后写了一个json数据合并并索引的脚本。
import os
import json
- 1 merge_w2v_feature:
每一个样本数据格式如下:
sid(学生id),sdt(学生规定上课时间),w2v_feature(w2v特征),tags(学生是否出席)
每个学生可能对应多个上课时间
{
'sid':{
{
'sdt':sdt1;'w2v_feature':w2v_feature;'tags':tags}..{
'sdt2':sdt2;...}}}
def merge_w2v_feature(path):
file_name=[]
#file_name
for _,_,files in os.walk(path): #进入相关目录获取相关文件名称
for file in files:
if file[-4:]=='json':
file_name.append(file)
#merge
print('all bag name %s' %file_name)
w2v_feature_bag=dict()
for name in file_name:
print('json bag name: %s' %name)
with open(path+'/'+name) as f:#读取json文件
k=f.readlines()
k=json.loads(k[0])
w2v_feature_bag=dict(w2v_feature_bag,**k)#合并json文件
return w2v_feature_bag
- 2 analysis_json
把数据和脚本交给小伙伴,merge以后就可以根据sid和sdt解析对应的w2v_feature数据。
def analysis_json(sid,sdt,feature_bag):# type: sid:str____sdt:str____feature_bag:json
#analysis json
if sid not in feature_bag:
if type(sid)==int:
return 'sid type should be string'
return 'no sid in feature_bag'
else:
values=feature_bag[sid]
for value in values:
if sdt in value.values():
w2v_feature=value['w2v_feature']
tags=value['tags']
return w2v_feature,tags #返回w2v_feature和标签
return 'no sdt in this sid'