-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdependency-tree.js
157 lines (150 loc) · 6.27 KB
/
dependency-tree.js
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Generated by CoffeeScript 1.4.0
var dependencyDict, levelHeight, maximum, parseConll, tagDict, under, wordHeight, wordWidth;
wordWidth = 60;
wordHeight = 20;
levelHeight = function(level) {
return 2 + Math.pow(level, 1.8) * 10;
};
window.drawTree = function(svgElement, conllData) {
var arrows, data, dependencies, e, edge, edges, item, svg, tags, treeHeight, treeWidth, triangle, words, _i, _j, _k, _len, _len1, _len2;
svg = d3.select(svgElement);
data = parseConll(conllData);
edges = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = data.length; _i < _len; _i++) {
item = data[_i];
if (item.id) {
_results.push(item);
}
}
return _results;
})();
for (_i = 0, _len = edges.length; _i < _len; _i++) {
edge = edges[_i];
for (_j = 0, _len1 = edges.length; _j < _len1; _j++) {
edge = edges[_j];
edge.level = 1 + maximum((function() {
var _k, _len2, _results;
_results = [];
for (_k = 0, _len2 = edges.length; _k < _len2; _k++) {
e = edges[_k];
if (under(edge, e)) {
_results.push(e.level);
}
}
return _results;
})());
}
}
treeWidth = wordWidth * data.length - wordWidth / 3;
treeHeight = levelHeight(maximum((function() {
var _k, _len2, _results;
_results = [];
for (_k = 0, _len2 = data.length; _k < _len2; _k++) {
edge = data[_k];
_results.push(edge.level);
}
return _results;
})())) + 2 * wordHeight;
for (_k = 0, _len2 = data.length; _k < _len2; _k++) {
item = data[_k];
item.bottom = treeHeight - 1.8 * wordHeight;
item.top = item.bottom - levelHeight(item.level);
item.left = treeWidth - item.id * wordWidth;
item.right = treeWidth - item.parent * wordWidth;
item.mid = (item.right + item.left) / 2;
item.diff = (item.right - item.left) / 4;
item.arrow = item.top + (item.bottom - item.top) * .25;
}
svg.selectAll('text, path').remove();
svg.attr('xmlns', 'http://www.w3.org/2000/svg');
svg.attr('width', treeWidth + 2 * wordWidth / 3).attr('height', treeHeight + wordHeight / 2);
words = svg.selectAll('.word').data(data).enter().append('text').text(function(d) {
return d.word;
}).attr('class', function(d) {
return "word w" + d.id;
}).attr('x', function(d) {
return treeWidth - wordWidth * d.id;
}).attr('y', treeHeight - wordHeight).on('mouseover', function(d) {
svg.selectAll('.word, .dependency, .edge, .arrow').classed('active', false);
svg.selectAll('.tag').attr('opacity', 0);
svg.selectAll(".w" + d.id).classed('active', true);
return svg.select(".tag.w" + d.id).attr('opacity', 1);
}).on('mouseout', function(d) {
svg.selectAll('.word, .dependency, .edge, .arrow').classed('active', false);
return svg.selectAll('.tag').attr('opacity', 0);
}).attr('text-anchor', 'middle');
tags = svg.selectAll('.tag').data(data).enter().append('text').text(function(d) {
return d.tag;
}).attr('class', function(d) {
return "tag w" + d.id;
}).attr('x', function(d) {
return treeWidth - wordWidth * d.id;
}).attr('y', treeHeight).attr('opacity', 0).attr('text-anchor', 'middle').attr('font-size', '90%');
edges = svg.selectAll('.edge').data(data).enter().append('path').filter(function(d) {
return d.id;
}).attr('class', function(d) {
return "edge w" + d.id + " w" + d.parent;
}).attr('d', function(d) {
return "M" + d.left + "," + d.bottom + " C" + (d.mid - d.diff) + "," + d.top + " " + (d.mid + d.diff) + "," + d.top + " " + d.right + "," + d.bottom;
}).attr('fill', 'none').attr('stroke', 'black').attr('stroke-width', '1.5');
dependencies = svg.selectAll('.dependency').data(data).enter().append('text').filter(function(d) {
return d.id;
}).text(function(d) {
return d.dependency;
}).attr('class', function(d) {
return "dependency w" + d.id + " w" + d.parent;
}).attr('x', function(d) {
return d.mid;
}).attr('y', function(d) {
return d.arrow - 7;
}).attr('text-anchor', 'middle').attr('font-size', '90%');
triangle = d3.svg.symbol().type('triangle-up').size(5);
return arrows = svg.selectAll('.arrow').data(data).enter().append('path').filter(function(d) {
return d.id;
}).attr('class', function(d) {
return "arrow w" + d.id + " w" + d.parent;
}).attr('d', triangle).attr('transform', function(d) {
return "translate(" + d.mid + ", " + d.arrow + ") rotate(" + (d.id < d.parent ? '' : '-') + "90)";
}).attr('fill', 'none').attr('stroke', 'black').attr('stroke-width', '1.5');
};
maximum = function(array) {
return Math.max(0, Math.max.apply(null, array));
};
under = function(edge1, edge2) {
var ma, mi, _ref;
_ref = edge1.id < edge1.parent ? [edge1.id, edge1.parent] : [edge1.parent, edge1.id], mi = _ref[0], ma = _ref[1];
return edge1.id !== edge2.id && edge2.id >= mi && edge2.parent >= mi && edge2.id <= ma && edge2.parent <= ma;
};
parseConll = function(conllData) {
var cpos, data, dependency, fpos, id, line, parent, tag, word, _, _i, _len, _ref, _ref1;
data = [];
data.push({
id: 0,
word: 'ریشه',
// tag: tagDict['ROOT'],
tag: 'ROOT',
level: 0
});
_ref = conllData.split('\n');
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
line = _ref[_i];
if (!(line)) {
continue;
}
_ref1 = line.split('\t'), id = _ref1[0], word = _ref1[1], _ = _ref1[2], cpos = _ref1[3], fpos = _ref1[4], _ = _ref1[5], parent = _ref1[6], dependency = _ref1[7];
// tag = cpos !== fpos ? tagDict[cpos] + ' ' + tagDict[fpos] : tagDict[cpos];
tag = cpos !== fpos ? cpos + ' ' + fpos : cpos;
data.push({
id: Number(id),
word: word,
tag: tag,
parent: Number(parent),
// dependency: dependencyDict[dependency],
dependency: dependency,
level: 1
});
}
return data;
};