File size: 7,723 Bytes
588b278
f31ce64
588b278
5a078e2
2a5f049
52e8844
9be9357
2a5f049
 
 
 
 
 
 
 
 
 
 
9be9357
 
 
52e8844
 
9be9357
 
 
52e8844
 
dacc5cf
52e8844
 
 
dacc5cf
52e8844
dacc5cf
52e8844
 
dacc5cf
52e8844
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59a1bbb
 
 
 
 
 
 
 
 
 
 
 
 
eb958ac
52e8844
 
 
dacc5cf
52e8844
 
 
 
 
 
 
 
eb958ac
52e8844
 
 
 
 
 
 
 
 
 
 
 
eb958ac
52e8844
 
 
 
 
 
 
 
 
 
 
 
7ad97e1
 
 
 
 
 
 
 
 
59a1bbb
 
 
 
 
 
52e8844
 
 
9be9357
 
 
 
 
 
 
 
 
 
52e8844
1790b31
9be9357
 
 
52e8844
9be9357
 
 
 
 
f31ce64
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
---
license: apache-2.0
---

# ๐Ÿš€ GSM8K training set
The original answer is "\n#### Value" and now is "\n#### Value\nThe answer is: Value", and the answer is cleaned, which is **consistent with the answer format with "meta-math/MetaMathQA"**. 

## ๐Ÿ’ป Dataset Usage
Run the following command to load the data:
```python
from datasets import load_dataset

dataset = load_dataset("shuyuej/metamath_gsm8k")
dataset = dataset['train']
print(dataset)
```

