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)