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

MAX_CITATION_LINE_CHARS

Longest line (characters) that can be a one-line citation, not a paragraph.

Functions

ai_extractor(text)

Placeholder extractor that signals AI extraction is needed.

chain(*extractors)

Try extractors in order, return the first non-empty result.

extract_references(text, *[, extractor])

Extract references from document text.

get_extractor(name)

Retrieve a registered extractor by name.

list_extractors()

Return names of all registered extractors.

load_extractors(path)

Load extractors from a JSON file and register them.

markdown_link_extractor(*[, context_chars])

Create an extractor that treats every [name](url) as a reference.

merge(*extractors)

Run all extractors, deduplicate by reference number.

plain_lines_extractor(*[, max_line_chars])

Create an extractor that reads each line as one short citation.

regex_extractor(*[, section_patterns, ...])

Create an extractor from regex patterns.

register(name, extractor)

Register a named extractor.

save_extractors(path, *[, names])

Save regex-based extractors to a JSON file.

Classes

ExtractionResult(references[, ...])

Result of extracting references from text.

Extractor(*args, **kwargs)

Any callable: (str) -> list[Reference].

Exceptions

AIExtractionRequested

Signal that AI-based extraction should be used.

exception citeget.extract.AIExtractionRequested[source]

Bases: Exception

Signal 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: object

Result of extracting references from text.

class citeget.extract.Extractor(*args, **kwargs)[source]

Bases: Protocol

Any 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.

Return type:

list[Reference]

citeget.extract.chain(*extractors)[source]

Try extractors in order, return the first non-empty result.

Return type:

Extractor

citeget.extract.extract_references(text, *, extractor=None)[source]

Extract references from document text.

Parameters:
  • text (str) – Full document text.

  • extractor (Extractor | str | None) – An Extractor callable, a registered extractor name (str), or None to use the "default" chain.

Return type:

ExtractionResult

Returns:

ExtractionResult with parsed references and metadata.

Raises:
citeget.extract.get_extractor(name)[source]

Retrieve a registered extractor by name.

Raises:

KeyError – If name is not registered.

Return type:

Extractor

citeget.extract.list_extractors()[source]

Return names of all registered extractors.

Return type:

list[str]

citeget.extract.load_extractors(path)[source]

Load extractors from a JSON file and register them.

Return type:

list[str]

Returns:

Names of the extractors that were loaded and registered.

Create an extractor that treats every [name](url) as a reference.

Each markdown hyperlink becomes a Reference with:

  • title ← the link text (name)

  • url ← the link target

  • raw ← the surrounding context (up to context_chars around the link)

  • source_text ← same context, preserving original formatting

  • year ← extracted from context if a 4-digit year is nearby

  • authors ← extracted from context if text before the link looks like an author list (heuristic: comma-separated capitalised words)

Parameters:

context_chars (int) – How many characters of surrounding text to capture on each side of the link.

Return type:

Extractor

citeget.extract.merge(*extractors)[source]

Run all extractors, deduplicate by reference number.

Fields from earlier extractors take priority; later ones fill blanks.

Return type:

Extractor

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' or author "Title" , 2019. A line counts only if it names a title, either in quotes or as the text after a name-like Author Name, head; longer lines (prose) are skipped. Numbers are assigned in order.

Return type:

Extractor

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). None means 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:

Extractor

citeget.extract.register(name, extractor)[source]

Register a named extractor.

Return type:

None

citeget.extract.save_extractors(path, *, names=None)[source]

Save regex-based extractors to a JSON file.

Only extractors whose underlying callable carries a _config dict (i.e. those created via regex_extractor()) can be serialised. Built-in names (default, ai) are skipped.

Parameters:
  • path (str | Path) – JSON file to write.

  • names (list[str] | None) – Extractor names to save. None saves all serialisable ones.

Return type:

None