from __future__ import annotations import argparse import concurrent.futures import json import random import shlex from dataclasses import dataclass from pathlib import Path from sedweave.execution import run_sed NAMES = ["alice", "bruno", "chen", "dina", "eli", "farah", "gus", "hana"] WORDS = ["amber", "birch", "cobalt", "delta", "ember", "fable", "grove", "harbor"] STATUSES = ["active", "pending", "closed", "ready", "failed"] @dataclass class Case: request: str command: str inputs: list[str] family: str difficulty: int def text(rng: random.Random, count: int | None = None, separator: str = " ") -> str: lines = [] for _ in range(count or rng.randint(6, 9)): fields = [rng.choice(NAMES), rng.choice(WORDS), str(rng.randint(0, 120)), rng.choice(STATUSES)] lines.append(separator.join(fields)) return "\n".join(lines) + "\n" def pair(rng: random.Random, separator: str = " ", transform=None) -> list[str]: values = [text(rng, separator=separator), text(rng, separator=separator)] if transform: values = [transform(value) for value in values] return values def make_case(family: str, seed: int) -> Case: rng = random.Random(seed) if family == "substitute_first": old, new = rng.sample(WORDS, 2) return Case(f"Replace the first occurrence of {old} with {new} on each line.", f"s/{old}/{new}/", pair(rng), family, 1) if family == "substitute_global": old, new = rng.sample(WORDS, 2) return Case(f"Replace every occurrence of {old} with {new} throughout the input.", f"s/{old}/{new}/g", pair(rng), family, 1) if family == "regex_delete": status = rng.choice(STATUSES) return Case(f"Delete every line containing {status}.", f"/{status}/d", pair(rng), family, 1) if family == "regex_print": status = rng.choice(STATUSES) return Case(f"Print only lines containing {status}; suppress all others.", f"-n /{status}/p", pair(rng), family, 1) if family == "line_range": start = rng.randint(2, 4) end = start + rng.randint(1, 3) return Case(f"Print lines {start} through {end}, inclusive, and nothing else.", f"-n {start},{end}p", pair(rng), family, 1) if family == "delete_range": start = rng.randint(2, 4) end = start + rng.randint(1, 2) return Case(f"Delete lines {start} through {end}, leaving all other lines unchanged.", f"{start},{end}d", pair(rng), family, 1) if family == "first_n": count = rng.randint(2, 4) return Case(f"Print only the first {count} lines.", f"-n 1,{count}p", pair(rng), family, 1) if family == "last_line": return Case("Print only the last line of the input.", "-n $p", pair(rng), family, 1) if family == "delete_last": return Case("Delete the last line of the input and print all remaining lines.", "$d", pair(rng), family, 1) if family == "every_other": return Case("Print odd-numbered lines: lines 1, 3, 5, and so on.", "-n 0~2p", pair(rng), family, 2) if family == "append_after": status = rng.choice(STATUSES) note = rng.choice(["follow_up", "reviewed", "complete"]) return Case(f"Append the line {note} immediately after every line containing {status}.", f"/{status}/a {note}", pair(rng), family, 2) if family == "insert_before": status = rng.choice(STATUSES) note = rng.choice(["follow_up", "reviewed", "complete"]) return Case(f"Insert the line {note} immediately before every line containing {status}.", f"/{status}/i {note}", pair(rng), family, 2) if family == "change_line": status = rng.choice(STATUSES) note = rng.choice(["follow_up", "reviewed", "complete"]) return Case(f"Replace every full line containing {status} with the single line {note}.", f"/{status}/c {note}", pair(rng), family, 2) if family == "delete_blank": return Case("Delete all empty lines while preserving every nonempty line.", "/^$/d", pair(rng, transform=lambda value: value.replace("\n", "\n\n", 2)), family, 1) if family == "strip_leading": return Case("Remove all leading spaces and tabs from each line.", "s/^[ \\t]*//", pair(rng, transform=lambda value: value.replace(" ", " ")), family, 1) if family == "strip_trailing": return Case("Remove all trailing spaces and tabs from each line.", "s/[ \\t]*$//", pair(rng, transform=lambda value: value.replace(" ", " ")), family, 1) if family == "collapse_space": return Case("Collapse every run of spaces or tabs to a single space.", "s/[ \\t]+/ /g", pair(rng, transform=lambda value: value.replace(" ", " ")), family, 1) if family == "uppercase": return Case("Convert every input line to uppercase.", "s/.*/\\U&/", pair(rng), family, 1) if family == "lowercase": return Case("Convert every input line to lowercase.", "s/.*/\\L&/", pair(rng), family, 1) if family == "swap_fields": return Case("Swap the first two whitespace-separated fields on every line.", "s/\\([^ ]*\\) \\([^ ]*\\)/\\2 \\1/", pair(rng), family, 2) if family == "comma_to_tab": return Case("Replace every comma with a tab character.", "s/,/\\t/g", pair(rng, separator=","), family, 1) if family == "add_prefix": prefix = rng.choice(["INFO", "record", "notice"]) return Case(f"Add the prefix {prefix} followed by a space to the start of every line.", f"s/^/{prefix} /", pair(rng), family, 1) if family == "add_suffix": suffix = rng.choice(["complete", "reviewed", "done"]) return Case(f"Append a space and {suffix} to the end of every line.", f"s/$/ {suffix}/", pair(rng), family, 1) if family == "comment_lines": return Case("Comment out every input line by adding # and a space at the start.", "s/^/# /", pair(rng), family, 1) if family == "delete_comments": return Case("Delete all lines beginning with #.", "/^#/d", pair(rng, transform=lambda value: value.replace("\n", "\n# note\n", 1)), family, 1) if family == "first_chars": count = rng.randint(2, 6) return Case(f"Print only the first {count} characters of each line.", f"s/^\\(.\\{{{count}\\}}\\).*/\\1/", pair(rng), family, 2) if family == "delete_between": start, end = rng.sample(WORDS, 2) return Case(f"Delete everything from the line containing {start} through the line containing {end}, inclusive.", f"/{start}/,/{end}/d", pair(rng), family, 2) if family == "print_between": start, end = rng.sample(WORDS, 2) return Case(f"Print only the block from the line containing {start} through the line containing {end}, inclusive.", f"-n /{start}/,/{end}/p", pair(rng), family, 2) if family == "remove_html": return Case("Remove all HTML-like angle-bracket tags from the text.", "s/<[^>]*>//g", ["
amber birch
\ndelta\n", "