File size: 4,793 Bytes
3a71c28 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | // 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;
} |