-
Notifications
You must be signed in to change notification settings - Fork 0
/
oneliners.py
52 lines (39 loc) · 1.44 KB
/
oneliners.py
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
import json
def process_file(content):
# Split content into blocks separated by 'copy'
blocks = content.split('\ncopy\n')
# Initialize list to store command-description pairs
command_pairs = []
for block in blocks:
if not block.strip():
continue
# Split each block into lines
lines = block.strip().split('\n')
if len(lines) < 2:
continue
# The first line is the instruction (description)
instruction = lines[0].strip()
# The second line is the command (output), remove leading '$ ' if present
output = lines[1].strip()
if output.startswith('$ '):
output = output[2:]
# Create a dictionary in the requested format
command_dict = {
"instruction": instruction,
"output": output
}
command_pairs.append(command_dict)
return command_pairs
def main():
# Read input from file
with open('oneliners.txt', 'r', encoding='utf-8') as file:
content = file.read()
# Process the content
command_pairs = process_file(content)
# Write to JSONL file
with open('commands.jsonl', 'w', encoding='utf-8') as outfile:
for pair in command_pairs:
json_line = json.dumps(pair, ensure_ascii=False)
outfile.write(json_line + '\n')
if __name__ == "__main__":
main()