Originally published on CodingCush.
Natural Language Processing, or NLP, helps computers understand, work with, and produce human language. When your phone turns speech into text, or a chatbot answers your question, NLP is doing the work. This guide explains what NLP is, how a computer reads a sentence, and how to run a small NLP demo in Python.
What Is Natural Language Processing?
NLP is a field of AI focused on how computers work with human language. The goal: let a machine read a sentence, work out what it means, and respond in a useful way. The Wikipedia article on natural language processing describes it as a subfield of linguistics, computer science, and artificial intelligence, and that mix is why it is tricky.
Human language is messy. The word "bank" can mean a river side or a place that holds money, and only the sentence tells you which one. To a computer a sentence is just a string of characters until NLP gives it structure.
How Computers Read Text
A computer reads a sentence through a small pipeline:
Step 1: Split the text into pieces. The computer breaks a sentence into tokens, usually words. This step is called tokenization.
Step 2: Clean the text. The computer lowercases everything, so "Love" and "love" count as the same word.
Step 3: Turn words into numbers. Computers like numbers, so each token becomes a number or a list of numbers. Word embeddings place similar words close together.
Step 4: Find patterns. Now the machine looks for patterns: which words appear together, and does the sentence sound happy or angry? The answer comes from these patterns.
Step 5: Produce a result. The last step turns the pattern into a reply, a translation, a spam label, or a summary.
Tokenization in Action: Split a Sentence with Python
Tokenization is the best place to start, because you can see it happen with a few lines of code. The demo uses only the Python standard library. Save it as nlp_demo.py.
import re def tokenize(text): # Lowercase the text, then split it into words. # re.findall picks out runs of letters and numbers, dropping punctuation. return re.findall(r"[a-z0-9]+", text.lower()) sentence = "I love this phone, the camera is amazing!" tokens = tokenize(sentence) print("Tokens:", tokens) print("Token count:", len(tokens)) The import re line brings in the regular expression module. Then tokenize() lowercases the text, and re.findall() with the pattern [a-z0-9]+ grabs every run of letters and digits. Here is the output:
Tokens: ['i', 'love', 'this', 'phone', 'the', 'camera', 'is', 'amazing'] Token count: 8 The tokens are lowercase and the punctuation is gone. Try your own sentence: "don't" becomes don and t, a classic beginner surprise.
A Tiny Sentiment Check in Python
Sentiment analysis guesses whether text sounds positive, negative, or neutral, and the simplest version just counts words from two small lists. Add this below the tokenize function.
def sentiment(tokens): # Count tokens in a small positive list and a small negative list. positive_words = {"love", "great", "amazing", "excellent", "happy", "best"} negative_words = {"terrible", "bad", "awful", "hate", "worst", "poor"} positive = sum(1 for word in tokens if word in positive_words) negative = sum(1 for word in tokens if word in negative_words) if positive > negative: return positive, negative, "positive" if negative > positive: return positive, negative, "negative" return positive, negative, "neutral" positive, negative, result = sentiment(tokens) print("Positive words:", positive) print("Negative words:", negative) print("Sentiment:", result) The function counts the tokens in each word list, then compares the two counts. More positive words means a positive result, more negative words means a negative result, and a tie means neutral. Here is the output:
Tokens: ['i', 'love', 'this', 'phone', 'the', 'camera', 'is', 'amazing'] Token count: 8 Positive words: 2 Negative words: 0 Sentiment: positive The sentence scores positive because it contains "love" and "amazing". This lexicon based approach still runs inside real products, but it is easy to fool: "Oh great, another update that broke everything" contains the word "great", so this demo calls it positive. Real systems learn from thousands of labelled examples instead.
How NLP Connects to Machine Learning
Early NLP systems used hand written rules, like "if a sentence contains the word 'refund', send it to the billing team". That works for simple cases, but language has too many exceptions for rules alone. Modern NLP leans on machine learning instead: the system studies examples and learns the patterns itself. If machine learning still feels fuzzy, start with our guide to what machine learning is, explained simply.
A sentiment model might train on 10,000 movie reviews labelled positive or negative. It soon notices that "boring" appears mostly in negative reviews, while "brilliant" appears mostly in positive ones. The most powerful NLP systems today are large language models, trained on huge amounts of text. ChatGPT is the most famous example: our guide to how ChatGPT works, explained simply covers what it can and cannot do.
Rule-Based NLP vs Machine-Learning NLP
Both approaches are still alive: rules win when the task is narrow and exact, learning wins when it is broad and messy.
| Feature | Rule-based NLP | Machine-learning NLP |
|---|---|---|
| How it works | Hand written rules and word lists | Learns patterns from training data |
| Best for | Fixed formats like dates and order numbers | Open tasks like chatbots and translation |
| Accuracy on new text | Exact on covered cases | Handles new wording |
| Effort to build | Fast to start | Needs labelled data |
| Example | A bot that reads "track order 4821" | A bot that understands "where is my parcel?" |
Many products mix the two: rules pull out the order number, a model works out what the customer wants. The Google Cloud Natural Language basics documentation lists common building blocks, such as entity analysis, sentiment analysis, and syntax analysis.
The Google Cloud Natural Language basics documentation.
Where You Meet NLP Every Day
You probably used NLP several times today without noticing. Chatbots answer questions and reset passwords by reading your message and matching it to an intent. Machine translation turns a page into another language in seconds. Search engines use NLP to understand your query, not just match keywords.
Sentiment analysis reads thousands of reviews and reports whether people are happy or upset. Spam filters decide what is junk. Voice assistants turn speech into text and work out your intent. All of them start with the same pipeline: split, clean, count, find patterns, and act.
Common Mistakes and Troubleshooting
Skipping tokenization and comparing raw strings. If you compare whole sentences, "I love this phone" and "i love this phone!" look different to the computer. Always lowercase and tokenize first.
Trusting a word list too much. The sentiment demo calls sarcastic sentences positive, because it cannot see tone. Word lists are a teaching tool, not a finished product. For real results, use a tested NLP library.
Forgetting that one word can have many meanings. A model trained on tech reviews may read "this film was sick" as negative, because "sick" usually meant ill there. Check your system on text from your own topic.
Removing stop words too early. Dropping "not" seems harmless, but "not good" and "good" mean the opposite of each other. Keep negation words when your task depends on them.
Getting a NameError or an empty token list. A NameError: name 're' is not defined means you forgot the import re line. An empty token list usually means the input was empty, so print it before tokenizing. If your code breaks, our guide to debugging code with AI prompts shows how to ask well.
Frequently Asked Questions
What is NLP in simple words?
NLP lets computers understand and produce human language. It turns text and speech into data a machine can use, then turns the answer back into words.
What is the difference between NLP and AI?
AI is the broad field of making computers do tasks that need human intelligence. NLP is one part of it, focused only on language.
Is ChatGPT an example of NLP?
Yes. ChatGPT is a large language model, one of the most powerful kinds of NLP system. It reads your prompt and predicts a useful reply word by word.
Do I need machine learning to learn NLP?
No, not to start. Begin with tokenization and word counting in plain Python. Machine learning helps when you want to handle new wording, not only cases you listed by hand.
Which programming language is best for NLP?
Python is the usual first choice, because it has the most NLP libraries and tutorials. Popular libraries include NLTK, spaCy, and Hugging Face Transformers.
What are some real world uses of NLP?
Chatbots, machine translation, sentiment analysis, spam filtering, voice assistants, search, and grammar checkers are the most common. Hospitals read medical notes with it, and shops sort feedback.
Conclusion
NLP is how computers learned to read: it starts by splitting a sentence into tokens and counting words, and grows into systems that translate languages and hold conversations. Run the demo, change the sentences, and watch the results change.


