import json input_file = "wikiLinksNED-test-kilt-short.jsonl" output_file = "wikiLinksNED-filtered-test-kilt-short.jsonl" filtered_lines = [] with open(input_file, "r") as file: for line in file: data = json.loads(line) id_ = data["id"] text = data["text"] target = data["target"] candidates = data["candidates"] mention = data["mention"] left_context = data["left_context"] right_context = data["right_context"] if not candidates or target not in candidates: # empty candidates or target not in candidates # filter out continue filtered_lines.append(line) with open(output_file, "w") as file: for line in filtered_lines: file.write(line)