Skip to content

Commit c5270ec

Browse files
add LLR + PPMI + revise SQL
1 parent b310767 commit c5270ec

File tree

1 file changed

+54
-9
lines changed

1 file changed

+54
-9
lines changed

scriptum.js

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3973,7 +3973,7 @@ Ait.interpolate = ({sep, trailing = false}) => async function* (ix) {
39733973
};
39743974

39753975

3976-
//█████ Chunk-Wise Traversal ██████████████████████████████████████████████████
3976+
//█████ Stream Files ██████████████████████████████████████████████████████████
39773977

39783978

39793979
/* Take a path and read the respective file chunk by chunk from a readable
@@ -4001,7 +4001,7 @@ Ait.streamFile = ({chunk: {read, parse}, rootPath = ""}) => path => {
40014001

40024002

40034003
/* Take a writable stream and an async iterator and write the yielded chunks to
4004-
a file using a the stream. Function is meant to be used with `Ait.streamFile`. */
4004+
a file using the stream. The function is meant to be used with `Ait.streamFile`. */
40054005

40064006
Ait.writeFile = stream => async ix => {
40074007
try {
@@ -12808,9 +12808,12 @@ export const Alg = {};
1280812808
███████████████████████████████████████████████████████████████████████████████*/
1280912809

1281012810

12811-
// calculate the exponent for a given base and result x
12811+
// create a log function for a given base
1281212812

12813-
Alg.logx = base => x => Math.log(x) / Math.log(base);
12813+
Alg.logx = base => x => {
12814+
if (x === 0) return -Infinity;
12815+
else return Math.log(x) / Math.log(base);
12816+
};
1281412817

1281512818

1281612819
Alg.log2 = Alg.logx(2);
@@ -12819,6 +12822,12 @@ Alg.log2 = Alg.logx(2);
1281912822
Alg.log3 = Alg.logx(3);
1282012823

1282112824

12825+
Alg.xLogY = (x, y) => {
12826+
if (x === 0 || y === 0) return 0;
12827+
else return x * Math.log(y);
12828+
};
12829+
12830+
1282212831
//█████ Fractions █████████████████████████████████████████████████████████████
1282312832

1282412833

@@ -13034,6 +13043,42 @@ Alg.manhattan = (xs, ys) => {
1303413043
};
1303513044

1303613045

13046+
//█████ Co-Occurrence █████████████████████████████████████████████████████████
13047+
13048+
13049+
/* Logarithmic likelihood ratio: find candidates for significant co-occurrences
13050+
(filtering). */
13051+
13052+
Alg.llr = (k1, n1, k2, n2) => {
13053+
const k = k1 + k2,
13054+
n = n1 + n2,
13055+
p1 = k1 / n1,
13056+
p2 = k2 / n2,
13057+
p = k / n,
13058+
entropy1 = xLogY(k1, p1) + xLogY(n1 - k1, 1 - p1),
13059+
entropy2 = xLogY(k2, p2) + xLogY(n2 - k2, 1 - p2),
13060+
entropy12 = xLogY(k, p) + xLogY(n - k, 1 - p);
13061+
13062+
return 2 * ((entropy1 + entropy2) - entropy12);
13063+
};
13064+
13065+
13066+
/* Positive poitwise mutual information: rank candidates with significant
13067+
co-occurrences (ranking). */
13068+
13069+
Alg.ppmi = (countX, countY, countXY, total_events) => {
13070+
if (countXY === 0) return 0;
13071+
else if (countX === 0 || countY === 0 || total_events === 0) return 0;
13072+
13073+
const ratioX = countX / total_events,
13074+
ratioY = countY / total_events,
13075+
ratioXY = countXY / total_events,
13076+
pmi = Alg.log2(ratioXY / (ratioX * ratioY));
13077+
13078+
return Math.max(0, pmi);
13079+
};
13080+
13081+
1303713082
/*█████████████████████████████████████████████████████████████████████████████
1303813083
███████████████████████████████████████████████████████████████████████████████
1303913084
████████████████████████████████████ NODE █████████████████████████████████████
@@ -13371,15 +13416,15 @@ Node.SQL = {};
1337113416
//█████ Types █████████████████████████████████████████████████████████████████
1337213417

1337313418

13374-
Node.SQL.sqlQuery = ({query, meta = {}}) => ({
13419+
Node.SQL.SqlQuery = ({query, meta = {}}) => ({
1337513420
[$]: "SqlQuery",
1337613421
[$$]: "SqlQuery",
1337713422
query,
1337813423
meta,
1337913424
});
1338013425

1338113426

13382-
Node.SQL.sqlResult = ({data, fields, query, meta}) => ({
13427+
Node.SQL.SqlResult = ({data, fields, query, meta}) => ({
1338313428
[$]: "SqlResult",
1338413429
[$$]: "SqlResult",
1338513430
data,
@@ -13425,10 +13470,10 @@ Node.SQL.disconnect = ressource =>
1342513470
? rej(new Err(e)) : res(true)));
1342613471

1342713472

13428-
Node.SQL.query = connection => sql => Cont((res, rej) => {
13429-
return connection.query(sql, (e, data, fields) => {
13473+
Node.SQL.query = connection => ({query, meta}) => Cont((res, rej) => {
13474+
return connection.query(query, (e, data, fields) => {
1343013475
if (e) return rej(new Err(e));
13431-
else return res({data, fields});
13476+
else return res(Node.SQL.SqlResult({data, fields, query, meta}));
1343213477
});
1343313478
});
1343413479

0 commit comments

Comments
 (0)