Documentation
¶
Overview ¶
Package normalize is the cleanup between parsing a document and splitting it: the same sentence written with a non-breaking space, a curly apostrophe or a decomposed accent should reach the extractor as one string, not three.
Every transform is a Step — a function from string to string — and steps compose into a Chain, so a caller assembles the pipeline the corpus needs instead of arguing with an options struct. Text and Clean are the two chains worth having by default.
The package also normalizes the values that end up as graph nodes: names ([Entity]), dates ([Date]), numbers ([Number]), quantities ([Measure]) and tables of records ([Rows]). Those return typed values and an error rather than a string, because a date that failed to parse is not a date.
Index ¶
- Constants
- func BOM(b []byte) int
- func Clean(s string) string
- func Control(s string) string
- func Decode(b []byte) string
- func DecodeAs(b []byte, enc string) string
- func Flat(s string) string
- func Guess(b []byte) (string, float64)
- func Lines(s string) string
- func Lower(s string) string
- func Marks(s string) string
- func NFC(s string) string
- func NFD(s string) string
- func NFKC(s string) string
- func NFKD(s string) string
- func Quotes(s string) string
- func Repair(s string) string
- func Repeats(pages []string, share float64) []string
- func Space(s string) string
- func Tags(s string) string
- func Text(s string) string
- func Title(s string) string
- func Upper(s string) string
- type Chain
- type Step
Constants ¶
const ( UTF8 = "utf-8" UTF16LE = "utf-16le" UTF16BE = "utf-16be" CP1252 = "windows-1252" Latin1 = "iso-8859-1" )
Encodings this package names. Detection is deliberately shallow: it tells UTF-8 and the UTF-16s apart exactly, and otherwise decides between the two single-byte encodings that account for nearly all Western legacy text.
Variables ¶
This section is empty.
Functions ¶
func BOM ¶
BOM reports the length in bytes of the byte order mark at the front of b, or zero if there is none. Slicing b past it removes the mark.
func Clean ¶
Clean is the default cleanup for text headed into a single field or a similarity key: everything Text does, plus HTML removal, and the line structure flattened to single spaces.
func Control ¶
Control drops the characters that carry no text: C0 and C1 control codes other than tab and newline, the byte order mark wherever it turns up, and the soft hyphen, which is a line-breaking hint that survives copy and paste as an invisible character inside a word.
func Decode ¶
Decode turns bytes into a Go string: byte order mark removed, encoding guessed, bytes that cannot be decoded replaced with U+FFFD. It never fails, because a pipeline that stops on one bad byte is worse than one that keeps a replacement character.
func DecodeAs ¶
DecodeAs turns bytes into a string using the named encoding, for callers who know it from a Content-Type header or an XML declaration. An unknown name falls back to the guess.
func Flat ¶
Flat collapses every run of whitespace, newlines included, to a single space. Use it for text that is going into one field or into a comparison key, where the line structure is noise.
func Guess ¶
Guess names the encoding of b and how far to trust the answer, from 0 to 1. A byte order mark settles it; otherwise valid UTF-8 is taken as UTF-8, a regular pattern of zero bytes as UTF-16, and anything else as one of the single-byte encodings, chosen by whether the bytes reserved for C1 controls are in use.
func Lines ¶
Lines settles line endings on "\n", so text that travelled through Windows or a classic Mac editor reads the same as text that did not.
func Marks ¶
Marks returns s without combining marks, so "Crème Brûlée" becomes "Creme Brulee". Letters that carry no mark are untouched.
func NFC ¶
NFC returns s with characters composed: "e" plus a combining acute becomes "é". Text that is already NFC is returned unchanged.
func NFD ¶
NFD returns s with characters decomposed: "é" becomes "e" plus a combining acute, and marks are put in canonical order.
func NFKC ¶
NFKC returns s with compatibility characters folded to their plain equivalents and the result composed: "fi" becomes "fi", "1" becomes "1".
func Repair ¶
Repair undoes mojibake: text that was UTF-8, read as Latin-1 or windows-1252, and stored that way, so "café" is sitting in the string as "café". It re-encodes the characters back to the bytes they came from and reads those as UTF-8, and only keeps the result if it decodes cleanly — text that was never damaged comes back untouched.
func Repeats ¶
Repeats removes the lines that recur across pages — running heads, footers, page numbers — and returns the pages without them. A line counts as furniture when it shows up on at least share of the pages; share of zero or less means three fifths of them.
Lines are compared with their digits blanked, so "Page 1 of 40" and "Page 2 of 40" count as the same line and a bare page number matches every other bare page number.
func Space ¶
Space regularizes horizontal space without destroying the shape of the text: every kind of space becomes an ordinary one, runs collapse, trailing space goes, and a run of blank lines becomes a single blank line. Paragraphs survive, which is what a splitter wants.
func Tags ¶
Tags removes HTML markup and decodes character references, leaving a space where each element stood so that words do not run together. It leaves the spacing itself alone; follow it with Space or Flat.
Script and style elements go with their contents, since their text is code rather than prose.
func Text ¶
Text is the default cleanup for prose that keeps its shape: compose characters, settle line endings, drop control codes, fold typographic punctuation to ASCII and regularize spacing. Paragraph breaks survive.