oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1package mapa
2
3import (
4 "amadan.net/oficina/mapa/internal/mapa/locale"
5
6 "math"
7 "sort"
8)
9
10// BM25's usual constants.
11const (
12 saturation = 1.2
13 lengthNorm = 0.75
14 // expansions caps how many dictionary terms a typed prefix stands
15 // for, per partition: the most frequent first.
16 expansions = 32
17)
18
19// teamStats is everything a ranking uses that is not the document
20// itself, computed over team pages only (§6.2): so nobody's private
21// writing can change the order of anybody's results, their own
22// included.
23type teamStats struct {
24 documents float64
25 average float64
26 index *Index
27}
28
29func (index *Index) stats() teamStats {
30 documents := float64(max(index.teamTerms.documents, 1))
31 average := float64(index.teamTerms.words) / documents
32 return teamStats{documents: documents, average: max(average, 1),
33 index: index}
34}
35
36// weight is a term's inverse document frequency over team pages. A term
37// in no team page is smoothed as if it appeared in one (BM25's +1).
38func (stats teamStats) weight(term string) float64 {
39 frequency := float64(max(stats.index.teamTerms.frequency(term), 1))
40 return math.Log(1 + (stats.documents-frequency+0.5)/(frequency+0.5))
41}
42
43// scored is one candidate page.
44type scored struct {
45 pageID int64
46 score float64
47}
48
49// bodyHits ranks one partition's pages whose words hold every token,
50// the last one as a prefix, leaving out the pages already taken. One
51// typed letter searches titles only: as a prefix it would stand for
52// most of the dictionary.
53func (index *Index) bodyHits(part *partition, tokens []string, limit int,
54 taken map[int64]bool, dates *locale.Locale) []Hit {
55 last := tokens[len(tokens)-1]
56 if len(tokens) == 1 && len([]rune(last)) < 2 {
57 return nil
58 }
59 candidates := part.match(tokens, index.stats())
60 kept := candidates[:0]
61 for _, candidate := range candidates {
62 if !taken[candidate.pageID] {
63 kept = append(kept, candidate)
64 }
65 }
66 // The backlink boost re-orders the head of the list only.
67 head := topK(kept, limit*4, better)
68 for number := range head {
69 head[number].score += index.boost(head[number].pageID)
70 }
71 sort.Slice(head, func(left, right int) bool {
72 return better(head[left], head[right])
73 })
74 var hits []Hit
75 for _, found := range head[:min(len(head), limit)] {
76 page := index.pages[found.pageID]
77 hits = append(hits, Hit{ID: page.id, Label: page.label(dates),
78 Href: page.href(), Kind: page.kind, Look: page.look(),
79 Snippet: snippet(page, tokens)})
80 }
81 return hits
82}
83
84func better(one, other scored) bool {
85 if one.score != other.score {
86 return one.score > other.score
87 }
88 return one.pageID < other.pageID
89}
90
91// searchPartitions is the team partition and the scope's own: read
92// directly, never filtered from a whole.
93func (index *Index) searchPartitions(scope Scope) []*partition {
94 parts := []*partition{index.teamTerms}
95 if own := index.ownTerms[scope.Member]; scope.Member != 0 && own != nil {
96 parts = append(parts, own)
97 }
98 return parts
99}
100
101// boost is how many team pages link to a page: team links only, so a
102// private note's links move nothing (§6.2).
103func (index *Index) boost(pageID int64) float64 {
104 links := 0
105 for _, fold := range index.pages[pageID].titleFolds() {
106 if found := index.linkers[fold]; found != nil {
107 links += len(found.team)
108 }
109 }
110 return 0.5 * math.Log1p(float64(links))
111}
112
113// match scores this partition's documents holding every token.
114func (part *partition) match(tokens []string, stats teamStats) []scored {
115 scores := make([]float64, len(part.pages))
116 matched := make([]uint8, len(part.pages))
117 for place, token := range tokens {
118 terms := []string{token}
119 if place == len(tokens)-1 {
120 terms = part.prefixed(token)
121 }
122 part.accumulate(terms, uint8(place+1), scores, matched, stats)
123 }
124 var out []scored
125 for number, count := range matched {
126 if int(count) == len(tokens) {
127 out = append(out, scored{pageID: part.pages[number],
128 score: scores[number]})
129 }
130 }
131 return out
132}
133
134// accumulate adds each term's BM25 to the documents holding it, and
135// counts a document once per token however many expansions it holds.
136func (part *partition) accumulate(terms []string, token uint8,
137 scores []float64, matched []uint8, stats teamStats) {
138 for _, term := range terms {
139 list := part.terms[term]
140 if list == nil {
141 continue
142 }
143 weight := stats.weight(term)
144 for at, number := range list.documents {
145 count := float64(list.counts[at])
146 length := float64(part.lengths[number])
147 scores[number] += weight * count * (saturation + 1) /
148 (count + saturation*(1-lengthNorm+
149 lengthNorm*length/stats.average))
150 if matched[number] == token-1 {
151 matched[number] = token
152 }
153 }
154 }
155}
156
157// prefixed is the terms a typed prefix stands for in this partition:
158// itself if it is a word here, and the most frequent words it begins.
159func (part *partition) prefixed(prefix string) []string {
160 return topK(part.expand(prefix, 4096), expansions,
161 func(one, other string) bool {
162 if part.frequency(one) != part.frequency(other) {
163 return part.frequency(one) > part.frequency(other)
164 }
165 return one < other
166 })
167}
168