Text as Data

BASC0005 Quantitative Methods 2: Data Science and Visualisation

Ollie Ballinger

Outline

  1. Case Study: The Grammar of Police Shootings
  2. Natural Language Processing
  3. Analysing Text

The Curious Grammar of Police Shootings

Civilian shoots Civilian

On February 10, 2014, around 6:10 p.m., the victim was in the parking lot in the 13640 block of Burbank Boulevard, to the rear, when he was confronted by the suspect. The suspect produced a semi-automatic handgun and fired numerous times striking the victim in the torso.

Source: Washington Post

Officer Shoots Civilian

When the officers arrived they were confronted by a Hispanic male armed with a sword. The officers attempted to take the suspect into custody by using a taser but it was ineffective. The suspect then ran towards the officers still armed with the sword and an officer-involved-shooting occurred.

The Grammar of a Police Shooting

Text is data, bias can be measured

  • Moreno-Medina, Jonathan, et al. Officer-Involved: The Media Language of Police Killings. No. w30209. National Bureau of Economic Research, 2022.
  • This paper studies the language used in television news broadcasts to describe police killings in the United States from 2013-19
  • Data on: 6,000 police killings, 8,000 non-police homicides, 200,000 stories, and 470,000 sentences that describe the killings.
  • Four dimensions of potential obfuscation relative to an active sentence in which a police officer is the subject
    • (‘police officer killed man’)

Main Findings

  1. The media is significantly more likely to use several language structures - e.g., passive voice, nominalization, intransitive verbs - that obfuscate responsibility for police killings compared to civilian homicides
    1. There is some obfuscation in 35.6% of stories about police killings, compared to 28.8% for civilian homicides
  2. News broadcasts are especially likely to use obfuscatory language structures when the decedent was unarmed or when body camera video is available.
  3. Survey participants (n=2,402) are less likely to hold a police officer morally responsible for a killing and to demand penalties after reading a story that uses obfuscatory language.

Natural Language Processing

Natural Language Processing Pipeline

  1. Sentence Segmentation
  2. Tokenization
  3. Parts-of-Speech Tagging
  4. Lemmatization
  5. Stop Words
  6. Dependency Parsing
  7. Named Entity Recognition
  8. Coreference Resolution

Source: Adam Geitgey

1. Sentence Segmentation

  • Split the text up into sentences
  1. “London is the capital and most populous city of England and the United Kingdom.”
  2. “Standing on the River Thames in the south east of the island of Great Britain, London has been a major settlement for two millennia.”
  3. “It was founded by the Romans, who named it Londinium.”

2. Word Tokenization

  • Split the sentences up into words
  • “London is the capital and most populous city of England and the United Kingdom.”
  • [“London”, “is”, “ the”, “capital”, “and”, “most”, “populous”, “city”, “of”, “England”, “and”, “the”, “United”, “Kingdom”, “.”]

3. Part-of-Speech tagging

Python Implementation

From Text to Table

From word counts to ChatGPT

  • Count which word follows which → a “bigram” language model
  • After “climate”, Exxon says “change” 46% of the time
  • Sample from those probabilities and it writes text
  • An LLM does the same job: predict the next token
    • a neural network replaces the count table

Step 1: Pretraining

  • Text → tokens (sub-word pieces)
  • Task: given the text so far, guess the next token
  • No labels: the text is its own answer key
  • Every wrong guess nudges billions of parameters
  • Learns grammar, facts, style… and biases

Pretraining: the text is its own label

Self-supervised: 1 sentence = 9 training examples, no human labels. A low % is a bad guess, and every bad guess nudges the model’s parameters.

An embedding: “climate” as 100 numbers

climate = [−1.09, 0.00, 1.43, 0.46, −0.01, −1.28, … 94 more]

Words used in similar contexts get similar numbers. Language models learn a vector like this for every token during pretraining.

Step 2: Post-training

  • Fine-tuning: imitate example conversations written by people
  • RLHF: people rank answers; a reward model learns their taste; the LLM chases high scores
    • the reward model is a text classifier — like sentiment analysis (later today)
  • Constitutional AI (Anthropic): the model critiques itself against written principles

Why this matters for you

  • Bias: a model trained on text learns its framing
    • if news writes “officer-involved shooting”, so will the model
  • Hallucination: trained to be plausible, not true
  • Whose text? Consent, copyright, English dominance
  • Next: how does a model represent meaning? → Embeddings (Week 9)
  • Generated by our Exxon bigram model:
  • “our most active research and work program yielded billion to lower cycle approach and there’s insufficient supply bill oil is really key metrics…”
  • Fluent-ish. Confident. Meaningless.

Bias in LLMs: same CV, different letter

  • Wan et al. (2023) asked ChatGPT for reference letters for matched men and women
    • Kelly: “a respected and well-liked member”
    • Joseph: “a natural leader and role model”
  • Measured with today’s tools: spaCy, sentiment classifiers, odds ratios, t-tests
  • Men’s letters: more agentic language (t = 10.5, p < 10⁻²⁵) and more positive
  • The model reproduces the bias in the letters people wrote

Odds that trait words appear in letters for men vs women (ChatGPT, 120 letters). Source: Wan et al. 2023, Findings of EMNLP, Table 2.

Analytical Approaches

Types of Text Analysis

  • The Statistical Features of the Text
    • Word frequency
    • Word proximity
    • Topic modelling
  • Language Processing
    • Entity extraction (Named Entity Recognition)
    • Semantic analysis
    • Sentiment analysis

Word Frequency

Sentiment Analysis

  • Sentiment analysis or opinion mining is the computational study of people’s opinions, sentiments, emotions, appraisals, and attitudes towards entities such as products, services, organizations, individuals, issues, events, topics, and their attributes
  • Uses ‘weighted’ dictionaries of words to assess how ‘positive’ or ‘negative’ a text might be
  • One of the fastest-growing areas of NLP research
  • But, beware the black box

Social Media Data

  • Tumasjan et. al., ‘Predicting Elections with Twitter: What 140 Characters Reveal about Political Sentiment’
  • http://www.aaai.org/ocs/index.php/ICWSM/ICWSM10/paper/viewFile/1441/1852
  • An analysis of over 100,000 Tweets prior to the German Federal Elections, September 2009
  • Questions Raised:
    • Does Twitter provide a platform for political deliberation online?
    • Can Twitter serve as a predictor of election results?

Any Questions?

o.ballinger@ucl.ac.uk