@zukhruf/relevance-filtering 0.4.2 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/README.md +110 -52
  2. package/dist/binormal.d.ts +1 -15
  3. package/dist/binormal.d.ts.map +1 -1
  4. package/dist/binormal.js +6 -12
  5. package/dist/binormal.js.map +1 -1
  6. package/dist/decision-scorer.d.ts +12 -0
  7. package/dist/decision-scorer.d.ts.map +1 -0
  8. package/dist/decision-scorer.js +29 -0
  9. package/dist/decision-scorer.js.map +1 -0
  10. package/dist/expected-f-threshold.d.ts +10 -0
  11. package/dist/expected-f-threshold.d.ts.map +1 -0
  12. package/dist/expected-f-threshold.js +33 -0
  13. package/dist/expected-f-threshold.js.map +1 -0
  14. package/dist/index.d.ts +8 -4
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +6 -2
  17. package/dist/index.js.map +1 -1
  18. package/dist/profile.d.ts +1 -16
  19. package/dist/profile.d.ts.map +1 -1
  20. package/dist/profile.js +3 -88
  21. package/dist/profile.js.map +1 -1
  22. package/dist/scorer.d.ts +12 -0
  23. package/dist/scorer.d.ts.map +1 -0
  24. package/dist/scorer.js +2 -0
  25. package/dist/scorer.js.map +1 -0
  26. package/dist/scores.d.ts +23 -0
  27. package/dist/scores.d.ts.map +1 -0
  28. package/dist/scores.js +51 -0
  29. package/dist/scores.js.map +1 -0
  30. package/dist/similarity-scorer.d.ts +16 -0
  31. package/dist/similarity-scorer.d.ts.map +1 -0
  32. package/dist/similarity-scorer.js +109 -0
  33. package/dist/similarity-scorer.js.map +1 -0
  34. package/dist/threshold.d.ts +6 -0
  35. package/dist/threshold.d.ts.map +1 -0
  36. package/dist/threshold.js +2 -0
  37. package/dist/threshold.js.map +1 -0
  38. package/dist/utility-threshold.d.ts +10 -0
  39. package/dist/utility-threshold.d.ts.map +1 -0
  40. package/dist/utility-threshold.js +22 -0
  41. package/dist/utility-threshold.js.map +1 -0
  42. package/package.json +7 -4
  43. package/dist/expected-f.d.ts +0 -2
  44. package/dist/expected-f.d.ts.map +0 -1
  45. package/dist/expected-f.js +0 -23
  46. package/dist/expected-f.js.map +0 -1
  47. package/dist/relevance-filter.d.ts +0 -47
  48. package/dist/relevance-filter.d.ts.map +0 -1
  49. package/dist/relevance-filter.js +0 -100
  50. package/dist/relevance-filter.js.map +0 -1
package/dist/profile.js CHANGED
@@ -1,13 +1,8 @@
1
- import { binormal } from "./binormal.js";
2
- import { ConflictingAnswerError, MissingVectorError, NoEvidenceError, } from "./errors.js";
3
- import { withKeywordEvidence } from "./evidence.js";
4
- import { expectedF05Threshold } from "./expected-f.js";
5
- import { matchesAny } from "./keyword.js";
6
- import { platt, plattProbability } from "./platt.js";
7
- import { CalibratedFilter, KeywordFilter, } from "./relevance-filter.js";
1
+ import { ConflictingAnswerError } from "./errors.js";
8
2
  // A reader's interest: a name, a description, optional keywords and the
9
3
  // answers that taught it. It is a value: it does no I/O, and a correction
10
- // gives a new profile (docs/adr/0001).
4
+ // gives a new profile (docs/adr/0001). A scorer scores a stream for it
5
+ // (docs/adr/0005).
11
6
  export class Profile {
12
7
  name;
13
8
  description;
@@ -36,85 +31,5 @@ export class Profile {
36
31
  ...changes,
37
32
  });
38
33
  }
