Browse Source

init

master
Cailean Finn 2 months ago
commit
eff4a607d8
  1. 1
      .gitignore
  2. 1198
      package-lock.json
  3. 17
      package.json
  4. 136
      server.js
  5. 1
      vp-tree-new.txt
  6. 1
      vp-tree.txt

1
.gitignore

@ -0,0 +1 @@
/node_modules/

1198
package-lock.json

File diff suppressed because it is too large

17
package.json

@ -0,0 +1,17 @@
{
"name": "vp-tree",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.20.2",
"express": "^4.19.2",
"vptree": "^1.0.0"
}
}

136
server.js

@ -0,0 +1,136 @@
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}`);
});

1
vp-tree-new.txt

@ -0,0 +1 @@
{i:4,m:7,M:9,μ:9,L:{i:6,m:7,M:8,μ:8,L:{i:8},R:{i:9,m:7,M:7,μ:7,R:{i:5}}},R:{i:2,m:8,M:8,μ:8,L:{i:0,m:7,M:7,μ:7,R:{i:3}},R:{i:7,m:7,M:7,μ:7,R:{i:1}}}}

1
vp-tree.txt

@ -0,0 +1 @@
{i:6,m:6,M:9,μ:8,L:{i:3,m:4,M:9,μ:7,L:{i:8},R:{i:4,m:9,M:9,μ:9,R:{i:0}}},R:{i:7,m:7,M:8,μ:8,L:{i:9,m:6,M:6,μ:6,R:{i:1}},R:{i:2,m:8,M:8,μ:8,R:{i:5}}}}
Loading…
Cancel
Save