Building an AI Calculator Using NLP and Python in 2 different ways
In this article, i'm going to give you a complete guide which is going to take you through creating an AI calculator that understands natural language commands like "add 2 and 3" or "multiply 5 by 10" using Python and basic NLP techniques. A unique thing about this calculator is, it understands human language using NLP as described earlier.
Let's talk about the tools and technologies that we are going to use, so in total you need 3 major things:
- Python
- Natural Language Toolkit (NLTK) for NLP
- Regular expressions (regex) for text pattern matching
Now let's dig deep in step by step process. Every step is connected to another major step so don't get jump into further steps without exactly getting the earlier one.
1. Data Collection
We’ll need a dataset of mathematical expressions and their corresponding results. You can create your own dataset by generating random expressions or use an existing dataset. Each data point should include the expression (e.g., “2 + 3”) and the correct answer (e.g., “5”).
2. Prepare your Environment
- Install Python if you don't have it already.
- Use pip (a tool for installing Python packages)
pip install nltk
3. Setup NLP Components:
There are two options first one is you can use NLP take input and perform operation and provide output to the user second one can be the use a simple neural network with an embedding layer3. LSTM layer, and a dense layer.
Here we will simply download NLTK resources for tokenization and part-of-speech tagging, i.e we are using NLP and python integrations.
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
4. Understand User Commands
Write a function to interpret what the user wants to do using simple text patterns (like "add 2 and 3"). By doing so we can break the command into actual operations need to be performed. An efficient way to do it is writing a function which directly interprets it.
def parse_command(command):
# Defining the patterns for each operation
patterns = [
(r'add (?P<num1>\d+) and (?P<num2>\d+)', 'add'),
(r'subtract (?P<num1>\d+) from (?P<num2>\d+)', 'subtract'),
(r'multiply (?P<num1>\d+) by (?P<num2>\d+)', 'multiply'),
(r'divide (?P<num1>\d+) by (?P<num2>\d+)', 'divide')
]
# Iterating through the patterns to find a match
for pattern, operation in patterns:
match = re.match(pattern, command)
if match:
# Extract operands using named capturing groups
num1 = int(match.group('num1'))
num2 = int(match.group('num2'))
operands = [num1, num2]
return operation, operands
# Returning None if no pattern matches, which is only happens in case of invalid input which is the users fault
return None, None
But what if you don't want to write a function and want to make your own model architecture which can perform well. We’ll use a simple neural network with an embedding layer, LSTM layer, and a dense layer.
import tensorflow as tf
from tensorflow.keras.layers import Embedding, LSTM, Dense
# Define the model
model = tf.keras.Sequential([
Embedding(input_dim=vocab_size, output_dim=embedding_dim, input_length=maseq_length),
LSTM(units=lstm_units),
Dense(units=output_dim, activation='linear')
])
After this if you are creating your own model, you have to use an NLP library to process user input as we are also doing it in the previous way. The basic steps include:
- Use an NLP library (e.g., spaCy) to process user input.
- Extract relevant tokens (numbers and operators) from the user query.
- Convert tokens to numerical representations using the trained embedding layer.
- Pass the embeddings through the LSTM layer to capture context.
- Compute the result using the dense layer.
Step 5: Perform Arithmetic Operations
Now, once we know what operations to do, we have to create a functions to do the math based on what the user asked for:
def perform_operation(operation, operands):
# Defining a dictionary to map operations to their corresponding functions
operations_map = {
'add': lambda x, y: x + y,
'subtract': lambda x, y: y - x,
'multiply': lambda x, y: x * y,
'divide': lambda x, y: y / x
}
# Retrieving the appropriate function based on the operation acc. to the users need
operation_func = operations_map.get(operation)
if operation_func:
# Apply the operation function to the operands
return operation_func(operands[0], operands[1])
else:
return None
But what if you have created your own model architecture and want to train it as per your requirements
We can easily perform training as:
model.compile(optimizer='adam', loss='mean_squared_error')
model.fit(X_train, y_train, epochs=num_epochs, validation_data=(X_val, y_val))
6. Error Handling
- Here we are going to handle cases where the user input is ambiguous or contains errors.
- We have to provide meaningful error messages (e.g., “I couldn’t understand the expression. Please try again.”).
# Check if valid operation and operands are obtained
if operation and operands:
try:
# Perform the requested operation
result = perform_operation(operation, operands)
print("Result:", result)
except ZeroDivisionError:
print("Error: Cannot divide by zero. Please provide valid inputs.")
except Exception as e:
print(f"Error: An unexpected error occurred - {e}. Please try again.")
else:
print("Sorry,“I couldn’t understand the expression. Please try again.")
Step 6: Put It All Together
- Write the main program that interacts with the user, understands their command, and gives the answer:
def main():
print("Welcome to AI Calculator!")
while True:
user_input = input("Please tell me what to calculate (e.g., 'add 2 and 3'): ")
# Parse the user input to extract operation and operands
operation, operands = parse_command(user_input)
# Check if valid operation and operands are obtained
if operation and operands:
try:
# Perform the requested operation
result = perform_operation(operation, operands)
print("Result:", result)
except ZeroDivisionError:
print("Error: Cannot divide by zero. Please provide valid inputs.")
except Exception as e:
print(f"Error: An unexpected error occurred - {e}. Please try again.")
else:
print("Sorry,“I couldn’t understand the expression. Please try again.")
if __name__ == "__main__":
main()
By following these steps, you’ll have a functional AI calculator that understands natural language input and performs basic arithmetic operations.
If you want to make your own, just follow the above steps and Run your Python script.
Leave Comment