Contents Language as Data Code Course home

Chapter 1What Natural Language Processing Is

After this chapter you will be able to

  • name the four kinds of difficulty that language poses (lexical, syntactic, semantic and pragmatic) and give an example of each;

  • explain why the methods that work for programming languages fail for human ones;

  • turn a goal such as “understand complaints” into a task with an input, an output and a way to score it;

  • describe the path this book follows from counting words to large language models.

Teaching machines to read

Natural language processing is the job of getting computers to work with human language. That means reading it, writing it, translating it, answering questions about it, and now holding a conversation in it. Put that way it sounds like any other programming problem, and the fact that it is not is the subject of this book.

A programming language such as Python was designed to be unambiguous. Every well-formed program has one meaning, fixed by a grammar of a few pages that a compiler applies mechanically. Human language was designed by nobody. It grew, and at every level it is full of the one thing a compiler cannot tolerate: ambiguity.

Understanding language is therefore an act of disambiguation. It is not a lookup. The information needed to choose between readings is often nowhere in the sentence at all.

Why language is hard

Take the word bank. It is a place that keeps money, the side of a river, what a plane does when it turns, and a verb meaning to rely on something. You picked the right sense a moment ago without noticing, because the surrounding words supplied it. On its own the word has four senses, and a machine has to settle that before it can do anything else. This is lexical ambiguity, and almost every common word has some.

Ambiguity grows when words combine. The sentence “I saw the man with the telescope” has two grammatical structures, because either I had the telescope or the man did. Figure 1.1 draws both. English supplies no rule for choosing between them, and only knowledge of the situation does.

Figure 1.1. Two parses of one sentence. On the left the prepositional phrase attaches to the verb, so I used the telescope to see. On the right it attaches to the noun, so the man had it. The labels are S for sentence, NP for noun phrase, VP for verb phrase, PP for prepositional phrase, V for verb, N for noun, Det for determiner and P for preposition.

Newspaper headlines show the effect in its purest form, because editors squeeze the words so hard:

“Juvenile court to try shooting defendant”
“Local high school dropouts cut in half”
“Safety experts say school bus passengers should be belted”

Each headline has a sensible reading and an absurd one, and both are equally grammatical. The court may try a defendant accused of a shooting, or try to shoot one. The dropouts may be halved in number, or cut in half themselves. The passengers may be strapped in, or struck with belts. What rules out the absurd reading is what we know about the world. The first turns on how the words group, so it shows syntactic ambiguity, as the telescope sentence did. The second and third turn on what cut and belted mean, so they show semantic ambiguity: the sentence is well-formed and its meaning is still open. Together the two explain why parsing, which is finding the grammatical structure of a sentence, cannot be separated from understanding it.

Beyond the sentence lies pragmatics: meaning that depends on context, intent and convention. “Can you pass the salt?” is a question about your abilities only if you read it literally. At a dinner table it is a request. Sarcasm reverses meaning outright, so that “Well, that went great” said after a disaster means the opposite of what it says, and even people find this hard when tone is the only clue.

All four kinds of difficulty point to one conclusion. The words on the page do not pin down the meaning, and closing that gap takes knowledge that the text assumes but never states. Language is an efficient code between minds that already share a world, and a computer begins by sharing none of it. The field is hard for this reason, and its methods look different from those of ordinary software for the same reason.

What the field does

Faced with a problem that large, the field breaks it into tasks. A task is a specific, measurable job that carves off one piece of the whole, and every task has the same three parts: an input, an expected output, and a way to score how well a system did. The scoring is what makes progress measurable. Table 1.1 lists the tasks you will meet in this book.

Table 1.1. Common tasks. The list continues through speech recognition, spelling correction and dialogue.
task input output
sentiment analysis a review positive or negative
question answering a question, and a passage or none an answer
textual entailment two sentences whether the second follows from the first
machine translation a sentence in one language the sentence in another
named entity recognition a text the people, places and organisations in it
summarisation a long text a shorter text
text generation the start of a text a continuation
Figure 1.2. The three parts of a task. A system turns an input into an output, and a score compares that output with a reference answer. Without the reference and the score there is a program but no task.

