| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "math/rand/v2" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | // SeedSynthetic writes count synthetic team pages as who, in one |
| 11 | // transaction, through the same create path as a person's page: events, |
| 12 | // revisions, title prints and all. Gate 6's 10,000-page fixture and the |
| 13 | // preview use it. Nothing routes to it. |
| 14 | // |
| 15 | // The text is drawn from a fixed vocabulary with a fixed seed, so a |
| 16 | // measurement is repeatable: bodies of 80 to 400 words, with a few |
| 17 | // [[links]] to other synthetic titles and some to titles nobody wrote. |
| 18 | func SeedSynthetic(ctx context.Context, store *Store, who string, |
| 19 | count int) error { |
| 20 | if count <= 0 { |
| 21 | return nil |
| 22 | } |
| 23 | bodies := SyntheticBodies(count) |
| 24 | return store.write(ctx, func(tx *Tx) error { |
| 25 | actor, err := tx.member(who) |
| 26 | if err != nil { |
| 27 | return err |
| 28 | } |
| 29 | for number := range count { |
| 30 | title := syntheticTitle(number) |
| 31 | record := Record{Title: title, Body: bodies[number]} |
| 32 | page, err := tx.create(KindPage, RungTeam, actor, record, actor, |
| 33 | VerbCreated, attachOwn(0)) |
| 34 | if err != nil { |
| 35 | return err |
| 36 | } |
| 37 | if err := tx.claimTitle(page.ID, title); err != nil { |
| 38 | return err |
| 39 | } |
| 40 | } |
| 41 | return nil |
| 42 | }) |
| 43 | } |
| 44 | |
| 45 | // SyntheticBodies is the bodies SeedSynthetic writes for count pages, in |
| 46 | // order. The markup corpus (gate L4) holds a run of them. |
| 47 | func SyntheticBodies(count int) []string { |
| 48 | random := rand.New(rand.NewPCG(7, 11)) |
| 49 | bodies := make([]string, count) |
| 50 | for number := range bodies { |
| 51 | bodies[number] = syntheticBody(random, count) |
| 52 | } |
| 53 | return bodies |
| 54 | } |
| 55 | |
| 56 | // SyntheticTitle is the title SeedSynthetic gives page number. |
| 57 | func SyntheticTitle(number int) string { return syntheticTitle(number) } |
| 58 | |
| 59 | // SyntheticWords is the fixture's vocabulary. |
| 60 | func SyntheticWords() []string { return append([]string(nil), vocabulary...) } |
| 61 | |
| 62 | func syntheticTitle(number int) string { |
| 63 | return fmt.Sprintf("%s %s %05d", strings.ToUpper( |
| 64 | vocabulary[number%len(vocabulary)][:1])+ |
| 65 | vocabulary[number%len(vocabulary)][1:], |
| 66 | vocabulary[(number*7+3)%len(vocabulary)], number) |
| 67 | } |
| 68 | |
| 69 | func syntheticBody(random *rand.Rand, count int) string { |
| 70 | var body strings.Builder |
| 71 | words := 80 + random.IntN(320) |
| 72 | for number := range words { |
| 73 | if number > 0 && number%40 == 0 { |
| 74 | body.WriteString(".\n\n") |
| 75 | } else if number > 0 { |
| 76 | body.WriteString(" ") |
| 77 | } |
| 78 | switch roll := random.IntN(100); { |
| 79 | case roll == 0: |
| 80 | body.WriteString("[[" + syntheticTitle(random.IntN(count)) + "]]") |
| 81 | case roll == 1: |
| 82 | body.WriteString("[[Unwritten " + |
| 83 | vocabulary[random.IntN(len(vocabulary))] + "]]") |
| 84 | default: |
| 85 | body.WriteString(vocabulary[skewed(random)]) |
| 86 | } |
| 87 | } |
| 88 | return body.String() + "." |
| 89 | } |
| 90 | |
| 91 | // skewed picks a word with a long tail, as prose does: a few words are |
| 92 | // everywhere and most are rare. |
| 93 | func skewed(random *rand.Rand) int { |
| 94 | position := random.Float64() |
| 95 | return int(position * position * position * float64(len(vocabulary))) |
| 96 | } |
| 97 | |
| 98 | // vocabulary is 3,000 pronounceable made-up words: no real word, so no |
| 99 | // accident of meaning in a fixture, and a size like a team's working |
| 100 | // vocabulary. |
| 101 | var vocabulary = func() []string { |
| 102 | onsets := strings.Fields("b d f g k l m n p r s t v z br tr st") |
| 103 | vowels := strings.Fields("a e i o u") |
| 104 | var syllables []string |
| 105 | for _, onset := range onsets { |
| 106 | for _, vowel := range vowels { |
| 107 | syllables = append(syllables, onset+vowel) |
| 108 | } |
| 109 | } |
| 110 | words := make([]string, 0, 3000) |
| 111 | for number := 0; len(words) < cap(words); number++ { |
| 112 | first := syllables[number%len(syllables)] |
| 113 | second := syllables[(number/len(syllables))%len(syllables)] |
| 114 | third := "" |
| 115 | if number >= len(syllables)*len(syllables) { |
| 116 | third = syllables[(number*7)%len(syllables)] |
| 117 | } |
| 118 | words = append(words, first+second+third) |
| 119 | } |
| 120 | return words |
| 121 | }() |
| 122 | |