Sub topic 4
Distant Supervision: Labels from Lexicons and Knowledge Bases
This chapter assumes you know basic Python, supervised text classification, precision, recall, F1, regular expressions, and basic probability. It also assumes you understand that a labeling function can return a class or abstain.
What Distant Supervision Means
Distant supervision creates training labels from an external resource instead of assigning every label by hand. The resource might be a sentiment lexicon, a product catalog, a medical terminology list, or a knowledge base.
For example, suppose a text classifier must detect whether a support message mentions a payment problem. A list of payment terms can provide weak labels:
- Text containing “charged twice” may receive the
payment_problemlabel. - Text containing “shipping address” may receive another label.
- Text containing neither term receives
ABSTAIN.
The labels are called weak because the resource does not fully understand the sentence. The phrase “I was not charged twice” contains a payment term but does not describe the target problem. Distant supervision increases coverage quickly, but its labels can be wrong.
Lexicons as Label Sources
A lexicon is a collection of words or phrases associated with classes. A sentiment lexicon may contain positive words such as “excellent” and negative words such as “broken.” A domain lexicon can map phrases to specific topics.
A simple labeling function should make its rule explicit and abstain when it lacks evidence:
import re
ABSTAIN, NEGATIVE, POSITIVE = -1, 0, 1
positive_terms = re.compile(r"\b(great|excellent|love)\b", re.I)
negative_terms = re.compile(r"\b(broken|terrible|refund)\b", re.I)
def sentiment_from_lexicon(text):
has_positive = bool(positive_terms.search(text))
has_negative = bool(negative_terms.search(text))
if has_positive and not has_negative:
return POSITIVE
if has_negative and not has_positive:
return NEGATIVE
return ABSTAIN
examples = [
"I love the battery life",
"The screen is broken",
"I love it, but the charger is terrible",
]
for text in examples:
print(sentiment_from_lexicon(text), text)The final example contains evidence for both classes, so the function abstains rather than hiding a conflict. Another labeling function can handle the same text, and the label matrix can record both results.
Knowledge Bases and Relations
A knowledge base stores structured facts. For instance, it might connect a product identifier to its category, or connect an organization to its industry. Distant supervision transfers a known relation to text that mentions the related entities.
A common workflow is:
- Find entities in a document.
- Look up those entities in a knowledge base.
- Assign a candidate relation or class.
- Abstain when an entity is missing or ambiguous.
- Treat the result as noisy evidence, not as ground truth.
For example, if a knowledge base says that Acme Bank belongs to the finance industry, a document mentioning Acme Bank may receive a finance label. The document might actually discuss a lawsuit or a hiring event, so the transferred label can be incorrect.
Strengths and Failure Modes
| Property | Benefit | Risk |
|---|---|---|
| Large external resource | Fast coverage over many examples | The resource may be incomplete or outdated |
| Exact phrase matching | Easy to test and explain | Misses spelling, context, and paraphrases |
| Knowledge-base lookup | Uses structured domain facts | Entity linking may select the wrong entity |
| Multiple resources | Provides diverse evidence | Sources may share the same errors |
Negation, sarcasm, domain-specific meanings, and entity ambiguity are common failure modes. Measure coverage, overlap, and conflict on a sample. Then compare weak labels with a small hand-labeled gold set before training the end model.
Exercise
A sentiment lexicon labels “The camera is great, but the battery is terrible” as positive and negative. Should a labeling function choose one class automatically? Why?
Show answer
No. It should usually abstain or emit a conflict that another source can resolve. Choosing one class silently converts contradictory evidence into an unreliable label.