# ๐Ÿ“ Dataset modification codes
```python
# coding=utf-8

import re

import jsonlines
from datasets import load_dataset, Features, Value


def clean_up(sentence):
    # Find all the locations of "<<"
    matches = [match.start() for match in re.finditer(r'<<', sentence)]

    for match in matches:
        # Get the left 20 characters of each "<<"
        left_chars = sentence[match-20:match]
        # Replace "x" or "X" to "*" if they are in the left 20 characters
        modified_chars = sentence[match-20:match].replace('x', '*').replace('X', '*')

        # Modify the original sentence
        if 'x' in left_chars or 'X' in left_chars:
            sentence = sentence.replace(left_chars, modified_chars)

    ##############################################################################################################

    # Define a pattern to match text between "<< and >>"
    pattern = r"<<(.*?)>>"

    # Use re.sub to replace matched patterns with an empty string
    sentence = re.sub(pattern, "", sentence)

    ##############################################################################################################
    # Find all occurrences of "*"
    asterisks = [i for i, char in enumerate(sentence) if char == '*']

    # Check and add spaces around "*"
    for index in reversed(asterisks):
        if index > 0 and index < len(sentence) - 1 and sentence[index - 1] != ' ' and sentence[index + 1] != ' ':
            sentence = sentence[:index] + ' ' + sentence[index] + ' ' + sentence[index + 1:]
        elif index > 0 and index < len(sentence) - 1 and sentence[index - 1] != ' ' and sentence[index + 1] == ' ':
            sentence = sentence[:index] + ' ' + sentence[index] + sentence[index + 1:]
        elif index > 0 and index < len(sentence) - 1 and sentence[index - 1] == ' ' and sentence[index + 1] != ' ':
            sentence = sentence[:index] + sentence[index] + ' ' + sentence[index + 1:]

    ##############################################################################################################
    # # Find all occurrences of "/"
    # asterisks = [i for i, char in enumerate(sentence) if char == '/']
    #
    # # Check and add spaces around "/"
    # for index in reversed(asterisks):
    #     if index > 0 and index < len(sentence) - 1 and sentence[index - 1] != ' ' and sentence[index + 1] != ' ':
    #         sentence = sentence[:index] + ' ' + sentence[index] + ' ' + sentence[index + 1:]
    #     elif index > 0 and index < len(sentence) - 1 and sentence[index - 1] != ' ' and sentence[index + 1] == ' ':
    #         sentence = sentence[:index] + ' ' + sentence[index] + sentence[index + 1:]
    #     elif index > 0 and index < len(sentence) - 1 and sentence[index - 1] == ' ' and sentence[index + 1] != ' ':
    #         sentence = sentence[:index] + sentence[index] + ' ' + sentence[index + 1:]

    ##############################################################################################################
    # Find all occurrences of "+"
    asterisks = [i for i, char in enumerate(sentence) if char == '+']

    # Check and add spaces around "+"
    for index in reversed(asterisks):
        if index > 0 and index < len(sentence) - 1 and sentence[index - 1] != ' ' and sentence[index + 1] != ' ':
            sentence = sentence[:index] + ' ' + sentence[index] + ' ' + sentence[index + 1:]
        elif index > 0 and index < len(sentence) - 1 and sentence[index - 1] != ' ' and sentence[index + 1] == ' ':
            sentence = sentence[:index] + ' ' + sentence[index] + sentence[index + 1:]
        elif index > 0 and index < len(sentence) - 1 and sentence[index - 1] == ' ' and sentence[index + 1] != ' ':
            sentence = sentence[:index] + sentence[index] + ' ' + sentence[index + 1:]

    ##############################################################################################################
    # Find all occurrences of "-"
    asterisks = [i for i, char in enumerate(sentence) if char == '-']

    # Check and add spaces around "-"
    for index in reversed(asterisks):
        if index > 0 and index < len(sentence) - 1 and sentence[index - 1] != ' ' and sentence[index + 1] != ' ':
            sentence = sentence[:index] + ' ' + sentence[index] + ' ' + sentence[index + 1:]
        elif index > 0 and index < len(sentence) - 1 and sentence[index - 1] != ' ' and sentence[index + 1] == ' ':
            sentence = sentence[:index] + ' ' + sentence[index] + sentence[index + 1:]
        elif index > 0 and index < len(sentence) - 1 and sentence[index - 1] == ' ' and sentence[index + 1] != ' ':
            sentence = sentence[:index] + sentence[index] + ' ' + sentence[index + 1:]

    ##############################################################################################################
    # Find all occurrences of "="
    asterisks = [i for i, char in enumerate(sentence) if char == '=']

    # Check and add spaces around "="
    for index in reversed(asterisks):
        if index > 0 and index < len(sentence) - 1 and sentence[index - 1] != ' ' and sentence[index + 1] != ' ':
            sentence = sentence[:index] + ' ' + sentence[index] + ' ' + sentence[index + 1:]
        elif index > 0 and index < len(sentence) - 1 and sentence[index - 1] != ' ' and sentence[index + 1] == ' ':
            sentence = sentence[:index] + ' ' + sentence[index] + sentence[index + 1:]
        elif index > 0 and index < len(sentence) - 1 and sentence[index - 1] == ' ' and sentence[index + 1] != ' ':
            sentence = sentence[:index] + sentence[index] + ' ' + sentence[index + 1:]

    ##############################################################################################################
    # Find all occurrences of "."
    dots_locations = [match.start() for match in re.finditer(r'\.', sentence)]

    # Check and modify "." if the left side is space and the right side is a numerical number
    for dot_location in reversed(dots_locations):
        if sentence[dot_location - 1].isspace() and sentence[dot_location + 1].isdigit():
            sentence = sentence[:dot_location] + '0' + sentence[dot_location:]

    ##############################################################################################################
    # Check if there is a "." before "\n#### "
    if ".\n#### " not in sentence:
        # If not, add a "."
        sentence = sentence.replace("\n#### ", ".\n#### ")

    return sentence


# Retrieve the path of training and testing databases
context_feat = Features({"question": Value(dtype='string', id=None), "answer": Value(dtype='string', id=None)})
train_set = load_dataset('json', data_files='train.jsonl', split='train', features=context_feat)

data = []
for example in train_set:
    number = example['answer'].split('#### ')[1]
    number = int(number.replace(',', ''))
    append = "\nThe answer is: " + str(number)
    answer = example['answer'] + append
    answer = clean_up(sentence=answer)

    question = example['question']
    data.append({"question": question, "answer": answer})

# Save the modified data to a jsonl file
output_file = 'gsm8k_train.jsonl'
with jsonlines.open(output_file, 'w') as writer:
    writer.write_all(data)

print(f"Modified data saved to {output_file}")
```