pharaouk lemonteaa commited on
Commit
8b305ec
·
verified ·
0 Parent(s):

Duplicate from lemonteaa/algorithmic-reasoning-seed

Browse files

Co-authored-by: Tom <[email protected]>

Files changed (4) hide show
  1. .gitattributes +54 -0
  2. Algorithm_Training.md +282 -0
  3. README.md +124 -0
  4. algorithmic-reasoning-seed.py +77 -0
.gitattributes ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.lz4 filter=lfs diff=lfs merge=lfs -text
12
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
13
+ *.model filter=lfs diff=lfs merge=lfs -text
14
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
15
+ *.npy filter=lfs diff=lfs merge=lfs -text
16
+ *.npz filter=lfs diff=lfs merge=lfs -text
17
+ *.onnx filter=lfs diff=lfs merge=lfs -text
18
+ *.ot filter=lfs diff=lfs merge=lfs -text
19
+ *.parquet filter=lfs diff=lfs merge=lfs -text
20
+ *.pb filter=lfs diff=lfs merge=lfs -text
21
+ *.pickle filter=lfs diff=lfs merge=lfs -text
22
+ *.pkl filter=lfs diff=lfs merge=lfs -text
23
+ *.pt filter=lfs diff=lfs merge=lfs -text
24
+ *.pth filter=lfs diff=lfs merge=lfs -text
25
+ *.rar filter=lfs diff=lfs merge=lfs -text
26
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
27
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ # Audio files - uncompressed
37
+ *.pcm filter=lfs diff=lfs merge=lfs -text
38
+ *.sam filter=lfs diff=lfs merge=lfs -text
39
+ *.raw filter=lfs diff=lfs merge=lfs -text
40
+ # Audio files - compressed
41
+ *.aac filter=lfs diff=lfs merge=lfs -text
42
+ *.flac filter=lfs diff=lfs merge=lfs -text
43
+ *.mp3 filter=lfs diff=lfs merge=lfs -text
44
+ *.ogg filter=lfs diff=lfs merge=lfs -text
45
+ *.wav filter=lfs diff=lfs merge=lfs -text
46
+ # Image files - uncompressed
47
+ *.bmp filter=lfs diff=lfs merge=lfs -text
48
+ *.gif filter=lfs diff=lfs merge=lfs -text
49
+ *.png filter=lfs diff=lfs merge=lfs -text
50
+ *.tiff filter=lfs diff=lfs merge=lfs -text
51
+ # Image files - compressed
52
+ *.jpg filter=lfs diff=lfs merge=lfs -text
53
+ *.jpeg filter=lfs diff=lfs merge=lfs -text
54
+ *.webp filter=lfs diff=lfs merge=lfs -text
Algorithm_Training.md ADDED
@@ -0,0 +1,282 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Question 1: Loop invariant
2
+
3
+ Given the code below:
4
+
5
+ ```python
6
+ mymax = 0
7
+ mymax_index = None
8
+ for i, n in enumerate(mylist):
9
+ if n > mymax:
10
+ mymax = n
11
+ mymax_index = i
12
+ ```
13
+
14
+ Uses loop invariant to prove that it computes the largest value in the list `mylist`, where mymax is the max value and mymax_index is the index in `mylist` where this maximum is attained. (Assume that all values in `mylist` is nonnegative and that `mylist` is non-empty)
15
+
16
+ --
17
+
18
+ *Thought:* Loop invariant is a technique to formally prove program correctness. It is a property of the program (e.g. predicate of its state as captured by the value of various variables) that remains true before and after/are preserved by each iteration. Once a loop invariant is discovered, we can often finish a proof of correctness by applying Mathematical induction to the predicate to prove the invariant - then after the whole loop ends the program state satisfies that predicate at the terminal value of the loop variable.
19
+
20
+ --
21
+
22
+ *Proof:*
23
+
24
+ Let $P(i)$ be the assertion that `mymax` is the maximum value in the sub-array `mylist[:i+1]` and that `mymax_index` is the index where such maximum is achieved.
25
+
26
+ Claim: At the end of the iteration at a particular value of i, `P(i)` is true.
27
+
28
+ We'll show this by induction.
29
+
30
+ Base case: `P(0)` is true after the first iteration because the list is non-empty, and `n == mylist[0]` is necessarily greater than or equal to 0 (initial value of `mymax`) from the assumption, so it will be updated correctly.
31
+
32
+ Induction case: Suppose `P(i)` is true at the end of iteration i. Then after the next iteration with i+1 ends `P(i+1)` is true because maximum of the sub-array `mylist[:i+2]` is the "maximum of maximum" - take the maximum of the sub-array `mylist[:i+1]` (which by induction hypothesis is equal to `mymax` at beginning of this iteration) and the value of `mylist[i+1]` (aka n), and then max. them together. More formally, `max(mylist[:i+2]) == max(mylist[:i+1] + mylist[i+1]) == max(max(mylist[:i+1]), mylist[i+1])`.
33
+
34
+ The correctness of the program as you stated follows from the claim simply by noticing that the statements we wish to prove coincide with `P(len(mylist)-1)`, which is true after the last iteration of the loop ends.
35
+
36
+ # Question 2: Linear merge
37
+
38
+ Show me the code in python to merge two sorted list into a sorted list in linear time.
39
+
40
+ --
41
+
42
+ *Thought:* This is a classic algorithm problem. The linear time constraint implies that there should be a very nice method as just reading the lists (which is information-theoretically necessary) already takes linear time. A naive method is to concatenate the list, then apply a O( n log n ) comparison sort, but it is too slow. We can improve by observing that the input itself satisfies some assumption/is nicely structured - namely that they are already sorted, and we should exploit that. Idea: How about try the "minimum of combined structure is minimum of minimum" idea? An example is the O(1) `find-min` in heap.
43
+
44
+ Let's try to develop the idea. We may say model it by recursion. Remembering that more advanced application of recursion to solve problem may involve trying to design our own state space to pass around in the recursion call, we may arrive at using the `reduce` pattern to build the final merged and sorted list by "extracting" the minimum of the combined list, then the minimum of the combined list with the first element removed, and so on:
45
+
46
+ ```python
47
+ def merge_sorted(list1, list2):
48
+ # Handle degenerate/terminal case
49
+ if not list1:
50
+ return list2
51
+ elif not list2:
52
+ return list1
53
+ else:
54
+ # min of min
55
+ if list1[0] < list2[0]:
56
+ return list1[0] + merge_sorted(list1[1:], list2)
57
+ else
58
+ return list2[0] + merge_sorted(list1, list2[1:])
59
+ ```
60
+
61
+ Once this is done, we may present a solution in imperative style simply by rewriting the above/unrolling it into loop/iteration:
62
+
63
+ ```python
64
+ def merge_sorted(list1, list2):
65
+ merged = []
66
+ i = 0
67
+ j = 0
68
+ while i < len(list1) and j < len(list2):
69
+ # Head of list comparison
70
+ if list1[i] < list2[j]:
71
+ merged.append(list1[i])
72
+ i += 1
73
+ else:
74
+ merged.append(list2[j])
75
+ j += 1
76
+ # Flush remainder of the list with leftovers
77
+ while i < len(list1):
78
+ merged.append(list1[i])
79
+ i += 1
80
+ while j < len(list2):
81
+ merged.append(list2[j])
82
+ j += 1
83
+ return merged
84
+ ```
85
+
86
+ # Question 3: Reverse list (Array)
87
+
88
+ Show me the code to reverse an ArrayList in Java without using libary functions.
89
+
90
+ --
91
+
92
+ *Thought:* This is an introductory programming exercise. A naive approach is to consruct a new list satisfying the requirement by copying from the original, but the question seems to imply that it want a mutative, or in-place answer. Generally speaking, we should be careful to avoid overwriting/state/information loss. We may do so by analyzing the data dependencies. Let the original list be `mylist` and the reversed list be `mylist_r`. Then for all `0 <= i < n`, we have `mylist[i] == mylist_r[n-1-i]`.
93
+
94
+ # Question 4: Index notation reasoning (0)
95
+
96
+ Suppose `x` is a list of length `n`. What is the last element of `x`? Hence show me code to loop through each element of the list.
97
+
98
+ --
99
+
100
+ The last element of `x` is `x[n-1]`. This is a known feature of zero-based indexing and can be reasoned as follows: Suppose instead we have one-based indexing, that is, elements start from index 1. In that case the number of element is obviously the index of the last element due to counting: `x[1], x[2], ... , x[n]` contains n elements. However, for zero-based indexing there is an "extra" element at `x[0]`, hence a list whose last element is `x[n]` will have n+1 element. Therefore, to have n element, the index of last element should be subtracted by one.
101
+
102
+ In view of the answer above, one way to loop through the list is:
103
+
104
+ ```
105
+ for (int i = 0; i < n; i++) {
106
+ //Do something with x[i]
107
+ }
108
+ ```
109
+
110
+ This works because when `i == n-1`, the condition `i < n` is still satisfied, so we will undergo one final iteration for the last element of `x`. But after that, `i++` is executed leading to `i == n`, the condition is violated, so the loop exits as intended.
111
+
112
+ Notice that since i and n are both integers, `i < n` can be replaced with `i <= n-1`. However humans generally find the version presented above more natural as the numbers themselves are not changed. This pattern also lead to adopting a convention of the semi-open interval: closed on the left, open on the right, so that something like `range(a,b)` in python refers to $[a, b)$, or inclusive of `a` but exclusive of `b`.
113
+
114
+ # Question 5: Index notation reasoning (1)
115
+
116
+ Suppose `b` is a reversed list of `a` and both list have length `n`. Formally state a property of the two lists in relation to each other. Show the details of your work while working with index notations.
117
+
118
+ --
119
+
120
+ *Thought:* Let's try to work out some initial cases to see if there is a pattern. Since `b` is a reversed list, the last element of `b` is the first element of `a`, formally: `b[n-1] == a[0]`. Notice the -1 is due to zero-based indexing. In a similar way, the second-to-last element of `b` is the second element of `a`, or `b[n-2] == a[1]`. The indices are obtained by adding one to the index of `a`, while subtracting one from the index of `b`. As this is a linear pattern, it suggests that the index expression should be a linear function of `i`, and the coefficient should be +1 for `a` (incrementing) and -1 for `b` (decrementing). Indeed we can see that `b[n-1-i] == a[i]` is the right answer: its general pattern is a reflection which is what a reversal mean, while the edge cases (`i == 0`, `i == 1`, `i == n-1`) are all corrects by substituting and then doing an explicit check.
121
+
122
+
123
+
124
+ # Question 6: Reverse list (Linked list)
125
+
126
+ Show me the code to reverse a LinkedList in Java without using library functions.
127
+
128
+ --
129
+
130
+ *Thought:* This is an introductory programming exercise. Suppose we go for an answer using imperative style and mutate in-place. We may visualize the process as a surgery as we walk over the list from left to right, moving the next pointer to point back at the previous element as we move forward. As is common in imperative paradigm, we must be careful of state loss problem, especially as we must not loss access to both the remaining part of the list, and the portion of the list we have already walked over. We may achieve this by maintaining backup/extra pointers to let us retain access.
131
+
132
+
133
+ # Question 7: Reverse list (recursion)
134
+
135
+ Show me the code to reverse a list in functional style.
136
+
137
+ --
138
+
139
+
140
+ # Question 8: Higher order function
141
+
142
+ Show me the code in functional style to get a list of the square of all numbers greater than 10 in another list.
143
+
144
+ --
145
+
146
+
147
+ # Question 9: Theory of functional style
148
+
149
+ Convert the following code that used recursion into one using higher order function instead:
150
+
151
+ --
152
+
153
+ # Question 10: Mergesort proof
154
+
155
+ Give me a proof of correctness for mergesort.
156
+
157
+ --
158
+
159
+
160
+ # Question 11: Bubblesort
161
+
162
+ Explain the bubblesort algortihm to me, and give a proof of correctness. Then analyze its runtime complexity.
163
+
164
+ --
165
+
166
+
167
+ # Question 12: Bit-manipulation tricks (1)
168
+
169
+ Write a C macro to count the number of ones in the bits of an integer using a single C expression.
170
+
171
+ --
172
+
173
+
174
+ # Question 13: Bit-manipulation tricks (2)
175
+
176
+ Write a C macro to compute the logarithm of an integer (rounded to integer) using a single C expression.
177
+
178
+ --
179
+
180
+ # Question 14: Amortized analysis
181
+
182
+ Use the potential method to prove that a sequence of insertion operation to a dynamic array/list has O(1) amortized runtime.
183
+
184
+ --
185
+
186
+
187
+ # Question 15: Index notation reasoning (2)
188
+
189
+ A virtual view into an array is a remapping of index to give the illusion of having a separate array when in fact both share the same storage. As example, suppose there is a list `under` of length `n`. We may then say have a virtual array `virtual` from `i` (inclusive) to `j` (exclusive). What this means is that the first element of `virtual` is `under[i]`, the second element is `under[i+1]`, etc, all the way to the last element `under[j-1]` (due to exclusive). Given this info, give me an expression relating the index of `virtual` to `under` and show your work. Also give concrete example of this concept implemented in programming language.
190
+
191
+ --
192
+
193
+ # Question 16: Index notation reasoning (3)
194
+
195
+ Consider the slice functionality in python to get a virtual view into a list/array. Suppose we are now in a pseudocode language, show me the code to expand this function to enable wrap-around - that is, we may begin at some element, go to the last element, then continue back at the first element. Ensure that your code handle all edge cases.
196
+
197
+ --
198
+
199
+ # Question 17: Index notation reasoning (4)
200
+
201
+ Suppose there is a two-dimensional array `A[i][j]` representing a matrix. Suppose this is an upper triangular matrix. Show me the code to iterate through each element.
202
+
203
+ --
204
+
205
+
206
+ # Question 18: Indexing locality
207
+
208
+ In C, expand `A[i][j]` into low level pointer code to access the correct memory address. Explain the implication it has.
209
+
210
+ --
211
+
212
+
213
+
214
+ # Question 19: Index notation reasoning (5)
215
+
216
+ Rewrite the following code exchanging the inner and outer loop:
217
+
218
+ --
219
+
220
+
221
+ # Question 20: Index notation reasoning (6)
222
+
223
+ Recall that a (balanced) band matrix is a matrix whose only non-zero entries are on diagonals within a small distance of the main or central diagonal. Formally, for a `n` by `n` square matrix `A[i][j]`, the entries are non-zero only if $| i - j | <= k$. Given these info, show me the code to iterate through all non-zero elements of `A`.
224
+
225
+ --
226
+
227
+ # Question 21: Tensor?
228
+
229
+
230
+ # Question 22: Tail recursion
231
+
232
+ Given the following implementation of the higher order function `filter`:
233
+
234
+ Rewrite the code so that it is tail recursive.
235
+
236
+ --
237
+
238
+ # Question 23: Tree recursion (easy)
239
+
240
+ Show me the code to compute the height of a tree using recursion.
241
+
242
+ --
243
+
244
+
245
+ # Question 24: Tree recursion
246
+
247
+ Given a tree whose leaf nodes contain a numeric value, show me the code to compute the weighted sum of all these value, where the weight is the depth of that leaf node.
248
+
249
+ --
250
+
251
+ # Question 25: Linked list
252
+
253
+ Show me the code to insert a circular doubly linked list into another linked list in O(1) time.
254
+
255
+ --
256
+
257
+ # Question 26: String Join
258
+
259
+ Show me the code in Java to join a list of string with a separator string given. Reimplement directly using primitives instead of using library function. Signature is `public String myjoin(List<String> strs, String sep)`.
260
+
261
+ Example: TODO (Show normal case, 1 string, and 0 string)
262
+
263
+ --
264
+
265
+ # Question 27: Set using Hashmap
266
+
267
+ Show me the code to reimplement set and basic operations ourselves, including "intersection", "union", and "subtract", using Hashmap.
268
+
269
+ --
270
+
271
+ # Question 28: Remove duplicates inplace
272
+
273
+ Show me the code to remove duplicate items in a list in-place - that is you should modify the input list directly.
274
+
275
+
276
+
277
+
278
+
279
+
280
+
281
+
282
+
README.md ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ task_categories:
4
+ - text-generation
5
+ - question-answering
6
+ language:
7
+ - en
8
+ tags:
9
+ - code
10
+ size_categories:
11
+ - n<1K
12
+ ---
13
+ # Dataset Card for Algorithmic Reasoning (seed)
14
+
15
+ **Note: This dataset is WIP and most question's answer section is empty or incomplete! See also "Other Known Limitations" section**
16
+
17
+ **Warning: If you somehow do use this dataset, remember to NOT do any eval after training on the questions in this dataset!**
18
+
19
+ ## Dataset Description
20
+
21
+ - **Homepage:**
22
+ - **Repository:**
23
+ - **Paper:**
24
+ - **Leaderboard:**
25
+ - **Point of Contact:** [email protected] or https://github.com/lemonteaa
26
+
27
+ ### Dataset Summary
28
+
29
+ Dataset to help LLM learn how to reason about code, especially on algorithmic tasks, by seeing human demostration.
30
+
31
+ ### Supported Tasks and Leaderboards
32
+
33
+ [More Information Needed]
34
+
35
+ ### Languages
36
+
37
+ [More Information Needed]
38
+
39
+ ## Dataset Structure
40
+
41
+ ### Data Instances
42
+
43
+ [More Information Needed]
44
+
45
+ ### Data Fields
46
+
47
+ - Question title
48
+ - Question
49
+ - Thought - Internal thought process that reason step by step/in an organized manner
50
+ - Answer presented to user (proof or code) - with explanation if necessary
51
+
52
+ ### Data Splits
53
+
54
+ No split as of now - all are in the training section.
55
+
56
+ ## Dataset Creation
57
+
58
+ ### Curation Rationale
59
+
60
+ [More Information Needed]
61
+
62
+ ### Source Data
63
+
64
+ #### Initial Data Collection and Normalization
65
+
66
+ Questions are those I personally remember in my career, selected based on:
67
+
68
+ - interesting
69
+ - involving CS, math, or similar knowledge
70
+ - Target specific known weaknesses of existing open source/source available LLM (eg index notation handling)
71
+ - pratical/likely to appear in production work settings
72
+
73
+ #### Who are the source language producers?
74
+
75
+ [More Information Needed]
76
+
77
+ ### Annotations
78
+
79
+ #### Annotation process
80
+
81
+ Manually created by me entirely, writing in a level of details exceeeding what usually appears on the internet (bootcamp/FANNG interview prep/leetcode style training website etc) to help AI/LLM access knowledge that may be too obvious to human to write down.
82
+
83
+ #### Who are the annotators?
84
+
85
+ [More Information Needed]
86
+
87
+ ### Personal and Sensitive Information
88
+
89
+ None as they are general, objective knowledge.
90
+
91
+ ## Considerations for Using the Data
92
+
93
+ ### Social Impact of Dataset
94
+
95
+ Although it is doubtful this dataset can actually work, in the event it does this may result in enhancing coding capability of LLM (which is intended), but which may create downstream effect simply due to LLM capability enhancement.
96
+
97
+ ### Discussion of Biases
98
+
99
+ As questions are selected partly based on my taste, areas in CS that I am not interested in may be underrepresented.
100
+
101
+ ### Other Known Limitations
102
+
103
+ - While I try to cover various mainstream programming language, each problem target only one specific language.
104
+ - It is currently in free-style markdown file. Maybe could make a script to convert to more structured format.
105
+ - Questions are asked in a conversational tone instead of leetcode style with strict I/O specification, hence may be more suitable for human eval than automated eval (eg extract and run code output in sandbox against test case automatically).
106
+ - As the dataset is completely manually created by a single human, the dataset size is extremely small.
107
+
108
+ ## Additional Information
109
+
110
+ ### Dataset Curators
111
+
112
+ [More Information Needed]
113
+
114
+ ### Licensing Information
115
+
116
+ MIT
117
+
118
+ ### Citation Information
119
+
120
+ [More Information Needed]
121
+
122
+ ### Contributions
123
+
124
+ [More Information Needed]
algorithmic-reasoning-seed.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datasets
2
+ from enum import Enum
3
+
4
+ class ParseState(Enum):
5
+ QUESTION = 1
6
+ THOUGHT = 2
7
+ ANSWER = 3
8
+
9
+ logger = datasets.logging.get_logger(__name__)
10
+
11
+ _DESCRIPTION = "TODO"
12
+
13
+ _URLS = {"train":"Algorithm_Training.md"}
14
+
15
+ class AlgorithmicReasoningSeed(datasets.GeneratorBasedBuilder):
16
+ def _info(self):
17
+ return datasets.DatasetInfo(
18
+ description=_DESCRIPTION,
19
+ features=datasets.Features(
20
+ {
21
+ "id": datasets.Value("string"),
22
+ "title": datasets.Value("string"),
23
+ "question": datasets.Value("string"),
24
+ "thought": datasets.Value("string"),
25
+ "answer": datasets.Value("string")
26
+ }
27
+ ),
28
+ # No default supervised_keys (as we have to pass both question
29
+ # and context as input).
30
+ supervised_keys=None,
31
+ homepage="",
32
+ citation=""
33
+ )
34
+
35
+ def _split_generators(self, dl_manager):
36
+ downloaded_files = dl_manager.download_and_extract(_URLS)
37
+ return [datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]})
38
+ ]
39
+
40
+ def _generate_examples(self, filepath):
41
+ """This function returns the examples in the raw (text) form."""
42
+ logger.info("generating examples from = %s", filepath)
43
+ key = 0
44
+ with open(filepath, encoding="utf-8") as f:
45
+ title = ""
46
+ question = []
47
+ thought = []
48
+ answer = []
49
+ first = True
50
+ state = ParseState.QUESTION
51
+ for line in f:
52
+ if len(line) >= 1 and line[0] == '#':
53
+ if not first:
54
+ logger.info("question id=%d, title=%s", key, title)
55
+ yield key, {"id":key, "title": title, "question":'\n'.join(question), "thought":'\n'.join(thought), "answer":'\n'.join(answer)}
56
+ first = False
57
+ key += 1
58
+ title = line.split(':')[1].strip()
59
+ question = []
60
+ thought = []
61
+ answer = []
62
+ state = ParseState.QUESTION
63
+ continue
64
+ if len(line) >= 2 and line[0:2] == '--':
65
+ if state == ParseState.QUESTION:
66
+ state = ParseState.THOUGHT
67
+ elif state == ParseState.THOUGHT:
68
+ state = ParseState.ANSWER
69
+ continue
70
+ if state == ParseState.QUESTION:
71
+ question.append(line)
72
+ elif state == ParseState.THOUGHT:
73
+ thought.append(line)
74
+ else:
75
+ answer.append(line)
76
+ yield key, {"id":key, "title": title, "question":'\n'.join(question), "thought":'\n'.join(thought), "answer":'\n'.join(answer)}
77
+ logger.info("finished.")