File size: 1,846 Bytes
3cdf7bc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import json
import jsonlines
import argparse
import pandas as pd
import pprint
def main(args):
mainDict = {}
with jsonlines.open(args.input_file) as reader:
for obj in reader:
#Check if the value already exists
neutral = ""
entailment = ""
contradiction = ""
prompt = ""
if mainDict.get(obj['promptID'], None):
prompt = obj['sentence1']
entailment = mainDict[obj['promptID']].get('entailment','')
contradiction = mainDict[obj['promptID']].get('contradiction','')
neutral = mainDict[obj['promptID']].get('neutral','')
if obj['gold_label'] == "neutral":
neutral = obj['sentence2']
elif obj['gold_label'] == "contradiction":
contradiction = obj['sentence2']
elif obj['gold_label'] == "entailment":
entailment = obj['sentence2']
mainDict[obj['promptID']] = {'prompt': prompt, 'entailment' : entailment, 'neutral' : neutral, 'contradiction' : contradiction}
myList = []
for promptID in mainDict:
myList.append({'sent0':mainDict[promptID]['prompt'], 'sent1':mainDict[promptID]['entailment'], 'hard_neg':mainDict[promptID]['contradiction']})
df = pd.DataFrame.from_records(myList)
#Drop empty
df.replace("", float("NaN"), inplace=True)
df.dropna(subset = ["sent0","sent1","hard_neg"], inplace=True)
#Save csv
df.to_csv(args.output_file, index=False)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--input_file', help="Input file.", required=True)
parser.add_argument('--output_file', help="Output file.", required=True)
args = parser.parse_args()
main(args)
|