citeget.extract¶
Composable reference extraction from documents.
An extractor is any Callable[[str], list[Reference]]. This module
provides a registry of named extractors, a regex-based factory for creating
new ones, and composition helpers (chain, merge) for combining them.
The main entry point is extract_references(), which runs the default
extraction chain (or a caller-specified extractor) and returns an
ExtractionResult.
Usage:
from citeget.extract import extract_references
result = extract_references(document_text)
for ref in result.references:
print(ref.number, ref.title)
# Custom extractor
from citeget.extract import regex_extractor, register
my_ext = regex_extractor(section_patterns=(r"(?mi)^REFERENCES\s*$",))
register("ieee", my_ext)
# Compose
from citeget.extract import chain
combined = chain(my_ext, extract_references)
Module Attributes
Longest line (characters) that can be a one-line citation, not a paragraph. |
Functions
|
Placeholder extractor that signals AI extraction is needed. |
|
Try extractors in order, return the first non-empty result. |
|
Extract references from document text. |
|
Retrieve a registered extractor by name. |
Return names of all registered extractors. |
|
|
Load extractors from a JSON file and register them. |
|
Create an extractor that treats every |
|
Run all extractors, deduplicate by reference number. |
|
Create an extractor that reads each line as one short citation. |
|
Create an extractor from regex patterns. |
|
Register a named extractor. |
|
Save regex-based extractors to a JSON file. |
Classes
|
Result of extracting references from text. |
|
Any callable: |
Exceptions
Signal that AI-based extraction should be used. |
- exception citeget.extract.AIExtractionRequested[source]¶
Bases:
ExceptionSignal that AI-based extraction should be used.
Not an error — a control-flow signal caught by the CLI or agent layer.
- class citeget.extract.ExtractionResult(references, extractor_name='', confidence='unknown')[source]¶
Bases:
objectResult of extracting references from text.
- class citeget.extract.Extractor(*args, **kwargs)[source]¶
Bases:
ProtocolAny callable:
(str) -> list[Reference].
- citeget.extract.MAX_CITATION_LINE_CHARS = 300¶
Longest line (characters) that can be a one-line citation, not a paragraph.
- citeget.extract.ai_extractor(text)[source]¶
Placeholder extractor that signals AI extraction is needed.
- citeget.extract.chain(*extractors)[source]¶
Try extractors in order, return the first non-empty result.
- Return type:
- citeget.extract.extract_references(text, *, extractor=None)[source]¶
Extract references from document text.
- Parameters:
- Return type:
- Returns:
ExtractionResultwith parsed references and metadata.- Raises:
AIExtractionRequested – If the
"ai"extractor is selected.KeyError – If a string name is not in the registry.
- citeget.extract.markdown_link_extractor(*, context_chars=200)[source]¶
Create an extractor that treats every
[name](url)as a reference.Each markdown hyperlink becomes a
Referencewith:title← the link text (name)url← the link targetraw← the surrounding context (up to context_chars around the link)source_text← same context, preserving original formattingyear← extracted from context if a 4-digit year is nearbyauthors← extracted from context if text before the link looks like an author list (heuristic: comma-separated capitalised words)
- citeget.extract.merge(*extractors)[source]¶
Run all extractors, deduplicate by reference number.
Fields from earlier extractors take priority; later ones fill blanks.
- Return type:
- citeget.extract.plain_lines_extractor(*, max_line_chars=300)[source]¶
Create an extractor that reads each line as one short citation.
For input with no
[N]numbering:Author, 'Title'orauthor "Title" , 2019. A line counts only if it names a title, either in quotes or as the text after a name-likeAuthor Name,head; longer lines (prose) are skipped. Numbers are assigned in order.- Return type:
- citeget.extract.regex_extractor(*, section_patterns=('(?mi)^#{1,3}\\s*(references|bibliography|works\\s+cited|literature)\\s*$',), entry_pattern='^\\s*\\[(\\d+)\\.?\\]', stop_pattern='(?m)^#{1,3}\\s+\\S', whole_document_fallback=True)[source]¶
Create an extractor from regex patterns.
- Parameters:
section_patterns (
tuple[str,...]) – Regexes to find the start of a references section. Tried in order; first match wins. Pass()to skip header detection entirely.entry_pattern (
str) – Regex matching the start of a reference entry. Must have group(1) capturing the reference number.stop_pattern (
str|None) – Regex signalling the end of the section (e.g. a new heading).Nonemeans extract to end of text.whole_document_fallback (
bool) – If no section header is found, scan the whole document for entry_pattern matches.
- Return type:
- citeget.extract.save_extractors(path, *, names=None)[source]¶
Save regex-based extractors to a JSON file.
Only extractors whose underlying callable carries a
_configdict (i.e. those created viaregex_extractor()) can be serialised. Built-in names (default,ai) are skipped.