File size: 4,964 Bytes
30e9731
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* Copyright 2021 Google LLC. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/


// console.clear()

window.init = function(){
  var initFns = [window.initUtil, window.initScatter, window.initPair]
  if (!initFns.every(d => d)) return

  window.util = initUtil()

  window.tidy = d3.csvParse(python_data.tidyCSV, d => {
    return {
      e0: +d.e0,
      e1: +d.e1,
      i0: +d.i0,
      i1: +d.i1,
      tokenIndex: +d.tokenIndex,
      sentenceIndex: +d.sentenceIndex,
    }
  })

  var bySentence = d3.nestBy(tidy, d => d.sentenceIndex)
  bySentence.forEach(sent => {
    sent.sentenceIndex = +sent.key
    sent.s0 = python_data.sentences[sent.sentenceIndex].s0
    sent.s1 = python_data.sentences[sent.sentenceIndex].s1
    sent.orig = python_data.sentences[sent.sentenceIndex].orig

    sent.corrA = ss.sampleCorrelation(sent.map(d => d.i0), sent.map(d => d.i1))
    // sent.corrA = ss.sampleCorrelation(sent.map(d => d.e0), sent.map(d => d.e1))
  })

  var sel = d3.select('.container').html(`
    <div class='left'>
      <div class='beeswarm'></div>
      <div class='pair'></div>
    </div>
    <div class='right'>
      <div class='list'></div>
    </div>
  `)
    .st({width: 1100})
  d3.selectAll('.left,.right').st({width: 500, display: 'inline-block', verticalAlign: 'top'})

  function initBeeswarm(bySentence, sel){
    var c = d3.conventions({
      sel: sel.append('div'),
      height: 80,
      totalWidth: 400,
      margin: {left: 0, top: 18}
    })

    c.x.domain(d3.extent(bySentence.map(d => +d.corrA))).nice()
    // c.x.domain([0, 1])
    c.xAxis.ticks(5)
    d3.drawAxis(c)
    util.ggPlotBg(c)
    c.svg.select('.y').remove()
    c.svg.selectAll('.tick').st({display: 'block'})

    var simulation = d3.forceSimulation(bySentence)
      .force("x", d3.forceX(d => c.x(d.corrA)).strength(1))
      .force("y", d3.forceY(c.height / 2))
      .force("collide", d3.forceCollide(4))
      .stop()

    for (var i = 0; i < 120; ++i) simulation.tick()

    c.svg.append('text').text('text')
      .text('Distribution of Spearman Correlation Coefficients')
      .at({dy: -5, fontWeight: 600})

    c.svg.appendMany('circle.sentence', bySentence)
      .translate(d => [d.x, d.y])
      .at({
        r: 3,
        fill: 'none',
        stroke: '#000'
      })
      .on('mouseover', setSentenceAsPair)
  }
  initBeeswarm(bySentence, d3.select('.beeswarm'))


  function initList(bySentence, sel){
    // var sentenceSel = sel.st({height: 500, overflowY: 'scroll', cursor: 'default'})
    //   .appendMany('div.sentence', _.sortBy(bySentence, d => d.corrA))
    //   .on('mouseover', setSentenceAsPair)
    //   .st({padding: 2, fontSize: 12})

    // sentenceSel.append('span')
    //   .text(d => (d3.format('+.2f')(d.corrA)).replace('0.', '.'))
    //   .st({marginRight: 10, color: '#aaa'})

    // sentenceSel.append('span')
    //   .text(d => d.orig.replace('[', '').replace(']', ''))

    var tableSel = sel
      .st({height: 470 + 17, overflowY: 'scroll', cursor: 'default', position: 'relative', left: -40})
      .append('table')
      .st({fontSize: 12})

    tableSel.append('tr.header')
      .html(`
        <th class='num'>corr</th>
        <th>template</th>
      `)

    var rowSel = tableSel
      .appendMany('tr.sentence', _.sortBy(bySentence, d => d.corrA))
      .on('mouseover', setSentenceAsPair)
      .st({padding: 2, fontSize: 12})
      .html(d => `
        <td class='num'>${(d3.format('+.2f')(d.corrA)).replace('0.', '.')}</td>
        <td>${d.orig.replace('[', '').replace(']', '')}</td>
      `)
  }
  initList(bySentence, d3.select('.list'))



  function setSentenceAsPair(s){
    s.e0 = d3.range(python_data.vocab.length).map(d => -Infinity)
    s.e1 = d3.range(python_data.vocab.length).map(d => -Infinity)
    s.forEach(d => {
      s.e0[d.tokenIndex] = d.e0
      s.e1[d.tokenIndex] = d.e1
    })

    s.label0 = s.s0
    s.label1 = s.s1
    s.vocab = python_data.vocab
    s.count = python_settings.count || 150
    s.isDifference = python_settings.isDifference

    var sel = d3.select('.pair').html('').st({width: 400})

    initPair(s, sel)

    d3.selectAll('.sentence').classed('active', d => d == s)

    d3.selectAll('div.sentence').filter(d => d == s)
      .each(function(){
        this.scrollIntoView({ block: 'nearest', inline: 'nearest'})
      })
  }

  setSentenceAsPair(bySentence[0])

}


window.init()