Sub topic 4
Distant supervision: labels from lexicons and knowledge bases
This chapter assumes you have read the earlier chapters on labeling functions and abstention. You should be comfortable with Python sets and dictionaries, regular expressions, and the idea that a labeling function returns a class or ABSTAIN.
What distant supervision is
Distant supervision creates labels by matching text against an existing resource. The resource was built for some other purpose, such as a sentiment word list, a product catalog, a gazetteer of city names, or a knowledge base (KB) of facts like "Acme is headquartered in Berlin". Nobody read your text and judged it. A program found a match and turned the match into a vote.
The term comes from relation extraction. Mintz and colleagues (2009) took pairs of entities from Freebase and assumed that any Wikipedia sentence mentioning both entities expresses the relation between them. That assumption is wrong for many sentences, but it produced millions of training examples at no labeling cost.
In the weak supervision framework, distant supervision is a family of labeling functions. Each resource-based function is one column in the label matrix. It has the same properties as any other source: limited coverage, imperfect accuracy, and the ability to conflict with other columns.
Lexicon-based labeling functions
A lexicon is a list of words or phrases tied to a class. Sentiment lists, toxic-word lists, and drug-name lists are common examples. The simplest function counts hits and votes for the class with more hits.
Three design choices decide whether such a function is useful:
- Tokenize before matching. Substring tests like
'ill' in textmatch "bill" and "chilly". Compare tokens, or use word-boundary regular expressions. - Abstain on ties and on zero hits. A tie carries no information, and a forced vote adds noise.
- Handle negation or abstain near it. "Not great" contains a positive word and a negative meaning. Abstaining when a negator appears is cheap and avoids a systematic error.
import re
ABSTAIN, NEG, POS = -1, 0, 1
POS_WORDS = {"great", "excellent", "love", "reliable"}
NEG_WORDS = {"broken", "terrible", "refund", "awful"}
NEGATORS = {"not", "no", "never"}
def tokens(text):
return re.findall(r"[a-z']+", text.lower())
def lf_lexicon(text):
toks = tokens(text)
if any(t in NEGATORS or t.endswith("n't") for t in toks):
return ABSTAIN # negation flips meaning; do not guess
pos = sum(t in POS_WORDS for t in toks)
neg = sum(t in NEG_WORDS for t in toks)
if pos == neg: # covers zero hits and ties
return ABSTAIN
return POS if pos > neg else NEG
texts = [
"I love this phone",
"Not great at all",
"Broken and terrible",
"Great screen but broken hinge",
"It is a phone",
]
print([lf_lexicon(t) for t in texts]) # [1, -1, 0, -1, -1]Only two of the five texts receive a vote that is both made and sensible. That is typical. Lexicon functions have high precision on the texts they cover and low coverage. The label model can combine them with broader, noisier sources.
Knowledge-base labeling functions
For relation extraction, each example is a sentence plus a candidate entity pair. The function looks the pair up in a KB and votes based on what it finds.
Absence from the KB is not evidence of a negative. KBs are incomplete, so "not listed" usually means "unknown". Abstain in that case. A negative vote is justified only when the KB gives a stronger signal, such as a functional relation. A company has one headquarters city, so if the KB lists a different city, the candidate pair is probably false.
ABSTAIN, NEG, POS = -1, 0, 1
# relation: headquartered_in (one value per company)
KB = {"Acme": "Berlin", "Globex": "Austin"}
def lf_kb_headquarters(company, city):
known = KB.get(company)
if known is None:
return ABSTAIN # missing from KB means unknown, not false
return POS if known == city else NEG
candidates = [
("Acme, based in Berlin, hired 50 people", "Acme", "Berlin"),
("Acme opened a sales office in Paris", "Acme", "Paris"),
("Initech announced a new product in Denver", "Initech", "Denver"),
("Globex moved its headquarters from Austin to Dallas", "Globex", "Austin"),
]
for text, company, city in candidates:
print(lf_kb_headquarters(company, city), text)
# 1 Acme, based in Berlin, hired 50 people
# 0 Acme opened a sales office in Paris
# -1 Initech announced a new product in Denver
# 1 Globex moved its headquarters from Austin to DallasTwo of the four votes are wrong. The Paris sentence is labeled negative, but it may be a perfectly valid statement about an office, and your relation definition decides whether that counts. The Globex sentence is labeled positive although the text says the headquarters moved away from Austin. The function never reads the sentence. It only checks that the entities match the KB.
These are the two core error types of distant supervision:
- Co-occurrence is not expression. Two entities can appear together without the sentence stating the relation.
- The resource and the text disagree in time or scope. The KB may be stale, or the text may describe a different sense of the relation.
Where lexicon and KB sources go wrong
| Problem | Example | Mitigation |
|---|---|---|
| Ambiguous terms | "Apple" the company and the fruit; "sick" as praise | Require context words, or restrict to unambiguous entries |
| Matching noise | "Paris" in "Paris Hilton" | Use entity linking, or require type agreement |
| Stale resource | Old headquarters, renamed products | Check the resource date; prefer recent snapshots |
| Incomplete KB | New company not listed | Abstain on misses; never treat a miss as negative |
| Domain mismatch | A general sentiment list applied to finance, where "liability" is neutral | Prune or rebuild the list for your domain |
| Class skew | KB facts are mostly positives, so negatives come only from heuristics | Check class balance in the label matrix |
Keeping the source honest
Resource-based functions are easy to write and easy to over-trust, because a large lexicon feels authoritative. Measure them like any other source.
- Check coverage first. Compute the fraction of examples where the function does not abstain. A function that never fires adds nothing, and one that fires on 95% of texts with a broad lexicon is probably voting on weak evidence.
- Spot-check precision on a sample. Read 30 to 50 texts where the function voted and mark each vote right or wrong. This is rough, but it shows whether a source is closer to 60% or 95% accurate. Take the sample from your development set, not the gold set you will use for final evaluation.
- Read the conflicts. When a lexicon function disagrees with a regex or an LLM source, the disagreement often points to a specific lexicon entry to prune.
- Split large resources. One function with a 5,000-word lexicon hides which entries are harmful. Split by confidence tier, for example strongly polar words and mildly polar words. The label model can then give each tier its own estimated accuracy.
Splitting also affects the label model. Functions built from the same resource are strongly correlated, because they hit the same texts for the same reason. The label model assumes sources err independently unless told otherwise. If you add five functions from one lexicon, it may treat them as five independent witnesses and become overconfident. Prefer a few functions over distinct resources, and watch for this dependence in the failure modes chapter.
When to use distant supervision
Use it when a resource already encodes the distinction you care about and entity or term matching is reliable. Named-entity types (gazetteers), relation extraction (KB facts), and sentiment or toxicity (lexicons) all fit. It is a poor fit when the label depends on the reading of the whole sentence, such as sarcasm, stance, or whether a fact is asserted or denied. In those cases the resource tells you what to look for but not what the text says about it. Use it as one source among several and let a model-based or LLM source handle the sentence-level reading.
Exercise
A knowledge base lists (Marie Curie, bornin, Warsaw). A labeling function votes POS for the relation bornin on any sentence containing both "Marie Curie" and "Warsaw", and votes NEG for any sentence containing "Marie Curie" and a city that is not Warsaw. Name two problems with this function and give a fix for each.
Show answer
First, co-occurrence does not mean the sentence states the relation. "Curie visited Warsaw in 1913" gets a POS vote. Fix: require a trigger pattern such as "born in", or abstain unless the sentence contains one. Second, the NEG rule fires on any other city ("Curie worked in Paris"), which is a true statement that does not contradict born_in. The relation is functional, but sentences mention many cities for other reasons. Fix: vote NEG only when the sentence also contains the trigger pattern with the wrong city, and abstain otherwise.
Exercise
You add four labeling functions that each use a different slice of the same sentiment lexicon. On your development set the label model's predicted accuracy for the group looks much higher than your spot-check precision. What is the likely cause, and what do you do?
Show answer
The four functions are correlated, since they match the same texts for the same reason. The label model treats them as independent evidence and becomes overconfident. Merge them into one or two functions, or add sources based on different resources, and compare the model's accuracy estimates with a hand-checked sample again.