Skip to content

Commit

Permalink
Graphing is now working. Still need to add support for images and sum…
Browse files Browse the repository at this point in the history
…maries.
  • Loading branch information
davidlivingrooms committed Jun 17, 2015
1 parent 815dadd commit 804d964
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 203 deletions.
1 change: 1 addition & 0 deletions conf/remove_disambiguous_articles.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DELETE FROM article WHERE array_length(links, 1) = 1;
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@
"watchify": "^2.1.1"
},
"dependencies": {
"express": "~4.0.0",
"static-favicon": "~1.0.0",
"morgan": "~1.0.0",
"cookie-parser": "~1.0.1",
"body-parser": "~1.0.0",
"cookie-parser": "~1.0.1",
"debug": "~0.7.4",
"ejs": "~0.8.5",
"stylus": "0.42.3",
"mongoose": "^3.8.19"
"express": "~4.0.0",
"mongoose": "^3.8.19",
"morgan": "~1.0.0",
"pg": "^4.4.0",
"static-favicon": "~1.0.0",
"stylus": "0.42.3"
},
"browserify-shim": {
"cola": "global:cola",
Expand Down
66 changes: 37 additions & 29 deletions public/js/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,54 @@ var Node = require('./node.js');
var Edge = require('./edge.js');
var Promise = require('bluebird');
var $ = require('jquery');
var StringUtils = new (require('./string_utils.js'));

function Graph() {
this.nodes = {};
this.edges = {};

Graph.prototype.addNode = function (title, rid) {
var node = new Node(title, rid);
return this.nodes[node.getDomCompatibleRid()] = node;
Graph.prototype.addNode = function (title) {
var node = new Node(title);
return this.nodes[StringUtils.encodeID(node.title)] = node;
};

Graph.prototype.getNode = function (title, rid, addViewNode) {
Graph.prototype.getNode = function (title, callback) {
var _this = this;
if (rid in this.nodes) {
return this.nodes[rid];
var encodedArticleTitle = StringUtils.encodeID(title);
if (encodedArticleTitle in this.nodes) {
return this.nodes[encodedArticleTitle];
}

var promise = articlePromise(title, rid);
var node = _this.addNode(title);
if (callback) {
callback(node);
}

var promise = articlePromise(title);
return promise.then(function (data) {
var node = _this.addNode(title, data.rid);
node.imageUrl = data.imageUrl;
node.summaryText = data.summaryText;
addViewNode(node);

if (typeof data.nodes !== 'undefined') {
data.nodes.map(function (nodeObject) {
var linkNodeObject = new Node(nodeObject.title, nodeObject.rid);
if (node.getDomCompatibleRid() !== linkNodeObject.getDomCompatibleRid()) {
node.links.push(nodeObject);
}
});

if (typeof data[0] !== 'undefined' && data[0].links !== null) {
node.links = data[0].links.splice(0,3);
}
else {
node.links = [];
}

//data[0].links.map(function (nodeObject) {
// var linkNodeObject = new Node(nodeObject.title);
// if (node.getDomCompatibleTitle() !== linkNodeObject.getDomCompatibleTitle()) {
// node.links.push(nodeObject);
// }
//});

return node;
});
};

Graph.prototype.addEdge = function (fromNode, toNode) {
var edge = new Edge(fromNode.getDomCompatibleRid(), toNode.getDomCompatibleRid());
var edge = new Edge(StringUtils.encodeID(fromNode.title), StringUtils.encodeID(toNode.title));
var edgeName = edge.toString();
if (!(edgeName in this.edges)) {
this.edges[edgeName] = edge;
Expand All @@ -54,34 +64,32 @@ function Graph() {

Graph.prototype.isFullyExpanded = function (node) {
var _this = this;
return node.links && node.links.every(function(element) {
var elementNode = new Node(element.title, element.rid);
return elementNode.getDomCompatibleRid() in _this.nodes;
return node.links && node.links.every(function(articleTitle) {
var elementNode = new Node(articleTitle);
return StringUtils.encodeID(elementNode.title) in _this.nodes;
})
};

Graph.prototype.expandNeighbours = function (node, callback) {
var _this = this;
var promisesArray = node.links.map(function (nodeObject) {
var linkNodeObject = new Node(nodeObject.title, nodeObject.rid);
if (node.getDomCompatibleRid() !== linkNodeObject.getDomCompatibleRid()) {
return _this.getNode(linkNodeObject.title, null, function(vertex) {
var promisesArray = node.links.map(function (articleTitle) {
var linkNodeObject = new Node(articleTitle);
return _this.getNode(linkNodeObject.title, function(vertex) {
_this.addEdge(node, vertex);
callback(vertex);
});
}
});

return Promise.all(promisesArray);
//TODO what if attached to previous node?
};
}

function articlePromise(title, rid) {
function articlePromise(title) {
return Promise.resolve(
$.ajax({
url: 'http://localhost:3000/wikimaps/getArticleInfo?title',
data: {title: title, rid: rid},
url: 'http://localhost:3000/wikimaps/getArticleInfo',
data: {title: title},
dataType: 'json'
}));
}
Expand Down
9 changes: 4 additions & 5 deletions public/js/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
* Created by david on 3/30/2015.
*/
var Promise = require('bluebird');
var StringUtils = new (require('./string_utils.js'));

function Node(title, rid) {
function Node(title) {
this.title = title;
this.links = [];
this.rid = rid;
this.degree = 0;

Node.prototype.getDomCompatibleRid = function () {
var rid = this.rid;
return [rid.slice(0, 0), "element", rid.slice(0)].join('').replace(/:/g, '');
Node.prototype.getDomCompatibleTitle = function() {
return "article" + StringUtils.encodeID(title);//TODO this needs to be added to the string utils method
};
}

Expand Down
14 changes: 14 additions & 0 deletions public/js/string_utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Created by david on 3/30/2015.
*/

function StringUtils() {
StringUtils.prototype.encodeID = function(s) {
if (s==='') return '_';
return 'article' + s.replace(/[^a-zA-Z0-9.-]/g, function(match) {
return '_'+match[0].charCodeAt(0).toString(16)+'_';
});
};
}

module.exports = StringUtils;
42 changes: 20 additions & 22 deletions public/js/wikigraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@ var d3 = require('d3');
var cola = require('cola');
var Promise = require('bluebird');
var Graph = require('./graph.js');
var StringUtils = new (require('./string_utils.js'));
var graph;

module.exports = {
initializeNewGraph: function(rootTitle) {
initializeNewGraph: function(rootArticle) {
d3.select("svg").remove();
drawGraph(rootTitle);
drawGraph(rootArticle);
}
}
};

function drawGraph(rootTitle) {
function drawGraph(rootArticle) {
graph = new wikiGraph("#svgdiv");
graph.start(rootTitle);
graph.start(rootArticle);
}

function wikiGraph()
{
this.start = function(rootTitle) {
this.start = function(rootArticle) {
var width = 960,
height = 500,
imageScale = 0.1;
Expand Down Expand Up @@ -97,21 +98,22 @@ function wikiGraph()
var viewgraph = { nodes: [], links: [] };
var nodeWidth = 30, nodeHeight = 35;
// get first node
var d = modelgraph.getNode(rootTitle, null, addViewNode);
var d = modelgraph.getNode(rootArticle.value, addViewNode);
d.then(function (startNode) {
//addViewNode(startNode);
refocus(startNode);
});

function refocus(focus) {
var neighboursExpanded = modelgraph.expandNeighbours(focus, function (v) {
if (!inView(v)){
addViewNode(v, focus);
loadImage(v);
//loadImage(v);
}
});
refreshViewGraph();
loadImage(focus);
$.when(neighboursExpanded).then(function f() {
//loadImage(focus);
neighboursExpanded.then(function f() {//TODO why $.when. just use the promise
refreshViewGraph();
});
}
Expand Down Expand Up @@ -155,7 +157,7 @@ function wikiGraph()
y = h / 2 + 30 * Math.sin(r * i),
rect = new cola.vpsc.Rectangle(0, w, 0, h),
vi = rect.rayIntersection(x, y);
var dview = d3.select("#"+v.getDomCompatibleRid()+"_spikes");
var dview = d3.select("#"+StringUtils.encodeID(v.title)+"_spikes");
dview.append("rect")
.attr("class", "spike")
.attr("rx", 1).attr("ry", 1)
Expand All @@ -167,7 +169,7 @@ function wikiGraph()
}

function unhintNeighbours(v) {
var dview = d3.select("#" + v.getDomCompatibleRid() + "_spikes");
var dview = d3.select("#" + StringUtils.encodeID(v.title) + "_spikes");
dview.selectAll(".spike").remove();
}

Expand All @@ -176,7 +178,7 @@ function wikiGraph()
}

function loadImage(v) {
d3.select("#" + v.getDomCompatibleRid()).append("image")
d3.select("#" + StringUtils.encodeID(v.title)).append("image")
.attr("transform", "translate(2,2)")
.attr("xlink:href", function (v) {
var url = v.imageUrl;
Expand All @@ -194,7 +196,6 @@ function wikiGraph()

function addViewNode(v, startpos) {
v.viewgraphid = viewgraph.nodes.length;

if (typeof startpos !== 'undefined') {
v.x = startpos.x;
v.y = startpos.y;
Expand All @@ -205,15 +206,13 @@ function wikiGraph()
function click(node) {
$("#wikipediaArticleLink").attr('href', 'http://en.wikipedia.org/wiki/' + node.title);
$("#wikipediaArticleLinkLabel").text(node.title);
$('#articleContent').text(node.summaryText);
//if (node.color !== red)
if (node.expanded || !node.links || node.links.length === 0)
//$('#articleContent').text(node.summaryText);
if (node.color !== red)
{
return;
}
var focus = modelgraph.getNode(node.title, node.getDomCompatibleRid());
var focus = modelgraph.getNode(node.title);
refocus(focus);
node.expanded = true;
}

function update() {
Expand Down Expand Up @@ -242,8 +241,7 @@ function wikiGraph()
.data(viewgraph.nodes, function (d) { return d.viewgraphid; })

var nodeEnter = node.enter().append("g")
//.attr("id", function (d) { return d.getTitle() })
.attr("id", function (d) { return d.getDomCompatibleRid() })
.attr("id", function (d) { return StringUtils.encodeID(d.title) })
.attr("class", "node" )
.on("mousedown", function () { nodeMouseDown = true; }) // recording the mousedown state allows us to differentiate dragging from panning
.on("mouseup", function () { nodeMouseDown = false; })
Expand All @@ -254,7 +252,7 @@ function wikiGraph()
.on('mouseover', tip.show)
.on('mouseout', tip.hide);

nodeEnter.append("g").attr("id", function (d) { return d.getDomCompatibleRid() + "_spikes" })
nodeEnter.append("g").attr("id", function (d) { return StringUtils.encodeID(d.title) + "_spikes" })
.attr("transform", "translate(3,3)");

nodeEnter.append("rect")
Expand Down
15 changes: 9 additions & 6 deletions public/js/wikisearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ module.exports = {
remote: {
url: 'http://localhost:3000/wikimaps/findArticles?title=%QUERY',
filter: function (articles) {
// Map the remote source JSON array to a JavaScript array
// Map the remote source JSsON array to a JavaScript array
return $.map(articles, function (article) {
return {
value: capitalizeWords(article.key)
value: capitalizeWords(article.title),
id: article.id
};
});
}
}
},
rateLimitWait: 1000
},
limit: 10
});

function capitalizeWords(str) {
Expand All @@ -41,8 +44,8 @@ module.exports = {
displayKey: 'value',
source: articles.ttAdapter()
})
.bind('typeahead:selected', function(obj, selected) {
WikiGraph.initializeNewGraph(selected.value);
.bind('typeahead:selected', function(obj, selectedArticle) {
WikiGraph.initializeNewGraph(selectedArticle);
})
.off('blur');
}
Expand Down
Loading

0 comments on commit 804d964

Please sign in to comment.