okapibm25 1.3.2 → 1.4.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.
- package/BM25.test.ts +97 -50
- package/BM25.ts +42 -9
- package/README.md +27 -2
- package/coverage/BM25.ts.html +350 -0
- package/coverage/base.css +224 -0
- package/coverage/block-navigation.js +79 -0
- package/coverage/coverage-summary.json +2 -2
- package/coverage/favicon.png +0 -0
- package/coverage/index.html +111 -0
- package/coverage/prettify.css +1 -0
- package/coverage/prettify.js +2 -0
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +170 -0
- package/dist/BM25.d.ts +22 -8
- package/dist/BM25.js +24 -10
- package/dist/BM25.js.map +1 -1
- package/dist/jest.config.js +1 -1
- package/dist/jest.config.js.map +1 -1
- package/jest.config.ts +1 -1
- package/package.json +2 -1
package/dist/BM25.d.ts
CHANGED
|
@@ -4,14 +4,28 @@ export declare const getWordCount: (corpus: string) => number;
|
|
|
4
4
|
export declare const getTermFrequency: (term: string, corpus: string) => number;
|
|
5
5
|
/** Inverse document frequency. */
|
|
6
6
|
export declare const getIDF: (term: string, documents: string[]) => number;
|
|
7
|
-
/**
|
|
8
|
-
* @param documents: string[]. Collection of documents.
|
|
9
|
-
* @param keywords: keywords within query.
|
|
10
|
-
* @param constants: Contains free parameters k1 and b, which are free parameters,
|
|
11
|
-
* where k1 is within [1.2, 2.0] and b = 0.75, in absence of advanced optimization.
|
|
12
|
-
* In this implementation, k1 = 1.2.
|
|
7
|
+
/** Represents a document; useful when sorting results.
|
|
13
8
|
*/
|
|
14
|
-
export
|
|
9
|
+
export interface BMDocument {
|
|
10
|
+
/** The document is originally scoreed. */
|
|
11
|
+
document: string;
|
|
12
|
+
/** The score that the document recieves. */
|
|
13
|
+
score: number;
|
|
14
|
+
}
|
|
15
|
+
/** Constants that are free parameters used in BM25, specifically when generating inverse document frequency. */
|
|
16
|
+
export interface BMConstants {
|
|
17
|
+
/** Free parameter. Is 0.75 by default. */
|
|
15
18
|
b?: number;
|
|
19
|
+
/** Free parameter. Is 1.2 by default. Generally in range [1.2, 2.0] */
|
|
16
20
|
k1?: number;
|
|
17
|
-
}
|
|
21
|
+
}
|
|
22
|
+
/** If returns positive, the sorting results in secondEl coming before firstEl, else, firstEl comes before secondEL */
|
|
23
|
+
export declare type BMSorter = (firstEl: BMDocument, secondEl: BMDocument) => number;
|
|
24
|
+
/** Implementation of Okapi BM25 algorithm.
|
|
25
|
+
* @param documents: Collection of documents.
|
|
26
|
+
* @param keywords: query terms.
|
|
27
|
+
* @param constants: Contains free parameters k1 and b. b=0.75 and k1=1.2 by default.
|
|
28
|
+
* @param sort: A function that allows you to sort queries by a given rule. If not provided, returns results corresponding to the original order.
|
|
29
|
+
* If this option is provided, the return type will not be an array of scores but an array of documents with their scores.
|
|
30
|
+
*/
|
|
31
|
+
export default function BM25(documents: string[], keywords: string[], constants?: BMConstants, sorter?: BMSorter): number[] | BMDocument[];
|
package/dist/BM25.js
CHANGED
|
@@ -2,39 +2,46 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getIDF = exports.getTermFrequency = exports.getWordCount = void 0;
|
|
4
4
|
/** Gets word count. */
|
|
5
|
-
|
|
5
|
+
var getWordCount = function (corpus) {
|
|
6
6
|
return ((corpus || "").match(/\w+/g) || []).length;
|
|
7
7
|
};
|
|
8
|
+
exports.getWordCount = getWordCount;
|
|
8
9
|
/** Number of occurences of a word in a string. */
|
|
9
|
-
|
|
10
|
+
var getTermFrequency = function (term, corpus) {
|
|
10
11
|
return ((corpus || "").match(new RegExp(term, "g")) || []).length;
|
|
11
12
|
};
|
|
13
|
+
exports.getTermFrequency = getTermFrequency;
|
|
12
14
|
/** Inverse document frequency. */
|
|
13
|
-
|
|
15
|
+
var getIDF = function (term, documents) {
|
|
14
16
|
// Number of relevant documents.
|
|
15
17
|
var relevantDocuments = documents.filter(function (document) {
|
|
16
18
|
return document.includes(term);
|
|
17
19
|
}).length;
|
|
18
20
|
return Math.log((documents.length - relevantDocuments + 0.5) / (relevantDocuments + 0.5) + 1);
|
|
19
21
|
};
|
|
22
|
+
exports.getIDF = getIDF;
|
|
20
23
|
/** Implementation of Okapi BM25 algorithm.
|
|
21
|
-
* @param documents:
|
|
22
|
-
* @param keywords:
|
|
23
|
-
* @param constants: Contains free parameters k1 and b
|
|
24
|
-
*
|
|
25
|
-
*
|
|
24
|
+
* @param documents: Collection of documents.
|
|
25
|
+
* @param keywords: query terms.
|
|
26
|
+
* @param constants: Contains free parameters k1 and b. b=0.75 and k1=1.2 by default.
|
|
27
|
+
* @param sort: A function that allows you to sort queries by a given rule. If not provided, returns results corresponding to the original order.
|
|
28
|
+
* If this option is provided, the return type will not be an array of scores but an array of documents with their scores.
|
|
26
29
|
*/
|
|
27
|
-
function BM25(documents, keywords, constants) {
|
|
30
|
+
function BM25(documents, keywords, constants, sorter) {
|
|
28
31
|
var b = constants && constants.b ? constants.b : 0.75;
|
|
29
32
|
var k1 = constants && constants.k1 ? constants.k1 : 1.2;
|
|
30
33
|
var documentLengths = documents.map(function (document) {
|
|
31
34
|
return exports.getWordCount(document);
|
|
32
35
|
});
|
|
33
36
|
var averageDocumentLength = documentLengths.reduce(function (a, b) { return a + b; }, 0) / documents.length;
|
|
37
|
+
var idfByKeyword = keywords.reduce(function (obj, keyword) {
|
|
38
|
+
obj.set(keyword, exports.getIDF(keyword, documents));
|
|
39
|
+
return obj;
|
|
40
|
+
}, new Map());
|
|
34
41
|
var scores = documents.map(function (document, index) {
|
|
35
42
|
var score = keywords
|
|
36
43
|
.map(function (keyword) {
|
|
37
|
-
var inverseDocumentFrequency =
|
|
44
|
+
var inverseDocumentFrequency = idfByKeyword.get(keyword);
|
|
38
45
|
var termFrequency = exports.getTermFrequency(keyword, document);
|
|
39
46
|
var documentLength = documentLengths[index];
|
|
40
47
|
return ((inverseDocumentFrequency * (termFrequency * (k1 + 1))) /
|
|
@@ -42,8 +49,15 @@ function BM25(documents, keywords, constants) {
|
|
|
42
49
|
k1 * (1 - b + (b * documentLength) / averageDocumentLength)));
|
|
43
50
|
})
|
|
44
51
|
.reduce(function (a, b) { return a + b; }, 0);
|
|
52
|
+
if (sorter) {
|
|
53
|
+
return { score: score, document: document };
|
|
54
|
+
}
|
|
45
55
|
return score;
|
|
46
56
|
});
|
|
57
|
+
// sort the results
|
|
58
|
+
if (sorter) {
|
|
59
|
+
return scores.sort(sorter);
|
|
60
|
+
}
|
|
47
61
|
return scores;
|
|
48
62
|
}
|
|
49
63
|
exports.default = BM25;
|
package/dist/BM25.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BM25.js","sourceRoot":"","sources":["../BM25.ts"],"names":[],"mappings":";;;AAAA,uBAAuB;
|
|
1
|
+
{"version":3,"file":"BM25.js","sourceRoot":"","sources":["../BM25.ts"],"names":[],"mappings":";;;AAAA,uBAAuB;AAChB,IAAM,YAAY,GAAG,UAAC,MAAc;IACzC,OAAO,CAAC,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC;AACrD,CAAC,CAAC;AAFW,QAAA,YAAY,gBAEvB;AAEF,kDAAkD;AAC3C,IAAM,gBAAgB,GAAG,UAAC,IAAY,EAAE,MAAc;IAC3D,OAAO,CAAC,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC;AACpE,CAAC,CAAC;AAFW,QAAA,gBAAgB,oBAE3B;AAEF,kCAAkC;AAC3B,IAAM,MAAM,GAAG,UAAC,IAAY,EAAE,SAAmB;IACtD,gCAAgC;IAChC,IAAM,iBAAiB,GAAG,SAAS,CAAC,MAAM,CAAC,UAAC,QAAgB;QAC1D,OAAA,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC;IAAvB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,IAAM,wBAAwB,GAAG,YAAY,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;YAC3D,IAAM,aAAa,GAAG,wBAAgB,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;YAC1D,IAAM,cAAc,GAAG,eAAe,CAAC,KAAK,CAAC,CAAC;YAC9C,OAAO,CACL,CAAC,wBAAwB,GAAG,CAAC,aAAa,GAAG,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC;gBACvD,CAAC,aAAa;oBACZ,EAAE,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,cAAc,CAAC,GAAG,qBAAqB,CAAC,CAAC,CAC/D,CAAC;QACJ,CAAC,CAAC;aACD,MAAM,CAAC,UAAC,CAAS,EAAE,CAAS,IAAK,OAAA,CAAC,GAAG,CAAC,EAAL,CAAK,EAAE,CAAC,CAAC,CAAC;QAC9C,IAAI,MAAM,EAAE;YACV,OAAO,EAAE,KAAK,OAAA,EAAE,QAAQ,UAAA,EAAe,CAAA;SACxC;QACD,OAAO,KAAK,CAAC;IACf,CAAC,CAAC,CAAC;IACH,mBAAmB;IACnB,IAAI,MAAM,EAAE;QACV,OAAQ,MAAuB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;KAC9C;IACD,OAAO,MAAkB,CAAC;AAC5B,CAAC;AAzCD,uBAyCC"}
|
package/dist/jest.config.js
CHANGED
package/dist/jest.config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jest.config.js","sourceRoot":"","sources":["../jest.config.ts"],"names":[],"mappings":";;AAAA,kBAAe;IACb,UAAU,EAAE,IAAI;IAChB,eAAe,EAAE,IAAI;IACrB,iBAAiB,EAAE,UAAU;IAC7B,iBAAiB,EAAE,CAAC,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"jest.config.js","sourceRoot":"","sources":["../jest.config.ts"],"names":[],"mappings":";;AAAA,kBAAe;IACb,UAAU,EAAE,IAAI;IAChB,eAAe,EAAE,IAAI;IACrB,iBAAiB,EAAE,UAAU;IAC7B,iBAAiB,EAAE,CAAC,cAAc,EAAE,MAAM,CAAC;CAC5C,CAAC"}
|
package/jest.config.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "okapibm25",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.1",
|
|
4
4
|
"description": "A simple, easy to use implementation of the Okapi BM25 algorithm.",
|
|
5
5
|
"main": "dist/BM25.js",
|
|
6
6
|
"types": "dist/BM25.d.ts",
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
},
|
|
24
24
|
"license": "MIT",
|
|
25
25
|
"devDependencies": {
|
|
26
|
+
"@babel/preset-env": "^7.25.4",
|
|
26
27
|
"@babel/preset-typescript": "^7.14.5",
|
|
27
28
|
"@types/jest": "^26.0.24",
|
|
28
29
|
"istanbul-badges-readme": "^1.4.0",
|