-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMathQuestionService.php
182 lines (157 loc) · 5.38 KB
/
MathQuestionService.php
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
<?php
/**
* ZnZend
*
* @link https://github.com/zionsg/ZnZend for canonical source repository
*/
namespace ZnZend\Captcha\Service;
use ZnZend\Captcha\Service\QuestionServiceInterface;
/**
* Generate simple addition/subtraction question for ZnZend\Captcha\Question adapter
*/
class MathQuestionService implements QuestionServiceInterface
{
/**
* Current question
*
* @var string
*/
protected $question;
/**
* Answer to current question
*
* @var mixed
*/
protected $answer;
/**
* Map operator to English text
*
* @var array
*/
protected $operators = [self::ADD => 'plus', self::SUBTRACT => 'minus'];
/**
* Operator constants
*/
const ADD = 1;
const SUBTRACT = -1;
/**
* Defined by QuestionServiceInterface; Generate new set of question and answer
*
* @return QuestionServiceInterface
*/
public function generate()
{
$operand1 = mt_rand(10, 100);
$operand2 = mt_rand(1, 9);
$operator = array_rand($this->operators);
$answer = $operand1 + ($operator * $operand2);
$question = sprintf(
'%s %s %s',
$this->translate($operand1),
$this->operators[$operator],
$this->translate($operand2)
);
$this->question = $question;
$this->answer = $answer;
return $this;
}
/**
* Defined by QuestionServiceInterface; Get current question
*
* @return string
*/
public function getQuestion()
{
return $this->question;
}
/**
* Defined by QuestionServiceInterface; Get answer to current question
*
* @return string
*/
public function getAnswer()
{
return $this->answer;
}
protected function separateThousands($num)
{
// separate a number by thousands using comma - meant for display only
$ans = '';
while (strlen($num) > 3) {
$ans = ', ' . substr($num, -3, 3) . $ans; // take the last 3 digits
$num = substr($num, 0, -3); //cut away last 3 digits
}
$ans = $num . $ans;
return $ans;
}
protected function translate($num)
{
// Translates a number into words, from 0 to 999 999 999.
// Eg: 264 073 458 = two hundred and sixty-four million, seventy-three thousand, four hundred and fifty-eight
$num = (int) $num;
// special case
if (0 == $num) {
return 'zero';
}
// index 0 not used. Non-empty place suffixes have ' ' in front to facilitate concatenation
$placeSuffix = [
'', '', '', ' hundred', ' thousand', ' thousand', ' thousand', ' million', ' million', ' million',
];
$onesPrefix = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
$teensPrefix = [
'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'
];
$tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
$ans = '';
$partAns = '';
while (strlen($num) > 0) {
$digit = (int) substr($num, 0, 1);
$place = strlen($num);
$prefix = '';
// parse number according to pronunciation types
if (in_array($place, [6, 9])) { // 100 thousand, 100 million
$function = __FUNCTION__;
$prefix = $onesPrefix[$digit] . ' hundred';
$next2digits = $this->$function(substr($num, 1, 2)); // process the next 2 digits
if ($next2digits != 'zero') {
$prefix .= ' and ' . $next2digits;
}
$partAns = $prefix . $placeSuffix[$place];
$num = substr($num, 3); // cut off the 3 leftmost digits
} elseif (in_array($place, [2, 5, 8])) { // 10, 10 thousand, 10 million
$nextdigit = (int) substr($num, 1, 1);
if (1 == $digit) { // 10 to 19
$prefix = $teensPrefix[$nextdigit];
} elseif ($digit > 1) {
$prefix = $tens[$digit];
if ($nextdigit != 0) {
$prefix = $tens[$digit] . '-' . $onesPrefix[$nextdigit];
}
} else {
$prefix = '';
}
$partAns = $prefix . $placeSuffix[$place];
$num = substr($num, 2); // cut off the 2 leftmost digits
} elseif (in_array($place, [1, 3, 4, 7])) { // 1, 1 hundred, 1 thousand, 1 million
$prefix = $onesPrefix[$digit];
$partAns = $prefix . $placeSuffix[$place];
$num = substr($num, 1); // cut off the leftmost digit
}
// Eliminate the redundant zeroes in front
if ($num != '') {
while ('0' === substr($num, 0, 1)) { // cannot use == as substr may return false
$num = substr($num, 1);
}
}
// Concatenate the new part to the whole answer
if ('' == $ans) {
$ans .= $partAns;
} elseif (strlen($num) > 0) {
$ans .= ', ' . $partAns;
} else {
$ans .= ' and ' . $partAns;
}
} //end while
return $ans;
}
}