Spaces:
Sleeping
Sleeping
| import re | |
| from collections import defaultdict | |
| TOPICS: dict[str, list[str]] = { | |
| "Mathematics": [ | |
| "equation", "solve", "integral", "derivative", "matrix", | |
| "polynomial", "algebra", "geometry", "theorem", "proof", | |
| "calculate", "function", "variable", "coefficient", "logarithm", | |
| "trigonometry", "sine", "cosine", "tangent", "factorial", | |
| "prime", "fraction", "ratio", "percentage", "probability", | |
| ], | |
| "Physics": [ | |
| "force", "energy", "mass", "velocity", "acceleration", | |
| "momentum", "gravity", "electric", "magnetic", "wave", | |
| "thermodynamics", "entropy", "quantum", "relativity", "circuit", | |
| "friction", "tension", "pressure", "temperature", "current", | |
| "voltage", "resistance", "frequency", "amplitude", "optics", | |
| ], | |
| "Chemistry": [ | |
| "atom", "molecule", "reaction", "element", "compound", | |
| "bond", "acid", "base", "electron", "proton", "ion", | |
| "periodic", "oxidation", "catalyst", "solution", "mole", | |
| "valence", "isotope", "equilibrium", "titration", "polymer", | |
| "organic", "inorganic", "covalent", "ionic", "redox", | |
| ], | |
| "Biology": [ | |
| "cell", "dna", "rna", "protein", "evolution", "organism", | |
| "photosynthesis", "gene", "mutation", "enzyme", "tissue", | |
| "nervous", "metabolism", "ecosystem", "species", "mitosis", | |
| "meiosis", "chromosome", "ribosome", "membrane", "bacteria", | |
| "virus", "antibody", "hormone", "respiration", | |
| ], | |
| "Computer Science": [ | |
| "algorithm", "array", "loop", "recursion", "complexity", | |
| "binary", "sorting", "compiler", "runtime", "memory", | |
| "stack", "queue", "pointer", "variable", "function", | |
| "class", "object", "inheritance", "database", "network", | |
| "protocol", "encryption", "cache", "thread", "process", | |
| ], | |
| } | |
| def detect_topic(text: str) -> str: | |
| text_lower = text.lower() | |
| scores: dict[str, int] = defaultdict(int) | |
| for topic, keywords in TOPICS.items(): | |
| for kw in keywords: | |
| if re.search(rf"\b{re.escape(kw)}\b", text_lower): | |
| scores[topic] += 1 | |
| if not scores: | |
| return "General STEM" | |
| return max(scores, key=scores.get) | |