File size: 4,899 Bytes
b6e391d
 
 
 
a84785e
d0d9c07
b6e391d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ee0b08
 
b6e391d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import random

# Read the JSONL file and parse each line into a dictionary
with open('manythings-translations-randomized.jsonl', 'r') as file:
    data = [json.loads(line) for line in file][:]  # Optionally limit the amount of lines to input.

# List of phrases to randomly choose from
instruction_phrases = [
    "i need a translation",
    "convert the following text",
    "convert the following message",
    "convert the following sentence",
    "communicate this text",
    "communicate this message",
    "communicate this sentence",
    "can you communicate this text",
    "can you communicate this message",
    "can you communicate this sentence",
    "please communicate this text",
    "please communicate this message",
    "please communicate this sentence",
    "please can you communicate this text",
    "please can you communicate this message",
    "please can you communicate this sentence",
    "can you please communicate this text",
    "can you please communicate this message",
    "can you please communicate this sentence",
    "translate this text",
    "translate this message",
    "translate this sentence",
    "can you translate this text",
    "can you translate this message",
    "can you translate this sentence",
    "please translate this text",
    "please translate this message",
    "please translate this sentence",
    "please can you translate this text",
    "please can you translate this message",
    "please can you translate this sentence",
    "can you please translate this text",
    "can you please translate this message",
    "can you please translate this sentence",
    "switch the text",
    "switch the message",
    "switch the sentence",
    "can you switch the text",
    "can you switch the message",
    "can you switch the sentence",
    "please switch the text",
    "please switch the message",
    "please switch the sentence",
    "please can you switch the text",
    "please can you switch the message",
    "please can you switch the sentence",
    "can you please switch the text",
    "can you please switch the message",
    "can you please switch the sentence",
    "translate this input text",
    "translate this input message",
    "translate this input sentence",
    "can you translate this input text",
    "can you translate this input message",
    "can you translate this input sentence",
    "please translate this input text",
    "please translate this input message",
    "please translate this input sentence",
    "please can you translate this input text",
    "please can you translate this input message",
    "please can you translate this input sentence",
    "can you please translate this input text",
    "can you please translate this input message",
    "can you please translate this input sentence",
    "convert",
    "please convert",
    "please can you convert",
    "can you please convert",
    "translate",
    "please translate",
    "please can you translate",
    "can you please translate",
]

# Function to create the instruction based on language pairs
def create_instruction(from_lang, to_lang):
    phrase = random.choice(instruction_phrases)
    # Randomly decide to capitalize the phrase
    if random.random() < 0.5:
        phrase = phrase.capitalize()
    phrase = f"{phrase} from {from_lang} to {to_lang}"
    # Randomly decide to add a question mark
    if random.random() < 0.5:
        phrase += "?"
    elif random.random() < 0.5:
        phrase += "."
    return phrase

# Initialize the final list to hold the formatted JSON data
formatted_data = []

# Iterate through each item in the original data and create two formatted entries for each
for item in data:
    english_text = item.get('english')
    if english_text:
        for target_lang, translation in item.items():
            if target_lang != 'english':
                # Add instruction and input/output pairs to the formatted_data list for English to target language
                formatted_data.append({
                    "instruction": create_instruction("English", target_lang.capitalize()),
                    "input": english_text,
                    "output": translation
                })

                # Add instruction and input/output pairs to the formatted_data list for target language to English
                formatted_data.append({
                    "instruction": create_instruction(target_lang.capitalize(), "English"),
                    "input": translation,
                    "output": english_text
                })

# Randomize the order of the formatted data
random.shuffle(formatted_data)

# Convert the formatted_data list to JSON format
formatted_json = json.dumps(formatted_data, indent=2)

# Save the formatted JSON data to a new file
with open('manythings-translations-alpaca.json', 'w') as output_file:
    output_file.write(formatted_json)