1---2name: run2-keyword-classification3description: Classify academic papers into subject categories using weighted keyword matching with regex word boundaries and case-insensitive search.4metadata:5 source: https://skillmd.com/skills/cxcscmu/run2-keyword-classification6---78# Keyword-Based Document Classification (Improved)910## Key Improvements from Round 1111. **Use `re.IGNORECASE`** instead of lowercasing text - preserves case-sensitive acronyms like DNA, RNA, LLM in patterns while matching case-insensitively.122. **Use `\b` word boundaries** in regex patterns to avoid substring false positives (e.g., "bar" in "Raman" matching music keyword "bar", "opera" matching "operator").133. **Weight keywords by specificity**: Highly specific terms (e.g., "Schwarzschild", "CRISPR-Cas9") get higher weights than generic terms (e.g., "model", "system").144. **Avoid ambiguous single-word keywords**: Words like "bar", "note", "rest", "measure", "scale", "opera" are common across domains. Either use multi-word phrases or add word boundaries.1516## Algorithm171. Extract text from each document (first 3 pages for PDFs, full text for PPTX/DOCX)182. For each category, compute a weighted score by matching regex patterns case-insensitively193. Assign document to the highest-scoring category204. Default to `music_history` if no keywords matched (catch-all)2122## Pattern Design Guidelines23- Use `\b` word boundaries for short/ambiguous words: `r"\bDNA\b"` not `"DNA"`24- Use multi-word phrases where possible: `"trapped ion"` not `"trapped"` or `"ion"`25- Assign higher weights (10) to domain-specific terms that uniquely identify a field26- Assign lower weights (1-2) to generic terms that appear across domains27- Use `re.findall(pattern, text, re.IGNORECASE)` to count matches2829## Example Score Computation30```python31import re3233def classify(text, keywords_dict):34 scores = {}35 for category, keywords in keywords_dict.items():36 score = 037 for pattern, weight in keywords:38 matches = re.findall(pattern, text, re.IGNORECASE)39 score += len(matches) * weight40 scores[category] = score4142 best = max(scores, key=scores.get)43 if scores[best] == 0:44 return "music" # catch-all45 return best46```4748## Category Keyword Strategy49- **LLM**: Focus on NLP/ML terminology, model names (GPT, BERT, Llama), training methods (RLHF, SFT, DPO)50- **Trapped Ion & QC**: Focus on quantum hardware terms (qubit, entanglement, gate), ion-specific terms (paul trap, Lamb-Dicke, sideband)51- **Black Hole**: Focus on GR/astrophysics terms (Schwarzschild, event horizon, spacetime, accretion)52- **DNA**: Focus on molecular biology terms (genome, nucleotide, CRISPR, polymerase, sequencing)53- **Music History**: Focus on music theory/history terms (composer, symphony, genre, melody, lyrics) - also serves as catch-all