Spaces:
Sleeping
Sleeping
Download src/context/entity_extractor.py from Darshan03/nonllm-chatbot-main: direct link, hf CLI and curl.
- Browser
- Download file 6.92 kB
-
https://huggingface.co/spaces/Darshan03/nonllm-chatbot-main/resolve/main/src/context/entity_extractor.py
- Command line
-
hf download hf://spaces/Darshan03/nonllm-chatbot-main/src/context/entity_extractor.py
-
curl -L -o entity_extractor.py https://huggingface.co/spaces/Darshan03/nonllm-chatbot-main/resolve/main/src/context/entity_extractor.py
6.92 kB
| """ | |
| Entity & Keyword Extractor | |
| -------------------------- | |
| Uses spaCy's lightweight `en_core_web_sm` model (~12 MB, CPU-only) to extract: | |
| - Named entities (PERSON, ORG, GPE, etc.) via doc.ents | |
| - Keywords (nouns / proper nouns) via doc.noun_chunks | |
| Falls back to a simple NLTK-free regex approach if spaCy is not installed. | |
| """ | |
| from __future__ import annotations | |
| import re | |
| import logging | |
| from typing import Optional | |
| logger = logging.getLogger(__name__) | |
| # --------------------------------------------------------------------------- | |
| # Attempt to load spaCy once at module import time (lazy-cached) | |
| # --------------------------------------------------------------------------- | |
| _nlp = None | |
| _spacy_available = False | |
| def _load_spacy() -> bool: | |
| """Load spaCy en_core_web_sm lazily. Returns True on success.""" | |
| global _nlp, _spacy_available | |
| if _spacy_available: | |
| return True | |
| try: | |
| import spacy # noqa: F401 | |
| _nlp = spacy.load("en_core_web_sm") | |
| _spacy_available = True | |
| logger.info("spaCy en_core_web_sm loaded successfully.") | |
| return True | |
| except OSError: | |
| logger.warning( | |
| "spaCy model 'en_core_web_sm' not found. " | |
| "Run: python -m spacy download en_core_web_sm" | |
| ) | |
| except ImportError: | |
| logger.warning( | |
| "spaCy is not installed. Run: pip install spacy" | |
| ) | |
| return False | |
| # --------------------------------------------------------------------------- | |
| # Fallback: simple regex tokeniser (no external deps) | |
| # --------------------------------------------------------------------------- | |
| _STOP_WORDS = { | |
| "what", "is", "how", "does", "why", "when", "where", "who", "which", | |
| "a", "an", "the", "of", "in", "to", "for", "on", "with", "and", "or", | |
| "its", "it", "be", "are", "was", "were", "will", "can", "do", "this", | |
| "that", "there", "then", "than", "me", "my", "you", "your", "give", "tell", | |
| "show", "explain", "find", "get", "make", "use", "using", "used", | |
| } | |
| def _fallback_extract(text: str) -> tuple[list[str], list[str]]: | |
| """Simple regex-based keyword extraction (no spaCy required).""" | |
| words = re.findall(r"[a-zA-Z][a-zA-Z_\-]*", text) | |
| keywords = [w.lower() for w in words if w.lower() not in _STOP_WORDS and len(w) > 2] | |
| # No NER in fallback — return empty entities | |
| return [], list(dict.fromkeys(keywords)) # deduplicated, order-preserving | |
| # --------------------------------------------------------------------------- | |
| # Public interface | |
| # --------------------------------------------------------------------------- | |
| class EntityExtractor: | |
| """ | |
| Extracts entities and keywords from a user question. | |
| Usage:: | |
| extractor = EntityExtractor() | |
| entities, keywords = extractor.extract("What is the time complexity of binary search?") | |
| # entities → ['binary search'] (spaCy NER / noun chunks) | |
| # keywords → ['time', 'complexity', 'binary', 'search'] | |
| """ | |
| def __init__(self) -> None: | |
| self._spacy_ready = _load_spacy() | |
| def extract(self, text: str) -> tuple[list[str], list[str]]: | |
| """ | |
| Returns | |
| ------- | |
| entities : list[str] | |
| Named entities and important noun phrases (lowercased). | |
| keywords : list[str] | |
| Deduplicated content words (lowercased), order-preserving. | |
| """ | |
| if not text or not text.strip(): | |
| return [], [] | |
| if self._spacy_ready and _nlp is not None: | |
| return self._spacy_extract(text) | |
| return _fallback_extract(text) | |
| def _spacy_extract(self, text: str) -> tuple[list[str], list[str]]: | |
| doc = _nlp(text) | |
| # --- Entities: named entity recognition --- | |
| entities: list[str] = [] | |
| seen: set[str] = set() | |
| for ent in doc.ents: | |
| key = ent.text.lower().strip() | |
| if key and key not in seen: | |
| entities.append(key) | |
| seen.add(key) | |
| # --- Keywords: noun chunks filtered by POS --- | |
| keywords: list[str] = [] | |
| for chunk in doc.noun_chunks: | |
| # Accept if the root token is a noun or proper noun | |
| if chunk.root.pos_ in {"NOUN", "PROPN"}: | |
| kw = chunk.text.lower().strip() | |
| if kw and kw not in seen and kw not in _STOP_WORDS: | |
| keywords.append(kw) | |
| seen.add(kw) | |
| # Additionally grab isolated NOUN / PROPN tokens not already captured | |
| for token in doc: | |
| if token.pos_ in {"NOUN", "PROPN"} and not token.is_stop: | |
| kw = token.lemma_.lower().strip() | |
| if kw and kw not in seen and len(kw) > 2: | |
| keywords.append(kw) | |
| seen.add(kw) | |
| return entities, keywords | |
| def backend(self) -> str: | |
| """Which backend is active: 'spacy' or 'regex'.""" | |
| return "spacy" if self._spacy_ready else "regex" | |
| DSA_KEYWORDS = { | |
| "dfs", "bfs", "dsa", "graph", "graphs", "tree", "trees", "node", "nodes", "edge", "edges", | |
| "sort", "sorting", "search", "searching", "array", "arrays", "matrix", "vector", "list", | |
| "stack", "stacks", "queue", "queues", "deque", "heap", "heaps", "priority queue", | |
| "hash", "hashing", "hashmap", "hashtable", "trie", "avl", "red-black", "bst", | |
| "binary search", "recursion", "recursive", "backtracking", "greedy", | |
| "dynamic programming", "dp", "memoization", "complexity", "big o", "runtime", | |
| "space complexity", "time complexity", "pointer", "pointers", "sliding window", | |
| "two pointers", "divide and conquer", "topological", "dijkstra", "bellman", "floyd", | |
| "kruskal", "prim", "minimum spanning tree", "mst", "segment tree", "fenwick", | |
| "bit manipulation", "string", "substring", "subsequence", "palindrome", | |
| "linked list", "cycle", "traversal", "inorder", "preorder", "postorder", | |
| "level order", "shortest path", "longest path", "interval", "intervals", | |
| "lru", "lfu", "cache", "knapsack", "levenshtein", "edit distance", "algorithm", | |
| "algorithms", "data structure", "data structures", "quicksort", "mergesort", "heapsort" | |
| } | |
| def is_dsa_query(text: str) -> bool: | |
| """Check if query is related to Computer Science, Data Structures, or Algorithms.""" | |
| if not text: | |
| return False | |
| lower = text.lower() | |
| words = set(re.findall(r"\b[a-z0-9_\-]+\b", lower)) | |
| if words.intersection(DSA_KEYWORDS): | |
| return True | |
| return any(phrase in lower for phrase in ( | |
| "binary search", "time complexity", "space complexity", "linked list", | |
| "dynamic programming", "two pointer", "sliding window", "breadth first", | |
| "depth first", "spanning tree", "shortest path", "topological sort" | |
| )) | |