setup for emotion embeddgins
This commit is contained in:
1757
embeddings.json
Normal file
1757
embeddings.json
Normal file
File diff suppressed because it is too large
Load Diff
79
package-lock.json
generated
79
package-lock.json
generated
@@ -10,6 +10,7 @@
|
|||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"body-parser": "^1.20.2",
|
"body-parser": "^1.20.2",
|
||||||
|
"compute-cosine-similarity": "^1.1.0",
|
||||||
"express": "^4.19.2",
|
"express": "^4.19.2",
|
||||||
"vptree": "^1.0.0"
|
"vptree": "^1.0.0"
|
||||||
}
|
}
|
||||||
@@ -80,6 +81,35 @@
|
|||||||
"url": "https://github.com/sponsors/ljharb"
|
"url": "https://github.com/sponsors/ljharb"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/compute-cosine-similarity": {
|
||||||
|
"version": "1.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/compute-cosine-similarity/-/compute-cosine-similarity-1.1.0.tgz",
|
||||||
|
"integrity": "sha512-FXhNx0ILLjGi9Z9+lglLzM12+0uoTnYkHm7GiadXDAr0HGVLm25OivUS1B/LPkbzzvlcXz/1EvWg9ZYyJSdhTw==",
|
||||||
|
"dependencies": {
|
||||||
|
"compute-dot": "^1.1.0",
|
||||||
|
"compute-l2norm": "^1.1.0",
|
||||||
|
"validate.io-array": "^1.0.5",
|
||||||
|
"validate.io-function": "^1.0.2"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/compute-dot": {
|
||||||
|
"version": "1.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/compute-dot/-/compute-dot-1.1.0.tgz",
|
||||||
|
"integrity": "sha512-L5Ocet4DdMrXboss13K59OK23GXjiSia7+7Ukc7q4Bl+RVpIXK2W9IHMbWDZkh+JUEvJAwOKRaJDiFUa1LTnJg==",
|
||||||
|
"dependencies": {
|
||||||
|
"validate.io-array": "^1.0.3",
|
||||||
|
"validate.io-function": "^1.0.2"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/compute-l2norm": {
|
||||||
|
"version": "1.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/compute-l2norm/-/compute-l2norm-1.1.0.tgz",
|
||||||
|
"integrity": "sha512-6EHh1Elj90eU28SXi+h2PLnTQvZmkkHWySpoFz+WOlVNLz3DQoC4ISUHSV9n5jMxPHtKGJ01F4uu2PsXBB8sSg==",
|
||||||
|
"dependencies": {
|
||||||
|
"validate.io-array": "^1.0.3",
|
||||||
|
"validate.io-function": "^1.0.2"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/content-disposition": {
|
"node_modules/content-disposition": {
|
||||||
"version": "0.5.4",
|
"version": "0.5.4",
|
||||||
"resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.4.tgz",
|
"resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.4.tgz",
|
||||||
@@ -687,6 +717,16 @@
|
|||||||
"node": ">= 0.4.0"
|
"node": ">= 0.4.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/validate.io-array": {
|
||||||
|
"version": "1.0.6",
|
||||||
|
"resolved": "https://registry.npmjs.org/validate.io-array/-/validate.io-array-1.0.6.tgz",
|
||||||
|
"integrity": "sha512-DeOy7CnPEziggrOO5CZhVKJw6S3Yi7e9e65R1Nl/RTN1vTQKnzjfvks0/8kQ40FP/dsjRAOd4hxmJ7uLa6vxkg=="
|
||||||
|
},
|
||||||
|
"node_modules/validate.io-function": {
|
||||||
|
"version": "1.0.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/validate.io-function/-/validate.io-function-1.0.2.tgz",
|
||||||
|
"integrity": "sha512-LlFybRJEriSuBnUhQyG5bwglhh50EpTL2ul23MPIuR1odjO7XaMLFV8vHGwp7AZciFxtYOeiSCT5st+XSPONiQ=="
|
||||||
|
},
|
||||||
"node_modules/vary": {
|
"node_modules/vary": {
|
||||||
"version": "1.1.2",
|
"version": "1.1.2",
|
||||||
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
|
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
|
||||||
@@ -752,6 +792,35 @@
|
|||||||
"set-function-length": "^1.2.1"
|
"set-function-length": "^1.2.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"compute-cosine-similarity": {
|
||||||
|
"version": "1.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/compute-cosine-similarity/-/compute-cosine-similarity-1.1.0.tgz",
|
||||||
|
"integrity": "sha512-FXhNx0ILLjGi9Z9+lglLzM12+0uoTnYkHm7GiadXDAr0HGVLm25OivUS1B/LPkbzzvlcXz/1EvWg9ZYyJSdhTw==",
|
||||||
|
"requires": {
|
||||||
|
"compute-dot": "^1.1.0",
|
||||||
|
"compute-l2norm": "^1.1.0",
|
||||||
|
"validate.io-array": "^1.0.5",
|
||||||
|
"validate.io-function": "^1.0.2"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"compute-dot": {
|
||||||
|
"version": "1.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/compute-dot/-/compute-dot-1.1.0.tgz",
|
||||||
|
"integrity": "sha512-L5Ocet4DdMrXboss13K59OK23GXjiSia7+7Ukc7q4Bl+RVpIXK2W9IHMbWDZkh+JUEvJAwOKRaJDiFUa1LTnJg==",
|
||||||
|
"requires": {
|
||||||
|
"validate.io-array": "^1.0.3",
|
||||||
|
"validate.io-function": "^1.0.2"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"compute-l2norm": {
|
||||||
|
"version": "1.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/compute-l2norm/-/compute-l2norm-1.1.0.tgz",
|
||||||
|
"integrity": "sha512-6EHh1Elj90eU28SXi+h2PLnTQvZmkkHWySpoFz+WOlVNLz3DQoC4ISUHSV9n5jMxPHtKGJ01F4uu2PsXBB8sSg==",
|
||||||
|
"requires": {
|
||||||
|
"validate.io-array": "^1.0.3",
|
||||||
|
"validate.io-function": "^1.0.2"
|
||||||
|
}
|
||||||
|
},
|
||||||
"content-disposition": {
|
"content-disposition": {
|
||||||
"version": "0.5.4",
|
"version": "0.5.4",
|
||||||
"resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.4.tgz",
|
"resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.4.tgz",
|
||||||
@@ -1184,6 +1253,16 @@
|
|||||||
"resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz",
|
"resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz",
|
||||||
"integrity": "sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA=="
|
"integrity": "sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA=="
|
||||||
},
|
},
|
||||||
|
"validate.io-array": {
|
||||||
|
"version": "1.0.6",
|
||||||
|
"resolved": "https://registry.npmjs.org/validate.io-array/-/validate.io-array-1.0.6.tgz",
|
||||||
|
"integrity": "sha512-DeOy7CnPEziggrOO5CZhVKJw6S3Yi7e9e65R1Nl/RTN1vTQKnzjfvks0/8kQ40FP/dsjRAOd4hxmJ7uLa6vxkg=="
|
||||||
|
},
|
||||||
|
"validate.io-function": {
|
||||||
|
"version": "1.0.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/validate.io-function/-/validate.io-function-1.0.2.tgz",
|
||||||
|
"integrity": "sha512-LlFybRJEriSuBnUhQyG5bwglhh50EpTL2ul23MPIuR1odjO7XaMLFV8vHGwp7AZciFxtYOeiSCT5st+XSPONiQ=="
|
||||||
|
},
|
||||||
"vary": {
|
"vary": {
|
||||||
"version": "1.1.2",
|
"version": "1.1.2",
|
||||||
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
|
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"body-parser": "^1.20.2",
|
"body-parser": "^1.20.2",
|
||||||
|
"compute-cosine-similarity": "^1.1.0",
|
||||||
"express": "^4.19.2",
|
"express": "^4.19.2",
|
||||||
"vptree": "^1.0.0"
|
"vptree": "^1.0.0"
|
||||||
}
|
}
|
||||||
|
|||||||
105
server.js
105
server.js
@@ -1,55 +1,38 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const bodyParser = require('body-parser');
|
const bodyParser = require('body-parser');
|
||||||
const vptree = require('vptree'); // Assuming you have the vptree library available
|
const vptree = require('vptree');
|
||||||
|
const similarity = require('compute-cosine-similarity');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const port = 3000;
|
const port = 2000;
|
||||||
|
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
|
|
||||||
|
// Function to load JSON data
|
||||||
var dataset = [
|
function loadJSON(filePath, callback) {
|
||||||
'culture',
|
fs.readFile(filePath, 'utf8', (err, data) => {
|
||||||
'democracy',
|
if (err) {
|
||||||
'metaphor',
|
return callback(err, null);
|
||||||
'irony',
|
}
|
||||||
'hypothesis',
|
try {
|
||||||
'science',
|
const jsonData = JSON.parse(data);
|
||||||
'fastuous',
|
callback(null, jsonData);
|
||||||
'integrity',
|
} catch (parseError) {
|
||||||
'synonym',
|
callback(parseError, null);
|
||||||
'empathy' // and on and on...
|
}
|
||||||
];
|
});
|
||||||
|
|
||||||
// Example Levenshtein distance function
|
|
||||||
function levenshteinDistance(a, b) {
|
|
||||||
// Implement or use an existing Levenshtein distance function
|
|
||||||
// Here's a simple implementation:
|
|
||||||
const d = [];
|
|
||||||
const alen = a.length;
|
|
||||||
const blen = b.length;
|
|
||||||
|
|
||||||
for (let i = 0; i <= alen; i++) d[i] = [i];
|
|
||||||
for (let j = 0; j <= blen; j++) d[0][j] = j;
|
|
||||||
|
|
||||||
for (let i = 1; i <= alen; i++) {
|
|
||||||
for (let j = 1; j <= blen; j++) {
|
|
||||||
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
|
||||||
d[i][j] = Math.min(
|
|
||||||
d[i - 1][j] + 1,
|
|
||||||
d[i][j - 1] + 1,
|
|
||||||
d[i - 1][j - 1] + cost
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return d[alen][blen];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let tree
|
function cosineDistanceMatching(vector1, vector2) {
|
||||||
|
let cosineSimilarity = similarity(vector1, vector2);
|
||||||
|
let distance = 2 * (1 - cosineSimilarity);
|
||||||
|
return Math.sqrt(distance);
|
||||||
|
}
|
||||||
|
|
||||||
|
let tree, vectors, jsonEmbeddings
|
||||||
|
|
||||||
|
buildVPTree()
|
||||||
loadVPTree()
|
loadVPTree()
|
||||||
|
|
||||||
// Load tree endpoint
|
// Load tree endpoint
|
||||||
@@ -61,9 +44,14 @@ app.post('/search/:word', (req, res) => {
|
|||||||
return res.status(400).send({ success: false, message: 'No word provided' });
|
return res.status(400).send({ success: false, message: 'No word provided' });
|
||||||
}
|
}
|
||||||
|
|
||||||
let idx = queryVPTree("democratic")
|
// vector = [angry, disgust, fear, happy, sad, suprise, neutral]
|
||||||
|
let query_vec = [0, 0, 1.0, 0, 0, 0, 0]
|
||||||
|
let idx = queryVPTree(query_vec)
|
||||||
|
|
||||||
res.send({idx});
|
// Retrieve the filename from the stored JSON data
|
||||||
|
let filename = idx !== undefined && idx < jsonEmbeddings.length ? jsonEmbeddings[idx].filename : null;
|
||||||
|
|
||||||
|
res.send({filename});
|
||||||
});
|
});
|
||||||
|
|
||||||
function queryVPTree(value){
|
function queryVPTree(value){
|
||||||
@@ -73,8 +61,31 @@ function queryVPTree(value){
|
|||||||
}
|
}
|
||||||
|
|
||||||
function buildVPTree(){
|
function buildVPTree(){
|
||||||
// building the tree
|
// Load JSON data from embeddings.json
|
||||||
tree = vptree.build(stringList, levenshteinDistance)
|
const jsonFilePath = path.join(__dirname, 'embeddings.json');
|
||||||
|
loadJSON(jsonFilePath, (err, jsonData) => {
|
||||||
|
if (err) {
|
||||||
|
console.error('Error loading JSON:', err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Extract vectors from the JSON data
|
||||||
|
jsonEmbeddings = jsonData
|
||||||
|
vectors = jsonEmbeddings.map(item => item.vector);
|
||||||
|
tree = vptree.build(vectors, cosineDistanceMatching)
|
||||||
|
const treeString = tree.stringify();
|
||||||
|
const fileName = "tree"
|
||||||
|
const filePath = path.join(__dirname, `${fileName}.txt`);
|
||||||
|
|
||||||
|
fs.writeFile(filePath, treeString, 'utf8', (err) => {
|
||||||
|
if (err) {
|
||||||
|
console.log("Tree did not save to file.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("Tree saved successfully.")
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveVPTree(fileName){
|
function saveVPTree(fileName){
|
||||||
@@ -94,7 +105,7 @@ function saveVPTree(fileName){
|
|||||||
}
|
}
|
||||||
|
|
||||||
function loadVPTree(){
|
function loadVPTree(){
|
||||||
loadTreeFromDisk("vp-tree-new", (err, treeData) => {
|
loadTreeFromDisk("tree", (err, treeData) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error('Failed to load tree from disk:', err);
|
console.error('Failed to load tree from disk:', err);
|
||||||
return;
|
return;
|
||||||
@@ -102,7 +113,7 @@ function loadVPTree(){
|
|||||||
|
|
||||||
// Rebuild the VP-tree using the saved structure
|
// Rebuild the VP-tree using the saved structure
|
||||||
try {
|
try {
|
||||||
tree = vptree.load(dataset, levenshteinDistance, treeData);
|
tree = vptree.load(vectors, cosineDistanceMatching, treeData);
|
||||||
console.log('Tree loaded successfully.');
|
console.log('Tree loaded successfully.');
|
||||||
|
|
||||||
} catch (loadError) {
|
} catch (loadError) {
|
||||||
|
|||||||
Reference in New Issue
Block a user