forked from nextup/nextup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocFetch.js
More file actions
72 lines (67 loc) · 2.45 KB
/
docFetch.js
File metadata and controls
72 lines (67 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
var rest = require('restler');
var Promise = require('bluebird');
exports.cosSimFetch = function(cypherURL, docURL, cosSim, limit) {
return new Promise(function(resolve, reject) {
// console.log("Looking for ", docURL, " inside neo4j!");
cosSim = cosSim || 0.0;
limit = limit || 5;
var resultArray = [];
var query = {
query: "MATCH (a:Document { url: '" + docURL + "' } )-[s:SIMILARITY]-(b:Document) WHERE s.similarity > " + cosSim + " WITH s.similarity AS sim,a,b ORDER BY sim desc LIMIT " + limit + " RETURN b,sim"
};
rest.postJson(cypherURL, query)
.on("success", function(result) {
// console.log("\nTop " + limit + " similar documents to: " + docURL + "\n");
for (var i = 0; i < result.data.length; i++) {
var sim = { title: result.data[i][0].data.title, url: result.data[i][0].data.url, similarity: result.data[i][1] };
resultArray.push(sim);
// console.log("Title: ", result.data[i][0].data.title, "\nSimilarity: ", result.data[i][1], "\nUrl: ", result.data[i][0].data.url, "\n");
}
// console.log("Result Array: ", resultArray);
resolve(resultArray);
})
.on("failure", function(result) {
reject(result);
});
});
};
exports.randNodeFetch = function(cypherURL, limit) {
return new Promise(function(resolve, reject) {
limit = limit || 5;
var resultArray = [];
var query = {
query: "MATCH (d:Document) WITH d, rand() AS rand RETURN d ORDER BY rand LIMIT " + limit
};
rest.postJson(cypherURL, query)
.on("success", function(result) {
for (var i = 0; i < result.data.length; i++) {
var sim = { title: result.data[i][0].data.title, url: result.data[i][0].data.url};
resultArray.push(sim);
}
resolve(resultArray);
})
.on("failure", function(result) {
reject(result);
});
});
};
exports.allDocFetch = function(cypherURL) {
return new Promise(function(resolve, reject) {
var resultArray = [];
var query = {
query: "MATCH (d:Document) RETURN d"
};
rest.postJson(cypherURL, query)
.on("success", function(result) {
for (var i = 0; i < result.data.length; i++) {
var sim = { title: result.data[i][0].data.title, url: result.data[i][0].data.url};
resultArray.push(sim);
}
console.log("Length of all document query: ", resultArray.length);
resolve(resultArray);
})
.on("failure", function(result) {
reject(result);
});
});
};