names: the folded form of a name and the match a query makes on it, pinned in Latin, Greek, Cyrillic, Arabic and Han (#77)
Fold: NFKD, lower case, combining marks dropped, letters that do not decompose mapped (ø æ œ ß ł đ ð þ ı, final sigma), each Han character a word, apostrophes dropped, other punctuation a space. Match: every word of the folded query a prefix of a word of the folded name, any order. Step 1 of story 007; the service and the page use it next. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
349e992fb1
commit
2423879c03
6 changed files with 209 additions and 3 deletions
|
|
@ -23,9 +23,11 @@ life-weaver id 20000000000 # jm1fj00
|
|||
life-weaver id jm1fj00 # 20000000000
|
||||
```
|
||||
|
||||
`pid` is a public package, as are `person` (the record and the register)
|
||||
and `archive` (the instance an archive tree says it is, below):
|
||||
bytestone-loader imports them to write records into a sandbox archive.
|
||||
`pid` is a public package, as are `person` (the record and the register),
|
||||
`archive` (the instance an archive tree says it is, below) and `names`
|
||||
(how a name is searched: its folded form and the match, story 007):
|
||||
bytestone-loader imports the first three to write records into a sandbox
|
||||
archive.
|
||||
|
||||
## Records
|
||||
|
||||
|
|
|
|||
64
docs/stories/007-search-over-folded-names.md
Normal file
64
docs/stories/007-search-over-folded-names.md
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
# 007 — Search over folded names (#77)
|
||||
|
||||
**Status:** in progress
|
||||
**Roadmap:** towards #10 (search and an index by name, place and year)
|
||||
|
||||
## Why
|
||||
|
||||
Search is a stand-in twice over: the front page narrows the list in
|
||||
the page by JavaScript substring (#76), and `GET /people?q=` matches
|
||||
case-insensitively. Neither reads a name the way its bearer would in
|
||||
their own script, and neither scales past a list in the page. The
|
||||
browser can never hold twenty billion names; what scales is the index,
|
||||
and the reason to run Go in the browser (wasm) is one matcher in both
|
||||
places: the page and the service, and later the key a database indexes.
|
||||
|
||||
## Vocabulary
|
||||
|
||||
| Term | Meaning |
|
||||
|---|---|
|
||||
| Name | What a record calls a person, as written: `name` and `short` |
|
||||
| Folded name | The search form of a name: what is left when script-specific variation that does not tell people apart is removed (`names.Fold`) |
|
||||
| Word | A run of a folded name between spaces; in scripts written without spaces (Han) each character is a word |
|
||||
| Query | What a searcher typed; folded the same way |
|
||||
| Match | Every word of the folded query is a prefix of some word of the folded name, in any order (`names.Match`); an empty query matches every name |
|
||||
| Shard | One file of the static index, the folded names that start with a prefix (later in this story) |
|
||||
|
||||
## Folding
|
||||
|
||||
| Variation | Rule | Example |
|
||||
|---|---|---|
|
||||
| Case | folded | *Drummond* → `drummond`; *Виктория* → `виктория` |
|
||||
| Compatibility forms | NFKD | fullwidth *Victoria* → `victoria` |
|
||||
| Combining marks | stripped | *José María* → `jose maria`; *ΒΑΣΊΛΗΣ* → `βασιλησ` (final sigma folds to sigma); Arabic harakat dropped |
|
||||
| Letters that do not decompose | mapped to their plain letters | ø→o, æ→ae, œ→oe, ß→ss, ł→l, đ→d, ð→d, þ→th |
|
||||
| Arabic alef with hamza or madda | plain alef | *أحمد* → `احمد`; tatweel dropped |
|
||||
| Han | each character a word | *李白* → `李 白` |
|
||||
| Apostrophes | dropped | *O'Brien* → `obrien` |
|
||||
| Other punctuation, brackets, hyphens | a space | *Drummond (née Tyssen-Amherst)* → `drummond nee tyssen amherst` |
|
||||
| Spaces | single, trimmed | |
|
||||
|
||||
Not folded, and so not matched, until a later story: transliteration
|
||||
between scripts (*Victoria* does not find *Виктория*), and German *ö*
|
||||
written *oe*.
|
||||
|
||||
## Matching
|
||||
|
||||
| Name | Query | Match |
|
||||
|---|---|---|
|
||||
| Victoria Alexandrina Drummond | `vict dru` | yes: each word a prefix |
|
||||
| Victoria Alexandrina Drummond | `Drummond Victoria` | yes: order free |
|
||||
| Victoria Alexandrina Drummond | `ria` | no: a prefix, not a substring |
|
||||
| Victoria Drummond | `Victoria Drummond Smith` | no: a query word unmatched |
|
||||
| Ødegaard | `odeg`, `ødeg` | yes |
|
||||
| 李白 | `白` | yes: each character a word |
|
||||
| Виктория Иванова | `Victoria` | no: no transliteration yet |
|
||||
| any | (empty) | yes |
|
||||
|
||||
## What changes
|
||||
|
||||
1. `names`, a public package: `Fold` and `Match`, pinned by the tables above.
|
||||
2. `GET /people?q=` and the front page's search both use it, the page through `kinship.wasm`, which becomes the page's Go.
|
||||
3. A sharded static index by folded prefix, fetched on demand.
|
||||
|
||||
This commit is step 1.
|
||||
1
go.mod
1
go.mod
|
|
@ -5,5 +5,6 @@ go 1.27.3
|
|||
require (
|
||||
github.com/yuin/goldmark v1.8.6
|
||||
golang.org/x/image v0.47.0
|
||||
golang.org/x/text v0.43.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
|
|
|||
2
go.sum
2
go.sum
|
|
@ -2,6 +2,8 @@ github.com/yuin/goldmark v1.8.6 h1:d0VcaP1sx9GkFVkoW+KtggpGi2KZ965i14b0+bDQST4=
|
|||
github.com/yuin/goldmark v1.8.6/go.mod h1:ip/1k0VRfGynBgxOz0yCqHrbZXhcjxyuS66Brc7iBKg=
|
||||
golang.org/x/image v0.47.0 h1:za/8gVz++PPZqmZJBNSISICN5Rrj5zixP/8K++fgmqo=
|
||||
golang.org/x/image v0.47.0/go.mod h1:IkN5o4A7vq55vXN8FWjjnhg9Rs2GOtffqsSi9mWAKYE=
|
||||
golang.org/x/text v0.43.0 h1:1QivrlhwAsnMOcqgOdl9my7s9OyZl8O5/UGUTJVXoWc=
|
||||
golang.org/x/text v0.43.0/go.mod h1:hbSIYA/amXcRXTFSZqh8tWHEoqRvvbKPJ2uz/KU+a+s=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
|
|
|
|||
75
names/names.go
Normal file
75
names/names.go
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
// Package names is how a name is searched: Fold gives a name's search
|
||||
// form, what is left when the variation of script, case and marks that
|
||||
// does not tell people apart is removed, and Match says whether a query
|
||||
// finds a name. The service and the front page (through wasm) share it,
|
||||
// so a name folds the same way wherever it is indexed or typed
|
||||
// (docs/stories/007-search-over-folded-names.md).
|
||||
package names
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"golang.org/x/text/unicode/norm"
|
||||
)
|
||||
|
||||
// letters that do not decompose under NFKD, mapped to their plain letters;
|
||||
// final sigma to sigma, so a Greek name folds the same typed with either.
|
||||
var letters = map[rune]string{
|
||||
'ø': "o", 'æ': "ae", 'œ': "oe", 'ß': "ss", 'ł': "l", 'đ': "d", 'ð': "d", 'þ': "th", 'ı': "i",
|
||||
'ς': "σ",
|
||||
}
|
||||
|
||||
// dropped runes: apostrophes join what they split (O'Brien is obrien); the
|
||||
// Arabic tatweel only stretches a word.
|
||||
const dropped = "'’ʼـ"
|
||||
|
||||
// Fold is the search form of a name: compatibility-decomposed (NFKD) and
|
||||
// lower-cased, combining marks dropped (so accents and Arabic vowel marks
|
||||
// go, and an alef with hamza is a plain alef), letters that do not
|
||||
// decompose mapped to plain ones, each Han character a word of its own,
|
||||
// apostrophes dropped, other punctuation a space, and spaces single.
|
||||
func Fold(s string) string {
|
||||
s = norm.NFKD.String(strings.ToLower(norm.NFKD.String(s)))
|
||||
var b strings.Builder
|
||||
for _, r := range s {
|
||||
switch {
|
||||
case unicode.Is(unicode.Mn, r), strings.ContainsRune(dropped, r):
|
||||
case unicode.Is(unicode.Han, r):
|
||||
b.WriteByte(' ')
|
||||
b.WriteRune(r)
|
||||
b.WriteByte(' ')
|
||||
case unicode.IsLetter(r), unicode.IsDigit(r):
|
||||
if m, ok := letters[r]; ok {
|
||||
b.WriteString(m)
|
||||
} else {
|
||||
b.WriteRune(r)
|
||||
}
|
||||
default:
|
||||
b.WriteByte(' ')
|
||||
}
|
||||
}
|
||||
return strings.Join(strings.Fields(b.String()), " ")
|
||||
}
|
||||
|
||||
// Match says whether query finds name: every word of the folded query is
|
||||
// a prefix of some word of the folded name, in any order. An empty query
|
||||
// finds every name.
|
||||
func Match(name, query string) bool {
|
||||
words := strings.Fields(Fold(name))
|
||||
for _, q := range strings.Fields(Fold(query)) {
|
||||
if !prefixOfAny(words, q) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func prefixOfAny(words []string, q string) bool {
|
||||
for _, w := range words {
|
||||
if strings.HasPrefix(w, q) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
62
names/names_test.go
Normal file
62
names/names_test.go
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
package names
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestFold(t *testing.T) {
|
||||
for _, c := range []struct{ in, want string }{
|
||||
{"Victoria Alexandrina Drummond", "victoria alexandrina drummond"},
|
||||
{"Geraldine Margaret Drummond (née Tyssen-Amherst)", "geraldine margaret drummond nee tyssen amherst"},
|
||||
{" Mary Smith ", "mary smith"},
|
||||
{"Victoria", "victoria"}, // fullwidth: a compatibility form
|
||||
{"José María", "jose maria"},
|
||||
{"Ødegaard", "odegaard"},
|
||||
{"Œuvre", "oeuvre"},
|
||||
{"Straße", "strasse"},
|
||||
{"Łukasz Đorđe", "lukasz dorde"},
|
||||
{"Þórður", "thordur"},
|
||||
{"Æthelred", "aethelred"},
|
||||
{"O'Brien", "obrien"},
|
||||
{"D’Arcy", "darcy"}, // the typographic apostrophe too
|
||||
{"Виктория Иванова", "виктория иванова"},
|
||||
{"ΒΑΣΊΛΗΣ", "βασιλησ"},
|
||||
{"محمَّد", "محمد"}, // shadda and fatha dropped
|
||||
{"أحمد إبراهيم آدم", "احمد ابراهيم ادم"},
|
||||
{"عبدـالله", "عبدالله"}, // tatweel dropped
|
||||
{"李白", "李 白"},
|
||||
{"毛泽东", "毛 泽 东"},
|
||||
{"John 李", "john 李"},
|
||||
{"", ""},
|
||||
} {
|
||||
if got := Fold(c.in); got != c.want {
|
||||
t.Errorf("Fold(%q) = %q, want %q", c.in, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMatch(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
name, query string
|
||||
want bool
|
||||
}{
|
||||
{"Victoria Alexandrina Drummond", "vict dru", true},
|
||||
{"Victoria Alexandrina Drummond", "Drummond Victoria", true},
|
||||
{"Victoria Alexandrina Drummond", "ria", false},
|
||||
{"Victoria Drummond", "Victoria Drummond Smith", false},
|
||||
{"Victoria Drummond", "Victoria Victoria", true},
|
||||
{"Ødegaard", "odeg", true},
|
||||
{"Ødegaard", "ødeg", true},
|
||||
{"José María", "jose", true},
|
||||
{"李白", "白", true},
|
||||
{"李白", "李白", true},
|
||||
{"李白", "杜", false},
|
||||
{"Виктория Иванова", "виктория", true},
|
||||
{"Виктория Иванова", "Victoria", false},
|
||||
{"Victoria Drummond", "", true},
|
||||
{"Victoria Drummond", " ", true},
|
||||
{"", "a", false},
|
||||
} {
|
||||
if got := Match(c.name, c.query); got != c.want {
|
||||
t.Errorf("Match(%q, %q) = %v, want %v", c.name, c.query, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue