infer

package module
v0.0.0-...-8233a1e Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: Apache-2.0 Imports: 0 Imported by: 0

README

go-infer

Guess the likely gender of a person from their name, on the CPU, with no network access and nothing to configure.

result, err := gender.Classify("Katarzyna Nowak", gender.WithLocale("pl"))
// result.Gender == gender.Female
// result.P      == 0.999
// result.Source == gender.SourceLexicon

Read this first

A name does not determine anyone's gender. This library reports what a name usually indicated among people recorded in public name registries. That is a statistical statement about populations, not a fact about the person in front of you, and it will be wrong for a nontrivial number of real people: names are ambiguous, ambiguity depends on language and era, and plenty of people's names simply do not match the expectation.

The API is shaped to keep that visible instead of hiding it:

  • Classify returns a probability, never a bare verdict.
  • GenderUnknown is a normal result, not an error. Names without a usable signal say so.
  • WithMinConfidence makes the library decline to guess below a threshold you choose.
  • Result.Source tells you whether an answer came from recorded data or from a guess about spelling.

If you can ask people their gender instead of inferring it, do that.

Installing

go get gitlab.com/zygoon/go-infer/gender

The module has no dependencies and needs no setup: the name database and the fallback model are embedded in the package. go-infer is the module; gender is its first package -- the layout leaves room to add other inferred attributes and data sources as sibling packages later.

How it answers

Three sources, in descending order of how much they can be trusted. Result.Source reports which one was used.

Source What it means Support
SourceLexicon The name is in the database. Strongest answer. recorded people
SourceSurname A gendered Polish surname ending decided it. 0
SourceModel Nothing was recognized; guessed from spelling. 0

Support is only meaningful for SourceLexicon — check Source before thresholding on it. The reported probability is also capped per source (0.999 / 0.95 / 0.90), so it alone conveys how good the evidence was.

SourceModel is itself locale-aware: it combines a pooled fallback table (trained on every locale's names together) with a small correction specific to the hinted locale, when one exists. An unhinted or unrecognized locale falls back to the pooled table alone, the same "hint, not a filter" rule WithLocale follows everywhere else in this library. See TRAINING.md for why this exists — it is the fix for a real bug, not a design taken on faith.

Surname morphology is consulted before the n-gram model, not after. The model is trained on given names only, so applying it to a surname asks it about input unlike anything it learned, whereas the Polish -ski/-ska ending is near-deterministic. A recognized given name still beats both.

Free-form input is tokenized and every token considered, so Jan Kowalski, Kowalski Jan and Anna-Maria Nowak all work. A recognized given name always beats surname morphology, which is why Maria Kowalski is female rather than being read as male from -ski.

This is a tokenizer, not a name parser: it has no notion of particles (van, de) or of which token is the family name.

Ambiguity is preserved, not resolved

The database stores occurrence counts per name and locale, not one label per name. That is what lets genuinely contested names stay contested:

gender.Classify("Nikola", gender.WithLocale("pl"))     // female, P = 0.999 -- an everyday Polish name
gender.Classify("Nikola", gender.WithLocale("en-GB"))  // female, P = 0.61  -- close to a coin flip in England and Wales

Nikola is recorded for both men and women, and in very different proportions depending on locale; Andrea is a milder version of the same thing (female, but P = 0.69 in Poland against P = 0.76 in England and Wales). A schema with one gender per name could not express this at all. It is also why WithLocale matters: the same name leans different ways in different languages.

WithLocale is a hint, not a filter. An unknown locale combines evidence from every locale rather than failing to answer.

Declining to guess
gender.Classify("Andrea", gender.WithLocale("pl"), gender.WithMinConfidence(0.95))
// Gender == gender.Unknown -- too close to call at that threshold

Use WithMinConfidence wherever the answer drives anything a person will notice. A threshold around 0.9 is reasonable; SourceModel's probability never exceeds 0.90 at all, so a 0.9+ threshold effectively requires a database or surname match.

Current coverage

The shipped artifact covers three locales:

Locale Source Distinct names Occurrences Years
pl PESEL register, Poland 3,596 8.06M 2000–2019
en-US SSA "National data", United States 105,966 375.4M 1880–2025
en-GB ONS "Baby names in England and Wales" 24,535 3.85M 2019–2025

en-US and en-GB are deliberately separate locales, not merged into one en: US and UK given-name distributions differ enough (see Nikola above) that blending them would make the locale hint mean two different things depending on which data happened to load.

Measured on names held out of training, with the lexicon bypassed so the lookup can't trivially cover for the model:

coverage            100.00%   names present in the shipped lexicon
held-out accuracy    90.01%   the only number that measures the model
  pl       92.14%   (293 of 318 held-out Polish names)
  en-US    90.04%   (9528 of 10582 held-out American names)
  en-GB    89.63%   (2205 of 2460 held-out English/Welsh names)

A single blended figure would hide any one locale collapsing behind another merely being larger — en-US alone is 79% of the merged corpus by name count, yet all three land within 3 points of each other. The full three-metric breakdown, and why they're never averaged into one figure, is in TRAINING.md.

Building or retraining the model

The shipped gender.bin is already trained — nothing above needs this. To fetch the source data, retrain with different settings, add a locale, or understand how the model works internally, see TRAINING.md.

Layout

.                      module root; build/ is the only other module -- see TRAINING.md
  docs.go              package comment only, points at gender/
gender/                library package: zero dependencies, embeds gender.bin
  gender.go            public API
  gender.bin           committed artifact (5.39 MB)
  README.md            how to regenerate gender.bin; TRAINING.md has the rest
build/                 separate module, used only to build gender.bin -- see TRAINING.md

Testing

go test ./...

No network access and no GOEXPERIMENT required — the embedded artifact is self-sufficient. See TRAINING.md for testing the trainer itself.

License

The code is this project's. The derived artifact in gender/gender.bin is built from CC0 1.0 public-domain data (Poland, United States) and Open Government Licence v3.0 data (United Kingdom), which is why it can be redistributed and embedded here — see TRAINING.md for the exact sources and their licenses.

Documentation

Overview

Package infer is this module's root and holds no code of its own.

Each inferred attribute lives in its own subpackage, embedding its own model and data sources, so that adding one never pulls in another's. See gitlab.com/zygoon/go-infer/gender to guess the likely gender of a person from their name.

Directories

Path Synopsis
Package gender guesses the likely gender of a person from their name.
Package gender guesses the likely gender of a person from their name.
internal
l10n
Package l10n registers this module's translation catalog and exposes the domain every package in this module uses for translatable errors.
Package l10n registers this module's translation catalog and exposes the domain every package in this module uses for translatable errors.
lexicon
Package lexicon answers "how many people with this name were male or female" from a decoded model artifact.
Package lexicon answers "how many people with this name were male or female" from a decoded model artifact.
modelfile
Package modelfile defines the on-disk format for the name database and the trained fallback model.
Package modelfile defines the on-disk format for the name database and the trained fallback model.
namefold
Package namefold normalizes personal names into stable lookup keys.
Package namefold normalizes personal names into stable lookup keys.
ngram
Package ngram extracts character n-gram features from a name and scores them with a hashed linear model.
Package ngram extracts character n-gram features from a name and scores them with a hashed linear model.

Jump to

Keyboard shortcuts

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