feat: solution for 'Повторный экзамен: Граф с рефлексией и доработкой'

This commit is contained in:
2026-06-30 14:34:54 +03:00
parent 9ab33ca6a8
commit babdcf160b
5 changed files with 126 additions and 82 deletions
+36
View File
@@ -0,0 +1,36 @@
import { Graph as GraphLib } from 'graphlib';
import _ from 'lodash';
export default class Graph {
constructor() {
this.graph = new GraphLib();
}
addNode(node) {
this.graph.setNode(node);
}
addEdge(from, to) {
this.graph.setEdge(from, to);
}
hasEdge(from, to) {
return this.graph.hasEdge(from, to);
}
reflexive() {
this.graph.nodes().forEach((node) => {
if (!this.graph.hasEdge(node, node)) {
this.graph.setEdge(node, node);
}
});
}
getAdjacencyList() {
const adjacency = {};
this.graph.nodes().forEach((node) => {
adjacency[node] = this.graph.successors(node) || [];
});
return adjacency;
}
}
+12 -57
View File
@@ -1,63 +1,18 @@
#!/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.
*/
import Graph from './graph.js';
const process = require('process');
const g = new Graph();
// 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",
};
g.addNode('A');
g.addNode('B');
g.addNode('C');
function correctSpelling(word) {
return MISSPELLINGS[word.toLowerCase()] || word;
}
g.addEdge('A', 'B');
g.addEdge('B', 'C');
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;
}
console.log('Before reflexive:');
console.log(g.getAdjacencyList());
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);
}
g.reflexive();
if (require.main === module) {
main();
}
console.log('After reflexive:');
console.log(g.getAdjacencyList());