Given “The cat sat on the mat” and asked “Where did the cat sit?”, a question answering system should return the mat. That example has all three parts of Figure 1.2: the input is the question with its passage, the output is a phrase, and the score is whether the phrase matches the right one.

Most of the tasks in Table 1.1 have been studied for decades. The way they are solved has changed completely, and Table 1.2 shows the sequence. Several terms in it are new at this point. Each is defined in the chapter where it first matters.

Table 1.2. Five eras of the field. The boundaries are approximate, and each era kept what the one before it learned.
era roughly how a task was solved
hand-written rules 1950s to 1980s experts wrote grammars and dictionaries
statistical models 1990s to 2000s counts from large text collections, one model per task
neural models 2010s learned word vectors, then recurrent networks
pretrained transformers from 2017 one model pretrained once, then adapted
large language models from 2020 one model, often used just by asking

The last two rows of Table 1.2 mark a change. For most of the field’s history each task had its own specialised machinery. Now a single kind of model handles most of them. That model is a large language model, or LLM. It is trained on a very large body of text to do one thing: predict the next token, meaning the next small piece of text, usually a word or part of a word. Later stages adjust which continuations it prefers, as Chapter 21 describes. The objective underneath stays the same.

The shape of this book

The book tells the story of how the field got from counting words to models that converse, and it follows one idea as that idea deepens. The idea is that language resists rules but yields to numbers and to learning.

The story has four movements, and they carry the first five parts. The first is language as data. We gather text into corpora (collections of text), clean it, and measure the statistical regularities that appear in every collection. This is the rest of Part I. The second is meaning as geometry. We turn words into vectors, so that similarity of meaning becomes nearness in space, and that is Part II. The third is language models. We learn to assign probabilities to word sequences. Part III begins with plain counting and builds through neural and recurrent models, and it also covers translation and classification. The fourth is the transformer era: the self-attention architecture, the large language models it made possible, and the craft of working with them, which occupy Parts IV and V.

Two shorter parts close the book. Part VI has one chapter, which puts the machinery to work on summarisation. Part VII asks what these systems cost and whom they can harm.

Each movement keeps what the previous one built and adds one new ability. By the end you will see that the GPT-style model (a generative pretrained transformer) of Chapter 19, the kind that powers chat assistants, is a direct descendant of the word counting in Chapter 10. It answers the same question, what comes next?, with machinery that has grown far more powerful.

Reading about a method is not enough. The text explains how each idea works, and the practice problems at the end of every chapter make you do it, usually by computing a small case by hand. The problems in the second and third tiers use Python, NLTK and a few standard corpora, and Appendix E says how to install and load them. The first worked example comes in the next chapter, and it starts with the raw material for everything that follows: the corpus.

What the book leaves out

A book with this scope has to leave fields out. Part-of-speech tagging, named entity recognition and syntactic parsing each deserve a book, and Jurafsky and Martin (2025) give all three careful treatment. Speech recognition is covered in the same text, and the Handbook of Natural Language Processing (Indurkhya and Damerau 2010) surveys the field’s tasks in breadth. Models that combine language with images or audio are too recent and too fast-moving for a printed chapter, and the current research literature is the place to look. What this book teaches is the machinery that all of these share: the representation of language as numbers, and the models that learn from those numbers.

Summary

Human language is difficult in four ways: the word, the grouping of words, the meaning of a sentence and the situation it is used in. Resolving them needs knowledge that the text does not contain. This is why natural language processing cannot borrow the methods of programming languages. The field splits the problem into tasks, each with an input, an output and a score. Over seventy years the methods for solving them moved from hand-written rules, through statistics and neural networks, to one pretrained model that predicts the next token. This book follows that route in four movements: data, geometry, language models, and the transformer era. The next chapter begins with the raw material, the corpus.

Key terms

ambiguity. a property of a word, phrase or sentence that has more than one reading.

