Read the paper graph
Load a paper's nodes in reading order and move between them.
The Paper Graph is the structured content of a paper: an ordered list of nodes such as titles, paragraphs and tables.
Load the graph
const graph = await paperful.papers.graph("00000000-0000-0000-0000-000000000000");
for (const node of graph.nodes) {
console.log(node.position, node.type, node.page);
} Load every node
Large papers are loaded one page at a time. Each call to next() adds the following page to graph.nodes, and previous() adds the page before it.
const graph = await paperful.papers.graph("00000000-0000-0000-0000-000000000000");
while (graph.cursor?.next) {
await graph.next();
}
console.log(graph.nodes.length === graph.total); Filter nodes
Only load nodes of some types, or from a single page of the document. Filters are kept when loading more nodes with next() and previous().
const graph = await paperful.papers.graph("00000000-0000-0000-0000-000000000000",
{
type: ["table", "chart"],
page: 3,
}
); See the List nodes API reference for every type.
Read a node
Each node includes:
| Field | Description |
|---|---|
id | The node's ID. |
type | The node's type, such as paragraph_title, text or table. |
text | The plain text content of the node. |
page | The page the node is on. |
position | The node's position in the paper's reading order. |
bbox | Where the node is on the page. |
refs | The text entries that make up the node, with their font, size and position. |
render | A signed URL to an image of the node's region. |
data | Data specific to the node's type, see below. |
Node data
Tables, charts, images and titles are enriched with extra data. Use is() to check a node's type, which also gives you the right type for data in TypeScript.
for (const node of graph.nodes) {
if (node.is("table")) {
console.log(node.data?.summary, node.data?.rows, node.data?.columns);
}
if (node.is("image", "header_image", "footer_image")) {
console.log(node.data?.alt);
}
} | Type | Data |
|---|---|
table | summary, tags, rows, columns, cells, insights and importance. |
chart | The chart type, title, alt, summary, tags, source, insights and importance. |
image, header_image, footer_image | The image type, title, alt, tags, detected objects and entities, and importance. |
doc_title, paragraph_title | The heading level. |
| Any other type | Always null. |
data is null until the node is enriched.
Get a node
Retrieve a single node by its ID, for example a node from a search result. It doesn't need to be loaded in the graph yet.
const graph = await paperful.papers.graph("00000000-0000-0000-0000-000000000000");
const node = await graph.node("00000000-0000-0000-0000-000000000000");
const context = await node.context(); A node retrieved this way isn't added to graph.nodes. Use its context to read around it, since next() and previous() only work for nodes loaded in the graph.
Move between nodes
Each node can reach its neighbours. Missing pages are loaded automatically. To load several neighbours at once, use the node's context.
const node = graph.nodes[0];
const next = await node.next();
const previous = await node.previous(); // undefined at the start of the paper Options
Prop
Type