milwright commited on
Commit
4f630fa
Β·
1 Parent(s): a4e8e99

fix: prevent numbers from being selected as blank words

Browse files

add validation to reject pure numeric strings and ensure all selected words contain at least one alphabetic character

Files changed (1) hide show
  1. src/aiService.js +36 -0
src/aiService.js CHANGED
@@ -354,8 +354,20 @@ Passage: "${passage}"`
354
  if (Array.isArray(words)) {
355
  // Validate word lengths based on level
356
  const validWords = words.filter(word => {
 
 
 
 
 
 
357
  const cleanWord = word.replace(/[^a-zA-Z]/g, '');
358
 
 
 
 
 
 
 
359
  // Check length constraints
360
  if (level <= 2) {
361
  return cleanWord.length >= 4 && cleanWord.length <= 7;
@@ -381,8 +393,20 @@ Passage: "${passage}"`
381
  const words = matches.map(m => m.replace(/"/g, ''));
382
  // Validate word lengths
383
  const validWords = words.filter(word => {
 
 
 
 
 
 
384
  const cleanWord = word.replace(/[^a-zA-Z]/g, '');
385
 
 
 
 
 
 
 
386
  // Check length constraints
387
  if (level <= 2) {
388
  return cleanWord.length >= 4 && cleanWord.length <= 7;
@@ -581,8 +605,20 @@ Return JSON: {"passage1": {"words": [${blanksPerPassage} words], "context": "one
581
  // Validate word lengths based on level
582
  const validateWords = (words, passageText) => {
583
  return words.filter(word => {
 
 
 
 
 
 
584
  const cleanWord = word.replace(/[^a-zA-Z]/g, '');
585
 
 
 
 
 
 
 
586
  // Check if word appears in all caps in the passage (like "VOLUME")
587
  if (passageText.includes(word.toUpperCase()) && word === word.toUpperCase()) {
588
  console.log(`Skipping all-caps word: ${word}`);
 
354
  if (Array.isArray(words)) {
355
  // Validate word lengths based on level
356
  const validWords = words.filter(word => {
357
+ // First check if the word contains at least one letter
358
+ if (!/[a-zA-Z]/.test(word)) {
359
+ console.log(`❌ Rejecting non-alphabetic word: "${word}"`);
360
+ return false;
361
+ }
362
+
363
  const cleanWord = word.replace(/[^a-zA-Z]/g, '');
364
 
365
+ // If cleanWord is empty after removing non-letters, reject
366
+ if (cleanWord.length === 0) {
367
+ console.log(`❌ Rejecting word with no letters: "${word}"`);
368
+ return false;
369
+ }
370
+
371
  // Check length constraints
372
  if (level <= 2) {
373
  return cleanWord.length >= 4 && cleanWord.length <= 7;
 
393
  const words = matches.map(m => m.replace(/"/g, ''));
394
  // Validate word lengths
395
  const validWords = words.filter(word => {
396
+ // First check if the word contains at least one letter
397
+ if (!/[a-zA-Z]/.test(word)) {
398
+ console.log(`❌ Rejecting non-alphabetic word: "${word}"`);
399
+ return false;
400
+ }
401
+
402
  const cleanWord = word.replace(/[^a-zA-Z]/g, '');
403
 
404
+ // If cleanWord is empty after removing non-letters, reject
405
+ if (cleanWord.length === 0) {
406
+ console.log(`❌ Rejecting word with no letters: "${word}"`);
407
+ return false;
408
+ }
409
+
410
  // Check length constraints
411
  if (level <= 2) {
412
  return cleanWord.length >= 4 && cleanWord.length <= 7;
 
605
  // Validate word lengths based on level
606
  const validateWords = (words, passageText) => {
607
  return words.filter(word => {
608
+ // First check if the word contains at least one letter
609
+ if (!/[a-zA-Z]/.test(word)) {
610
+ console.log(`❌ Rejecting non-alphabetic word: "${word}"`);
611
+ return false;
612
+ }
613
+
614
  const cleanWord = word.replace(/[^a-zA-Z]/g, '');
615
 
616
+ // If cleanWord is empty after removing non-letters, reject
617
+ if (cleanWord.length === 0) {
618
+ console.log(`❌ Rejecting word with no letters: "${word}"`);
619
+ return false;
620
+ }
621
+
622
  // Check if word appears in all caps in the passage (like "VOLUME")
623
  if (passageText.includes(word.toUpperCase()) && word === word.toUpperCase()) {
624
  console.log(`Skipping all-caps word: ${word}`);