Skip to content

Commit 9aff189

Browse files
committed
add drag and drop support for 2 files (.txt + .ann file)
1 parent 2d4ebb2 commit 9aff189

File tree

4 files changed

+127
-49
lines changed

4 files changed

+127
-49
lines changed

dist/tag/js/tag.min.js

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33387,8 +33387,8 @@ var Main = function () {
3338733387
Promise.all(promises).then(function (files) {
3338833388
try {
3338933389
var message = parser.parseFiles(files);
33390+
redrawVisualization();
3339033391
if (message) {
33391-
redrawVisualization();
3339233392
printMessage(message);
3339333393
}
3339433394
} catch (err) {
@@ -34546,7 +34546,7 @@ var BratParser = function () {
3454634546
output.texts[tbm_i].tokenId = tokens.length - 1;
3454734547
++tbm_i;
3454834548
}
34549-
} else if (text[ch] === ' ') {
34549+
} else if (/\s/.test(text[ch])) {
3455034550
if (token_start < ch) {
3455134551
tokens.push({
3455234552
word: text.slice(token_start, ch),
@@ -34715,8 +34715,8 @@ var BratParser = function () {
3471534715
}
3471634716
}
3471734717
}, {
34718-
key: 'parseAttributes',
34719-
value: function parseAttributes(tokens, mentions) {
34718+
key: 'parseAttribute',
34719+
value: function parseAttribute(tokens, mentions) {
3472034720
var id = +tokens[0].slice(1),
3472134721
attribute = tokens[1],
3472234722
target = tokens[2];
@@ -34758,6 +34758,8 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
3475834758

3475934759
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
3476034760

34761+
var re = /.*(?=\.(\S+))|.*/;
34762+
3476134763
var Parser = function () {
3476234764
function Parser() {
3476334765
_classCallCheck(this, Parser);
@@ -34781,10 +34783,11 @@ var Parser = function () {
3478134783

3478234784
// get format from extension
3478334785
if (!format) {
34784-
var extension = path.slice(path.lastIndexOf('.'));
34785-
if (extension === '.json') {
34786+
var extension = path.toLowerCase().match(re)[1];
34787+
34788+
if (extension === 'json') {
3478634789
format = 'json';
34787-
} else if (extension === '.ann') {
34790+
} else {
3478834791
format = 'brat';
3478934792
}
3479034793
}
@@ -34812,6 +34815,42 @@ var Parser = function () {
3481234815
this.parseText(file.content);
3481334816
}
3481434817
return file.name;
34818+
} else if (files.length > 1) {
34819+
// find two files that match in name
34820+
files.sort(function (a, b) {
34821+
return a.name.localeCompare(b.name);
34822+
});
34823+
34824+
// file to check against
34825+
var i = 0;
34826+
var iname = files[i].name.match(re);
34827+
34828+
var annRe = /a/;
34829+
var txtRe = /s/;
34830+
34831+
for (var j = 1; j < files.length; ++j) {
34832+
var jname = files[j].name.match(re);
34833+
if (jname[1] && jname[1] !== 'txt' && jname[1] !== 'ann') {
34834+
// not a valid extension
34835+
++i;
34836+
continue;
34837+
}
34838+
34839+
// check if name matches
34840+
if (jname[0] === iname[0]) {
34841+
if (iname[1] === 'ann' && jname[1] !== 'ann') {
34842+
this.parseText(files[j].content, files[i].content);
34843+
return;
34844+
} else if ((iname[1] === 'txt' || !iname[1]) && jname[1] === 'ann') {
34845+
this.parseText(files[i].content, files[j].content);
34846+
return;
34847+
}
34848+
}
34849+
34850+
// iterate to next file
34851+
++i;
34852+
iname = jname;
34853+
}
3481534854
}
3481634855
}
3481734856
}, {
@@ -34822,8 +34861,8 @@ var Parser = function () {
3482234861
}
3482334862
}, {
3482434863
key: 'parseText',
34825-
value: function parseText(text) {
34826-
this.ann.parse(text);
34864+
value: function parseText(text, ann) {
34865+
this.ann.parse(text, ann);
3482734866
this.parsedData = this.ann.data;
3482834867
}
3482934868
}]);

src/js/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,8 @@ const Main = (function() {
266266
Promise.all(promises).then(files => {
267267
try {
268268
let message = parser.parseFiles(files);
269+
redrawVisualization();
269270
if (message) {
270-
redrawVisualization();
271271
printMessage(message);
272272
}
273273
}

src/js/parse/ann.js

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class BratParser {
133133
++tbm_i;
134134
}
135135
}
136-
else if (text[ch] === ' ') {
136+
else if (/\s/.test(text[ch])) {
137137
if (token_start < ch) {
138138
tokens.push({
139139
word: text.slice(token_start, ch),
@@ -265,42 +265,42 @@ class BratParser {
265265
}
266266

267267
parseRelationMention(tokens, mentions) {
268-
const id = +tokens[0].slice(1),
269-
label = tokens[1],
270-
arg1 = tokens[2],
271-
arg2 = tokens[3];
272-
273-
if (id > 0 && arg2) {
274-
const split1 = arg1.split(this.re),
275-
split2 = arg2.split(this.re);
276-
277-
if (mentions[split1[1]] && mentions[split2[1]]) {
278-
return {
279-
id: 'R' + id,
280-
label,
281-
arguments: [{
282-
type: split1[0],
283-
id: split1[1]
284-
}, {
285-
type: split2[0],
286-
id: split2[1]
287-
}]
288-
};
268+
const id = +tokens[0].slice(1),
269+
label = tokens[1],
270+
arg1 = tokens[2],
271+
arg2 = tokens[3];
272+
273+
if (id > 0 && arg2) {
274+
const split1 = arg1.split(this.re),
275+
split2 = arg2.split(this.re);
276+
277+
if (mentions[split1[1]] && mentions[split2[1]]) {
278+
return {
279+
id: 'R' + id,
280+
label,
281+
arguments: [{
282+
type: split1[0],
283+
id: split1[1]
284+
}, {
285+
type: split2[0],
286+
id: split2[1]
287+
}]
288+
};
289+
}
289290
}
290291
}
291-
}
292292

293-
parseAttributes(tokens, mentions) {
294-
const id = +tokens[0].slice(1),
295-
attribute = tokens[1],
296-
target = tokens[2];
297-
298-
if (id > 0 && mentions[target]) {
299-
return {
300-
id,
301-
target,
302-
attribute,
303-
value: tokens.slice(3).join(' ')
293+
parseAttribute(tokens, mentions) {
294+
const id = +tokens[0].slice(1),
295+
attribute = tokens[1],
296+
target = tokens[2];
297+
298+
if (id > 0 && mentions[target]) {
299+
return {
300+
id,
301+
target,
302+
attribute,
303+
value: tokens.slice(3).join(' ')
304304
};
305305
}
306306
}

src/js/parse/parse.js

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import ReachParser from './reach.js';
22
import BratParser from './ann.js';
33
import load from '../xhr.js';
44

5+
const re = /.*(?=\.(\S+))|.*/;
6+
57
class Parser {
68
constructor() {
79
/* output */
@@ -19,11 +21,12 @@ class Parser {
1921
loadFile(path, format) {
2022
// get format from extension
2123
if (!format) {
22-
const extension = path.slice(path.lastIndexOf('.'));
23-
if (extension === '.json') {
24+
const extension = path.toLowerCase().match(re)[1];
25+
26+
if (extension === 'json') {
2427
format = 'json';
2528
}
26-
else if (extension === '.ann') {
29+
else {
2730
format = 'brat';
2831
}
2932
}
@@ -53,15 +56,51 @@ class Parser {
5356
}
5457
return file.name;
5558
}
59+
else if (files.length > 1) {
60+
// find two files that match in name
61+
files.sort((a, b) => a.name.localeCompare(b.name));
62+
63+
// file to check against
64+
let i = 0;
65+
let iname = files[i].name.match(re);
66+
67+
let annRe = /a/;
68+
let txtRe = /s/;
69+
70+
for (let j = 1; j < files.length; ++j) {
71+
let jname = files[j].name.match(re);
72+
if (jname[1] && jname[1] !== 'txt' && jname[1] !== 'ann') {
73+
// not a valid extension
74+
++i;
75+
continue;
76+
}
77+
78+
// check if name matches
79+
if (jname[0] === iname[0]) {
80+
if (iname[1] === 'ann' && jname[1] !== 'ann') {
81+
this.parseText(files[j].content, files[i].content);
82+
return;
83+
}
84+
else if ((iname[1] === 'txt' || !iname[1]) && jname[1] === 'ann') {
85+
this.parseText(files[i].content, files[j].content);
86+
return;
87+
}
88+
}
89+
90+
// iterate to next file
91+
++i;
92+
iname = jname;
93+
}
94+
}
5695
}
5796

5897
parseJson(data) {
5998
this.reach.parse(data);
6099
this.parsedData = this.reach.data;
61100
}
62101

63-
parseText(text) {
64-
this.ann.parse(text);
102+
parseText(text, ann) {
103+
this.ann.parse(text, ann);
65104
this.parsedData = this.ann.data;
66105
}
67106
}

0 commit comments

Comments
 (0)