A text classifier with its vocabulary inside.
Fit TF–IDF and a classifier as one object so words from held-out text cannot leak in.
Why this workflow exists.
Text needs a numeric representation before a classifier can use it. The TF–IDF vectorizer learns a vocabulary and word weights; the logistic classifier learns how those weights relate to a label. Putting them in one pipeline means a split or cross-validation fold fits both stages only on its training messages.
The toy labels here are topics, not an assessment of whether a message is good or bad. The documents are intentionally obvious so the code's flow is readable. Real text systems need more data, near-duplicate controls, language coverage, and source-aware evaluation.
Code context. Python 3 with scikit-learn installed. The example messages are included in the script.
The whole example.
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.metrics import classification_report
messages = [
"the team won the final match", "a player scored twice",
"the coach planned a training session", "fans watched the game",
"the goalkeeper saved the shot",
"the market closed higher", "the company reported revenue",
"investors reviewed the budget", "shares rose after earnings",
"the finance team revised costs",
]
labels = ["sport"] * 5 + ["finance"] * 5
X_train, X_test, y_train, y_test = train_test_split(
messages, labels, test_size=0.4, stratify=labels, random_state=3
)
model = make_pipeline(TfidfVectorizer(ngram_range=(1, 2)),
LogisticRegression(max_iter=1000))
model.fit(X_train, y_train)
print(classification_report(y_test, model.predict(X_test), zero_division=0))
print(model.predict(["the match ended after extra time"]))What each stage does.
The first five messages are sports-related, and the last five are finance-related, matching labels. The split holds out four documents while keeping both classes represented. TfidfVectorizer learns tokens and adjacent token pairs only from training messages, then LogisticRegression learns a boundary in that numeric feature space. The same pipeline transforms held-out and new text using its fitted vocabulary.
classification_report shows precision, recall, and support for each class. With so few documents, one held-out mistake moves a score dramatically, so the displayed decimals are not a dependable performance estimate. The final prediction illustrates how the pipeline accepts raw text and returns a label without the caller manually applying the vectorizer.
What the output means.
The script prints a small classification report and a topic prediction for a new sentence. The tiny dataset exists to explain the representation and pipeline boundary.
Where the example stops.
Duplicate templates, different sources, multilingual text, and topic drift can break a seemingly strong text classifier. Those conditions need evaluation with representative documents.