Ingestion
Installed with pip install 'credencegraph[ingest]'. read_latex(path) reads a
.tex document, and the files it inputs, into Chunk objects located by file
and line; chunks_to_graph(chunks, document) turns them into a graph of
anchored document nodes with cites relations. The ingest command does both;
see Ingesting a LaTeX document.
The ingestion layer: readers that turn a document into chunks, and the graph of document nodes built from them.
This package is installed with the ingest extra. The core never imports it. chunks needs no
extra dependency; latex, the LaTeX reader, needs pylatexenc.
Chunk
dataclass
¶
A chunk of a document, as a reader found it.
Attributes:
| Name | Type | Description |
|---|---|---|
unit |
str
|
One of |
text |
str
|
The chunk's text, normalised with |
locator |
str
|
Where it is, such as |
cites |
tuple[str, ...]
|
The bibliography keys the chunk cites, in order of first citation. |
labels |
tuple[str, ...]
|
An equation's labels, in order. |
key |
str | None
|
A bibliography entry's key. |
Source code in src/credencegraph/ingest/chunks.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
chunks_to_graph(chunks, document, initial=None)
¶
Build the graph of document nodes for a document's chunks.
Every chunk becomes a node anchored verbatim: its statement and its anchor's quote are the
chunk's text, and the anchor's digest is the text's digest. An equation with labels carries
attributes.label: its label, or the list of them when it has several. A bibliography entry
carries attributes.key. Each citation becomes a cites relation from the citing chunk to
the first entry with that key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunks
|
Iterable[Chunk]
|
The chunks, in document order. |
required |
document
|
str
|
The document id of every anchor. |
required |
initial
|
Mapping[str, str] | None
|
The type each unit starts with, by unit, as a rubric's |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Graph, list[str]]
|
The graph, and the cited keys that name no bibliography entry, in order of first citation. |
Source code in src/credencegraph/ingest/chunks.py
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 | |
The LaTeX reader: a .tex document, with the files it inputs, as chunks located by file and line.
The reader parses with pylatexenc and keeps the LaTeX source of each chunk, so inline
mathematics, \ref and \cite stay in the text as written. It
- follows
\inputand\include, relative to the main file's directory; - expands the macros defined with
\newcommand,\renewcommand,\providecommandand\def(with#1-style parameters), wherever they are defined; a macro still undefined, or nested deeper thanMAX_EXPANSION_DEPTH, stays as written; - splits running prose into sentences, at
.,?or!followed by a space and a word not starting in lower case, except after a common abbreviation or an initial, and at paragraph breaks, headings, list items and displays; - makes each display (
\[ \],$$ $$,equation,align, ...) an equation chunk with its\labelvalues, each\captiona caption chunk, each\footnotea footnote chunk and each\bibitemof athebibliographya reference chunk; the bibliography is read from the document, or from<main>.bblbeside the main file when the document uses\bibliography; - drops comments,
\labeland headings from the text.
Locators are file=<path relative to the main file's directory>;lines=<first>-<last>.
LatexError
¶
Bases: Exception
A LaTeX document cannot be read: it inputs itself, or a \def body is never closed.
Source code in src/credencegraph/ingest/latex.py
141 142 | |
read_latex(path)
¶
Read a LaTeX document into chunks, in document order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
The main |
required |
Returns:
| Type | Description |
|---|---|
list[Chunk]
|
The chunks. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the main file, or a file it inputs, does not exist. |
LatexError
|
If the document inputs itself. |
Source code in src/credencegraph/ingest/latex.py
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 | |