keyword_extractor
zocrates.rag.keyword_extractor ¶
Zero-dependency 5-feature statistical domain keyword extractor and tag retrieval.
Extracts high-precision keyphrases deterministically from academic text and structural digests in < 0.2 ms on CPU using standard library primitives only (re, math, collections). Supplements extracted keyphrases with native and user tags from Zotero SQLite.
GENERIC_SECTION_HEADERS
module-attribute
¶
GENERIC_SECTION_HEADERS = {'abstract', 'introduction', 'background', 'preliminaries', 'related work', 'methods', 'method', 'materials and methods', 'methodology', 'results', 'discussion', 'results and discussion', 'conclusion', 'conclusions', 'references', 'bibliography', 'acknowledgments', 'acknowledgements', 'data availability', 'code availability', 'online content', 'supplementary material', 'appendix', 'overview', 'setup', 'table of contents', 'author contributions'}
GENERIC_TAGS
module-attribute
¶
STOPWORDS
module-attribute
¶
STOPWORDS = {'a', 'about', 'above', 'after', 'again', 'against', 'all', 'am', 'an', 'and', 'any', 'are', 'aren', 'as', 'at', 'be', 'because', 'been', 'before', 'being', 'below', 'between', 'both', 'but', 'by', 'can', 'could', 'did', 'do', 'does', 'doing', 'down', 'during', 'each', 'few', 'for', 'from', 'further', 'had', 'has', 'have', 'having', 'he', 'her', 'here', 'hers', 'herself', 'him', 'himself', 'his', 'how', 'i', 'if', 'in', 'into', 'is', 'it', 'its', 'itself', 'just', 'me', 'more', 'most', 'my', 'myself', 'no', 'nor', 'not', 'now', 'of', 'off', 'on', 'once', 'only', 'or', 'other', 'our', 'ours', 'ourselves', 'out', 'over', 'own', 'same', 'she', 'should', 'so', 'some', 'such', 'than', 'that', 'the', 'their', 'theirs', 'them', 'themselves', 'then', 'there', 'these', 'they', 'this', 'those', 'through', 'to', 'too', 'under', 'until', 'up', 'very', 'was', 'we', 'were', 'what', 'when', 'where', 'which', 'while', 'who', 'whom', 'why', 'with', 'would', 'you', 'your', 'yours', 'yourself', 'study', 'propose', 'paper', 'article', 'present', 'show', 'using', 'based', 'approach', 'novel', 'results', 'method', 'methods', 'methodology', 'work', 'one', 'two', 'three', 'also', 'however', 'although', 'introduction', 'background', 'discussion', 'conclusion', 'conclusions', 'references', 'preliminaries', 'et', 'al', 'fig', 'figure', 'table', 'section', 'sec', 'given', 'let', 'may', 'see', 'since', 'well', 'http', 'https', 'com', 'org', 'www', 'html', 'pdf', 'url', 'like', 'get', 'make'}
build_search_query ¶
Build a deduplicated, high-signal search query combining title, keywords, and tags.
Guarantees high-IDF lexical coverage without repeating words or bloating the query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
Document title. |
required |
keywords
|
list[str]
|
Extracted statistical keywords. |
required |
tags
|
list[str] | None
|
Optional user or automatic Zotero tags. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Deduplicated space-separated query string capped at 35 terms. |
Source code in zocrates/rag/keyword_extractor.py
clean_markdown_for_kw ¶
Strip code blocks, URLs, HTML tags, LaTeX math formulas, and link markup.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Raw Markdown text. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Cleaned text suitable for keyword extraction. |
Source code in zocrates/rag/keyword_extractor.py
extract_statistical_keywords ¶
Pure-Python 5-feature statistical keyphrase extractor (RAKE + YAKE principles).
Extracts high-precision compound keyphrases in < 0.2 ms on CPU using: 1. Co-occurrence degree / frequency ratio. 2. Casing & acronym multipliers (1.8x ALL_CAPS, 1.3x proper nouns). 3. Title positional boost (2.0x). 4. Sentence spread log-multiplier. 5. Length normalization favoring 2-3 word technical noun phrases.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
Document title. |
required |
text
|
str
|
Text to extract from (e.g. structural digest or abstract). |
required |
max_keywords
|
int
|
Maximum number of keywords to return. |
6
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of extracted keyphrase strings. |
Source code in zocrates/rag/keyword_extractor.py
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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |
extract_structural_digest ¶
Extract lead abstract/intro plus topical H1/H2/H3 section headers.
Filters out standard academic IMRAD boilerplate headers so only substantive topical section headings are retained alongside lead text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc_text
|
str
|
Full document markdown string. |
required |
max_lead
|
int
|
Maximum character length for lead prose. |
2000
|
Returns:
| Type | Description |
|---|---|
str
|
Structural digest string combining lead text and topical section headers. |
Source code in zocrates/rag/keyword_extractor.py
get_item_tags ¶
Retrieve user and automatic tags for an item from zotero.sqlite.
Excludes generic workflow tags (e.g. 'read', 'unread', 'to-read', 'favorite'). If item is an attachment, also resolves tags from its parent item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
zotero_dir
|
Path | str
|
Path to the root Zotero data directory. |
required |
item_id
|
int | None
|
Optional integer itemID in zotero.sqlite. |
None
|
item_key
|
str | None
|
Optional string key in zotero.sqlite (items.key). |
None
|
tag_type
|
int | None
|
Optional integer tag type filter (0 = user/manual tag, 1 = automatic tag). If None (default), returns all tag types. |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of non-generic tag name strings. |
Source code in zocrates/rag/keyword_extractor.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | |
is_generic_section_header ¶
Check if a section heading is standard academic boilerplate (IMRAD, References, etc.).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
h
|
str
|
Raw header string (e.g. '## 1. Introduction'). |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the header is generic and should be excluded. |