-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy patha.js
144 lines (124 loc) Β· 3.78 KB
/
a.js
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
document.addEventListener('DOMContentLoaded', function() {
var input = document.getElementById('input');
var output = document.getElementById('output');
// Remove styling from text
input.addEventListener('paste', function (e) {
e.preventDefault();
var text = e.clipboardData.getData("text/plain");
document.execCommand("insertHTML", false, text);
});
input.addEventListener('keyup', function () {
getInfo(input.innerText);
return false;
});
getInfo(input.innerText);
});
renderInfo = function (info) {
var output = document.getElementById('output');
var entities = document.getElementById('entities');
var wordFreq = document.getElementById('word-freq');
var sentimentTable = document.getElementById('sentiment-table');
// Marked up
output.innerHTML = info.taggedText;
// Entitiy list
entities.innerHTML = '';
info.entities.forEach( function (e) {
entities.innerHTML += '<li class="' + e.type + '">' + e.value + '</li>';
} );
// Document info
document.getElementById('sentences-stat').innerHTML = info.documentInfo.sentences;
document.getElementById('words-stat').innerHTML = info.documentInfo.words;
document.getElementById('tokens-stat').innerHTML = info.documentInfo.tokens;
// Word frequency
wordFreq.innerHTML = ''
info.wordFreq.forEach(function (f) {
wordFreq.innerHTML += '<tr>' +
'<td>' + f[0] + '</td>' +
'<td>' + f[1] + '</td>' +
'</tr>';
})
// Sentiment
sentimentTable.innerHTML = '';
info.sentiments.forEach(function (s) {
sentimentTable.innerHTML += '<tr>' +
'<td class="sentence-emoji">' + getSentimentEmoji(s.sentiment) + '</td>' +
'<td class="sentence-text">' + s.sentence + '</td>' +
'<td>' + s.sentiment + '</td>' +
'</tr>';
})
}
getSentimentEmoji = function (s) {
if( s > 1 ) return 'π';
if( s > 0 ) return 'π';
if( s < -1 ) return 'π’';
if( s < 0 ) return 'βΉοΈ';
if( s === 0 ) return 'πΆ';
}
getInfo = function (v) {
fetch(
'https://showcase-serverless.herokuapp.com/pos-tagger',
{
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: 'POST',
body: JSON.stringify({sentence: v})
})
.then(function (res) {
return res.json();
}).then(function (info) {
renderInfo(info);
})
return;
// TODO: This is a mock function and should be replaced by an API call
var info = {
taggedText: tag(v),
entities: entities(v),
documentInfo: documentInfo(v),
wordFreq: wordFreq(v),
sentiments: sentiment(v)
}
window.setTimeout( function () {
renderInfo(info);
}, 2000);
}
// TODO: These are all mock functions and should be removed
tag = function (v) {
v = v.replace('wink', '<span class="tag org">wink</span>');
v = v.replace('sanjaya', '<span class="tag person">sanjaya</span>');
v = v.replace('noida', '<span class="tag location">noida</span>');
v = v.replace('work', '<span class="tag verb">work</span>');
return v;
}
entities = function (v) {
var entities = [];
if (v.indexOf('wink') > -1 ) entities.push({entity: 'wink', type: 'org'});
if (v.indexOf('noida') > -1 ) entities.push({entity: 'noida', type: 'location'});
if (v.indexOf('sanjaya') > -1 ) entities.push({entity: 'sanjaya', type: 'person'});
return entities;
}
documentInfo = function (v) {
return {
sentences: v.split('.').length,
words: v.split(' ').length,
tokens: (v.split('.').length + v.split(' ').length)
};
}
wordFreq = function (v) {
return [
['Life', 42],
['Universe', 21],
['Everything', 7]
];
}
sentiment = function (v) {
var sentiments = [];
v.split('.').forEach( function (s) {
sentiments.push({
sentence: s,
sentiment: Math.floor(Math.random() * 5) - 2
})
} );
return sentiments;
}