Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/Seaus-tech/Calculator-App/llms.txt

Use this file to discover all available pages before exploring further.

Calculator App understands a set of natural language phrases so you don’t have to remember operator syntax for common operations. The parse_natural_language() function in parser.py processes input before it reaches the evaluator, converting phrases into standard Python-compatible expressions.

How It Works

Before any evaluation takes place, the raw input string is:
  1. Converted to lowercase.
  2. Scanned by a series of regex substitutions that replace recognised phrases with their mathematical equivalents.
  3. Passed on to the rest of the pipeline (fraction parser, variable substitution, eval).
Because this happens at the pre-processing stage, natural language phrases can be combined with other operations and variables.

Supported Patterns

Pattern: {N} squared
Regex: (\d+(?:\.\d+)?)\s*squared(\1)**2
> 9 squared
= 81
> 4 squared
= 16

All Patterns at a Glance

PhraseConverts toExample inputResult
{N} squared({N})**29 squared81
{N} cubed({N})**35 cubed125
sqrt of {N}sqrt({N})sqrt of 164.0
square root of {N}sqrt({N})square root of 255.0
sqrt {N}sqrt({N})sqrt 164.0

Source Code

def parse_natural_language(expr: str) -> str:
    expr = expr.lower()
    expr = re.sub(r'(\d+(?:\.\d+)?)\s*squared', r'(\1)**2', expr)
    expr = re.sub(r'(\d+(?:\.\d+)?)\s*cubed',   r'(\1)**3', expr)
    expr = re.sub(r'sqrt\s*of\s*(\d+(?:\.\d+)?)', r'sqrt(\1)', expr)
    expr = re.sub(r'square\s*root\s*of\s*(\d+(?:\.\d+)?)', r'sqrt(\1)', expr)
    expr = re.sub(r'sqrt\s*(\d+(?:\.\d+)?)', r'sqrt(\1)', expr)
    return expr

Combining with Other Expressions

Natural language phrases are converted before evaluation, so they compose freely with other operators and expressions. For example, sqrt of 9 + 9 squared is parsed to sqrt(9) + (9)**2 and evaluates to 84.0.
> sqrt of 9 + 9 squared
= 84.0

> 2 cubed + 4 squared
= 24
Input is automatically lowercased before pattern matching, so SQRT OF 16, Sqrt Of 16, and sqrt of 16 all work identically.

Build docs developers (and LLMs) love