Skip to content

Commit d863da2

Browse files
committed
Odin: Ensure that Relation labels are ordered
- Relation labels for Odin data comprise the labels of the two arguments, separated by a hyphen. Because the relative order of the two arguments is not guaranteed at execution time (they are keys of a JS Object), we should make sure to sort them before generating the label.
1 parent b975472 commit d863da2

File tree

4 files changed

+77
-16
lines changed

4 files changed

+77
-16
lines changed

demo/demo.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/tag/js/tag.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44162,18 +44162,45 @@ function () {
4416244162

4416344163

4416444164
if (mention.type === "RelationMention") {
44165-
// There is no trigger for RelationMentions, but there is a reltype
44166-
var reltype = Object.keys(mention["arguments"]).join("-");
44167-
var _linkArgs = []; // `mentions.arguments` is an Object keyed by argument type.
44165+
// `mentions.arguments` is an Object keyed by argument type.
4416844166
// The value of each key is an array of nested Mentions as arguments
44167+
// Sort the keys properly so that we can generate an accurate label;
44168+
// the arguments with lower starting tokens should be on the left.
44169+
var argTypes = Object.keys(mention["arguments"]);
44170+
argTypes.sort(function (a, b) {
44171+
// Go through the array of mentions and pick out the lowest starting
44172+
// token
44173+
var mentionsA = mention["arguments"][a];
44174+
var mentionsB = mention["arguments"][b];
44175+
var firstTokenA = mentionsA.reduce(function (prev, next) {
44176+
if (next.tokenInterval.start < prev) {
44177+
return next.tokenInterval.start;
44178+
} else {
44179+
return prev;
44180+
}
44181+
}, mentionsA[0].tokenInterval.start);
44182+
var firstTokenB = mentionsB.reduce(function (prev, next) {
44183+
if (next.tokenInterval.start < prev) {
44184+
return next.tokenInterval.start;
44185+
} else {
44186+
return prev;
44187+
}
44188+
}, mentionsB[0].tokenInterval.start);
44189+
44190+
if (firstTokenA <= firstTokenB) {
44191+
return -1;
44192+
} else {
44193+
return 1;
44194+
}
44195+
}); // Generate the relation label
4416944196

44170-
var _arr2 = Object.entries(mention["arguments"]);
44197+
var reltype = argTypes.join("-"); // Generate the arguments array
4417144198

44172-
for (var _i3 = 0; _i3 < _arr2.length; _i3++) {
44173-
var _arr2$_i = (0, _slicedToArray2.default)(_arr2[_i3], 2),
44174-
type = _arr2$_i[0],
44175-
args = _arr2$_i[1];
44199+
var _linkArgs = [];
4417644200

44201+
for (var _i3 = 0; _i3 < argTypes.length; _i3++) {
44202+
var type = argTypes[_i3];
44203+
var args = mention["arguments"][type];
4417744204
var _iteratorNormalCompletion6 = true;
4417844205
var _didIteratorError6 = false;
4417944206
var _iteratorError6 = undefined;

dist/tag/js/tag.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/parse/odin.js

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,14 +247,48 @@ class OdinParser {
247247
// RelationMention
248248
// Will become a Link
249249
if (mention.type === "RelationMention") {
250-
// There is no trigger for RelationMentions, but there is a reltype
251-
const reltype = Object.keys(mention["arguments"]).join("-");
252-
253-
const linkArgs = [];
254-
255250
// `mentions.arguments` is an Object keyed by argument type.
256251
// The value of each key is an array of nested Mentions as arguments
257-
for (const [type, args] of Object.entries(mention["arguments"])) {
252+
253+
// Sort the keys properly so that we can generate an accurate label;
254+
// the arguments with lower starting tokens should be on the left.
255+
let argTypes = Object.keys(mention["arguments"]);
256+
argTypes.sort((a, b) => {
257+
// Go through the array of mentions and pick out the lowest starting
258+
// token
259+
const mentionsA = mention["arguments"][a];
260+
const mentionsB = mention["arguments"][b];
261+
262+
const firstTokenA = mentionsA
263+
.reduce((prev, next) => {
264+
if (next.tokenInterval.start < prev) {
265+
return next.tokenInterval.start;
266+
} else {
267+
return prev;
268+
}
269+
}, mentionsA[0].tokenInterval.start);
270+
const firstTokenB = mentionsB
271+
.reduce((prev, next) => {
272+
if (next.tokenInterval.start < prev) {
273+
return next.tokenInterval.start;
274+
} else {
275+
return prev;
276+
}
277+
}, mentionsB[0].tokenInterval.start);
278+
279+
if (firstTokenA <= firstTokenB) {
280+
return -1;
281+
} else {
282+
return 1;
283+
}
284+
});
285+
// Generate the relation label
286+
const reltype = argTypes.join("-");
287+
288+
// Generate the arguments array
289+
const linkArgs = [];
290+
for (const type of argTypes) {
291+
const args = mention["arguments"][type];
258292
for (const arg of args) {
259293
// Ensure that the argument mention has been parsed before
260294
const anchor = this._parseMention(arg);

0 commit comments

Comments
 (0)