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.
49 lines (38 loc) • 1.94 kB
JavaScript
const fs = require("fs");
const path = require("path");
const nlp = require("./nlp"); // Import NLP correction
// Load dataset for NLP correction
const datasetPath = path.join(__dirname, "..", "data", "nlp.txt");
// **Filter mathematical content from text**
function extractMathExpression(text) {
// Remove non-math-related words
const ignoreWords = new Set([
"find", "?", "me", "maths", "homework", "solution", "to", "where", "the", "is", "unknown", "question", "in", "my", "help", "can", "you", "bro", "what", "whats"
]);
// Tokenize text and filter only math-related parts
let tokens = text.match(/\b\w+|\S/g) || []; // Split by words and symbols
let mathTokens = tokens.filter(token => !ignoreWords.has(token.toLowerCase())); // Remove useless words
return mathTokens.join(" "); // Convert back to string
}
// **Extract variables and assign default values**
function assignVariables(expression) {
let variables = {};
let variableRegex = /\b[a-zA-Z]\b/g; // Match single-letter variables like x, y, z
let foundVars = [...new Set(expression.match(variableRegex) || [])]; // Find unique variables
foundVars.forEach(v => variables[v] = 10); // Assign default 10
return variables;
}
// **Main function**
function nlmathsBackend(text) {
let correctedText = nlp.correctTextBackend(text, datasetPath); // NLP correction
let mathExpression = extractMathExpression(correctedText); // Remove useless words
let variables = assignVariables(mathExpression); // Assign unknowns
return {
finalExpression: mathExpression,
variables: variables
};
}
// **Test Cases**
//console.log(nlmathsBackend("Whats 10+10?"));
//console.log(nlmathsBackend("Bro can you help me in my maths homework the question is x^2 + y and y = 5"));
//console.log(nlmathsBackend("Find the solution to 2x + 5 where x is unknown"));