File size: 2,648 Bytes
d96cf4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import argparse
import json
from pathlib import Path

from sedweave.execution import run_sed

EXCLUDE = {
    "so-22099623",
    "so-3832068",
    "so-6245293",
}

COMMAND_FIXES = {
    "so-4348166": "sed -n 's/.*_\\([0-9][0-9]*\\)\\(\\.gz\\)\\{0,1\\}$/\\1/p'",
    "so-6739258": "sed '1,/^nameserver/ { /^nameserver/i\\\nnameserver 127.0.0.1\n}'",
    "so-10754682": "sed 's/\\r$//; s/.*/\"&\"/'",
    "so-28795479": "sed 's/\\([a-z0-9]\\)\\([A-Z]\\)/\\1_\\2/g; s/.*/\\L&/'",
    "so-8225822": "sed -n 's/A/a/gp'",
}

REQUEST_FIXES = {
    "so-1898553": "Replace each line with its first sequence of digits; leave lines without digits unchanged.",
    "so-2957094": "Print only lines whose first two whitespace-separated fields are a number and a value starting with 'LINUX'.",
    "so-3795512": "Remove the first five characters from every line that contains at least five characters.",
    "so-6156259": "Print lines that contain the substring 'teste', optionally preceded by 'www.'. Use POSIX Basic Regular Expressions only.",
    "so-14046878": "Delete from the first line exactly matching '-----THIS STUFF IS USELESS-----' to the end of the input stream.",
    "so-9970124": "Insert the line 'New Inserted Line' immediately after the first occurrence of 'Matched Keyword', before any remaining text on that line.",
}


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("input", type=Path)
    parser.add_argument("output", type=Path)
    parser.add_argument("--split", choices=("train", "test"), required=True)
    args = parser.parse_args()
    rows = [json.loads(line) for line in args.input.open(encoding="utf-8")]
    reviewed = []
    for row in rows:
        if row["id"] in EXCLUDE:
            continue
        row["command"] = COMMAND_FIXES.get(row["id"], row["command"])
        row["request"] = REQUEST_FIXES.get(row["id"], row["request"])
        row["split"] = args.split
        row["expected_output"] = run_sed(row["command"], row["input_text"])
        row["validation_output"] = run_sed(row["command"], row["validation_input"])
        if not row["expected_output"].strip() or not row["validation_output"].strip():
            raise ValueError(f"Empty output: {row['id']}")
        row["reviewed"] = True
        reviewed.append(row)
    with args.output.open("w", encoding="utf-8") as handle:
        for row in reviewed:
            handle.write(json.dumps(row, ensure_ascii=False, separators=(",", ":")) + "\n")
    print(f"wrote {len(reviewed)} reviewed {args.split} rows; excluded {len(rows) - len(reviewed)}")


if __name__ == "__main__":
    main()