Julien Chaumond commited on
Commit
6556bb5
·
1 Parent(s): 4bd86de

Checkpoint 2

Browse files
index.html CHANGED
@@ -32,15 +32,7 @@
32
  </a>-->
33
  </div>
34
 
35
- <div class="result">
36
- displaCy uses JavaScript, SVG and CSS to show you how computers understand language
37
-
38
- <br>
39
- <br>
40
- <br>
41
- <br>
42
-
43
- <div class="displacy-google">'<mark data-entity="work_of_art">Lawrence of Arabia</mark>' is a highly rated <mark data-entity="work_of_art">film biography</mark> about <mark data-entity="location">British</mark> <mark data-entity="person">Lieutenant</mark> <mark data-entity="person">T. E. Lawrence</mark>. <mark data-entity="person">Peter O'Toole</mark> plays <mark data-entity="person">Lawrence</mark> in the <mark data-entity="work_of_art">film</mark>.</div>
44
  </div>
45
 
46
  <script src="/dist/script.js"></script>
 
32
  </a>-->
33
  </div>
34
 
35
+ <div class="container">
 
 
 
 
 
 
 
 
36
  </div>
37
 
38
  <script src="/dist/script.js"></script>
js-src/Displacy.ts ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ /**
4
+ * Indicates position of spans of text inside the string.
5
+ * (for visual applications only, no semantic sense here.)
6
+ */
7
+ interface Span {
8
+ type: string;
9
+ start: number;
10
+ end: number;
11
+ }
12
+
13
+ class Displacy {
14
+
15
+ /**
16
+ * Render a text string and its entity spans
17
+ *
18
+ * *see displacy-ent.js*
19
+ */
20
+ static render(text: string, spans: Span[]): string {
21
+ let out = {
22
+ __content: "",
23
+ append(s: string) {
24
+ this.__content += s;
25
+ }
26
+ };
27
+ let offset = 0;
28
+
29
+ spans.forEach(({ type, start, end }) => {
30
+ const entity = text.slice(start, end);
31
+ const fragments = text.slice(offset, start).split('\n');
32
+
33
+ fragments.forEach((fragment, i) => {
34
+ out.append(fragment);
35
+ if (fragments.length > 1 && i !== fragments.length - 1) {
36
+ out.append('<br>');
37
+ }
38
+ });
39
+
40
+ // Breaking change from displacy-ent.js:
41
+ // We do not filter any entity type out.
42
+ out.append(`<mark data-entity=${ type.toLowerCase() }>${ entity }</mark>`);
43
+
44
+ offset = end;
45
+ });
46
+
47
+ out.append(text.slice(offset, text.length));
48
+ return out.__content;
49
+ }
50
+ }
js-src/coref.ts CHANGED
@@ -31,6 +31,7 @@ class Coref {
31
  endpoint: string;
32
  onStart = () => {};
33
  onSuccess = () => {};
 
34
 
35
  constructor(endpoint: string, opts: any) {
36
  this.endpoint = endpoint;
@@ -55,13 +56,16 @@ class Coref {
55
  this.render(res);
56
  }
57
  else {
58
- console.log('Error', request);
59
  }
60
  };
61
  request.send();
62
  }
63
 
64
  render(res: Response) {
65
- console.log(res);
 
 
 
66
  }
67
  }
 
31
  endpoint: string;
32
  onStart = () => {};
33
  onSuccess = () => {};
34
+ container?: HTMLElement;
35
 
36
  constructor(endpoint: string, opts: any) {
37
  this.endpoint = endpoint;
 
56
  this.render(res);
57
  }
58
  else {
59
+ console.error('Error', request);
60
  }
61
  };
62
  request.send();
63
  }
64
 
65
  render(res: Response) {
66
+ const markup = Displacy.render(res.cleanedText, (<any>res).entities);
67
+ if (!this.container) { return ; }
68
+ console.log(markup);
69
+ this.container.innerHTML = `<div class="text">${markup}</div>`;
70
  }
71
  }