disambiguation. choosing the intended reading of an ambiguous expression.

lexical ambiguity. ambiguity in the meaning of a single word, such as bank.

syntactic ambiguity. ambiguity in how the words of a sentence are grouped.

semantic ambiguity. ambiguity in what a grammatical sentence means.

pragmatics. meaning that depends on context, intent and convention.

task. a specific job with a defined input, an expected output and a way to score a system.

large language model. a model pretrained to predict the next token on very large amounts of text.

Further reading.

Jurafsky and Martin’s Speech and Language Processing (Jurafsky and Martin 2025) is the standard textbook and a good companion to this one. Manning and Schütze’s Foundations of Statistical Natural Language Processing (Manning and Schütze 1999) remains the classic treatment of the statistical foundations, and the Handbook of Natural Language Processing (Indurkhya and Damerau 2010) surveys the full breadth of the field’s tasks.

Practice problems

1.1 Find the ambiguity. For each sentence, describe the two readings and say what knowledge a reader uses to prefer one: (a) “The chicken is ready to eat.” (b) “Flying planes can be dangerous.” (c) “She told her mother that she had won.” Which of the three is settled by world knowledge rather than by grammar?

1.2 Programming versus natural language. List three concrete properties of a programming language that make it mechanically interpretable, and for each one give the property of English that makes it not. Then say why this difference means that parsing a sentence cannot be cleanly separated from understanding it.

1.3 Turn a goal into a task. Pick something you would like a computer to do, such as flagging toxic comments or answering questions about a manual, and specify it as a concrete task. What are the inputs, and what are the outputs? How would you score whether a system did it well, and why is the scoring definition the part that makes progress possible?

1.4 Lexical ambiguity census. Take the word run and list as many distinct senses as you can, both noun and verb. For three of them, write a short sentence and underline the context words that settle the meaning. What does the length of your list suggest about representing a word by one fixed meaning? Chapter 16 returns to that difficulty.

1.5 Draw the parses. Draw two parse trees for “The man saw the dog with a telescope”, following the pattern of Figure 1.1. For each tree, say which word the prepositional phrase with a telescope attaches to, and what that attachment means.

Build and measure

1.6 Count the senses. Use the WordNet interface in NLTK to count the noun senses and the verb senses of run, bank and set. Report the six counts, say which word is the most ambiguous, and compare the count for run with your own list from the census above. Allow ten minutes once NLTK and WordNet are installed.

1.7 Two tokenisers. Split the sentence She didn’t pay $4.50 in New York. on whitespace, and then with nltk.word_tokenize. Report the number of tokens each gives and list the tokens that differ. Say which result you would give to a model and why, and what the exercise shows about quoting a token count without naming the tokeniser.

1.8 The baseline to beat. The NLTK movie_reviews corpus holds 2,000 reviews labelled positive or negative. Before you run anything, write down the accuracy you expect from a classifier that always predicts the more common label. Then compute it. If your prediction was off, say what you had assumed about the labels.

1.9 Ambiguous words in a corpus. Using the Brown corpus in NLTK, count how often bank and run occur in the news category. Read ten occurrences of each and label the sense. Report the counts, the share of each sense you found, and whether the most frequent sense is the one you thought of first.

In practice

1.10 Complaints. A product manager asks for “a model that understands customer complaints”. Turn the request into two measurable tasks, giving each an input, an output and a score. State what data each task needs and which of the two would be cheaper to start on.

1.11 The smart speaker. A user says “Play something by the Stones.” List every ambiguity in the request, name the kind of ambiguity each one is, and say what knowledge or context would resolve it. Which would you ask the user about, and which would you settle without asking?

1.12 Open question. Find a sentence from a recent news story that two careful readers could read differently. Describe both readings, say which kind of ambiguity produces the difference, and explain what a language system would need to know to choose.

Code for this chapter

Each one reproduces the tables above. Run it in the page, change an input and watch which numbers move.

From Natural Language Processing: From Corpus Statistics to Grounded LLMs, by Ramaseshan Ramachandran.