Sub topic 1
Why Labeled Text Is the Bottleneck
Prerequisites
This chapter assumes you can write basic Python, know what supervised text classification is (features, training, evaluation), and can read a precision, recall, and F1 score. Familiarity with regular expressions helps for the examples.
The labeling bottleneck
A supervised classifier needs labeled examples: texts paired with the correct class. Hand-labeling costs time and money, and it scales linearly with the data you need. Domain experts are often required, and their labels disagree with each other. Weak supervision addresses this by trading some label accuracy for much larger label volume.
Labeling functions that can abstain
A labeling function (LF) is a small program that returns a class for an input or abstains. Abstaining means the function declines to vote. Abstention lets each LF cover only the cases it understands.
import re
ABSTAIN, NEG, POS = -1, 0, 1
def lf_positive_words(text):
return POS if re.search(r"\b(great|excellent|love)\b", text, re.I) else ABSTAIN
def lf_negative_words(text):
return NEG if re.search(r"\b(refund|broken|terrible)\b", text, re.I) else ABSTAIN
lfs = [lf_positive_words, lf_negative_words]
texts = [
"I love this phone, great battery",
"Arrived broken, I want a refund",
"It is a phone",
]
print([[lf(t) for lf in lfs] for t in texts]) # [[1, -1], [-1, 0], [-1, -1]]Distant supervision from lexicons
Distant supervision is a labeling approach that uses an external resource, such as a sentiment lexicon (a word list with polarity scores) or a knowledge base, to assign labels. A lexicon-based LF is a dictionary lookup wrapped in a labeling function. Its accuracy depends on how well the lexicon matches your domain.
The label matrix
Apply every LF to every example and store the results in a matrix. Rows are examples, and columns are sources. Each cell holds a class or ABSTAIN. The label matrix is the input to the aggregation step, which combines the votes into one label per example.
Exercise
In the label matrix above, which LF abstains on "It is a phone", and why does that matter?
Show answer
Both LFs abstain. No positive or negative keyword appears, so the example gets no vote. Aggregation must handle examples with no votes, for instance by leaving them unlabeled.