Skip to content

Commit b4c2341

Browse files
committed
modify parser.js to handle new json format
1 parent a45d108 commit b4c2341

File tree

3 files changed

+551
-173
lines changed

3 files changed

+551
-173
lines changed

index.html

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,13 @@
6262
<body>
6363

6464
<div id="header">
65-
<input type="file">
66-
<button id="POS">POS</button>
67-
<button class="active" id="REACH">REACH</button>
65+
<select>
66+
<option>-- select dataset --</option>
67+
<option>data1</option>
68+
<option>data2</option>
69+
<option>paragraph</option>
70+
<option>passage</option>
71+
</select>
6872
</div>
6973

7074
<div id="drawing">
@@ -166,6 +170,15 @@
166170
}
167171
}, false);
168172

173+
var parser = new Parser();
174+
document.querySelector('select').onchange = function() {
175+
if (this.selectedIndex > 0) {
176+
parser.readJson(`./data/data${this.selectedIndex}.json`, function() {
177+
console.log('hey',parser);
178+
});
179+
}
180+
}
181+
169182
function resize() {
170183
svgResize();
171184
Panel.resize();

js/parser.js

Lines changed: 87 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -1,183 +1,100 @@
1-
function readJson(path) {
2-
3-
var json = path || './data/angus-ex.json';
4-
5-
function loadJSON(path, callback) {
6-
var httpRequest = new XMLHttpRequest();
7-
httpRequest.onreadystatechange = function() {
8-
if (httpRequest.readyState === 4) {
9-
if (httpRequest.status === 200) {
10-
var data = JSON.parse(httpRequest.responseText);
11-
if (callback) callback(data);
12-
}
13-
}
14-
};
15-
httpRequest.open('GET', path);
16-
httpRequest.send();
1+
class Parser {
2+
constructor() {
3+
this.text = "";
4+
this.tokens = [];
5+
this.data = {};
176
}
187

19-
/* parse json file*/
20-
loadJSON(json, parseData);
21-
}
22-
23-
24-
/** init words and links from json data
25-
* - first hierarchy: documents--sentences--words (+associated syntax tags)
26-
* - edges connect parts of speech
27-
* - second hierarchy: events--paths | arguments--themes
28-
*/
29-
function parseData(data) {
30-
let wordDataArray = [];
31-
let wordDataMap = {};
32-
let syntaxDataArray = [];
33-
let mentionDataArray = [];
34-
35-
for (var i in data.documents) {
36-
let doc = data.documents[i];
37-
38-
doc.sentences.forEach(function(sentence, j) {
39-
// parse word data
40-
let sentenceData = sentence.words.map(function(word, k) {
41-
42-
let wordDataObject = {
43-
text: word,
44-
documentId: i,
45-
sentenceId: j,
46-
locationInSentence: k,
47-
charLocationInSentence: sentence.startOffsets[k],
48-
syntaxData: {
49-
tag: sentence.tags[k]
50-
},
51-
bioData: {
52-
tag: ''
53-
}
8+
/* load a json from a path */
9+
readJson(path, callback) {
10+
11+
var json = path || './data/data1.json';
12+
13+
function loadJSON(path, callback2) {
14+
var httpRequest = new XMLHttpRequest();
15+
httpRequest.onreadystatechange = function() {
16+
if (httpRequest.readyState === 4) {
17+
if (httpRequest.status === 200) {
18+
var data = JSON.parse(httpRequest.responseText);
19+
if (callback2) {
20+
callback2(data);
21+
}
22+
if (callback) {
23+
callback();
24+
}
25+
}
26+
}
5427
};
55-
56-
wordDataMap[[i,j,k].join('-')] = wordDataArray.length;
57-
wordDataArray.push(wordDataObject);
58-
return wordDataObject
59-
});
60-
61-
// create POS links
62-
sentence.graphs["stanford-collapsed"].edges.forEach(function(edge) {
63-
syntaxDataArray.push({
64-
destination: sentenceData[edge.destination],
65-
label: edge.relation,
66-
type: edge.relation,
67-
source: sentenceData[edge.source]
68-
})
69-
});
70-
71-
})
72-
}
73-
74-
// flatten data.mentions array
75-
let printMention = function(mention, i) {
76-
if (mention.arguments) {
77-
for (var j in mention.arguments) {
78-
mention.arguments[j] = mention.arguments[j].map(printMention)
79-
}
28+
httpRequest.open('GET', path);
29+
httpRequest.send();
8030
}
8131

82-
switch (mention.type) {
83-
case "CorefTextBoundMention":
84-
// has text(s) only
85-
let start = wordDataMap[[mention.document, mention.sentence, mention.tokenInterval.start].join('-')];
86-
let end = wordDataMap[[mention.document, mention.sentence, mention.tokenInterval.end].join('-')];
87-
88-
var link = {
89-
sourceId: null,
90-
destinationId: null,
91-
words: wordDataArray.slice(start, end),
92-
label: mention.displayLabel,
93-
id: mention.id,
94-
charOffset: mention.characterStartOffset,
95-
type: mention.type
96-
};
97-
mentionDataArray.push(link);
98-
return link;
99-
case "CorefRelationMention":
100-
// has argument(s)
101-
// hard-coded the property --- need better data to parse this correctly
102-
103-
let keys = Object.keys(mention.arguments);
104-
if (keys.length != 2 || !mention.arguments.controlled || !mention.arguments.controller) {
105-
console.log("bad data parse: check CorefRelationMention", mention.arguments);
106-
}
107-
var link = {
108-
sourceId: mention.arguments.controller.map(arg => arg.id),
109-
destinationId: [{
110-
name: mention.displayLabel,
111-
id: mention.arguments.controlled.map(arg => arg.id)
112-
}],
113-
label: mention.displayLabel,
114-
id: mention.id,
115-
charOffset: mention.characterStartOffset,
116-
type: mention.type
117-
};
118-
119-
mentionDataArray.push(link);
120-
return link;
121-
case "CorefEventMention":
122-
// has a trigger & argument(s)
123-
124-
if (mention.trigger.type == "TextBoundMention") {
125-
let start = wordDataMap[[mention.trigger.document, mention.trigger.sentence, mention.trigger.tokenInterval.start].join('-')];
126-
let end = wordDataMap[[mention.trigger.document, mention.trigger.sentence, mention.trigger.tokenInterval.end].join('-')];
32+
/* parse json file*/
33+
loadJSON(json, this.parseData.bind(this));
34+
}
12735

128-
var link = {
129-
sourceId: null,
130-
destinationId: null,
131-
words: wordDataArray.slice(start, end),
132-
label: mention.displayLabel,
133-
id: mention.trigger.id,
134-
charOffset: mention.trigger.characterStartOffset,
135-
type: mention.trigger.type
36+
/**
37+
* parse a json file
38+
*/
39+
parseData(data) {
40+
/* first get string tokens from the syntaxData */
41+
this.text = data.syntaxData.text;
42+
this.tokens = data.syntaxData.entities
43+
.map(x => x[2][0])
44+
.sort((a, b) => a[0] - b[0])
45+
.map(interval => this.text.substring(interval[0], interval[1]));
46+
47+
/* parse the event data: entities, triggers, events, and relations */
48+
const e = data.eventData;
49+
const entities = e.entities.map(arr => {
50+
return {
51+
id: arr[0],
52+
type: arr[1],
53+
interval: arr[2][0],
54+
string: e.text.substring(arr[2][0][0], arr[2][0][1])
13655
};
137-
mentionDataArray.push(link);
138-
}
139-
var link = {
140-
sourceId: [mention.trigger.id],
141-
destinationId: Object.keys(mention.arguments).map(key => {
142-
143-
return {
144-
name: key,
145-
charOffset: mention.characterStartOffset,
146-
id: mention.arguments[key].map(arg => arg.id)
147-
}
56+
});
14857

149-
}),
150-
label: mention.displayLabel,
151-
id: mention.id,
152-
type: mention.type
153-
};
154-
mentionDataArray.push(link);
155-
return link;
156-
default:
157-
console.log("invalid type", mention.type);
158-
break;
159-
}
160-
}
161-
data.mentions.forEach(printMention);
58+
const triggers = e.triggers.map(arr => {
59+
return {
60+
id: arr[0],
61+
type: arr[1],
62+
interval: arr[2][0],
63+
string: e.text.substring(arr[2][0][0], arr[2][0][1])
64+
};
65+
});
16266

163-
// done parsing into semi-flat datasets...
67+
const events = e.events.map(arr => {
68+
return {
69+
id: arr[0],
70+
trigger: arr[1],
71+
arguments: arr[2].map(argument => {
72+
return {
73+
id: argument[1],
74+
type: argument[0]
75+
};
76+
})
77+
};
78+
});
16479

165-
console.log('words',wordDataArray);
166-
console.log('pos',syntaxDataArray);
167-
console.log('events',mentionDataArray);
168-
}
80+
const relations = e.relations.map(arr => {
81+
return {
82+
id: arr[0],
83+
type: arr[1],
84+
arguments: arr[2].map(argument => {
85+
return {
86+
id: argument[1],
87+
type: argument[0]
88+
};
89+
})
90+
};
91+
});
16992

170-
document.querySelector('input[type=file]').onchange = function() {
171-
var fr = new FileReader();
172-
fr.onload = function(e) {
173-
var object = {};
174-
try {
175-
object = JSON.parse(e.target.result);
176-
}
177-
catch(e) {
178-
console.log("error",e);
179-
}
180-
parseData(object);
93+
this.data = {
94+
entities,
95+
triggers,
96+
events,
97+
relations
98+
};
18199
}
182-
fr.readAsText(this.files[0]);
183100
}

0 commit comments

Comments
 (0)