MCPcopy Create free account
hub / github.com/ChatLab/ChatLab / rankPercentiles

Function rankPercentiles

packages/core/src/query/contact-scoring.ts:86–111  ·  view source on GitHub ↗
(items: readonly T[], valueSelector: (item: T) => number)

Source from the content-addressed store, hash-verified

84}
85
86export function rankPercentiles<T>(items: readonly T[], valueSelector: (item: T) => number): Map<T, number> {
87 const result = new Map<T, number>()
88 if (items.length === 0) return result
89
90 const values = items.map((item) => ({ item, value: nonNegative(valueSelector(item)) }))
91 const min = Math.min(...values.map((entry) => entry.value))
92 const max = Math.max(...values.map((entry) => entry.value))
93
94 if (min === max) {
95 const percentile = max > 0 ? 1 : 0
96 for (const entry of values) result.set(entry.item, percentile)
97 return result
98 }
99
100 const sorted = [...values].sort((a, b) => a.value - b.value)
101 let index = 0
102 while (index < sorted.length) {
103 let end = index
104 while (end + 1 < sorted.length && sorted[end + 1].value === sorted[index].value) end++
105 const percentile = (index + end) / 2 / (sorted.length - 1)
106 for (let i = index; i <= end; i++) result.set(sorted[i].item, percentile)
107 index = end + 1
108 }
109
110 return result
111}
112
113export function computePrivateRegularity(activeMonths: readonly string[]): number {
114 const monthIndexes = getActiveMonthIndexes(activeMonths)

Callers 3

computeFriendScoresFunction · 0.85
computeNonFriendScoresFunction · 0.85

Calls 2

nonNegativeFunction · 0.85
setMethod · 0.80

Tested by

no test coverage detected