From 2f0f71cf6896e12fe495bef3b2957eab50e754ff Mon Sep 17 00:00:00 2001 From: 韩晓伟 Date: Fri, 13 Apr 2018 14:42:55 +0800 Subject: [PATCH] 修改答案为空 --- ChangeNoAnswer.py | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100755 ChangeNoAnswer.py diff --git a/ChangeNoAnswer.py b/ChangeNoAnswer.py new file mode 100755 index 0000000..d5a10b8 --- /dev/null +++ b/ChangeNoAnswer.py @@ -0,0 +1,88 @@ +# coding:utf-8 + +import json +from collections import OrderedDict +from tqdm import tqdm + + +def JsonConvert(InputFile,OutputFile): + root = {} + data = [] + id_count = 0 + + with open(InputFile,'r') as fh: + source = json.load(fh) + for article in tqdm(source["data"]): + for para in article["paragraphs"]: + context = '# ' + para["context"].replace( # 替代''为“ + "''", '" ').replace("``", '" ') + for qa in para["qas"]: + qas = list(dict() for i in range(1)) + paragraphs = list(dict() for i in range(1)) + article = OrderedDict() + + question = qa["question"].replace( # 替代''为“ + "''", '" ').replace("``", '" ') + answers = qa["answers"] + + if('train' in OutputFile): + answers = answers4train(context,answers) + else: + answers = answers4test(context,answers) + + qas[0]["answers"] = answers + qas[0]["question"] = question + qas[0]["id"] = qa['id'] + + paragraphs[0]["context"] = context + paragraphs[0]["qas"] = qas + + article["title"] = " " + article["paragraphs"] = paragraphs + + data.append(article) + + + + root["data"] = data + json.dump(root,open(OutputFile,'w')) + + + +def answers4train(context,answers): + answer = OrderedDict() + + if(answers == []): + answer["text"] = "#" + answer["answer_start"] = context.find('#') + answers.append(answer) + else: + answers[0]["answer_start"] += len('# ') + + return answers + +def answers4test(context,answers): + if(answers == []): + answers = list(OrderedDict() for i in range(3)) + answers[0]["text"] = answers[1]["text"] = answers[2]["text"] = '#' + answers[0]["answer_start"]=answers[1]["answer_start"]=answers[2]["answer_start"] = 0 + else: + for i in range(len(answers)): + answers[i]["answer_start"] += len('# ') + + return answers + + + +JsonConvert('d0/train-v1.1.json','d0/a-train-v1.1.json') +JsonConvert('d0/dev-v1.1.json','d0/a-dev-v1.1.json') + +JsonConvert('d1/train-v1.1.json','d1/a-train-v1.1.json') +JsonConvert('d1/dev-v1.1.json','d1/a-dev-v1.1.json') + +JsonConvert('d2/train-v1.1.json','d2/a-train-v1.1.json') +JsonConvert('d2/dev-v1.1.json','d2/a-dev-v1.1.json') + + +JsonConvert('d3/train-v1.1.json','d3/a-train-v1.1.json') +JsonConvert('d3/dev-v1.1.json','d3/a-dev-v1.1.json') -- libgit2 0.27.1