Documentation
¶
Overview ¶
Package parse turns a raw document into text plus the structure the rest of the pipeline needs: section boundaries a splitter can respect, link targets, and — for tabular and tree formats — the decoded value itself.
A Format reads one format and is registered under a short name. Any picks that name from Doc.Meta["format"] when the ingester already knew it, and otherwise from the document's extension and the shape of its text, so a pipeline that does not know what it is reading still needs only one parser.
The Office Open XML formats — docx, xlsx, pptx — are zip archives of XML, and are read here with archive/zip and encoding/xml. Formats this package names but does not read — pdf, and the legacy binary Office formats doc, xls and ppt — are registered as placeholders that fail with ErrFormat. Supplying a reader is one Register call under the same name; nothing else changes.
Parsing never opens a file. Turning a source into a Doc is ingest's job; what parse reads is Doc.Text. Parsing also does not touch the caller's Doc: the returned Doc carries a copy of Meta.
Formats record what they found in Doc.Meta under these keys. Read them with the accessors rather than asserting the types by hand:
format string the format that read the document title string document title, where the format carries one sections []Section spans of Doc.Text links []Link link targets images []Link image targets, alt text as the link text tags map[string]string the document's own metadata pairs fields []string column names (csv, tsv) data any decoded value: JSON any, CSV []map[string]string, XML *Node
A binary format reads its file from Doc.Text, which holds the file's bytes as ingest read them.
Index ¶
- Variables
- func Data(d semantic.Doc) any
- func Detect(d semantic.Doc) string
- func Fields(d semantic.Doc) []string
- func Flatten(v any) map[string]any
- func Names() []string
- func Register(name string, f Format)
- func Tags(d semantic.Doc) map[string]string
- func Title(d semantic.Doc) string
- type Any
- type CSV
- type Docx
- type Email
- type Format
- type HTML
- type JSON
- type Link
- type Markdown
- type Node
- type Pptx
- type Section
- type Text
- type XML
- type Xlsx
Constants ¶
This section is empty.
Variables ¶
var ErrFormat = errors.New("no reader for format")
ErrFormat reports a format this package can name but not read, because no reader is registered under that name or only a placeholder is. Register supplies one.
Functions ¶
func Data ¶
Data returns the decoded value of a structured document: any for JSON, []map[string]string for CSV, *Node for XML, nil for prose.
func Detect ¶
Detect names the format of d, from the extension of its source when that says anything and from the shape of its text otherwise. It falls back to "text", which reads anything.
func Flatten ¶
Flatten reduces a decoded JSON value to its leaves, keyed by path: object keys and array indices joined by ".". An empty object or array has no leaves and so contributes nothing.
Types ¶
type Any ¶
type Any struct{}
Any parses whatever the document turns out to be. It takes the format from Doc.Meta["format"] when the ingester set one and otherwise detects it, so a pipeline reading a mixed corpus needs only this one parser.
type CSV ¶
type CSV struct {
// Sep is the field separator. Zero takes it from the document.
Sep rune
// Bare says the document has no header row, so its fields are numbered
// from one.
Bare bool
}
CSV reads delimited text. The header row names the fields, the rows become the decoded value, and the text becomes one "field: value" line per cell — which is what gives a splitter and an extractor something to work on, since a grid of bare cells says nothing about what the cells mean.
One reader covers the family. Sep set to a tab reads TSV; Sep left at zero takes the separator from the document, which is what a file named .csv but written with semicolons needs.
Rows come back rectangular: every field the header named is present in every row, empty where the row had no cell. Empty cells contribute no text, an empty cell being nothing to assert, so a row of them is no section.
type Docx ¶ added in v0.3.1
type Docx struct{}
Docx reads a WordprocessingML document (.docx). The body is read in order: a paragraph is a line, a heading is a line that starts a section at its level, and a table is written as records, its first row naming the columns. Blocks are separated by a blank line. The core properties — title, creator, dates — come back as tags, and the document's title is its first top-level heading, as in Markdown, falling back to the title in its properties.
A heading is what Word itself outlines as one: a paragraph with an outline level, set on the paragraph or inherited from its style, or a paragraph in one of the built-in "heading N" styles. Word stores those style names in English whatever language the document is written in, so the match holds for every document.
type Email ¶
type Email struct{}
Email reads a mail message. Its headers become the tags, decoded, so a subject written in another script arrives as words rather than as an encoded word; the subject becomes the title; the body becomes the text, one section per part, titled with the part's media type.
A multipart/alternative holds one message written more than once, so only its plainest part is read. An HTML part is read as HTML, which keeps its markup out of the text and puts its link targets in Links. Parts that are not text — the attachments — are not read: their bytes are a different document, and ingest is what turns a source into one.
type Format ¶
Format reads one document format: it returns the document with its text normalized and what it found recorded in Meta.
type HTML ¶
type HTML struct {
// Base resolves relative link and image targets. When it is empty the
// document's own <base href> is used, and failing that its Source.
Base string
}
HTML strips the markup and keeps what the markup meant: the text with its block structure as line breaks, a section per heading, the link and image targets, the title, and the meta elements as tags. Script, style and template content is dropped rather than read as prose.
The scanner is tolerant by design — unclosed tags, stray angle brackets and unquoted attributes are all common in the wild, and a document that a strict parser rejects is exactly the one an ingest pipeline still has to read.
type JSON ¶
type JSON struct{}
JSON reads JSON and, without being told, the line-delimited form: it decodes values until the input runs out, so one object and a stream of records are the same code path. The decoded value goes in Meta as data; the text becomes one "path: value" line per leaf, which is what makes a JSON document something a splitter and an extractor can work on at all. Each record of a stream is a section.
Numbers keep the digits they were written with rather than becoming floats.
type Link ¶
Link is one target and the text that pointed at it. For an image the text is the alt attribute.
type Markdown ¶
type Markdown struct{}
Markdown reads Markdown. The text it returns is the source with any front matter removed — nothing is rewritten, so a reader still sees the emphasis and the code the author wrote — and the structure the source declares comes back in Meta: a section per heading, the links and images, and the front matter as tags.
Headings and links inside a fenced code block are code, not structure, and are ignored.
type Node ¶
type Node struct {
Name string // local name, without its namespace prefix
Space string // namespace URI, empty when the element is in none
Attr map[string]string // attributes by local name
Text string // the element's own character data, spacing collapsed
Nodes []*Node
}
Node is one element: its name, the namespace it is in, its attributes, the character data it holds directly, and the elements inside it.
type Pptx ¶ added in v0.3.1
type Pptx struct{}
Pptx reads a PresentationML deck (.pptx): one section per slide, in the order the deck shows them, titled by the slide's title. A slide's text is its shapes in the order they are drawn — each shape's paragraphs as lines, a table as records — and then its speaker notes, under a "Notes:" line, since the notes are often where a deck says what its slides only list. Slide numbers, dates and footers repeat on every slide and say nothing about any of them, and are left out.
The core properties come back as tags, and the deck's title is the first slide's title, falling back to the title in its properties.
type Section ¶
Section is a span of Doc.Text that stands on its own: a heading and the body under it, a CSV row, a JSON record, a child of an XML root. Start and End are byte offsets into Doc.Text, so a splitter can respect boundaries it did not compute itself. Sections neither nest nor overlap.
type Text ¶
type Text struct{}
Text is the plain-text format. It normalizes line endings and leaves the content alone, recording no sections: prose has no boundaries the document declares, and inventing them here would compete with the splitter's own rule for dividing it.
type XML ¶
type XML struct{}
XML reads XML into a tree and lays that tree out as text: one "path: text" line per element that carries character data, and one "path@name: value" line per attribute, the path being the element names from the root down. Siblings that share a name are numbered, so two <item> elements do not land on the same lines.
The tree itself comes back as Data, since the document's shape is the point of writing it in XML at all. Each child of the root is a section, which for the two feed vocabularies is one section per entry.
Reading is tolerant of what documents in the wild get wrong: a missing end tag is supplied, an unquoted attribute value is read, an unknown entity is left as it was written, and the encoding named in the prolog is ignored, ingest having decoded the bytes to UTF-8 long before parse sees them. A document that ends in the middle of an element is an error, since half a tree is not a smaller tree.
type Xlsx ¶ added in v0.3.1
type Xlsx struct{}
Xlsx reads a SpreadsheetML workbook (.xlsx): one section per sheet, in the workbook's order, titled and headed by the sheet's name. The first row with a value names the columns and every row after it is written as a record of "column: value" lines, as CSV is, so a sheet is something a splitter and an extractor can work on.
A cell is written as the value it holds, not as it is displayed: shared and inline strings as their text, a number as stored, a boolean as TRUE or FALSE. A number in a date or time format is the date it stands for, written in RFC 3339's forms: full-date, partial-time, or the two joined by "T". A workbook records no zone for its dates, so none is written rather than one invented. A format that counts elapsed time, [h]:mm, is a duration and not a date, and its number is left as it is.