julien-c HF staff commited on
Commit
b083f74
1 Parent(s): b3d6894

Deploy as a HF Space

Browse files
Files changed (4) hide show
  1. .gitignore +1 -1
  2. README.md +9 -0
  3. dist/script.js +304 -0
  4. dist/style.css +1 -0
.gitignore CHANGED
@@ -5,4 +5,4 @@ npm-debug.log*
5
  node_modules
6
  bower_components
7
  typings
8
- /dist
 
5
  node_modules
6
  bower_components
7
  typings
8
+ # /dist
README.md ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: coref
3
+ emoji: ⚡
4
+ colorFrom: yellow
5
+ colorTo: yellow
6
+ sdk: static
7
+ ---
8
+
9
+ ## Coref
dist/script.js ADDED
@@ -0,0 +1,304 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ var Coref = (function () {
2
+ function Coref(endpoint, opts) {
3
+ this.onStart = function () { };
4
+ this.onSuccess = function () { };
5
+ this.endpoint = endpoint;
6
+ if (opts.onStart) {
7
+ this.onStart = opts.onStart;
8
+ }
9
+ if (opts.onSuccess) {
10
+ this.onSuccess = opts.onSuccess;
11
+ }
12
+ window.addEventListener('resize', this.svgResize);
13
+ }
14
+ Coref.prototype.svgResize = function () {
15
+ if (!this.container || !this.svgContainer) {
16
+ return;
17
+ }
18
+ this.svgContainer.setAttribute('width', "" + this.container.scrollWidth);
19
+ this.svgContainer.setAttribute('height', "" + this.container.scrollHeight);
20
+ };
21
+ Coref.prototype.parse = function (text) {
22
+ var _this = this;
23
+ this.onStart();
24
+ var path = this.endpoint + "?text=" + encodeURIComponent(text);
25
+ var request = new XMLHttpRequest();
26
+ request.open('GET', path);
27
+ request.onload = function () {
28
+ if (request.status >= 200 && request.status < 400) {
29
+ _this.onSuccess();
30
+ var res = JSON.parse(request.responseText);
31
+ _this.render(res);
32
+ }
33
+ else {
34
+ console.error('Error', request);
35
+ }
36
+ };
37
+ request.send();
38
+ };
39
+ Coref.prototype.render = function (res) {
40
+ var _this = this;
41
+ var mentions = res.mentions;
42
+ for (var _i = 0, mentions_1 = mentions; _i < mentions_1.length; _i++) {
43
+ var m = mentions_1[_i];
44
+ m.singleScore = res.singleScores[m.index] || undefined;
45
+ }
46
+ var markup = Displacy.render(res.cleanedText, mentions);
47
+ if (!this.container || !this.svgContainer) {
48
+ return;
49
+ }
50
+ this.container.innerHTML = "<div class=\"text\">" + markup + "</div>";
51
+ this.svgContainer.textContent = "";
52
+ this.svgResize();
53
+ window.container = this.container;
54
+ window.svgContainer = this.svgContainer;
55
+ var endY = document.querySelector('.container .text').getBoundingClientRect().top
56
+ - this.container.getBoundingClientRect().top
57
+ - 2;
58
+ SvgArrow.yArrows = endY;
59
+ for (var _a = 0, _b = Object.entries(res.pairScores); _a < _b.length; _a++) {
60
+ var _c = _b[_a], __from = _c[0], scores = _c[1];
61
+ var from = parseInt(__from, 10);
62
+ for (var _d = 0, _e = Object.entries(scores); _d < _e.length; _d++) {
63
+ var _f = _e[_d], __to = _f[0], score = _f[1];
64
+ var to = parseInt(__to, 10);
65
+ var markFrom = document.querySelector("mark[data-index=\"" + from + "\"]");
66
+ var markTo = document.querySelector("mark[data-index=\"" + to + "\"]");
67
+ var arrow = new SvgArrow(this.container, markFrom, markTo, score);
68
+ if (score >= Math.max.apply(Math, Object.values(scores))) {
69
+ arrow.classNames.push('score-ok');
70
+ var singleScore = res.singleScores[from];
71
+ if (singleScore && score >= singleScore) {
72
+ arrow.classNames.push('score-best');
73
+ }
74
+ }
75
+ this.svgContainer.appendChild(arrow.generate());
76
+ }
77
+ }
78
+ document.querySelectorAll('.displacy-arrow.score-ok').forEach(function (arw) {
79
+ _this.svgContainer.appendChild(arw);
80
+ });
81
+ };
82
+ return Coref;
83
+ }());
84
+ var Displacy = (function () {
85
+ function Displacy() {
86
+ }
87
+ Displacy.sortSpans = function (spans) {
88
+ spans.sort(function (a, b) {
89
+ if (a.start === b.start) {
90
+ return b.end - a.end;
91
+ }
92
+ return a.start - b.start;
93
+ });
94
+ spans.forEach(function (s, i) {
95
+ if (i < spans.length - 1) {
96
+ var sNext = spans[i + 1];
97
+ if (s.start < sNext.start && s.end > sNext.start) {
98
+ console.log("ERROR", "Spans: strict overlapping");
99
+ }
100
+ }
101
+ });
102
+ };
103
+ Displacy.render = function (text, spans) {
104
+ this.sortSpans(spans);
105
+ var tags = {};
106
+ var __addTag = function (i, s, tag) {
107
+ if (Array.isArray(tags[i])) {
108
+ tags[i].push({ span: s, tag: tag });
109
+ }
110
+ else {
111
+ tags[i] = [{ span: s, tag: tag }];
112
+ }
113
+ };
114
+ for (var _i = 0, spans_1 = spans; _i < spans_1.length; _i++) {
115
+ var s = spans_1[_i];
116
+ __addTag(s.start, s, "start");
117
+ __addTag(s.end, s, "end");
118
+ }
119
+ var out = {
120
+ __content: "",
121
+ append: function (s) {
122
+ this.__content += s;
123
+ }
124
+ };
125
+ var offset = 0;
126
+ var indexes = Object.keys(tags).map(function (k) { return parseInt(k, 10); }).sort(function (a, b) { return a - b; });
127
+ for (var _a = 0, indexes_1 = indexes; _a < indexes_1.length; _a++) {
128
+ var i = indexes_1[_a];
129
+ var spanTags = tags[i];
130
+ if (i > offset) {
131
+ out.append(text.slice(offset, i));
132
+ }
133
+ offset = i;
134
+ for (var _b = 0, spanTags_1 = spanTags; _b < spanTags_1.length; _b++) {
135
+ var sT = spanTags_1[_b];
136
+ if (sT.tag === "start") {
137
+ out.append("<mark data-entity=\"" + sT.span.type.toLowerCase() + "\" data-index=\"" + sT.span.index + "\">");
138
+ var singleScore = sT.span.singleScore;
139
+ if (singleScore) {
140
+ out.append("<span class=\"single-score\">" + singleScore.toFixed(3) + "</span>");
141
+ }
142
+ }
143
+ else {
144
+ out.append("</mark>");
145
+ }
146
+ }
147
+ }
148
+ out.append(text.slice(offset, text.length));
149
+ return out.__content;
150
+ };
151
+ return Displacy;
152
+ }());
153
+ var SvgArrow = (function () {
154
+ function SvgArrow(container, markFrom, markTo, score) {
155
+ this.classNames = [];
156
+ this.container = container;
157
+ this.markFrom = markFrom;
158
+ this.markTo = markTo;
159
+ this.score = score;
160
+ }
161
+ SvgArrow.prototype._el = function (tag, options) {
162
+ var _a = options.classnames, classnames = _a === void 0 ? [] : _a, _b = options.attributes, attributes = _b === void 0 ? [] : _b, _c = options.style, style = _c === void 0 ? [] : _c, _d = options.children, children = _d === void 0 ? [] : _d, text = options.text, id = options.id, xlink = options.xlink;
163
+ var ns = 'http://www.w3.org/2000/svg';
164
+ var nsx = 'http://www.w3.org/1999/xlink';
165
+ var el = document.createElementNS(ns, tag);
166
+ classnames.forEach(function (name) { return el.classList.add(name); });
167
+ attributes.forEach(function (_a) {
168
+ var attr = _a[0], value = _a[1];
169
+ return el.setAttribute(attr, value);
170
+ });
171
+ style.forEach(function (_a) {
172
+ var prop = _a[0], value = _a[1];
173
+ return el.style[prop] = value;
174
+ });
175
+ if (xlink)
176
+ el.setAttributeNS(nsx, 'xlink:href', xlink);
177
+ if (text)
178
+ el.appendChild(document.createTextNode(text));
179
+ if (id)
180
+ el.id = id;
181
+ children.forEach(function (child) { return el.appendChild(child); });
182
+ return el;
183
+ };
184
+ SvgArrow.prototype.generate = function () {
185
+ var rand = Math.random().toString(36).substr(2, 8);
186
+ var startX = this.markTo.getBoundingClientRect().left
187
+ - this.container.getBoundingClientRect().left
188
+ + this.markTo.getBoundingClientRect().width / 2;
189
+ var endX = this.markFrom.getBoundingClientRect().left
190
+ - this.container.getBoundingClientRect().left
191
+ + this.markFrom.getBoundingClientRect().width / 2;
192
+ var curveY = Math.max(-50, SvgArrow.yArrows - (endX - startX) / 3.2);
193
+ return this._el('g', {
194
+ classnames: ['displacy-arrow'].concat(this.classNames),
195
+ children: [
196
+ this._el('path', {
197
+ id: "arrow-" + rand,
198
+ classnames: ['displacy-arc'],
199
+ attributes: [
200
+ ['d', "M" + startX + "," + SvgArrow.yArrows + " C" + startX + "," + curveY + " " + endX + "," + curveY + " " + endX + "," + SvgArrow.yArrows],
201
+ ['stroke-width', '2px'],
202
+ ['fill', 'none'],
203
+ ['stroke', 'currentColor'],
204
+ ]
205
+ }),
206
+ this._el('text', {
207
+ attributes: [
208
+ ['dy', '1em']
209
+ ],
210
+ children: [
211
+ this._el('textPath', {
212
+ xlink: "#arrow-" + rand,
213
+ classnames: ['displacy-label'],
214
+ attributes: [
215
+ ['startOffset', '50%'],
216
+ ['fill', 'currentColor'],
217
+ ['text-anchor', 'middle'],
218
+ ],
219
+ text: this.score.toFixed(2)
220
+ })
221
+ ]
222
+ }),
223
+ ]
224
+ });
225
+ };
226
+ SvgArrow.yArrows = 0;
227
+ return SvgArrow;
228
+ }());
229
+ var ENDPOINT = "https://coref.huggingface.co/coref";
230
+ var DEFAULT_NLP_TEXT = function () {
231
+ var items = [
232
+ "I love my father and my mother. They work hard. She is always nice but he is sometimes rude.",
233
+ "My sister is swimming with her classmates. They are not bad, but she is better. I love watching her swim.",
234
+ "My mother's name is Sasha, she likes dogs."
235
+ ];
236
+ return items[Math.floor(Math.random() * items.length)];
237
+ };
238
+ var loading = function () {
239
+ document.body.classList.toggle('loading');
240
+ };
241
+ var toggleDebug = function () {
242
+ document.body.classList.toggle('debug');
243
+ var icons = document.querySelectorAll('.svg-checkbox');
244
+ icons.forEach(function (icon) {
245
+ icon.classList.toggle('hide');
246
+ });
247
+ window.localStorage.setItem('debug', document.body.classList.contains('debug').toString());
248
+ };
249
+ var coref = new Coref(ENDPOINT, {
250
+ onStart: loading,
251
+ onSuccess: loading,
252
+ });
253
+ var getQueryVar = function (key) {
254
+ var query = window.location.search.substring(1);
255
+ var params = query.split('&').map(function (param) { return param.split('='); });
256
+ for (var _i = 0, params_1 = params; _i < params_1.length; _i++) {
257
+ var param = params_1[_i];
258
+ if (param[0] === key) {
259
+ return decodeURIComponent(param[1]);
260
+ }
261
+ }
262
+ return undefined;
263
+ };
264
+ var updateURL = function (text) {
265
+ history.pushState({ text: text }, "", "?text=" + encodeURIComponent(text));
266
+ };
267
+ document.addEventListener('DOMContentLoaded', function () {
268
+ var $input = document.querySelector('input.input-message');
269
+ var $form = document.querySelector('form.js-form');
270
+ var $checkbox = document.querySelector('.js-checkbox');
271
+ var $svgContainer = document.querySelector('.svg-container');
272
+ coref.container = document.querySelector('.container');
273
+ coref.svgContainer = $svgContainer;
274
+ {
275
+ var queryText = getQueryVar('text');
276
+ if (queryText) {
277
+ $input.value = queryText;
278
+ coref.parse(queryText);
279
+ }
280
+ else {
281
+ coref.parse(DEFAULT_NLP_TEXT());
282
+ }
283
+ }
284
+ $input.addEventListener('keydown', function (evt) {
285
+ if (evt.charCode === 13) {
286
+ evt.preventDefault();
287
+ $form.submit();
288
+ }
289
+ });
290
+ $form.addEventListener('submit', function (evt) {
291
+ evt.preventDefault();
292
+ var text = ($input.value.length > 0)
293
+ ? $input.value
294
+ : DEFAULT_NLP_TEXT();
295
+ updateURL(text);
296
+ coref.parse(text);
297
+ });
298
+ $checkbox.addEventListener('click', function () {
299
+ toggleDebug();
300
+ });
301
+ if (window.localStorage.getItem('debug') !== 'false') {
302
+ toggleDebug();
303
+ }
304
+ });
dist/style.css ADDED
@@ -0,0 +1 @@
 
 
1
+ /*! normalize.css v7.0.0 | MIT License | github.com/necolas/normalize.css */html{line-height:1.15;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%}body{margin:0}article,aside,footer,header,nav,section{display:block}h1{font-size:2em;margin:.67em 0}figcaption,figure,main{display:block}figure{margin:1em 40px}hr{box-sizing:content-box;height:0;overflow:visible}pre{font-family:monospace,monospace;font-size:1em}a{background-color:transparent;-webkit-text-decoration-skip:objects}abbr[title]{border-bottom:none;text-decoration:underline;text-decoration:underline dotted}b,strong{font-weight:inherit}b,strong{font-weight:bolder}code,kbd,samp{font-family:monospace,monospace;font-size:1em}dfn{font-style:italic}mark{background-color:#ff0;color:#000}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sub{bottom:-0.25em}sup{top:-0.5em}audio,video{display:inline-block}audio:not([controls]){display:none;height:0}img{border-style:none}svg:not(:root){overflow:hidden}button,input,optgroup,select,textarea{font-family:sans-serif;font-size:100%;line-height:1.15;margin:0}button,input{overflow:visible}button,select{text-transform:none}button,html [type="button"],[type="reset"],[type="submit"]{-webkit-appearance:button}button::-moz-focus-inner,[type="button"]::-moz-focus-inner,[type="reset"]::-moz-focus-inner,[type="submit"]::-moz-focus-inner{border-style:none;padding:0}button:-moz-focusring,[type="button"]:-moz-focusring,[type="reset"]:-moz-focusring,[type="submit"]:-moz-focusring{outline:1px dotted ButtonText}fieldset{padding:.35em .75em .625em}legend{box-sizing:border-box;color:inherit;display:table;max-width:100%;padding:0;white-space:normal}progress{display:inline-block;vertical-align:baseline}textarea{overflow:auto}[type="checkbox"],[type="radio"]{box-sizing:border-box;padding:0}[type="number"]::-webkit-inner-spin-button,[type="number"]::-webkit-outer-spin-button{height:auto}[type="search"]{-webkit-appearance:textfield;outline-offset:-2px}[type="search"]::-webkit-search-cancel-button,[type="search"]::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}details,menu{display:block}summary{display:list-item}canvas{display:inline-block}template{display:none}[hidden]{display:none}.bfc{overflow:hidden}.clearfix:before,.clearfix:after{content:" ";display:table}.clearfix:after{clear:both}body{font-family:"Helvetica Neue",Helvetica,Arial,sans-serif;-webkit-font-smoothing:antialiased}code,pre{background-color:rgba(0,0,0,0.04);border-radius:3px;padding:2px 6px;font-family:Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;color:#2f2f2f}.hide{display:none !important}.header{padding:18px 18px}.header:before,.header:after{content:" ";display:table}.header:after{clear:both}.header .input-wrapper{background:#c5c5c5;padding:.5rem 1rem;margin:0 .75rem .75rem 0;border-radius:.5rem;float:left;width:76%}.header .input-wrapper .wrapper-inner{width:100%;position:relative}.header .input-wrapper .wrapper-inner:before,.header .input-wrapper .wrapper-inner:after{content:" ";display:table}.header .input-wrapper .wrapper-inner:after{clear:both}.header .input-wrapper .wrapper-inner .input-message-wrapper{position:absolute;left:0;right:50px}.header .input-wrapper .wrapper-inner input.input-message{outline:none;border:none;padding:0;background-color:transparent;font-size:20px;line-height:28px;width:100%}.header .input-wrapper .wrapper-inner button.input-button{float:right;outline:none;border:none}.header div.menu-button{background:#c5c5c5;padding:.5rem 1rem;margin:0 .75rem .75rem 0;border-radius:.5rem;float:left;height:29px;line-height:28px;color:#444;cursor:pointer;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}.header div.menu-button.logo{background-color:#6779f7}.header div.menu-button.logo img.svg-logo{height:30px}.header div.menu-button img.svg-checkbox{position:relative;top:3px;margin-left:3px}.container-wrapper{overflow-x:auto;position:relative}.container{font-size:20px;white-space:nowrap;padding:300px 40px 180px 40px}.container mark{position:relative}.container mark span.single-score{position:absolute;bottom:-32px;left:0;right:0;text-align:center;font-family:Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;visibility:hidden}.container mark:hover span.single-score{visibility:visible}body.debug span.single-score{visibility:visible !important}.svg-container{position:absolute;top:0;left:0;right:0;bottom:0;z-index:-1}.displacy-arrow{color:#cad2cd;visibility:hidden}.displacy-arrow .displacy-label{font-family:Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px}.displacy-arrow.score-ok{color:#ff9378}.displacy-arrow.score-best{color:#ff211d;visibility:visible}body.debug .displacy-arrow{visibility:visible !important}.footer{padding:18px 18px;color:#2f2f2f;font-size:14px;max-width:700px;line-height:1.3}.footer a{color:inherit}.footer .footline{height:2px;width:200px;background-color:#696969}@media (min-height:700px) and (max-height:800px){.container{padding-bottom:70px}}@media (max-height:700px){.container{padding-bottom:40px}}.c-input__icon{margin-left:.75em}.c-input__button{font-size:inherit;background:transparent;color:inherit;cursor:pointer}.c-input__button.c-input__button--large{font-size:1.5em}.c-input__button__icon{width:1.25em;height:1.25em}.loading .c-input__button__icon{display:none}.c-input__button__spinner{display:none;-webkit-animation:spinner .5s linear infinite;animation:spinner .5s linear infinite}.loading .c-input__button__spinner{display:inline-block}@-webkit-keyframes spinner{to{-webkit-transform:rotate(360deg);transform:rotate(360deg)}}@keyframes spinner{to{-webkit-transform:rotate(360deg);transform:rotate(360deg)}}.entities{line-height:2}[data-entity]{padding:.25em .35em;margin:0 .25em;line-height:1;display:inline-block;border-radius:.25em;border:1px solid}[data-entity]:after{box-sizing:border-box;content:attr(data-entity);font-size:.6em;line-height:1;padding:.35em .35em;border-radius:.35em;text-transform:uppercase;display:inline-block;vertical-align:middle;margin:0 0 .1rem .5rem}[data-entity][data-entity="proper"]{background:rgba(166,226,45,0.2);border-color:#a6e22d}[data-entity][data-entity="proper"]:after{background:#a6e22d}[data-entity][data-entity="nominal"]{background:rgba(224,0,132,0.2);border-color:#e00084}[data-entity][data-entity="nominal"]:after{background:#e00084}[data-entity][data-entity="pronominal"]{background:rgba(67,198,252,0.2);border-color:#43c6fc}[data-entity][data-entity="pronominal"]:after{background:#43c6fc}[data-entity][data-entity="list"]{background:rgba(253,151,32,0.2);border-color:#fd9720}[data-entity][data-entity="list"]:after{background:#fd9720}