# NLP for Mobility and Transportation

Applying language processing in transport contexts — incident reports, passenger communication and operational text.

**Rests on:** Sentiment Analysis

*Deep Learning · Natural Language Processing*


## Why learn this

This unlocks the ability to build systems that read and act on transport text: automatically flagging a pothole report, rerouting a bus when a driver writes 'delay due to accident', or summarising a day of incident logs. From here you can go on to the neighbouring topics in your library — tools for matching reports to routes with high precision, Sentiment Analysis for passenger feedback, and Speech Emotion Recognition for voice-based driver alerts. You will also need the basics of splitting text into words or phrases and sorting messages that appear in the parent concept, Natural Language Processing, before you meet Large Language Models.


## The intuition

Think of all the written text in a transport system: an accident report typed by a police officer, a passenger's tweet about a delayed train, a bus driver's log entry 'road closed, taking detour'. Each piece is unstructured — it is a string of words, not a neat table of fields. Structured data might tell you the accident happened at a specific GPS coordinate, but only the free text will say 'the driver swerved to avoid a cow and hit the divider'. That detail matters. NLP for mobility is the skill of turning such messy text into something a computer can query and act on. The job is not to understand the text like a human, but to extract the bits that matter — the location, the severity, the action taken — and to do it fast enough that a dispatcher can act before conditions worsen.


## An analogy

Imagine a busy traffic control room. Operators wear headsets and listen to many channels. When a call comes in, they don't just hear words; they filter, interpret, and decide. A report 'broken signal at MG Road, traffic building' becomes a log entry: location = MG Road, type = signal fault, severity = moderate. The operator also knows local shorthand—'jam' means congestion, 'divert' means reroute. The NLP system is that operator, but automated. It learns the jargon: 'accident', 'blocked', 'late', 'rush hour'. But the analogy breaks: the operator has common sense and world knowledge. An NLP model does not know that 'cow on road' is more dangerous than 'cow on farm' unless it has seen such examples. It cannot infer that a 'minor splash' might become a flood. So the model is only as good as the data it was trained on, and it will miss things a human would catch. That is why real systems combine NLP with human review for critical decisions.


## Definition

NLP for mobility and transportation is the application of natural language processing techniques to extract, classify, and act on information from free-text sources in transport settings, such as incident reports, passenger messages, and operational logs.


## How it connects

You have not studied other topics yet, so this is your first step. But the library shows this topic belongs to Natural Language Processing, and it connects to your future notes on Sentiment Analysis (classifying passenger feedback as positive/negative), tools for matching a report to the right road segment with high precision, and Speech Emotion Recognition (if you go into voice alerts). The parent concept of NLP lists splitting text into words or phrases and sorting messages as foundations, which you will touch here.


## The mathematics

This concept does not rest on calculus or algebra in the way training a neural network does. The mathematics is mostly counting and probability — specifically, the frequency of words and the likelihood of a category given a piece of text. The branch is probability, because we are often saying 'given these words, how likely is this report to be about a pothole?' The operation you will learn is the counting of word occurrences and the simple division to turn counts into proportions (probabilities). You will multiply a few counts together to get a single score for a category. No letters needed yet — just numbers. At the heart is the idea of a conditional probability: the chance of one thing happening given that another thing has happened. For example, what is the chance a report is about an 'accident' given that the word 'collision' appears? We estimate that from data by counting how many accident reports contain 'collision' and dividing by the total number of reports that contain 'collision'.

To classify a new text, you compute a score for each category (like pothole, accident) by multiplying the prior probability of that category by the likelihood of the words given that category. The formula for the score of category C is: score(C) = P(C) × product of P(word_i | C) for each word in the text. The category with the largest score is the predicted label.


## A worked example

```
Suppose you have 100 incident reports from a city's road department. 30 of them are about potholes, 20 about signal failures, and 50 about accidents. Among the pothole reports, the word 'crack' appears in 15. Among the accident reports, 'crash' appears in 40. A new report comes in: 'Large crack on the highway, please fix.' You want to decide whether it is about a pothole.

Step 1: P(pothole) = 30 / 100 = 0.30   # The base rate of pothole reports in the dataset.
Step 2: P('crack' | pothole) = 15 / 30 = 0.50   # Of the pothole reports, half contain the word 'crack'.
Step 3: P('crack' | accident) = 0 / 50 = 0.00   # No accident report contained 'crack', so this is zero.
Step 4: Score(pothole) = 0.30 * 0.50 = 0.15   # Multiplying the base rate by the word likelihood gives a raw score.
Step 5: Score(accident) = 0.50 * 0.00 = 0.00   # Accident gets a zero score because of the missing word.

Result: The model chooses the category with the highest score: pothole (0.15) over accident (0.00). So the new report is classified as a pothole. In reality, you would smooth these zeros to avoid overconfidence, but for this example the idea stands.
```


## From scratch, in Python

```python
# Step 1: Teach Python lists and dictionaries first (already done in the lesson)
# Here we define a simple bag-of-words classifier with plain Python.

from collections import Counter

# Training data: list of (text, label) tuples
reports = [
    ("large crack on highway", "pothole"),
    ("pothole near bus stop", "pothole"),
    ("signal not working at junction", "signal"),
    ("traffic light broken", "signal"),
    ("car crash at main road", "accident"),
    ("collision between two buses", "accident"),
]

# Build vocabulary and counts per label
label_counts = Counter()
word_counts = {}  # label -> Counter of words
vocab = set()

for text, label in reports:
    words = text.lower().split()  # simple tokenisation
    label_counts[label] += 1
    if label not in word_counts:
        word_counts[label] = Counter()
    for w in words:
        word_counts[label][w] += 1
        vocab.add(w)

def predict(text):
    words = text.lower().split()
    scores = {}
    total_reports = sum(label_counts.values())
    for label in label_counts:
        # prior
        prior = label_counts[label] / total_reports
        score = prior
        for w in words:
            # likelihood with Laplace smoothing (add 1)
            count = word_counts[label].get(w, 0)
            # total words in this label + vocab size for smoothing
            total_words = sum(word_counts[label].values()) + len(vocab)
            likelihood = (count + 1) / total_words
            score *= likelihood
        scores[label] = score
    # return label with highest score
    return max(scores, key=scores.get)

# Test on a new report
print(predict("broken traffic light"))  # should be 'signal'
print(predict("huge crack on road"))    # should be 'pothole'
```


