-
Notifications
You must be signed in to change notification settings - Fork 0
/
ItemGenTest 0.1.5.R
340 lines (258 loc) · 12.2 KB
/
ItemGenTest 0.1.5.R
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
rm(list = ls(all = TRUE))
packages <- c('stringr', 'Formula', 'stats')
package.check <- lapply(packages, FUN = function(x) {
if (!require(x, character.only = TRUE)) {
install.packages(x, dependencies = TRUE)
library(x, character.only = TRUE)
}
})
itemgen <- function(stem_text = stem_text, formulae = formulae, N = N, C, options_affix, ans_key, save.csv) {
## Check for the default inputs
if(missing(stem_text) || missing(formulae) || missing(N)) {
stop("Inputs must be provided for all the default arguments: 'stem_text', 'formulae' and 'N'")
}
## Construct a data frame for the available inputs
(num_inputs <- expand.grid(N))
if (!missing(C)) {
char_inputs <- list()
for (i in 1:length(C)) {
char_inputs[[i]] <- rep(C[[i]], nrow(num_inputs))[1 : nrow(num_inputs)]
}
inputs_frame <- data.frame(char_inputs, num_inputs)
names(inputs_frame) <- c(names(C), names(N))
} else {
inputs_frame <- data.frame(num_inputs)
names(inputs_frame) <- names(N)
}
## Extract formulae for correct answer and distractors
(formulae_split <- trimws(unlist(strsplit(formulae, split = '\n'))))
(response_split <- stringr::str_subset(formulae_split, '[~?]'))
(support_values <- stringr::str_flatten(formulae_split[stringr::str_detect(formulae_split, '[?~]') == F], '\n'))
if(length(support_values) != 0){eval(parse(text = support_values))}
## Extract correct answer key
(correct_optn <- response_split[grep('~', response_split)])
if(missing(ans_key) && length(correct_optn) == 1){
correct_key <- trimws(gsub("\\~.*", "", correct_optn))
} else if(!missing(ans_key) && length(correct_optn) == 0){
correct_key <- ans_key
} else {
stop("Write formula for the correct option using single '~' symbol within formulae OR declare ans_key as function argument.")
}
## Extract working function per option from formulae
response_functions <- c()
for (i in 1 : length(response_split)){
response_functions[i] <- stringr::str_split(response_split, '[~?]')[[i]][2]
}
## Extract Option names from the formulae
option_names <- c()
for (i in 1 : length(response_split)){
option_names[i] <- trimws(stringr::str_split(response_split, '[~?]')[[i]][1])
}
## Construct inputs model for the items and calculate the options values per item
(response_functions <- trimws(response_functions))
if (missing(C)) {
## Introduce a dummy column 'c1'
(c1 <- sample(LETTERS, nrow(inputs_frame), rep = T))
(inputs_frame <- data.frame(c1 = c1, inputs_frame))
(model_formulae <- as.formula(stringr::str_flatten(c('c1 ~ .', paste('I(' , response_functions , ')')), '|')))
F1 <- Formula::Formula(model_formulae)
inputs_model <- model.frame(F1, data = inputs_frame)
inputs_model <- inputs_model[, -(which(colnames(inputs_model) == 'c1'))]
inputs_frame <- data.frame(inputs_frame[, -(which(colnames(inputs_frame) == 'c1'))])
names(inputs_frame) <- names(N)
} else if (!missing(C)) {
(model_formulae <- as.formula(stringr::str_flatten(c('c1 ~ .', paste('I(' , response_functions , ')')), '|')))
F1 <- Formula::Formula(model_formulae)
inputs_model <- do.call(rbind, lapply(seq_len(nrow(inputs_frame)), function(i) stats::model.frame(F1, data = inputs_frame[i,])))
}
## Check whether individual formula model per option are different to each other
if (ncol(inputs_model) == ncol(inputs_frame)+length(option_names)) {
colnames(inputs_model) <- c(colnames(inputs_frame), option_names)
} else {
stop('Formulae-models used for the options must be distinct per option and each option-formula must produce single numeric value.')
}
## Attach the affixes to the options
if (!missing(options_affix)) {
if (all.equal(option_names, names(options_affix)[1 : length(option_names)]) == TRUE) {
for (i in option_names) {
inputs_model[[i]] <- paste0(options_affix[[i]][1], inputs_model[[i]], options_affix[[i]][2])
}
} else {
stop("Affixes are NOT mapped sequentially to same option names specified within formulae")
}
## Add options with only text if there is any
if (length(options_affix) > length(option_names)) {
for (i in names(options_affix[(length(option_names)+1) : length(options_affix)])) {
inputs_model[[i]] <- rep(options_affix[[i]], nrow(inputs_model))
}
}
}
## Break the stem text in words and extract variables
(stem_words <- unlist(strsplit(stem_text, split = ' ')))
(vrble_positions <- intersect(grep('\\[', stem_words), grep('\\]', stem_words)))
(raw_vrbles <- unique(stem_words[vrble_positions]))
## Trace the positions in the stem text for replacements by the inputs
replace_stem_positions <- list()
for (j in 1 : ncol(inputs_frame)) {
replace_stem_positions[[j]] <- grep('\\TRUE', str_detect(tolower(stem_words), colnames(inputs_frame)[j]))
}
## Generate the stem clones by replacing variables by the inputs
if (length(inputs_frame) == length(raw_vrbles)) {
stemclones <- c()
for (i in 1 : nrow(inputs_frame)) {
for (j in 1 : length(replace_stem_positions)) {
stem_words[replace_stem_positions[[j]]] <- as.character(inputs_frame[i, j])
}
stemclones[i] <- stringr::str_flatten(stem_words, ' ')
}
} else {
stop("Declared variables in stem should be kept in the format of [.] without space inside
and declared variables in stem must be similar to the input variables")
}
## Sort out all the new items along with calculated options and correct answer key
newitems <- data.frame(Stem = stemclones, inputs_model[, -c(1 : length(inputs_frame))], Answer_Key = rep(correct_key, length(stemclones)))
rownames(newitems) <- paste0('[Q', 1 : nrow(newitems), ']')
## Save a .csv file of all the new items in working directory
time_now <- gsub('\\:', '..', Sys.time())
if (!missing(save.csv)){
write.csv(newitems, paste(save.csv, time_now, '.csv'))
message('Newly generated items have been saved in a .csv file in the working directory')
}
## View the generated items
return(t(newitems))
}
#################################################
## Example schema
stem_text <- 'What is the sum of first [N1] natural numbers ?'
n1 <- c(5, 8, 11, 14, 17)
N <- list(n1 = n1)
formulae <- "Option_A ? 2*n1-1
Option_B ? 3*n1-2
Option_C ~ n1*(n1+1)/2
Option_D ? n1*(n1-1)/2
"
itemgen(stem_text = stem_text, formulae = formulae, N = N)
################################################
## Example schema
stem_text <- '[C1] has $[N1] and [C2] has $ [N2] . If [C2] takes $ [N3] from [C1] later, then how much more amount than [C1] does [C3] have now?'
c1 <- c('Sam', 'Sean')
c2 <- c('Max', 'Martha', 'Mandy')
c3 <- c('he', 'she', 'she')
n1 <- c(4, 5, 6, 7)
n2 <- c(8, 9, 10)
n3 <- c(2, 3)
C <- list(c1 = c1, c2 = c2, c3 = c3)
N <- list(n1 = n1, n2 = n2, n3 = n3)
formulae <- 'Option_A ? (n1 + n2)
Option_B ~ (n2 + 2*n3 - n1)
Option_C ? (n1 + n2 + 1)
Option_D ? (n1 + n2 - 2)
'
## Attach prefix/suffix in the options and insert options with text only if there is any.
options_affix <- list(Option_A = c('$ ', ''), Option_B = c('$ ', ''), Option_C = c('$ ', ''), Option_D = c('$ ', ''), Difficulty = 'Easy')
## itemgen() function can be used as
itemgen(stem_text = stem_text, formulae = formulae, N = N, C = C)
itemgen(stem_text = stem_text, formulae = formulae, N = N, C = C, options_affix = options_affix)
itemgen(stem_text = stem_text, formulae = formulae, N = N, C = C, options_affix, save.csv = 'New Items')
## Example of Error detection
itemgen(stem_text = stem_text, formulae = formulae, N = N, C = C, options_affix, ans_key = 'Option_E')
################################################
## Example schema
stem_text <- "A [C1] was delayed somewhere for [N1] minutes but made up for the delay on a section of [N2] km travelling at a speed of [N3] km per hour higher than that which accorded the schedule. The speed of the [C1] which accorded the schedule, was"
c1 <- c('car', 'bus', 'truck', 'train')
n1 <- c(16, 18, 20, 22, 24)
n2 <- c(80, 90, 100, 110)
n3 <- c(10, 12, 15, 18)
C <- list(c1 = c1)
N <- list(n1 = n1, n2 = n2, n3 = n3)
formulae <- 'p <- 1
Option_A ~ round((-n3 + sqrt(n3^2 - 4*p*(-60*n2*n3/n1)))/2,2)
Option_B ? round((-n3 - sqrt(n3^2 - 4*p*(-60*n2*n3/n1)))*(-1)/2-20,2)
Option_C ? round((-n3 - sqrt(n3^2 - 4*p*(-60*n2*n3/n1)))*(-1)/2,2)
Option_D ? round((-n3 + sqrt(n3^2 - 4*p*(-60*n2*n3/n1)))/2+30,2)
'
## Attach prefix/suffix in the options and insert options with text only if there is any.
options_affix <- list(Option_A = c('', ' km/hr'), Option_B = c('', ' km/hr'), Option_C = c('', ' km/hr'), Option_D = c('', ' km/hr'), Option_E = 'Cannot be determined')
## itemgen() function can be used as
itemgen(stem_text = stem_text, formulae = formulae, N = N, C = C)
itemgen(stem_text = stem_text, formulae = formulae, N = N, C = C, options_affix = options_affix)
itemgen(stem_text = stem_text, formulae = formulae, N = N, C = C, options_affix = options_affix, save.csv = 'New Items')
#################################################
## Example schema
stem_text <- '[C1] bought a [C2] at $ [N1] . [C3] spent $ [N2] for repairing it and then sold it at $ [N3] . What was [C4] percentage of profit or loss?'
c1 <- c('Samuel', 'April')
c2 <- c('motorcycle', 'moped')
c3 <- c('He', 'She')
c4 <- c('his', 'her')
n1 <- c(925, 862, 784)
n2 <- c(92, 102)
n3 <- 1030
C <- list(c1 = c1, c2 = c2, c3 = c3, c4 = c4)
N <- list(n1 = n1, n2 = n2, n3 = n3)
formulae <- 'Option_A ? round((n2/n1)*100, 2)
Option_B ? round(((n3-n2-n1)/n3)*100, 1)
Option_C ? round(((n3-n2-n1+0)/n3)*100, 1)
Option_D ~ round((((n3-n2-n1)/(n1+n2))*100), 2)
'
## Attach prefix/suffix in the options and insert options with text only if there is any.
options_affix <- list(Option_A = c('', '% loss'), Option_B = c('', '% profit'), Option_C = c('', '% loss'), Option_D = c('', '% profit'), Option_E = 'No profit no loss')
## itemgen() function can be used as
itemgen(stem_text = stem_text, formulae = formulae, C = C, N = N, options_affix = options_affix)
itemgen(stem_text = stem_text, formulae = formulae, C = C, N = N, options_affix = options_affix, save.csv = 'New Items')
## Example of Error detection
formulae <- 'Option_A ? round((n2/n1)*100, 2)
Option_B ? round(((n3-n2-n1)/n3)*100, 1)
Option_C ? round(((n3-n2-n1)/n3)*100, 1)
Option_D ~ round((((n3-n2-n1)/(n1+n2))*100), 2)
'
itemgen(stem_text = stem_text, formulae = formulae, C = C, N = N, options_affix)
#################################################
## Example schema
stem_text <- 'Sum of present ages of [C1] and [C2] father is [N1] . After [N2] years [C2] father will be thrice as old as [C1] . The present age of [C2] father is'
n1 <- c(74,80,72,68)
n2 <- c(8,10)
c1 <- c('Sophia', 'Viktor')
c2 <- c('her', 'his')
N <- list(n1 = n1, n2 = n2)
C <- list(c1 = c1, c2 = c2)
formulae <- 'a <- 5
sol <- function(x, y){
A <- matrix(c(1, 1, 1, -3), nrow=2)
B <- matrix(c(x, 2*y), nrow=2)
return((as.matrix(solve(A)%*%B)))
}
Option_A ? round(sol(n1, n2)[1,]+a, 2)
Option_B ? round(sol(n1, n2)[2,]+a, 2)
Option_C ~ round(sol(n1, n2)[1,], 2)
Option_D ? round(sol(n1, n2)[2,], 2)
'
itemgen(stem_text = stem_text, formulae = formulae, N = N, C = C)
#################################################
## Example Schema
stem_text <- "What is the sum of first [N1] [C1] ?"
n1 <- c(5, 8, 11, 14, 17)
c1 <- c("natural numbers", "non-zero positive integers")
N <- list(n1 = n1)
C <- list(c1 = c1)
formulae <- "Option_A ? 2*n1-1
Option_B ? 3*n1-2
Option_C ? n1*(n1+1)/2
Option_D ? n1*(n1-1)/2
"
options_affix <- list(Option_A = c("Sum value = ", ""), Option_B = c("Sum value = ", ""), Option_C = c("Sum value = ", ""), Option_D = c("Sum value = ", ""))
itemgen(stem_text = stem_text, formulae = formulae, N = N, C = C, ans_key = "Option_C", options_affix = options_affix)
#################################################
## Example schema
stem_text <- "The sum value of all the odd [C1] between [N1] and [N2] is"
n1 <- c(20, 24, 28, 32)
n2 <- c(48, 52, 56)
c1 <- c("natural numbers", "integers")
N <- list(n1 = n1, n2 = n2)
C <- list(c1 = c1)
formulae <- "Option_A ? sum((n1+1) : (n2-1))/2\n
Option_B ~ (length(seq(n1+1, n2-1, by = 2)))*(n1+n2)/2\n
Option_C ? sum(n1 : n2)/2\n
Option_D ? (length(seq(n1, n2, by = 2)))*(n1+n2)/2\n
"
options_affix <- list(Option_A = c("", ""), Option_B = c("", ""), Option_C = c("", ""), Option_D = c("", ""), Difficulty = "MEDIUM")
itemgen(stem_text = stem_text, formulae = formulae, N = N, C = C, options_affix = options_affix)