Datasets:

Modalities:
Text
Libraries:
Datasets
File size: 11,517 Bytes
ad576ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
"""
This module generates datasets doing string manipulations
"""
import enum
import random
from collections import deque
import os
import string
import json


class StringOperations(str, enum.Enum):

    SLICE = "slicing"
    STARTS_ENDS_WITH = "starts_ends_with"
    LEN= "len"
    CONCAT= "concat"
    REPEAT= "repeat"
    UPPER_LOWER_SWAP_CASE= "upper_lower_swap_case"
    IS= "is"


def generate_random_string(length, charset = None):
    """
    Generates a random string of length length
    :param length: the length of the string
    :param alphabet: the alphabet to use
    :return: the string
    """
    if charset is None:
        charset = string.ascii_letters + string.digits
    return ''.join(charset[b % len(charset)] for b in os.urandom(length))

def generate_reverse_string_prompt(samples,  length=20, rev_op="'{sample}'[::-1]",result_var="res", **kwargs):
    """
    provides samples examples of reversing a random string.
    :param s: the string
    :return: the reversed string
    """
    for i in range(samples):
        s = generate_random_string(length)
        o = rev_op.format(sample=s)
        yield o, s[::-1], f"{result_var}='{o}'[::-1]", result_var, "reverse"

def generate_slicing_examples(samples, length=20, #char_at_op="'{sample}'[{pos}]",
                                     pos_range=(1,2),
                                     slice_range = (1,1),
                                     step_size = (1,1),
                                     result_var="res", **kwargs):
    """
    provides samples examples of reversing a random string.
    :param s: the string
    :return: yields inp, outp, code, res_var, todo_str
    """
    for _ in range(samples):
        s = generate_random_string(length)
        for i in range(*pos_range):
            for j in range(*slice_range):
                if j == 1: # only one character means no slice
                    o = f"'{s}'[{i}]"
                    yield o, s[i], f"{result_var}={o}", result_var, f"char_at_{i}"
                else:
                    for k in range(*step_size):
                        if k==0:
                            continue
                        elif j==0 and i==0: # only step size
                            o = f"'{s}'[::{k}]"
                            yield o, s[::k], f"{result_var}={o}", result_var, f"step_::{k}"
                        elif k==1:
                            o = f"'{s}'[{i}:{i+j}]"
                            yield o, s[i:i+j], f"{result_var}={o}", result_var, f"slice_{i}:{i+j}"
                        elif abs(k)<j: # step size needs to be smaller
                            if k>0:
                                o = f"'{s}'[{i}:{i+j}:{k}]"
                                yield o, s[i:i+j:k], f"{result_var}={o}", result_var, f"slice_step_{i}:{i+j}:{k}"
                            else:
                                o = f"'{s}'[{i}:{i+j}][::{k}]"
                                yield o, s[i:i+j][::k], f"{result_var}={o}", result_var, f"slice_reverse_{i}:{i+j}:{k}"



class StringOperationGenerator:
    """


    """
    data=None


    def set_samples(self, equations):
        self.equations = equations
        return self

    @staticmethod
    def get_prompt(template_name:str = "simple", with_code:bool = False):
        if template_name == "simple":
            returns = StringOperationGenerator._get_simple_string_op_prompt()
        else: returns = StringOperationGenerator._get_plain_prompt()
        if with_code:
            returns[-1].extend( [{"templates": ["###ACTION: exec-python\n{code}\n###/ACTION"],
                         "keys": ["code"],
                         "component": "action",},
                        ])
        return returns

    @staticmethod
    def _get_plain_prompt():
        """
        :return: template for generating value filled equation, e.g. 1+2=3 and mapping needed for the dataset
        """
        inp, out = [], []
        inp.extend([{"templates": ["{input}="],
                      "keys": ["input"],
                      "component": "input",
                     },
                    ])
        out.extend([{"templates": ["{output}\n"],
                     "keys": ["output"],
                     "component": "output",
                     "tags": ["exact"]}])

        return [inp, out]

    @staticmethod
    def _get_simple_string_op_prompt():
        inp, out = [], []
        # todo: this is a problem here, since the prompt is context sensitive, i.e. it depends on the data.
        inp.extend([{"templates": ["Conduct the string operation {operation} as follows: {input}.\n"],
                      "keys": ["operation", "input"],
                      "component": "input",
                     },

                    ])
        out.extend([{"templates": ["{res_var}={output}\n"],
                     "keys": ["res_var", "output"],
                     "component": "output",
                     "tags": ["exact"]}])

        return [inp, out]


    def create_data (self, samples=100, operations=(StringOperations.SLICE),
                              valid_data_only=True, **kwargs):
        self.data = deque()

        for op in operations:
            if op==StringOperations.SLICE.value:
                g = generate_slicing_examples(samples, **kwargs)
            else:
                raise NotImplementedError(f"Operation {op} not implemented")
            for inp, outp, code, res_var, todo_str in g:
                if valid_data_only and (outp=="" or outp is None): continue
                self.data.append({"input": inp, "output": outp, "code": code,"res_var": res_var, "operation": todo_str })

        self.data = list(self.data)
        return self

    def save(self, filename):
        # load prompts and equations from file
        import json
        with open(filename, "w") as f:
            json.dump(self.data, f)
        return self

    def load(self, filename):
        # load data and equations from file
        import json
        with open(filename, "r") as f:
            self.data = json.load(f)
        return self


