normalize

package
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 9, 2026 License: MIT Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

func BOM(b []byte) int

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

func Clean(s string) string

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

func Control(s string) string

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

func Decode(b []byte) string

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

func DecodeAs(b []byte, enc string) string

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

func Flat(s string) string

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

func Guess(b []byte) (string, float64)

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

func Lines(s string) string

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 Lower

func Lower(s string) string

Lower returns s in lower case.

func Marks

func Marks(s string) string

Marks returns s without combining marks, so "Crème Brûlée" becomes "Creme Brulee". Letters that carry no mark are untouched.

func NFC

func NFC(s string) string

NFC returns s with characters composed: "e" plus a combining acute becomes "é". Text that is already NFC is returned unchanged.

func NFD

func NFD(s string) string

NFD returns s with characters decomposed: "é" becomes "e" plus a combining acute, and marks are put in canonical order.

func NFKC

func NFKC(s string) string

NFKC returns s with compatibility characters folded to their plain equivalents and the result composed: "fi" becomes "fi", "1" becomes "1".

func NFKD

func NFKD(s string) string

NFKD returns s folded like NFKC but left decomposed.

func Quotes

func Quotes(s string) string

Quotes folds curly quotes, dashes and ellipses to their ASCII spelling.

func Repair

func Repair(s string) string

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

func Repeats(pages []string, share float64) []string

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

func Space(s string) string

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

func Tags(s string) string

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

func Text(s string) string

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.

func Title

func Title(s string) string

Title capitalizes the first letter of every word and lowers the rest.

func Upper

func Upper(s string) string

Upper returns s in upper case.

Types

type Chain

type Chain []Step

Chain applies steps in order. The zero Chain returns its input unchanged.

func (Chain) Run

func (c Chain) Run(s string) string

Run applies every step in the chain to s.

type Step

type Step func(string) string

Step is one text transform. Every exported function of the form func(string) string in this package is a Step and can be used as one.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL