vp tree builder and query for node js
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

136 lines
3.2 KiB

const express = require('express');
const bodyParser = require('body-parser');
const vptree = require('vptree'); // Assuming you have the vptree library available
const fs = require('fs');
const path = require('path');
const app = express();
const port = 3000;
app.use(bodyParser.json());
var dataset = [
'culture',
'democracy',
'metaphor',
'irony',
'hypothesis',
'science',
'fastuous',
'integrity',
'synonym',
'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
loadVPTree()
// Load tree endpoint
app.post('/search/:word', (req, res) => {
const searchWord = req.params.word;
if (!searchWord) {
return res.status(400).send({ success: false, message: 'No word provided' });
}
let idx = queryVPTree("democratic")
res.send({idx});
});
function queryVPTree(value){
let nearest = tree.search(value)
let index = nearest[0].i
return index
}
function buildVPTree(){
// building the tree
tree = vptree.build(stringList, levenshteinDistance)
}
function saveVPTree(fileName){
const treeString = tree.stringify();
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 loadVPTree(){
loadTreeFromDisk("vp-tree-new", (err, treeData) => {
if (err) {
console.error('Failed to load tree from disk:', err);
return;
}
// Rebuild the VP-tree using the saved structure
try {
tree = vptree.load(dataset, levenshteinDistance, treeData);
console.log('Tree loaded successfully.');
} catch (loadError) {
console.error('Error loading the VP-tree:', loadError);
}
});
}
function loadTreeFromDisk(fileName, callback){
const filePath = path.join(__dirname, `${fileName}.txt`);
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading the file:', err);
return callback(err, null);
}
// Parse the data as a JavaScript object
try {
const treeData = eval(`(${data})`); // Using eval to convert to object
callback(null, treeData);
} catch (parseError) {
console.error('Error parsing the tree data:', parseError);
callback(parseError, null);
}
});
}
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});