rack-node
Version:
write like human, search like computer
65 lines (64 loc) • 2.76 kB
JavaScript
;
/**
* Parvez M Robin
* this@parvezmrobin.com
* Jul 07, 2020
*/
Object.defineProperty(exports, "__esModule", { value: true });
const db_1 = require("../db");
const CosineSimilarity_1 = require("../similarity/CosineSimilarity");
const app_json_1 = require("../config/app.json");
class AdjacencyScoreProvider {
constructor(tokenMap) {
this.tokenMap = tokenMap;
this.adjacencyMap = new Map();
this.tokenScores = new Map();
}
async getScore() {
await this.collectAdjacentTerms();
const textTokens = [...this.tokenMap.keys()];
const dim = textTokens.length;
for (let i = 0; i < dim; i++) {
const firstToken = textTokens[i];
const firstAdjacencyList = this.adjacencyMap.get(firstToken);
if (!firstAdjacencyList) {
throw new Error(`no adjacency list found for ${firstToken}`);
}
for (let j = i + 1; j < dim; j++) {
const secondToken = textTokens[j];
const secondAdjacencyList = this.adjacencyMap.get(secondToken);
if (!secondAdjacencyList) {
throw new Error(`no adjacency list found for ${secondToken}`);
}
const similarityScore = new CosineSimilarity_1.default(firstAdjacencyList, secondAdjacencyList).getScore();
if (similarityScore <= app_json_1.gamma) {
continue;
}
const firstApiList = this.tokenMap.get(firstToken);
const secondApiList = this.tokenMap.get(secondToken);
const commonApiList = firstApiList.filter(token => secondApiList.includes(token));
for (const api of commonApiList) {
const currentScore = this.tokenScores.get(api) || 0;
this.tokenScores.set(api, currentScore + similarityScore);
}
}
}
return this.tokenScores;
}
async collectAdjacentTerms() {
try {
const query = 'select distinct Token from TextToken where EntryID in (select EntryID from TextToken where Token=?) and Token!=?';
for (const textToken of this.tokenMap.keys()) {
// eslint-disable-next-line no-await-in-loop
const result = await db_1.exec(query, [textToken, textToken]);
const tokens = result.map(row => row.Token);
this.adjacencyMap.set(textToken, tokens);
}
}
catch (error) {
// eslint-disable-next-line no-console
console.error(error);
}
}
}
exports.default = AdjacencyScoreProvider;