UNPKG

algoritms.ai

Version:

The Algoritms.AI build tools to create better and lightweight AI models with Acuuracy, Context, Frequency, Memory, Maths in Natural Language, Natural Language Processing, Probablities and Vectors.

78 lines (63 loc) 2.83 kB
const fs = require("fs"); const path = require("path"); const userFilePath = path.join(__dirname, "..", "user", "fornow.txt"); // Store session memory (history + last known entity) let sessionMemory = { lastEntity: null, lastContext: [], history: [] }; // **Load context from file** function loadContextData() { const contextPath = path.join(__dirname, "..", "data", "context.txt"); if (!fs.existsSync(contextPath)) return {}; const data = fs.readFileSync(contextPath, "utf-8").split("\n"); let contextMap = {}; for (let line of data) { let match = line.match(/'([^']*)'\s*->\s*(.*)/); // Match 'keyword' -> context if (match) { let key = match[1].toLowerCase(); let categories = match[2].split(",").map(c => c.trim()); contextMap[key] = categories; } } return contextMap; } // **Resolve Pronouns & Maintain Context** function resolvePronoun(variable) { let pronouns = ["he", "she", "it", "they", "them", "this", "that"]; // If the input is a pronoun, return the last known entity if (pronouns.includes(variable.toLowerCase()) && sessionMemory.lastEntity) { return sessionMemory.lastEntity; } return variable; } // **Get Context & Link Past Data** function getContext(variable) { const contextData = loadContextData(); let key = resolvePronoun(variable.toLowerCase()); let context = contextData[key] || ["unknown"]; // **If pronoun was used, inherit last context** if (context.includes("unknown") && sessionMemory.lastContext.length > 0) { context = sessionMemory.lastContext; // Use previous context } // **Update last entity and context only for actual named entities** if (!["he", "she", "it", "they", "them", "this", "that"].includes(variable.toLowerCase())) { sessionMemory.lastEntity = key; sessionMemory.lastContext = context; } return { key, context }; } // **Save Blockchain-style History** function saveContextToFile(variable, context) { let questionNumber = sessionMemory.history.length + 1; let logEntry = `question_${questionNumber}: [\ncontext: ${JSON.stringify(context)},\nasked_question: "${variable}"\n]`; sessionMemory.history.push(logEntry); let finalData = sessionMemory.history.join("\n\n"); // Chain history fs.writeFileSync(userFilePath, finalData, "utf-8"); } // **Main Function** function contextBackend(variable) { let { key, context } = getContext(variable); saveContextToFile(variable, context); return { variable, context }; } function tokenize(){ // Make a tokenizer so that if ContextBackend is called with a sentence then tokenize the sentense and then send the context to the user }