Spaces:
Running
Running
fix: clean contextualization artifacts and ensure passages start properly
Browse files- Remove word-colon patterns like 'mas:' from AI responses
- Always start passages with first word of complete sentence
- Find capital letter after punctuation for proper sentence starts
- Fallback to first capital letter if no sentence break found
- src/aiService.js +4 -3
- src/clozeGameEngine.js +11 -4
src/aiService.js
CHANGED
|
@@ -74,10 +74,10 @@ class OpenRouterService {
|
|
| 74 |
|
| 75 |
let content = data.choices[0].message.content.trim();
|
| 76 |
|
| 77 |
-
// Clean up AI response artifacts
|
| 78 |
content = content
|
| 79 |
.replace(/^\s*["']|["']\s*$/g, '') // Remove leading/trailing quotes
|
| 80 |
-
.replace(/^\s
|
| 81 |
.replace(/\*+/g, '') // Remove asterisks (markdown bold/italic)
|
| 82 |
.replace(/_+/g, '') // Remove underscores (markdown)
|
| 83 |
.replace(/#+\s*/g, '') // Remove hash symbols (markdown headers)
|
|
@@ -212,7 +212,8 @@ Passage: "${passage}"`
|
|
| 212 |
// Clean up AI response artifacts
|
| 213 |
content = content
|
| 214 |
.replace(/^\s*["']|["']\s*$/g, '') // Remove leading/trailing quotes
|
| 215 |
-
.replace(/^\s
|
|
|
|
| 216 |
.replace(/\*+/g, '') // Remove asterisks (markdown bold/italic)
|
| 217 |
.replace(/_+/g, '') // Remove underscores (markdown)
|
| 218 |
.replace(/#+\s*/g, '') // Remove hash symbols (markdown headers)
|
|
|
|
| 74 |
|
| 75 |
let content = data.choices[0].message.content.trim();
|
| 76 |
|
| 77 |
+
// Clean up AI response artifacts
|
| 78 |
content = content
|
| 79 |
.replace(/^\s*["']|["']\s*$/g, '') // Remove leading/trailing quotes
|
| 80 |
+
.replace(/^\s*[:;]+\s*/, '') // Remove leading colons and semicolons
|
| 81 |
.replace(/\*+/g, '') // Remove asterisks (markdown bold/italic)
|
| 82 |
.replace(/_+/g, '') // Remove underscores (markdown)
|
| 83 |
.replace(/#+\s*/g, '') // Remove hash symbols (markdown headers)
|
|
|
|
| 212 |
// Clean up AI response artifacts
|
| 213 |
content = content
|
| 214 |
.replace(/^\s*["']|["']\s*$/g, '') // Remove leading/trailing quotes
|
| 215 |
+
.replace(/^\s*[:;]+\s*/, '') // Remove leading colons and semicolons
|
| 216 |
+
.replace(/^\s*[a-z]+:\s*/i, '') // Remove any word followed by colon (like "mas:")
|
| 217 |
.replace(/\*+/g, '') // Remove asterisks (markdown bold/italic)
|
| 218 |
.replace(/_+/g, '') // Remove underscores (markdown)
|
| 219 |
.replace(/#+\s*/g, '') // Remove hash symbols (markdown headers)
|
src/clozeGameEngine.js
CHANGED
|
@@ -76,10 +76,17 @@ class ClozeGame {
|
|
| 76 |
// Extract longer initial passage for better sentence completion
|
| 77 |
let passage = text.substring(startIndex, startIndex + 1000);
|
| 78 |
|
| 79 |
-
// Clean up start - find first complete sentence
|
| 80 |
-
const
|
| 81 |
-
if (
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
}
|
| 84 |
|
| 85 |
// Clean up end - ensure we end at a complete sentence
|
|
|
|
| 76 |
// Extract longer initial passage for better sentence completion
|
| 77 |
let passage = text.substring(startIndex, startIndex + 1000);
|
| 78 |
|
| 79 |
+
// Clean up start - find first complete sentence that starts with capital letter
|
| 80 |
+
const firstSentenceMatch = passage.match(/[.!?]\s+([A-Z][^.!?]*)/);
|
| 81 |
+
if (firstSentenceMatch && firstSentenceMatch.index < 200) {
|
| 82 |
+
// Start from the capital letter after punctuation
|
| 83 |
+
passage = passage.substring(firstSentenceMatch.index + firstSentenceMatch[0].length - firstSentenceMatch[1].length);
|
| 84 |
+
} else {
|
| 85 |
+
// If no good sentence break found, find first capital letter
|
| 86 |
+
const firstCapitalMatch = passage.match(/[A-Z][^.!?]*/);
|
| 87 |
+
if (firstCapitalMatch) {
|
| 88 |
+
passage = passage.substring(firstCapitalMatch.index);
|
| 89 |
+
}
|
| 90 |
}
|
| 91 |
|
| 92 |
// Clean up end - ensure we end at a complete sentence
|