Extract TF-IDF Keywords from Text Documents
Rank the most distinctive keywords in a small document set with term frequency, inverse document frequency, and stop-word filtering.
Code
from __future__ import annotations
import math
import re
from collections import Counter
TOKEN_RE = re.compile(r"[a-z][a-z0-9+#.-]{1,}")
STOPWORDS = {
"the", "and", "for", "with", "that", "this",
"from", "into", "your", "are", "was", "have",
}
def tokenize(text: str) -> list[str]:
return [
token
for token in TOKEN_RE.findall(text.lower())
if token not in STOPWORDS and len(token) > 2
]
def keyword_scores(documents: list[str], top_n: int = 5) -> list[tuple[str, float]]:
tokenized = [tokenize(document) for document in documents]
document_frequency: Counter[str] = Counter()
for tokens in tokenized:
document_frequency.update(set(tokens))
total_documents = len(tokenized)
scores: Counter[str] = Counter()
for tokens in tokenized:
counts = Counter(tokens)
length = sum(counts.values()) or 1
for term, count in counts.items():
tf = count / length
idf = math.log((1 + total_documents) / (1 + document_frequency[term])) + 1
scores[term] += tf * idf
return sorted(scores.items(), key=lambda item: (-item[1], item[0]))[:top_n]
def main() -> None:
documents = [
"Use connection pooling to reduce database latency and reuse open sockets.",
"A connection pool keeps sockets warm so database queries avoid handshake latency.",
"Caching responses can hide database latency but risks stale data.",
]
for term, score in keyword_scores(documents, 4):
print(f"{term:12s} {score:.4f}")
assert keyword_scores(documents, 1)[0][0] == "database"
if __name__ == "__main__":
main()
How the snippet works
All of the logic lives in the functions tokenize, keyword_scores and main.
Repetition is confined to for token in TOKEN_RE.findall(text.lower()) — the rest of the file is straight-line. Reach for __future__ and what you get is annotations, Counter — that is the whole API contact area.
The result is a fresh construction, so callers can keep their originals.
Points of care
- Plan for an install step:
__future__is a real dependency, resolved by the package manager.
Applying it
The main signature reads def tokenize(text: str) -> list[str]
Start the program and main runs keyword_scores one after another.
Statements that matter
def tokenize(text: str) -> list[str]:
return [
token
for token in TOKEN_RE.findall(text.lower())
if token not in STOPWORDS and len(token) > 2
]
Interactive Tools for This Topic
N-gram GeneratorGenerate character or word n-grams from text with frequency counts for n from 1 to 10, sorted by occurrence.RAG Document Chunk VisualizerSplit long documents into RAG chunks by character or word count with configurable overlap, boundary alignment, token stats, and highlighted overlaps.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.Text Tokenizer LiteSplit text into word, number, punctuation, and whitespace tokens, with counts per category and the list of unique words.
FAQ
- Does `tokenize` modify its input?
- It does not. Inputs are only read; the return value is freshly built.
- Are there third-party dependencies?
- `__future__` — install it with the package manager before running.
Report a problem with this exampleOpens a pre-filled issue on GitHub.