veenz / commandParser.ts
Naveenz's picture
Upload 26 files
3a71c28 verified
Raw History Blame Contribute Delete
4.79 kB
// Parse voice commands into structured calculator commands
interface CalculateCommand {
type: 'calculate';
expression: string;
}
interface MemoryCommand {
type: 'memory';
action: 'store' | 'recall' | 'clear';
}
interface ClearCommand {
type: 'clear';
}
interface UnknownCommand {
type: 'unknown';
}
type Command = CalculateCommand | MemoryCommand | ClearCommand | UnknownCommand;
export function parseVoiceCommand(transcript: string): Command {
// Convert to lowercase for easier matching
const text = transcript.toLowerCase().trim();
// Check for clear commands
if (
text.includes('clear all') ||
text.includes('clear calculator') ||
text.includes('reset calculator') ||
text === 'clear' ||
text === 'reset'
) {
return { type: 'clear' };
}
// Check for memory commands
if (text.includes('store in memory') || text.includes('save to memory') || text.includes('memory store')) {
return { type: 'memory', action: 'store' };
}
if (text.includes('recall memory') || text.includes('memory recall') || text === 'recall' || text === 'memory') {
return { type: 'memory', action: 'recall' };
}
if (text.includes('clear memory') || text.includes('memory clear')) {
return { type: 'memory', action: 'clear' };
}
// Parse calculation commands
const calculationPatterns = [
// "Calculate" command pattern
/(?:calculate|compute|what is|evaluate|solve)(.*)/i,
// Question pattern
/(?:what(?:'s| is) |how much is |compute |calculate )(.*)\??/i,
// Direct expression patterns
/(\d+(?:\.\d+)?)\s*(plus|minus|times|divided by|mod|modulo|power|to the power of|squared|cubed)\s*(\d+(?:\.\d+)?)/i,
/(sine|sin|cosine|cos|tangent|tan|arc\s*sine|arc\s*sin|arc\s*cosine|arc\s*cos|arc\s*tangent|arc\s*tan|log|ln|logarithm|natural log|square root|cube root)\s*(?:of)?\s*(\d+(?:\.\d+)?)/i
];
for (const pattern of calculationPatterns) {
const match = text.match(pattern);
if (match) {
let expression = match[1] ? match[1].trim() : text;
// Replace spoken math terms with operators
expression = convertSpokenMathToExpression(expression);
return {
type: 'calculate',
expression
};
}
}
// Direct input handling - try to parse the raw input
// If it contains numbers and operators, treat as a calculation
if (/\d+|plus|minus|times|divided|multiply|add|subtract|sin|cos|tan|log|square root/i.test(text)) {
const expression = convertSpokenMathToExpression(text);
return {
type: 'calculate',
expression
};
}
// If we can't determine the command
return { type: 'unknown' };
}
// Convert spoken mathematical terms to expression syntax
function convertSpokenMathToExpression(text: string): string {
let expression = text
// Replace basic operators
.replace(/plus/gi, '+')
.replace(/minus/gi, '-')
.replace(/negative/gi, '-')
.replace(/times|multiplied by|multiply by/gi, '*')
.replace(/divided by|divide by/gi, '/')
.replace(/mod|modulo/gi, '%')
.replace(/power|to the power of/gi, '^')
// Replace function names
.replace(/sine of|sin of|sine|sin/gi, 'sin(')
.replace(/cosine of|cos of|cosine|cos/gi, 'cos(')
.replace(/tangent of|tan of|tangent|tan/gi, 'tan(')
.replace(/arc\s*sine of|arc\s*sin of|arc\s*sine|arc\s*sin|inverse sine|asin/gi, 'asin(')
.replace(/arc\s*cosine of|arc\s*cos of|arc\s*cosine|arc\s*cos|inverse cosine|acos/gi, 'acos(')
.replace(/arc\s*tangent of|arc\s*tan of|arc\s*tangent|arc\s*tan|inverse tangent|atan/gi, 'atan(')
.replace(/log of|log/gi, 'log(')
.replace(/natural log of|natural log|ln of|ln/gi, 'ln(')
.replace(/square root of|square root/gi, 'sqrt(')
.replace(/cube root of|cube root/gi, 'cbrt(')
.replace(/factorial of|factorial/gi, 'factorial(')
.replace(/absolute value of|absolute value|abs of|abs/gi, 'abs(')
// Replace special values
.replace(/pi/gi, 'pi')
.replace(/euler's number|e constant/gi, 'e')
// Handle "squared" and "cubed" special cases
.replace(/(\d+(?:\.\d+)?)\s*squared/gi, '$1^2')
.replace(/(\d+(?:\.\d+)?)\s*cubed/gi, '$1^3')
// Replace "of" after function with opening parenthesis if needed
.replace(/(?:sin|cos|tan|asin|acos|atan|log|ln|sqrt|cbrt|factorial|abs)\s+(?:of\s+)?/gi, (match) => {
return match.trim().toLowerCase() + '(';
});
// Add closing parentheses for functions
const openingCount = (expression.match(/\(/g) || []).length;
const closingCount = (expression.match(/\)/g) || []).length;
const missingClosing = openingCount - closingCount;
if (missingClosing > 0) {
expression += ')'.repeat(missingClosing);
}
return expression;
}