39
- // Learns from the answers and ranks and decides on the stream
40
- // (docs/adr/0002, docs/adr/0004).
41
- train(options) {
42
- const { stream } = options;
43
- if (new Set(stream.map(({ id }) => id)).size !== stream.length)
44
- throw new RangeError('Each article of the stream must have its own id.');
45
- const scores = streamScores(options);
46
- const ranked = stream.map(({ id, publishedAt }, index) => ({
47
- id,
48
- publishedAt,
49
- similarity: scores[index] ?? 0,
50
- }));
51
- const matches = () => stream.map(({ text }) => matchesAny(this.keywords, text));
52
- if (this.relevant.length === 0)
53
- return this.#keywordFilter(ranked, matches);
54
- const taught = taughtScores(options, this.relevant, this.notRelevant);
55
- const fitted = binormal(scores, taught.relevant, taught.notRelevant);
56
- let probabilities;
57
- let calibration;
58
- if (fitted !== undefined) {
59
- probabilities = fitted.probabilities;
60
- calibration = { method: 'binormal', ...fitted.fit };
61
- }
62
- else if (this.notRelevant.length > 0) {
63
- const fit = platt(taught.relevant, taught.notRelevant);
64
- probabilities = scores.map((score) => plattProbability(fit, score));
65
- calibration = fit;
66
- }
67
- else
68
- return this.#keywordFilter(ranked, matches);
69
- if (this.keywords.length > 0)
70
- probabilities = withKeywordEvidence(probabilities, matches(), this.relevant.map((id) => matchesAny(this.keywords, options.taughtText(id))));
71
- return new CalibratedFilter({
72
- stream: ranked,
73
- probabilities: new Map(stream.map(({ id }, index) => [id, probabilities[index] ?? 0])),
74
- calibration,
75
- threshold: expectedF05Threshold(probabilities),
76
- });
77
- }
78
- #keywordFilter(stream, matches) {
79
- if (this.keywords.length === 0)
80
- throw new NoEvidenceError(this.name);
81
- const matched = matches();
82
- return new KeywordFilter({
83
- stream,
84
- matches: new Map(stream.map(({ id }, index) => [id, matched[index] ?? false])),
85
- });
86
- }
87
- }
88
- // The similarity of the description to each article of the stream, in the
89
- // order of the stream, from one query over the publish dates of the stream.
90
- function streamScores({ vectors, descriptionVector, stream }) {
91
- const dates = stream.map(({ publishedAt }) => publishedAt.getTime());
92
- const similarities = new Map(vectors
93
- .query(descriptionVector, {
94
- from: new Date(dates.reduce((a, b) => Math.min(a, b))),
95
- to: new Date(dates.reduce((a, b) => Math.max(a, b))),
96
- })
97
- .map(({ id, similarity }) => [id, similarity]));
98
- return stream.map(({ id }) => {
99
- const similarity = similarities.get(id);
100
- if (similarity === undefined)
101
- throw new MissingVectorError(id);
102
- return similarity;
103
- });
104
- }
105
- // The cosine of the description with each taught article: the dot product
106
- // of the stored unit vector and the description made a unit vector.
107
- function taughtScores({ vectors, descriptionVector }, relevant, notRelevant) {
108
- const length = Math.hypot(...descriptionVector);
109
- const stored = vectors.fetch([...relevant, ...notRelevant]);
110
- const score = (id) => {
111
- const vector = stored.get(id) ?? [];
112
- let total = 0;
113
- for (let index = 0; index < descriptionVector.length; index++)
114
- total +=
115
- (vector[index] ?? 0) * ((descriptionVector[index] ?? 0) / length);
116
- return total;
117
- };
118
- return { relevant: relevant.map(score), notRelevant: notRelevant.map(score) };
119
34
  }
120
35
  //# sourceMappingURL=profile.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"profile.js","sourceRoot":"","sources":["../src/profile.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EACL,sBAAsB,EACtB,kBAAkB,EAClB,eAAe,GAChB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AACpD,OAAO,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AACvD,OAAO,EAAgB,UAAU,EAAE,MAAM,cAAc,CAAC;AACxD,OAAO,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AACrD,OAAO,EACL,gBAAgB,EAEhB,aAAa,GAGd,MAAM,uBAAuB,CAAC;AAuC/B,wEAAwE;AACxE,0EAA0E;AAC1E,uCAAuC;AACvC,MAAM,OAAO,OAAO;IACT,IAAI,CAAS;IACb,WAAW,CAAS;IACpB,QAAQ,CAAqB;IAC7B,QAAQ,CAAoB;IAC5B,WAAW,CAAoB;IAExC,YAAY,MAAqB;QAC/B,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QAC1C,MAAM,QAAQ,GAAG,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC;QACnE,IAAI,QAAQ,KAAK,SAAS;YAAE,MAAM,IAAI,sBAAsB,CAAC,QAAQ,CAAC,CAAC;QACvE,IAAI,CAAC,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;QACxB,IAAI,CAAC,WAAW,GAAG,MAAM,CAAC,WAAW,CAAC;Q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,CAAC,QAAQ,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;QAC5D,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC;QACpD,IAAI,CAAC,WAAW,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC;IAC5D,CAAC;IAED,uDAAuD;IACvD,IAAI,CAAC,OAA+B;QAClC,OAAO,IAAI,OAAO,CAAC;YACjB,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,WAAW,EAAE,IAAI,CAAC,WAAW;YAC7B,QAAQ,EAAE,IAAI,CAAC,QAAQ;YACvB,QAAQ,EAAE,IAAI,CAAC,QAAQ;YACvB,WAAW,EAAE,IAAI,CAAC,WAAW;YAC7B,GAAG,OAAO;SACX,CAAC,CAAC;IACL,CAAC;CACF"}