js-src/zController.ts CHANGED
@@ -27,6 +27,7 @@ const updateURL = (text) => {
27
  document.addEventListener('DOMContentLoaded', () => {
28
  const $input = document.querySelector('input.input-message') as HTMLInputElement;
29
  const $form = document.querySelector('form.js-form') as HTMLFormElement;
 
30
 
31
  {
32
  // Initial text
 
27
  document.addEventListener('DOMContentLoaded', () => {
28
  const $input = document.querySelector('input.input-message') as HTMLInputElement;
29
  const $form = document.querySelector('form.js-form') as HTMLFormElement;
30
+ coref.container = document.querySelector('.container') as HTMLElement;
31
 
32
  {
33
  // Initial text
less/style.less CHANGED
@@ -55,7 +55,12 @@ code, pre {
55
  }
56
  }
57
 
58
-
 
 
 
 
 
59
 
60
 
61
  /**
 
55
  }
56
  }
57
 
58
+ .container {
59
+ font-size: 20px;
60
+ overflow-x: auto;
61
+ white-space: nowrap;
62
+ padding: 180px 40px;
63
+ }
64
 
65
 
66
  /**
less/zDisplacy.less ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ .entities {
3
+ line-height: 2
4
+ }
5
+ [data-entity] {
6
+ padding: 0.25em 0.35em;
7
+ margin: 0 0.25em;
8
+ line-height: 1;
9
+ display: inline-block;
10
+ border-radius: 0.25em;
11
+ border: 1px solid
12
+ }
13
+ [data-entity]:after {
14
+ box-sizing: border-box;
15
+ content: attr(data-entity);
16
+ font-size: 0.6em;
17
+ line-height: 1;
18
+ padding: 0.35em 0.35em;
19
+ border-radius: 0.35em;
20
+ text-transform: uppercase;
21
+ display: inline-block;
22
+ vertical-align: middle;
23
+ margin: 0 0 0.1rem 0.5rem
24
+ }
25
+ [data-entity][data-entity="person"] {
26
+ background: rgba(166, 226, 45, 0.2);
27
+ border-color: #a6e22d
28
+ }
29
+ [data-entity][data-entity="person"]:after {
30
+ background: #a6e22d
31
+ }
32
+ [data-entity][data-entity="norp"] {
33
+ background: rgba(224, 0, 132, 0.2);
34
+ border-color: #e00084
35
+ }
36
+ [data-entity][data-entity="norp"]:after {
37
+ background: #e00084
38
+ }
39
+ [data-entity][data-entity="facility"] {
40
+ background: rgba(67, 198, 252, 0.2);
41
+ border-color: #43c6fc
42
+ }
43
+ [data-entity][data-entity="facility"]:after {
44
+ background: #43c6fc
45
+ }
46
+ [data-entity][data-entity="org"] {
47
+ background: rgba(67, 198, 252, 0.2);
48
+ border-color: #43c6fc
49
+ }
50
+ [data-entity][data-entity="org"]:after {
51
+ background: #43c6fc
52
+ }
53
+ [data-entity][data-entity="gpe"] {
54
+ background: rgba(253, 151, 32, 0.2);
55
+ border-color: #fd9720
56
+ }
57
+ [data-entity][data-entity="gpe"]:after {
58
+ background: #fd9720
59
+ }
60
+ [data-entity][data-entity="loc"] {
61
+ background: rgba(253, 151, 32, 0.2);
62
+ border-color: #fd9720
63
+ }
64
+ [data-entity][data-entity="loc"]:after {
65
+ background: #fd9720
66
+ }
67
+ [data-entity][data-entity="product"] {
68
+ background: rgba(142, 125, 255, 0.2);
69
+ border-color: #8e7dff
70
+ }
71
+ [data-entity][data-entity="product"]:after {
72
+ background: #8e7dff
73
+ }
74
+ [data-entity][data-entity="event"] {
75
+ background: rgba(255, 204, 0, 0.2);
76
+ border-color: #fc0
77
+ }
78
+ [data-entity][data-entity="event"]:after {
79
+ background: #fc0
80
+ }
81
+ [data-entity][data-entity="work_of_art"] {
82
+ background: rgba(255, 204, 0, 0.2);
83
+ border-color: #fc0
84
+ }
85
+ [data-entity][data-entity="work_of_art"]:after {
86
+ background: #fc0
87
+ }
88
+ [data-entity][data-entity="language"] {
89
+ background: rgba(255, 204, 0, 0.2);
90
+ border-color: #fc0
91
+ }
92
+ [data-entity][data-entity="language"]:after {
93
+ background: #fc0
94
+ }
95
+ [data-entity][data-entity="date"] {
96
+ background: rgba(47, 187, 171, 0.2);
97
+ border-color: #2fbbab
98
+ }
99
+ [data-entity][data-entity="date"]:after {
100
+ background: #2fbbab
101
+ }
102
+ [data-entity][data-entity="time"] {
103
+ background: rgba(47, 187, 171, 0.2);
104
+ border-color: #2fbbab
105
+ }
106
+ [data-entity][data-entity="time"]:after {
107
+ background: #2fbbab
108
+ }
109
+ [data-entity][data-entity="percent"] {
110
+ background: rgba(153, 153, 153, 0.2);
111
+ border-color: #999
112
+ }
113
+ [data-entity][data-entity="percent"]:after {
114
+ background: #999
115
+ }
116
+ [data-entity][data-entity="money"] {
117
+ background: rgba(153, 153, 153, 0.2);
118
+ border-color: #999
119
+ }
120
+ [data-entity][data-entity="money"]:after {
121
+ background: #999
122
+ }
123
+ [data-entity][data-entity="quantity"] {
124
+ background: rgba(153, 153, 153, 0.2);
125
+ border-color: #999
126
+ }
127
+ [data-entity][data-entity="quantity"]:after {
128
+ background: #999
129
+ }
130
+ [data-entity][data-entity="ordinal"] {
131
+ background: rgba(153, 153, 153, 0.2);
132
+ border-color: #999
133
+ }
134
+ [data-entity][data-entity="ordinal"]:after {
135
+ background: #999
136
+ }
137
+ [data-entity][data-entity="cardinal"] {
138
+ background: rgba(153, 153, 153, 0.2);
139
+ border-color: #999
140
+ }
141
+ [data-entity][data-entity="cardinal"]:after {
142
+ background: #999
143
+ }