feat: solution for 'Экзамен: Самокорректирующийся агент'

This commit is contained in:
2026-06-30 11:51:44 +03:00
parent db2278e693
commit 7b2e405dfa
3 changed files with 148 additions and 128 deletions
+63
View File
@@ -0,0 +1,63 @@
#!/usr/bin/env node
/**
* Simple Self-Correcting Agent
*
* This script demonstrates a minimal selfcorrecting agent that
* takes a string input and attempts to correct common typos such as
* extra spaces, missing punctuation, and simple misspellings using
* a small dictionary.
*
* The implementation uses only the Node.js standard library
* and does not depend on any external frameworks.
*/
const process = require('process');
// A very small dictionary of common misspellings
const MISSPELLINGS = {
"teh": "the",
"recieve": "receive",
"adress": "address",
"occured": "occurred",
"seperate": "separate",
"definately": "definitely",
"goverment": "government",
"untill": "until",
"accomodate": "accommodate",
"wich": "which",
};
function correctSpelling(word) {
return MISSPELLINGS[word.toLowerCase()] || word;
}
function correctSentence(sentence) {
// Strip whitespace
sentence = sentence.trim();
// Collapse multiple spaces
sentence = sentence.replace(/\s+/g, ' ');
// Tokenise and correct words
const words = sentence.split(' ');
const correctedWords = words.map(correctSpelling);
let corrected = correctedWords.join(' ');
// Ensure ending punctuation
if (!/[.!?]$/.test(corrected)) {
corrected += '.';
}
return corrected;
}
function main() {
const args = process.argv.slice(2);
if (args.length === 0) {
console.log('Usage: node src/index.js "<sentence>"');
process.exit(1);
}
const inputSentence = args.join(' ');
const corrected = correctSentence(inputSentence);
console.log(corrected);
}
if (require.main === module) {
main();
}