Spaces:
Sleeping
Sleeping
File size: 6,916 Bytes
ef31c8e ddcb55f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | """
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
@property
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"
))
|