## The same thing, with a framework

In a real project, you would use scikit-learn's CountVectorizer and MultinomialNB, which do the same counting and multiplication underneath.

```python
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB

# Same data as above
X = [text for text, _ in reports]
y = [label for _, label in reports]

vectorizer = CountVectorizer()
X_vec = vectorizer.fit_transform(X)
clf = MultinomialNB()
clf.fit(X_vec, y)

print(clf.predict(vectorizer.transform(["broken traffic light"])))  # ['signal']
```

What it does underneath: `CountVectorizer` counts word occurrences (like our `word_counts`), and `MultinomialNB` computes the same probability scores we did by hand, with built-in smoothing.


## What the code does

We start with a Python list of tuples — each tuple has a text string and a label. We use Counter to count how many times each label appears. Then for each label, we create a Counter for word frequencies. The `predict` function tokenises the input by splitting on spaces (a simple approach), then for each label it multiplies the prior by the likelihood of each word, using Laplace smoothing to avoid multiplying by zero. Finally, it picks the label with the highest score. This is the essence of a Naive Bayes classifier, which works surprisingly well for text.


## Common misconceptions

**All transport text is the same.** — An accident report and a passenger tweet are just text, so the same model should work for both.

The vocabulary, style, and intent differ greatly. Accident reports are formal and structured; tweets are informal with emojis and abbreviations. A model trained on one will perform poorly on the other. In fact: Each type of text needs its own data and often its own model. Domain adaptation is essential.

**NLP understands language like a human.** — If the model can classify reports, it must understand what 'collision' means.

The model only learns patterns of word co-occurrence. It has no concept of what a collision is. It cannot reason about cause and effect. In fact: NLP models are statistical pattern matchers. They are useful, but they lack common sense.

**You need a huge dataset to do any NLP.** — I can't build anything useful without millions of reports.

With a few hundred labelled examples, you can train a simple bag-of-words classifier that works for a specific domain. It won't be perfect, but it can automate a lot. In fact: Start small. Simple models with a modest dataset can already provide value, and you can grow from there.

**Free-text reports are redundant.** — Structured fields like location and type capture everything important.

Structured fields are limited by what the form designer thought to ask. Free text contains nuances like severity, unexpected causes, and multi-part events. In fact: Free text is a valuable source of information that complements structured data.


## In short

NLP for mobility is about turning messy, free-text transport reports into structured, actionable data. It relies on basic probability counting — the Naive Bayes view — and the code is simple: tokenise, count words, multiply probabilities. Real deployments go further with LLMs and retrieval, but the core is classification.


## Where this is used

In large language models (LLMs) like GPT-4, this concept appears as part of instruction tuning and fine-tuning on domain-specific data. A transport authority might fine-tune an LLM on thousands of incident reports so it can generate structured summaries from free text, extracting location, type, and severity automatically. The mechanism is still classification — the LLM is choosing the most likely next token given the input, but the underlying pattern is the same as our toy classifier: it has seen many examples of reports with labels and learned the statistical associations.

For small language models running on devices (like keyword spotters in a car or a bus), the concept is even more direct. A tiny model might be trained to classify voice commands like 'report accident' or 'road closed' using the same bag-of-words or a small neural network. Real-time constraints mean the model must be fast and memory-efficient, so instead of a full LLM you use a compact classifier like the one we wrote, possibly running on a microcontroller.

In retrieval-augmented generation (RAG), when a dispatcher asks a system 'any pothole reports near MG Road?', the system retrieves relevant incident reports from a database. The retrieval step often uses bi-encoders (which you will learn later) to embed the query and the documents into vectors, and then finds the nearest ones. But the initial filtering of which documents to consider can also use a simple classifier to narrow down to reports about potholes, saving computation. The underlying idea of assigning a category based on words is the same.

In agentic systems that plan and call tools, imagine an agent that monitors traffic reports and automatically sends a message to a maintenance crew when it detects a pothole report. The agent would use a classifier to decide whether the report is actionable (pothole, signal failure) or just chatter. Then it calls a tool to create a work order. The whole loop — perceive, classify, act — relies on NLP classification.

In everyday applied ML, this concept is used for: predictive maintenance (flagging vehicles with high repair frequency from text in service logs), fraud detection in transportation claims (classifying a claim as suspicious based on the wording of a damage report), and passenger feedback analysis (categorising complaints as about punctuality, cleanliness, or staff behaviour). What the developer writes is often a few lines of scikit-learn code to fit a text classifier, then a REST API that takes a report text and returns a category. What breaks when done badly: if the model is trained on a different region's vocabulary (like 'tarmac' vs 'asphalt'), the accuracy drops; if you do not handle typos and abbreviations (like 'pothl' or 'sig. failure'), the word counts become sparse. Also, if the training data has a skewed distribution (e.g., 95% pothole reports), the model will overpredict the majority class. The key is to collect diverse, labelled data and to test on real-world unseen reports.


---

Read this on the web: https://basavapurushottam.com/learn-ai/nlp-for-mobility-and-transportation
