qb-answer-checker 1.0.3 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -310,22 +310,30 @@ function stringMatchesReference({ string, reference, strictness = 7, acceptSubst
310
310
  * directedPrompt: String | null
311
311
  * }}
312
312
  */
313
- function checkAnswer(answerline, givenAnswer) {
313
+ function checkAnswer(answerline, givenAnswer, strictness = 7) {
314
+ if (!answerline || !givenAnswer) {
315
+ return { directive: 'reject', directedPrompt: null };
316
+ }
317
+
318
+ if (typeof answerline !== 'string' || typeof givenAnswer !== 'string') {
319
+ return { directive: 'reject', directedPrompt: null };
320
+ }
321
+
314
322
  answerline = answerline.toLowerCase();
315
323
  givenAnswer = utils.replaceSpecialCharacters(givenAnswer.toLowerCase());
316
324
  const answerlineIsFormatted = answerline.includes('<u>');
317
325
 
318
326
  const answerWorks = (answerline, givenAnswer) => {
319
327
  if (answerlineIsFormatted) {
320
- return stringMatchesReference({ string: answerline, reference: givenAnswer });
328
+ return stringMatchesReference({ string: answerline, reference: givenAnswer, strictness: strictness });
321
329
  } else {
322
- return stringMatchesReference({ string: givenAnswer, reference: answerline, acceptSubstring: true });
330
+ return stringMatchesReference({ string: givenAnswer, reference: answerline, strictness: strictness, acceptSubstring: true });
323
331
  }
324
332
  };
325
333
 
326
334
  const parsedAnswerline = parseAnswerline(answerline);
327
335
 
328
- if (!answerlineIsFormatted && parsedAnswerline.accept[0].length > 1 && givenAnswer.length === 1 && isNaN(givenAnswer))
336
+ if (!answerlineIsFormatted && parsedAnswerline.accept[0] && parsedAnswerline.accept[0].length > 1 && givenAnswer.length === 1 && isNaN(givenAnswer))
329
337
  return { directive: 'reject', directedPrompt: null };
330
338
 
331
339
  for (const answer of parsedAnswerline.reject) {
@@ -340,11 +348,9 @@ function checkAnswer(answerline, givenAnswer) {
340
348
  return { directive: 'reject', directedPrompt: null };
341
349
  }
342
350
 
343
- for (const answer of parsedAnswerline.accept) {
344
- if (answerWorks(answer, givenAnswer)) {
345
- return { directive: 'accept', directedPrompt: null };
346
- }
347
- }
351
+ if (parsedAnswerline.accept.some(answer => answerWorks(answer, givenAnswer)) {
352
+ return { directive: 'accept', directedPrompt: null };
353
+ }
348
354
 
349
355
  for (const answer of parsedAnswerline.prompt) {
350
356
  const directedPrompt = answer[1];
@@ -2,31 +2,152 @@ import { removeHTMLTags } from './utils.js';
2
2
 
3
3
 
4
4
  const answerEquivalents = {
5
- 'atomic bombs': ['atomic weapons', 'nuclear bombs', 'nuclear weapons', 'nukes', 'fission bombs', 'A-bombs'],
6
- 'nuclear weapons': ['atomic bombs', 'atomic weapons', 'nuclear bombs', 'nukes', 'fission bombs', 'A-bombs'],
7
- 'nukes': ['atomic bombs', 'atomic weapons', 'nuclear bombs', 'nuclear weapons', 'fission bombs', 'A-bombs'],
8
- 'house': ['home', 'dwelling', 'residence'],
9
- 'mouse': ['mice'],
10
- 'rail': ['railroad'],
11
- 'railroad': ['rail'],
12
- 'nineteen eighty-four': ['1984', 'nineteen eighty four'],
13
- 'nineteen eighty four': ['1984', 'nineteen eighty-four'],
14
- 'oxidation number': ['oxidation state'],
15
- 'oxidation state': ['oxidation number'],
16
- 'ralph vaughan-williams': ['rvw'],
17
- 'spacewalk': ['space walk'],
18
- 'sugar cane': ['sugarcane'],
19
- 'sugarcane': ['sugar cane'],
20
- 'wavefunction': ['wave function'],
21
- 'wave function': ['wavefunction'],
22
- 'world war 1': ['first world war', 'great war', 'world war i', 'world war one'],
23
- 'world war i': ['first world war', 'great war', 'world war 1', 'world war one'],
24
- 'world war one': ['first world war', 'great war', 'world war 1', 'world war i'],
25
- 'world war ii': ['ww2', 'wwii', 'world war 2', 'world war two', 'second world war'],
26
- 'world war two': ['ww2', 'wwii', 'world war ii', 'world war 2', 'second world war'],
27
- 'world war 2': ['ww2', 'wwii', 'world war ii', 'world war two', 'second world war'],
5
+ 'atomic bombs': ['atomic weapons', 'nuclear bombs', 'nuclear weapons', 'nukes', 'fission bombs', 'A-bombs'],
6
+ 'nuclear weapons': ['atomic bombs', 'atomic weapons', 'nuclear bombs', 'nukes', 'fission bombs', 'A-bombs'],
7
+ 'nukes': ['atomic bombs', 'atomic weapons', 'nuclear bombs', 'nuclear weapons', 'fission bombs', 'A-bombs'],
8
+ 'house': ['home', 'dwelling', 'residence'],
9
+ 'mouse': ['mice'],
10
+ 'rail': ['railroad'],
11
+ 'railroad': ['rail'],
12
+ 'nineteen eighty-four': ['1984', 'nineteen eighty four'],
13
+ 'nineteen eighty four': ['1984', 'nineteen eighty-four'],
14
+ 'oxidation number': ['oxidation state'],
15
+ 'oxidation state': ['oxidation number'],
16
+ 'ralph vaughan-williams': ['rvw'],
17
+ 'spacewalk': ['space walk'],
18
+ 'sugar cane': ['sugarcane'],
19
+ 'sugarcane': ['sugar cane'],
20
+ 'wavefunction': ['wave function'],
21
+ 'wave function': ['wavefunction'],
22
+ 'world war 1': ['first world war', 'great war', 'world war i', 'world war one'],
23
+ 'world war i': ['first world war', 'great war', 'world war 1', 'world war one'],
24
+ 'world war one': ['first world war', 'great war', 'world war 1', 'world war i'],
25
+ 'world war ii': ['ww2', 'wwii', 'world war 2', 'world war two', 'second world war'],
26
+ 'world war two': ['ww2', 'wwii', 'world war ii', 'world war 2', 'second world war'],
27
+ 'world war 2': ['ww2', 'wwii', 'world war ii', 'world war two', 'second world war'],
28
+
29
+ // Elements
30
+ 'hydrogen': ['h'],
31
+ 'helium': ['he'],
32
+ 'lithium': ['li'],
33
+ 'beryllium': ['be'],
34
+ 'boron': ['b'],
35
+ 'carbon': ['c'],
36
+ 'nitrogen': ['n'],
37
+ 'oxygen': ['o'],
38
+ 'fluorine': ['f'],
39
+ 'neon': ['ne'],
40
+ 'sodium': ['na'],
41
+ 'magnesium': ['mg'],
42
+ 'aluminum': ['al'],
43
+ 'silicon': ['si'],
44
+ 'phosphorus': ['p'],
45
+ 'sulfur': ['s'],
46
+ 'chlorine': ['cl'],
47
+ 'argon': ['ar'],
48
+ 'potassium': ['k'],
49
+ 'calcium': ['ca'],
50
+ 'scandium': ['sc'],
51
+ 'titanium': ['ti'],
52
+ 'vanadium': ['v'],
53
+ 'chromium': ['cr'],
54
+ 'manganese': ['mn'],
55
+ 'iron': ['fe'],
56
+ 'cobalt': ['co'],
57
+ 'nickel': ['ni'],
58
+ 'copper': ['cu'],
59
+ 'zinc': ['zn'],
60
+ 'gallium': ['ga'],
61
+ 'germanium': ['ge'],
62
+ 'arsenic': ['as'],
63
+ 'selenium': ['se'],
64
+ 'bromine': ['br'],
65
+ 'krypton': ['kr'],
66
+ 'rubidium': ['rb'],
67
+ 'strontium': ['sr'],
68
+ 'yttrium': ['y'],
69
+ 'zirconium': ['zr'],
70
+ 'niobium': ['nb'],
71
+ 'molybdenum': ['mo'],
72
+ 'technetium': ['tc'],
73
+ 'ruthenium': ['ru'],
74
+ 'rhodium': ['rh'],
75
+ 'palladium': ['pd'],
76
+ 'silver': ['ag'],
77
+ 'cadmium': ['cd'],
78
+ 'indium': ['in'],
79
+ 'tin': ['sn'],
80
+ 'antimony': ['sb'],
81
+ 'tellurium': ['te'],
82
+ 'iodine': ['i'],
83
+ 'xenon': ['xe'],
84
+ 'cesium': ['cs'],
85
+ 'barium': ['ba'],
86
+ 'lanthanum': ['la'],
87
+ 'cerium': ['ce'],
88
+ 'praseodymium': ['pr'],
89
+ 'neodymium': ['nd'],
90
+ 'promethium': ['pm'],
91
+ 'samarium': ['sm'],
92
+ 'europium': ['eu'],
93
+ 'gadolinium': ['gd'],
94
+ 'terbium': ['tb'],
95
+ 'dysprosium': ['dy'],
96
+ 'holmium': ['ho'],
97
+ 'erbium': ['er'],
98
+ 'thulium': ['tm'],
99
+ 'ytterbium': ['yb'],
100
+ 'lutetium': ['lu'],
101
+ 'hafnium': ['hf'],
102
+ 'tantalum': ['ta'],
103
+ 'tungsten': ['w'],
104
+ 'rhenium': ['re'],
105
+ 'osmium': ['os'],
106
+ 'iridium': ['ir'],
107
+ 'platinum': ['pt'],
108
+ 'gold': ['au'],
109
+ 'mercury': ['hg'],
110
+ 'thallium': ['tl'],
111
+ 'lead': ['pb'],
112
+ 'bismuth': ['bi'],
113
+ 'polonium': ['po'],
114
+ 'astatine': ['at'],
115
+ 'radon': ['rn'],
116
+ 'francium': ['fr'],
117
+ 'radium': ['ra'],
118
+ 'actinium': ['ac'],
119
+ 'thorium': ['th'],
120
+ 'protactinium': ['pa'],
121
+ 'uranium': ['u'],
122
+ 'neptunium': ['np'],
123
+ 'plutonium': ['pu'],
124
+ 'americium': ['am'],
125
+ 'curium': ['cm'],
126
+ 'berkelium': ['bk'],
127
+ 'californium': ['cf'],
128
+ 'einsteinium': ['es'],
129
+ 'fermium': ['fm'],
130
+ 'mendelevium': ['md'],
131
+ 'nobelium': ['no'],
132
+ 'lawrencium': ['lr'],
133
+ 'rutherfordium': ['rf'],
134
+ 'dubnium': ['db'],
135
+ 'seaborgium': ['sg'],
136
+ 'bohrium': ['bh'],
137
+ 'hassium': ['hs'],
138
+ 'meitnerium': ['mt'],
139
+ 'darmstadtium': ['ds'],
140
+ 'roentgenium': ['rg'],
141
+ 'copernicium': ['cn'],
142
+ 'nihonium': ['nh'],
143
+ 'flerovium': ['fl'],
144
+ 'moscovium': ['mc'],
145
+ 'livermorium': ['lv'],
146
+ 'tennessine': ['ts'],
147
+ 'oganesson': ['og']
28
148
  };
29
149
 
150
+
30
151
  /**
31
152
  * Given an answer, return an array of equivalent answers (i.e. answers that should always match).
32
153
  * @param {string} answer
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qb-answer-checker",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "type": "module",
5
5
  "description": "A package to automatically check/judge answers against quizbowl answerlines.",
6
6
  "main": "lib/check-answer.js",