File size: 9,868 Bytes
4d23e7a |
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
import json
import argparse
import os
import logging
from typing import Any
import re
# Configure logging
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)
def process_jsonl(input_file: str, output_file: str) -> None:
"""
Process a JSONL file to add a 'text' field combining 'title' and 'description'.
Args:
input_file (str): Path to the input JSONL file.
output_file (str): Path to the output JSONL file.
"""
logging.info(f"Processing JSONL file: {input_file}")
with open(input_file, "r") as infile, open(output_file, "w") as outfile:
for line in infile:
entry: dict[str, Any] = json.loads(line)
title: str = entry.get("title", "")
description: str = entry.get("description", "")
if not title and not description:
logging.warning(
f"File '{input_file}' contains an entry with no title and no"
" description."
)
entry["text"] = (
f"{title}: {description}" if title and description else description
)
if entry["text"].strip(): # Only write entries with non-empty 'text'
outfile.write(json.dumps(entry, ensure_ascii=False) + "\n")
logging.info(f"Finished processing JSONL file: {input_file}")
logging.info(f"Output file saved at: {output_file}")
def load_exclusion_ids(exclusion_file: str) -> set:
"""
Load exclusion IDs from a JSONL file.
Args:
exclusion_file (str): Path to the JSONL file containing exclusion IDs.
Returns:
set: A set of IDs to exclude.
"""
exclusion_ids = set()
with open(exclusion_file, "r") as file:
for line in file:
obj = json.loads(line)
if "id" in obj:
exclusion_ids.add(obj["id"])
return exclusion_ids
def process_directory(
input_dir: str, output_file: str, exclusion_file: str = None
) -> None:
"""
Process all JSON files in a directory to add a 'text' field and write to a single JSONL file.
Args:
input_dir (str): Path to the input directory containing JSON files.
output_file (str): Path to the output JSONL file to save processed entries.
exclusion_file (str, optional): Path to a JSONL file containing IDs to exclude.
Note:
All processed entries from the JSON files in the directory will be combined
and written into a single JSONL file specified by `output_file`.
"""
logging.info(f"Processing directory: {input_dir}")
total_converted = 0
total_excluded = 0
exclusion_ids = load_exclusion_ids(exclusion_file) if exclusion_file else set()
with open(output_file, "w") as outfile:
for filename in os.listdir(input_dir):
if filename.endswith(".json"):
input_file: str = os.path.join(input_dir, filename)
logging.info(f"Processing file: {input_file}")
with open(input_file, "r") as infile:
data = json.load(infile) # Load single JSON object
if not isinstance(data, dict):
logging.warning(
f"File '{input_file}' does not contain a valid JSON object."
" Skipping."
)
total_excluded += 1
continue
# Handle nested structure under 'ns0:dc'
dc_data = data.get("ns0:dc", {})
title = dc_data.get("dc:title", "")
description = dc_data.get("dc:description", "")
creator = ", ".join(dc_data.get("dc:creator", []))
subject = ", ".join(dc_data.get("dc:subject", []))
publisher = dc_data.get("dc:publisher", "")
date = dc_data.get("dc:date", "")
# Extract identifier and construct 'id'
identifiers = dc_data.get("dc:identifier", [])
if type(identifiers) is not list:
identifiers = [identifiers]
id_value = None
for identifier in identifiers:
if "https://www.zora.uzh.ch/id/eprint/" in identifier:
id_value = identifier.split("/id/eprint/")[-1].split("/")[0]
break
# "https://www.zora.uzh.ch/140521"
match = re.match(r"https://www.zora.uzh.ch/(\d+).*", identifier)
if match:
id_value = match.group(1)
break
if id_value:
id_field = f"oai:www.zora.uzh.ch:{id_value}"
else:
id_field = None
logging.warning(
"No valid ID found in identifiers: %s", identifiers
)
if not id_field:
logging.warning(
f"File '{input_file}' does not contain a valid ID."
" Skipping."
)
total_excluded += 1
continue
# Check if the ID is in the exclusion list
if id_field in exclusion_ids:
logging.info(f"Excluding file with ID: {id_field}")
total_excluded += 1
continue
text = f"{title}: {description}".strip()
if text:
entry = {
"id": id_field,
"title": title,
"description": description,
"text": text,
"creator": creator,
"subject": subject,
"publisher": publisher,
"date": date,
}
outfile.write(json.dumps(entry, ensure_ascii=False) + "\n")
total_converted += 1
else:
total_excluded += 1
logging.info(f"Finished processing directory: {input_dir}")
logging.info(f"Total converted files: {total_converted}")
logging.info(f"Total excluded files: {total_excluded}")
logging.info(f"Output file saved at: {outfile.name}")
def process_single_json_file(input_file: str, output_file: str) -> None:
"""
Process a single JSON file to extract relevant fields and add a 'text' field and 'id'.
Args:
input_file (str): Path to the input JSON file.
output_file (str): Path to the output JSONL file.
"""
logging.info(f"Processing single JSON file: {input_file}")
with open(input_file, "r") as infile, open(output_file, "w") as outfile:
data = json.load(infile)
if not isinstance(data, dict):
raise ValueError("Expected a JSON object at the root.")
# Handle nested structure under 'ns0:dc'
dc_data = data.get("ns0:dc", {})
title = dc_data.get("dc:title", "")
description = dc_data.get("dc:description", "")
creator = ", ".join(dc_data.get("dc:creator", []))
subject = ", ".join(dc_data.get("dc:subject", []))
publisher = dc_data.get("dc:publisher", "")
date = dc_data.get("dc:date", "")
# Extract identifier and construct 'id'
identifiers = dc_data.get("dc:identifier", [])
id_value = None
for identifier in identifiers:
if "https://www.zora.uzh.ch/id/eprint/" in identifier:
id_value = identifier.split("/id/eprint/")[-1].split("/")[0]
break
if id_value:
id_field = f"oai:www.zora.uzh.ch:{id_value}"
else:
id_field = None
text = f"{title}: {description}".strip()
if text:
entry = {
"text": text,
"id": id_field,
"title": title,
"description": description,
"creator": creator,
"subject": subject,
"publisher": publisher,
"date": date,
}
outfile.write(json.dumps(entry, ensure_ascii=False) + "\n")
logging.info(f"Finished processing single JSON file: {input_file}")
def main() -> None:
"""
Main function to parse arguments and process files or directories.
If the input is a directory, all JSON files in the directory will be processed,
and their entries will be combined into a single JSONL file specified by the output path.
If the input is a single JSONL file, it will be processed and written to the output file.
"""
logging.info("Starting the processing script.")
parser = argparse.ArgumentParser(
description=(
"Process JSON or JSONL files to add a 'text' field consisting of {title}:"
" {description}."
" If the result is empty, the document is not added to the output file."
)
)
parser.add_argument("input", help="Path to the input JSONL file or directory")
parser.add_argument("output", help="Path to the output JSONL file or directory")
parser.add_argument(
"--exclude", help="Path to a JSONL file containing IDs to exclude", default=None
)
args = parser.parse_args()
if os.path.isdir(args.input):
process_directory(args.input, args.output, args.exclude)
else:
process_jsonl(args.input, args.output)
logging.info("Processing script completed.")
if __name__ == "__main__":
main()
|