@@ -0,0 +1,12 @@
1
+ import type { Profile } from './profile.ts';
2
+ import type { Scores } from './scores.ts';
3
+ export type Article = {
4
+ readonly id: string;
5
+ readonly publishedAt: Date;
6
+ readonly text: string;
7
+ };
8
+ export type Stream = readonly [Article, ...Article[]];
9
+ export interface Scorer {
10
+ score(profile: Profile, stream: Stream): Promise<Scores>;
11
+ }
12
+ //# sourceMappingURL=scorer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scorer.d.ts","sourceRoot":"","sources":["../src/scorer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAG1C,MAAM,MAAM,OAAO,GAAG;IACpB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IAGpB,QAAQ,CAAC,WAAW,EAAE,IAAI,CAAC;IAG3B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB,CAAC;AAGF,MAAM,MAAM,MAAM,GAAG,SAAS,CAAC,OAAO,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;AAGtD,MAAM,WAAW,MAAM;IACrB,KAAK,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CAC1D"}
package/dist/scorer.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=scorer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scorer.js","sourceRoot":"","sources":["../src/scorer.ts"],"names":[],"mappings":""}
@@ -0,0 +1,23 @@
1
+ import type { Profile } from './profile.ts';
2
+ import type { Threshold } from './threshold.ts';
3
+ export type PublishRange = {
4
+ readonly from: Date;
5
+ readonly to: Date;
6
+ };
7
+ export type ScoredArticle = {
8
+ readonly id: string;
9
+ readonly publishedAt: Date;
10
+ readonly probability: number;
11
+ };
12
+ export declare class Scores {
13
+ #private;
14
+ constructor(options: {
15
+ readonly profile: Profile;
16
+ readonly ranked: readonly ScoredArticle[];
17
+ });
18
+ probability(id: string): number;
19
+ ranked(range?: PublishRange): string[];
20
+ accepted(threshold: Threshold): string[];
21
+ }
22
+ export declare function compareIds(left: string, right: string): number;
23
+ //# sourceMappingURL=scores.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scores.d.ts","sourceRoot":"","sources":["../src/scores.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAGhD,MAAM,MAAM,YAAY,GAAG;IAAE,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAA;CAAE,CAAC;AAGtE,MAAM,MAAM,aAAa,GAAG;IAC1B,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,WAAW,EAAE,IAAI,CAAC;IAC3B,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;CAC9B,CAAC;AAIF,qBAAa,MAAM;;gBAML,OAAO,EAAE;QACnB,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;QAC1B,QAAQ,CAAC,MAAM,EAAE,SAAS,aAAa,EAAE,CAAC;KAC3C;IAaD,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM;IAS/B,MAAM,CAAC,KAAK,CAAC,EAAE,YAAY,GAAG,MAAM,EAAE;IAYtC,QAAQ,CAAC,SAAS,EAAE,SAAS,GAAG,MAAM,EAAE;CAMzC;AAID,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAI9D"}
package/dist/scores.js ADDED
@@ -0,0 +1,51 @@
1
+ import { UnknownArticleError } from "./errors.js";
2
+ // What a scorer gives for one profile and one stream: the articles, the best
3
+ // first, each with its probability (docs/adr/0005).
4
+ export class Scores {
5
+ #profile;
6
+ #ranked;
7
+ #probabilities;
8
+ // `ranked` is the stream in the order of the scorer, the best first.
9
+ constructor(options) {
10
+ const { profile, ranked } = options;
11
+ const probabilities = new Map(ranked.map(({ id, probability }) => [id, probability]));
12
+ if (probabilities.size !== ranked.length)
13
+ throw new RangeError('Each article of the stream must have its own id.');
14
+ this.#profile = profile;
15
+ this.#ranked = ranked;
16
+ this.#probabilities = probabilities;
17
+ }
18
+ // The probability that the article is relevant to the profile.
19
+ probability(id) {
20
+ const probability = this.#probabilities.get(id);
21
+ if (probability === undefined)
22
+ throw new UnknownArticleError(id);
23
+ return probability;
24
+ }
25
+ // The ids of the stream, the best first (docs/adr/0004). With a range, only
26
+ // the articles published in it, in the same order: the best N of a day are
27
+ // ranked({ from, to }).slice(0, n).
28
+ ranked(range) {
29
+ const inRange = range === undefined
30
+ ? this.#ranked
31
+ : this.#ranked.filter(({ publishedAt }) => publishedAt >= range.from && publishedAt <= range.to);
32
+ return inRange.map(({ id }) => id);
33
+ }
34
+ // The articles that the threshold accepts, in the order of ranked().
35
+ accepted(threshold) {
36
+ const cut = threshold.cut(this, this.#profile);
37
+ return this.#ranked
38
+ .filter(({ probability }) => probability >= cut)
39
+ .map(({ id }) => id);
40
+ }
41
+ }
42
+ // Compares by code unit, not by locale, so the order is the same on every
43
+ // machine.
44
+ export function compareIds(left, right) {
45
+ if (left < right)
46
+ return -1;
47
+ if (left > right)
48
+ return 1;
49
+ return 0;
50
+ }
51
+ //# sourceMappingURL=scores.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scores.js","sourceRoot":"","sources":["../src/scores.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAclD,6EAA6E;AAC7E,oDAAoD;AACpD,MAAM,OAAO,MAAM;IACR,QAAQ,CAAU;IAClB,OAAO,CAA2B;IAClC,cAAc,CAA8B;IAErD,qEAAqE;IACrE,YAAY,OAGX;QACC,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;QACpC,MAAM,aAAa,GAAG,IAAI,GAAG,CAC3B,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,WAAW,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,EAAE,WAAW,CAAC,CAAC,CACvD,CAAC;QACF,IAAI,aAAa,CAAC,IAAI,KAAK,MAAM,CAAC,MAAM;YACtC,MAAM,IAAI,UAAU,CAAC,kDAAkD,CAAC,CAAC;QAC3E,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;QACtB,IAAI,CAAC,cAAc,GAAG,aAAa,CAAC;IACtC,CAAC;IAED,+DAA+D;IAC/D,WAAW,CAAC,EAAU;QACpB,MAAM,WAAW,GAAG,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAChD,IAAI,WAAW,KAAK,SAAS;YAAE,MAAM,IAAI,mBAAmB,CAAC,EAAE,CAAC,CAAC;QACjE,OAAO,WAAW,CAAC;IACrB,CAAC;IAED,4EAA4E;IAC5E,2EAA2E;IAC3E,oCAAoC;IACpC,MAAM,CAAC,KAAoB;QACzB,MAAM,OAAO,GACX,KAAK,KAAK,SAAS;YACjB,CAAC,CAAC,IAAI,CAAC,OAAO;YACd,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,CACjB,CAAC,EAAE,WAAW,EAAE,EAAE,EAAE,CAClB,WAAW,IAAI,KAAK,CAAC,IAAI,IAAI,WAAW,IAAI,KAAK,CAAC,EAAE,CACvD,CAAC;QACR,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC;IACrC,CAAC;IAED,qEAAqE;IACrE,QAAQ,CAAC,SAAoB;QAC3B,MAAM,GAAG,GAAG,SAAS,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;QAC/C,OAAO,IAAI,CAAC,OAAO;aAChB,MAAM,CAAC,CAAC,EAAE,WAAW,EAAE,EAAE,EAAE,CAAC,WAAW,IAAI,GAAG,CAAC;aAC/C,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC;IACzB,CAAC;CACF;AAED,0EAA0E;AAC1E,WAAW;AACX,MAAM,UAAU,UAAU,CAAC,IAAY,EAAE,KAAa;IACpD,IAAI,IAAI,GAAG,KAAK;QAAE,OAAO,CAAC,CAAC,CAAC;IAC5B,IAAI,IAAI,GAAG,KAAK;QAAE,OAAO,CAAC,CAAC;IAC3B,OAAO,CAAC,CAAC;AACX,CAAC"}
@@ -0,0 +1,16 @@
1
+ import type { Profile } from './profile.ts';
2
+ import type { Scorer, Stream } from './scorer.ts';
3
+ import { Scores } from './scores.ts';
4
+ import type { Vectors } from './vectors.ts';
5
+ type SimilarityScorerOptions = {
6
+ readonly vectors: Vectors;
7
+ readonly embedDescription: (description: string) => Promise<readonly number[]>;
8
+ readonly taughtText: (id: string) => string;
9
+ };
10
+ export declare class SimilarityScorer implements Scorer {
11
+ #private;
12
+ constructor(options: SimilarityScorerOptions);
13
+ score(profile: Profile, stream: Stream): Promise<Scores>;
14
+ }
15
+ export {};
16
+ //# sourceMappingURL=similarity-scorer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"similarity-scorer.d.ts","sourceRoot":"","sources":["../src/similarity-scorer.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,EAAE,MAAM,EAAc,MAAM,aAAa,CAAC;AACjD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAE5C,KAAK,uBAAuB,GAAG;IAE7B,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAG1B,QAAQ,CAAC,gBAAgB,EAAE,CACzB,WAAW,EAAE,MAAM,KAChB,OAAO,CAAC,SAAS,MAAM,EAAE,CAAC,CAAC;IAGhC,QAAQ,CAAC,UAAU,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,MAAM,CAAC;CAC7C,CAAC;AAgBF,qBAAa,gBAAiB,YAAW,MAAM;;gBAGjC,OAAO,EAAE,uBAAuB;IAItC,KAAK,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CAmD/D"}
@@ -0,0 +1,109 @@
1
+ import { binormal } from "./binormal.js";
2
+ import { MissingVectorError, NoEvidenceError } from "./errors.js";
3
+ import { withKeywordEvidence } from "./evidence.js";
4
+ import { matchesAny } from "./keyword.js";
5
+ import { platt, plattProbability } from "./platt.js";
6
+ import { Scores, compareIds } from "./scores.js";
7
+ // The frozen design of research round 6 (docs/adr/0002): the similarity of
8
+ // each article to the description, calibrated by a binormal model on the
9
+ // stream and the answers, or by Platt's method on the answers, with the
10
+ // evidence of the keywords. A profile with no relevant answer ranks the
11
+ // keyword matches first.
12
+ export class SimilarityScorer {
13
+ #options;
14
+ constructor(options) {
15
+ this.#options = options;
16
+ }
17
+ async score(profile, stream) {
18
+ const descriptionVector = await this.#options.embedDescription(profile.description);
19
+ const similar = streamScores(this.#options.vectors, descriptionVector, stream, profile);
20
+ if (profile.relevant.length === 0)
21
+ return keywordScores(profile, similar);
22
+ const scores = similar.map(({ similarity }) => similarity);
23
+ const taught = taughtScores(this.#options.vectors, descriptionVector, profile);
24
+ let probabilities = binormal(scores, taught.relevant, taught.notRelevant);
25
+ if (probabilities === undefined && profile.notRelevant.length > 0) {
26
+ const fit = platt(taught.relevant, taught.notRelevant);
27
+ probabilities = scores.map((score) => plattProbability(fit, score));
28
+ }
29
+ if (probabilities === undefined)
30
+ return keywordScores(profile, similar);
31
+ if (profile.keywords.length > 0)
32
+ probabilities = withKeywordEvidence(probabilities, similar.map(({ match }) => match), profile.relevant.map((id) => matchesAny(profile.keywords, this.#options.taughtText(id))));
33
+ const scored = similar.map((article, index) => ({
34
+ ...article,
35
+ probability: probabilities[index] ?? 0,
36
+ }));
37
+ return new Scores({
38
+ profile,
39
+ ranked: scored
40
+ .toSorted((a, b) => b.probability - a.probability ||
41
+ b.similarity - a.similarity ||
42
+ compareIds(a.id, b.id))
43
+ .map(({ id, publishedAt, probability }) => ({
44
+ id,
45
+ publishedAt,
46
+ probability,
47
+ })),
48
+ });
49
+ }
50
+ }
51
+ // A profile with no relevant answer to learn from: the probability is 1 for
52
+ // a keyword match and 0 for the others, and it is not calibrated. The
53
+ // matches come first, the newest first, and then the other articles by
54
+ // similarity.
55
+ function keywordScores(profile, similar) {
56
+ if (profile.keywords.length === 0)
57
+ throw new NoEvidenceError(profile.name);
58
+ return new Scores({
59
+ profile,
60
+ ranked: similar
61
+ .toSorted((a, b) => {
62
+ if (a.match !== b.match)
63
+ return a.match ? -1 : 1;
64
+ const key = a.match
65
+ ? b.publishedAt.getTime() - a.publishedAt.getTime()
66
+ : b.similarity - a.similarity;
67
+ return key || compareIds(a.id, b.id);
68
+ })
69
+ .map(({ id, publishedAt, match }) => ({
70
+ id,
71
+ publishedAt,
72
+ probability: match ? 1 : 0,
73
+ })),
74
+ });
75
+ }
76
+ // Each article of the stream, in the order of the stream, with its
77
+ // similarity to the description from one query over the publish dates of
78
+ // the stream, and its keyword match.
79
+ function streamScores(vectors, descriptionVector, stream, { keywords }) {
80
+ const dates = stream.map(({ publishedAt }) => publishedAt.getTime());
81
+ const similarities = new Map(vectors
82
+ .query(descriptionVector, {
83
+ from: new Date(dates.reduce((a, b) => Math.min(a, b))),
84
+ to: new Date(dates.reduce((a, b) => Math.max(a, b))),
85
+ })
86
+ .map(({ id, similarity }) => [id, similarity]));
87
+ return stream.map(({ id, publishedAt, text }) => {
88
+ const similarity = similarities.get(id);
89
+ if (similarity === undefined)
90
+ throw new MissingVectorError(id);
91
+ return { id, publishedAt, similarity, match: matchesAny(keywords, text) };
92
+ });
93
+ }
94
+ // The cosine of the description with each taught article: the dot product
95
+ // of the stored unit vector and the description made a unit vector.
96
+ function taughtScores(vectors, descriptionVector, { relevant, notRelevant }) {
97
+ const length = Math.hypot(...descriptionVector);
98
+ const stored = vectors.fetch([...relevant, ...notRelevant]);
99
+ const score = (id) => {
100
+ const vector = stored.get(id) ?? [];
101
+ let total = 0;
102
+ for (let index = 0; index < descriptionVector.length; index++)
103
+ total +=
104
+ (vector[index] ?? 0) * ((descriptionVector[index] ?? 0) / length);
105
+ return total;
106
+ };
107
+ return { relevant: relevant.map(score), notRelevant: notRelevant.map(score) };
108
+ }
109
+ //# sourceMappingURL=similarity-scorer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"similarity-scorer.js","sourceRoot":"","sources":["../src/similarity-scorer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,kBAAkB,EAAE,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,CAAC,KAAK,CAAC,GAAG,iBAAiB,CAAC,CAAC;IAChD,MAAM,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC,GAAG,QAAQ,EAAE,GAAG,WAAW,CAAC,CAAC,CAAC;IAC5D,MAAM,KAAK,GAAG,CAAC,EAAU,EAAE,EAAE;QAC3B,MAAM,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC;QACpC,IAAI,KAAK,GAAG,CAAC,CAAC;QACd,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,iBAAiB,CAAC,MAAM,EAAE,KAAK,EAAE;YAC3D,KAAK;gBACH,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,iBAAiB,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC;QACtE,OAAO,KAAK,CAAC;IACf,CAAC,CAAC;IACF,OAAO,EAAE,QAAQ,EAAE,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,WAAW,EAAE,WAAW,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC;AAChF,CAAC"}
@@ -0,0 +1,6 @@
1
+ import type { Profile } from './profile.ts';
2
+ import type { Scores } from './scores.ts';
3
+ export interface Threshold {
4
+ cut(scores: Scores, profile: Profile): number;
5
+ }
6
+ //# sourceMappingURL=threshold.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"threshold.d.ts","sourceRoot":"","sources":["../src/threshold.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAM1C,MAAM,WAAW,SAAS;IAExB,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,MAAM,CAAC;CAC/C"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=threshold.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"threshold.js","sourceRoot":"","sources":["../src/threshold.ts"],"names":[],"mappings":""}
@@ -0,0 +1,10 @@
1
+ import type { Threshold } from './threshold.ts';
2
+ export declare class UtilityThreshold implements Threshold {
3
+ #private;
4
+ constructor(options: {
5
+ readonly falseAlarm: number;
6
+ readonly miss: number;
7
+ });
8
+ cut(): number;
9
+ }
10
+ //# sourceMappingURL=utility-threshold.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utility-threshold.d.ts","sourceRoot":"","sources":["../src/utility-threshold.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAOhD,qBAAa,gBAAiB,YAAW,SAAS;;gBAGpC,OAAO,EAAE;QAAE,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;KAAE;IAe3E,GAAG,IAAI,MAAM;CAGd"}
@@ -0,0 +1,22 @@
1
+ // The Bayes decision rule on calibrated probabilities (Lewis, SIGIR 1995).
2
+ // Accepting an article with the probability p costs falseAlarm * (1 - p) on
3
+ // average, and missing it costs miss * p, so the rule accepts when
4
+ // p >= falseAlarm / (falseAlarm + miss). The reader sets the two costs, for
5
+ // example per action: a notification costs more than a mark.
6
+ export class UtilityThreshold {
7
+ #cut;
8
+ constructor(options) {
9
+ const { falseAlarm, miss } = options;
10
+ if (!Number.isFinite(falseAlarm) ||
11
+ !Number.isFinite(miss) ||
12
+ falseAlarm < 0 ||
13
+ miss < 0 ||
14
+ falseAlarm + miss === 0)
15
+ throw new RangeError(`The costs ${falseAlarm} and ${miss} give no threshold. Give two finite costs of 0 or more, and not both 0.`);
16
+ this.#cut = falseAlarm / (falseAlarm + miss);
17
+ }
18
+ cut() {
19
+ return this.#cut;
20
+ }
21
+ }
22
+ //# sourceMappingURL=utility-threshold.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utility-threshold.js","sourceRoot":"","sources":["../src/utility-threshold.ts"],"names":[],"mappings":"AAEA,2EAA2E;AAC3E,4EAA4E;AAC5E,mEAAmE;AACnE,4EAA4E;AAC5E,6DAA6D;AAC7D,MAAM,OAAO,gBAAgB;IAClB,IAAI,CAAS;IAEtB,YAAY,OAA+D;QACzE,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC;QACrC,IACE,CAAC,MAAM,CAAC,QAAQ,CAAC,UAAU,CAAC;YAC5B,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC;YACtB,UAAU,GAAG,CAAC;YACd,IAAI,GAAG,CAAC;YACR,UAAU,GAAG,IAAI,KAAK,CAAC;YAEvB,MAAM,IAAI,UAAU,CAClB,aAAa,UAAU,QAAQ,IAAI,yEAAyE,CAC7G,CAAC;QACJ,IAAI,CAAC,IAAI,GAAG,UAAU,GAAG,CAAC,UAAU,GAAG,IAAI,CAAC,CAAC;IAC/C,CAAC;IAED,GAAG;QACD,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;CACF"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@zukhruf/relevance-filtering",
3
- "version": "0.4.2",
4
- "description": "Decides which articles of a stream match a reader's profile: embedding similarity to the profile's description, calibrated by a semi-supervised binormal model on the reader's yes and no answers, naive-Bayes keyword evidence and an expected-F0.5 threshold.",
3
+ "version": "0.5.1",
4
+ "description": "Ranks a stream of articles for a reader's profile through a scorer port (a decision model, or embedding similarity calibrated on the reader's answers), with thresholds that turn the probabilities into yes or no.",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
@@ -39,8 +39,11 @@
39
39
  "publishConfig": {
40
40
  "access": "public"
41
41
  },
42
- "devDependencies": {
43
- "@zukhruf/embeddings": "^0.4.2",
42
+ "dependencies": {
44
43
  "ai": "^7.0.154"
44
+ },
45
+ "devDependencies": {
46
+ "@stdlib/blas-ext-base-gsumpw": "^0.3.1",
47
+ "@zukhruf/embeddings": "^0.5.1"
45
48
  }
46
49
  }
@@ -1,2 +0,0 @@
1
- export declare function expectedF05Threshold(probabilities: readonly number[]): number | undefined;
2
- //# sourceMappingURL=expected-f.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"expected-f.d.ts","sourceRoot":"","sources":["../src/expected-f.ts"],"names":[],"mappings":"AAQA,wBAAgB,oBAAoB,CAClC,aAAa,EAAE,SAAS,MAAM,EAAE,GAC/B,MAAM,GAAG,SAAS,CAapB"}
@@ -1,23 +0,0 @@
1
- import { sum } from "./statistics.js";
2
- // The probability at and above which an article is accepted: the plug-in
3
- // rule that maximises the expected F0.5 over the probabilities themselves.
4
- // Accepting the m most probable articles has the expected F0.5
5
- // 1.25 * (sum of their probabilities) / (0.25 * sum of all probabilities + m).
6
- // Undefined when no article has a probability above 0, so nothing is
7
- // accepted (docs/adr/0002).
8
- export function expectedF05Threshold(probabilities) {
9
- const order = probabilities.toSorted((a, b) => b - a);
10
- const total = sum(order);
11
- let best;
12
- let cumulative = 0;
13
- for (const [index, probability] of order.entries()) {
14
- cumulative += probability;
15
- const value = (1.25 * cumulative) / (0.25 * total + index + 1);
16
- if (best === undefined || value > best.value)
17
- best = { value, threshold: probability };
18
- }
19
- if (best === undefined || best.value <= 0)
20
- return undefined;
21
- return best.threshold;
22
- }
23
- //# sourceMappingURL=expected-f.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"expected-f.js","sourceRoot":"","sources":["../src/expected-f.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAEtC,yEAAyE;AACzE,2EAA2E;AAC3E,+DAA+D;AAC/D,+EAA+E;AAC/E,qEAAqE;AACrE,4BAA4B;AAC5B,MAAM,UAAU,oBAAoB,CAClC,aAAgC;IAEhC,MAAM,KAAK,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;IACtD,MAAM,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC;IACzB,IAAI,IAAsD,CAAC;IAC3D,IAAI,UAAU,GAAG,CAAC,CAAC;IACnB,KAAK,MAAM,CAAC,KAAK,EAAE,WAAW,CAAC,IAAI,KAAK,CAAC,OAAO,EAAE,EAAE,CAAC;QACnD,UAAU,IAAI,WAAW,CAAC;QAC1B,MAAM,KAAK,GAAG,CAAC,IAAI,GAAG,UAAU,CAAC,GAAG,CAAC,IAAI,GAAG,KAAK,GAAG,KAAK,GAAG,CAAC,CAAC,CAAC;QAC/D,IAAI,IAAI,KAAK,SAAS,IAAI,KAAK,GAAG,IAAI,CAAC,KAAK;YAC1C,IAAI,GAAG,EAAE,KAAK,EAAE,SAAS,EAAE,WAAW,EAAE,CAAC;IAC7C,CAAC;IACD,IAAI,IAAI,KAAK,SAAS,IAAI,IAAI,CAAC,KAAK,IAAI,CAAC;QAAE,OAAO,SAAS,CAAC;IAC5D,OAAO,IAAI,CAAC,SAAS,CAAC;AACxB,CAAC"}
@@ -1,47 +0,0 @@
1
- import type { BinormalFit } from './binormal.ts';
2
- import type { PlattFit } from './platt.ts';
3
- export type Calibration = ({
4
- readonly method: 'binormal';
5
- } & BinormalFit) | PlattFit;
6
- export type RankedArticle = {
7
- readonly id: string;
8
- readonly publishedAt: Date;
9
- readonly similarity: number;
10
- };
11
- export type PublishRange = {
12
- readonly from: Date;
13
- readonly to: Date;
14
- };
15
- declare abstract class Decisions {
16
- #private;
17
- constructor(options: {
18
- readonly probabilities: ReadonlyMap<string, number>;
19
- readonly accepted: ReadonlySet<string>;
20
- readonly order: readonly RankedArticle[];
21
- });
22
- probability(id: string): number;
23
- ranked(range?: PublishRange): string[];
24
- accepts(id: string): boolean;
25
- accepted(): string[];
26
- }
27
- export declare class CalibratedFilter extends Decisions {
28
- readonly kind = "calibrated";
29
- readonly calibration: Calibration;
30
- readonly threshold: number | undefined;
31
- constructor(options: {
32
- readonly stream: readonly RankedArticle[];
33
- readonly probabilities: ReadonlyMap<string, number>;
34
- readonly calibration: Calibration;
35
- readonly threshold: number | undefined;
36
- });
37
- }
38
- export declare class KeywordFilter extends Decisions {
39
- readonly kind = "keywords";
40
- constructor(options: {
41
- readonly stream: readonly RankedArticle[];
42
- readonly matches: ReadonlyMap<string, boolean>;
43
- });
44
- }
45
- export type RelevanceFilter = CalibratedFilter | KeywordFilter;
46
- export {};
47
- //# sourceMappingURL=relevance-filter.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"relevance-filter.d.ts","sourceRoot":"","sources":["../src/relevance-filter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAEjD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAG3C,MAAM,MAAM,WAAW,GACnB,CAAC;IAAE,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAA;CAAE,GAAG,WAAW,CAAC,GAG/C,QAAQ,CAAC;AAGb,MAAM,MAAM,aAAa,GAAG;IAC1B,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,WAAW,EAAE,IAAI,CAAC;IAE3B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC7B,CAAC;AAGF,MAAM,MAAM,YAAY,GAAG;IAAE,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAA;CAAE,CAAC;AAGtE,uBAAe,SAAS;;gBAKV,OAAO,EAAE;QACnB,QAAQ,CAAC,aAAa,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QACpD,QAAQ,CAAC,QAAQ,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;QACvC,QAAQ,CAAC,KAAK,EAAE,SAAS,aAAa,EAAE,CAAC;KAC1C;IAOD,WAAW,CAAC,EAAE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