def write_data(dump_dir, file, out, compress, indent=2 ):
    import json, gzip
    filename = os.path.join(dump_dir, file)
    if compress:
        with gzip.open(f'{filename}.gz', 'wt', encoding='utf-8') as f:
            json.dump(out, f, indent=indent)
    else:
        json.dump(out, open(filename, "w"), indent=indent)
    return filename


def generate_data_for_config(dump_dir, about, s_length = (10,25, 5), pos_range = (0,5), slice_range = (0,4),
                    step_size = (-1,2), samples_per_config = 10, valid_data_only = True):

    samples = samples_per_config * (step_size[1]-step_size[0]) \
              * (slice_range[1]-slice_range[0])\
              *  (pos_range[1]-pos_range[0])

    about["data_files"] = {"train": [], "test": []}

    markdown = ["", "|Length|Set|Group|Amount|File|", "|---|---|---|---|---|" ]
    train_total, test_total, id = 0, 0, 1
    for length in tqdm.tqdm(range(*s_length), desc="Generating data"):
        generator = StringOperationGenerator()
        data = generator.create_data(samples=samples,
                                            operations=("slicing",),
                                            length=length,
                                     pos_range=pos_range,
                                     slice_range = slice_range,
                                     step_size = step_size,
                                            valid_data_only=valid_data_only,
                                     result_var="res",).data

        for e in data:
            e["id"] = id
            id=id+1
        cnt = Counter([e["operation"] for e in data])
        test, train = {}, {}
        for d in cnt.keys(): test[d], train[d] = [], []

        for ix, e in enumerate(data): # not very smart, but it is late
            if len(test[e["operation"]])>cnt[e["operation"]] *(1-split_ratio):
                train[e["operation"]].append(e)
            else:
                test[e["operation"]].append(e)

        markdown.extend([f"|{length}|train|{k}|{len(v)}|stop_{length}_train.json|" for k, v in train.items()])
        markdown.extend([f"|{length}|test|{k}|{len(v)}|stop_{length}_train.json|" for k, v in test.items()])

        about["length"]= length
        about["set"] = "train"
        data = [v for value in train.values() for v in value]
        write_data(dump_dir, f"stop_{length}_train.json", data, compress)
        about["data_files"]["train"].append({"length": length,
                                             "files": [f"stop_{length}_train.json"],
                                             "entries": len(data),
                                            "groups": [{"name": k, "amount": len(v)} for k, v in train.items()]})
        train_total+=len(data)

        data = [v for value in test.values() for v in value]
        write_data(dump_dir,  f"stop_{length}_test.json", data, compress)
        about["data_files"]["test"].append({"length": length,
                                             "files": [f"stop_{length}_test.json"],
                                             "entries": len(data),
                                            "groups": [{"name": k, "amount": len(v)} for k, v in test.items()]})
        test_total+=len(data)

    about["items"] = {"train": train_total, "test": test_total}
    # now add all about key value pairs except data_files to makrdown varialbe as separate table
    pre_md = ["# Metadata", "|Key|Value|", "|---|---|"]
    pre_md.extend([f"|{k}|{v}|" for k, v in about.items() if k!="data_files"])
    markdown = pre_md + markdown
    with open(os.path.join(dump_dir, "about.json"), "w") as f:
        json.dump(about, f, indent=2)

    with open(os.path.join(dump_dir, "Readme.md"), "w") as f:
        f.write("\n".join(markdown))

    return about, markdown



if __name__=="__main__":
    import datetime, os, tqdm
    from collections import Counter

    split_ratio = 0.7
    compress = True
    about = {
        "dataset_name" : "StOp-small",
        "hfuser":"mgrani",
        "version": "0.0.1",
        # add date today as created field with the date of now
        "created" : datetime.datetime.now().strftime("%Y-%m-%d"),
        "creator" : "Michael Granitzer, [email protected]",
        "split_ratio" : split_ratio,
        "prompt": {"plain": StringOperationGenerator.get_prompt(template_name="plain"),
                   "simple": StringOperationGenerator.get_prompt(template_name="simple"),
                   "simple_with_code": StringOperationGenerator.get_prompt(template_name="simple_with_code")}
    }

    # get the date for today, but nicely formatted as string

    dump_dir = os.path.expanduser("./small")
    if not os.path.exists(dump_dir): os.mkdir(dump_dir)
    about, markdown = generate_data_for_config(dump_dir, about, s_length=(10,25, 5), pos_range=(0,5),
                                               slice_range=(0,4), step_size=(-1,2), samples_per_config=10,
                                               valid_data_only=True)


   # with open(os.path.join(dump_dir, "README.md"), "w") as readme:

   #     card = StringOperationGenerator.dataset_card(train_total, test_total,
   #                                       group_stats_table="\n".join(group_stats),
   #                                       metadata= "\n".join([f"{k}={v}" for k,v in about.items()]))

   #     readme.write(card)
   #     readme.close()