@@ -16,15 +16,18 @@ async function submitForm(event) {
16
16
let url =
17
17
"https://umwi7bvika.execute-api.us-east-1.amazonaws.com/dev/ftlabs-sentiment/" +
18
18
uuid ;
19
-
19
+ let providersSelected ;
20
20
if ( inputs . length !== 0 ) {
21
+ providersSelected = inputs . length ;
21
22
let providers = [ ] ;
22
23
inputs . forEach ( input => {
23
24
if ( input . checked ) {
24
25
providers . push ( input . value ) ;
25
26
}
26
27
} ) ;
27
28
url = url + "?providers=" + providers . join ( "," ) ;
29
+ } else {
30
+ providersSelected = 3 ;
28
31
}
29
32
30
33
let result = await fetch ( url , {
@@ -50,14 +53,199 @@ async function submitForm(event) {
50
53
sentenceData [ provider ] = result [ provider ] . sentences ;
51
54
}
52
55
} ) ;
53
- sentenceFormatter ( sentenceData ) ;
54
- sentenceFormatterGroups ( sentenceData ) ;
56
+ const matchedSentenceData = sentenceFormatter ( sentenceData ) ;
57
+ sentenceFormatterGroups ( matchedSentenceData , providersSelected ) ;
55
58
56
59
const resultContainer = document . querySelector ( ".result" ) ;
57
60
resultContainer . classList . remove ( "hidden" ) ;
58
61
loading . classList . add ( "hidden" ) ;
59
62
}
60
63
64
+ function sentenceFormatterGroups ( matchedSentenceData , providersSelected ) {
65
+ const onlyPairableSentences = matchedSentenceData . filter (
66
+ sentenceData => sentenceData . length === providersSelected
67
+ ) ;
68
+ const standardisedSentenceData = onlyPairableSentences . map ( sentenceData =>
69
+ getStandardisedSentenceData ( sentenceData )
70
+ ) ;
71
+ const groupedStandardisedSentenceData = groupStandardisedSentenceData (
72
+ standardisedSentenceData
73
+ ) ;
74
+ // console.log(groupedStandardisedSentenceData);
75
+ displaySentenceData ( groupedStandardisedSentenceData ) ;
76
+ }
77
+
78
+ function displaySentenceData ( groupedStandardisedSentenceData ) {
79
+ groupedStandardisedSentenceData . forEach ( sentenceData => {
80
+ console . log ( sentenceData ) ;
81
+ const selector =
82
+ sentenceData . length === 3
83
+ ? ".grouped-three-container"
84
+ : ".grouped-two-container" ;
85
+ sentenceData . matches . forEach ( match => {
86
+ const sentencesContainer = document . querySelector ( selector ) ;
87
+ andSentenceElement ( match , sentencesContainer ) ;
88
+ } ) ;
89
+ } ) ;
90
+ }
91
+
92
+ function groupStandardisedSentenceData ( standardisedSentenceData ) {
93
+ let threeMatches = [ ] ;
94
+ let twoMatches = [ ] ;
95
+
96
+ standardisedSentenceData . forEach ( sentenceData => {
97
+ let positive = 0 ;
98
+ let negative = 0 ;
99
+ let neutral = 0 ;
100
+ sentenceData . forEach ( providerSentenceData => {
101
+ providerSentenceData . standardisedSentiment . forEach ( sentiment => {
102
+ if ( sentiment === "POSITIVE" ) {
103
+ positive = positive + 1 ;
104
+ } else if ( sentiment === "NEUTRAL" ) {
105
+ neutral = neutral + 1 ;
106
+ } else if ( sentiment === "NEGATIVE" ) {
107
+ negative = negative + 1 ;
108
+ }
109
+ } ) ;
110
+ } ) ;
111
+
112
+ if ( positive === 2 ) {
113
+ twoMatches = [
114
+ ...twoMatches ,
115
+ sentenceData . map ( providerData => ( {
116
+ ...providerData ,
117
+ matchedData : "POSTIVE"
118
+ } ) )
119
+ ] ;
120
+ }
121
+ if ( negative === 2 ) {
122
+ twoMatches = [
123
+ ...twoMatches ,
124
+ sentenceData . map ( providerData => ( {
125
+ ...providerData ,
126
+ matchedData : "NEGATIVE"
127
+ } ) )
128
+ ] ;
129
+ }
130
+ if ( neutral === 2 ) {
131
+ twoMatches = [
132
+ ...twoMatches ,
133
+ sentenceData . map ( providerData => ( {
134
+ ...providerData ,
135
+ matchedData : "NEUTRAL"
136
+ } ) )
137
+ ] ;
138
+ }
139
+ if ( positive === 3 ) {
140
+ threeMatches = [
141
+ ...threeMatches ,
142
+ sentenceData . map ( providerData => ( {
143
+ ...providerData ,
144
+ matchedData : "POSTIVE"
145
+ } ) )
146
+ ] ;
147
+ }
148
+ if ( negative === 3 ) {
149
+ threeMatches = [
150
+ ...threeMatches ,
151
+ sentenceData . map ( providerData => ( {
152
+ ...providerData ,
153
+ matchedData : "NEGATIVE"
154
+ } ) )
155
+ ] ;
156
+ }
157
+ if ( neutral === 3 ) {
158
+ threeMatches = [
159
+ ...threeMatches ,
160
+ sentenceData . map ( providerData => ( {
161
+ ...providerData ,
162
+ matchedData : "NEUTRAL"
163
+ } ) )
164
+ ] ;
165
+ }
166
+ } ) ;
167
+ return [
168
+ { length : 3 , matches : threeMatches } ,
169
+ { length : 2 , matches : twoMatches }
170
+ ] ;
171
+ }
172
+
173
+ function getStandardisedSentenceData ( sentenceData ) {
174
+ const standardisedData = sentenceData . map ( sentence => {
175
+ switch ( sentence . provider ) {
176
+ case "aws" :
177
+ return { ...sentence , standardisedSentiment : [ sentence . Sentiment ] } ;
178
+ case "ibm" :
179
+ const standardisedScores = ibmStandardised ( sentence . tones ) ;
180
+ return { ...sentence , standardisedSentiment : standardisedScores } ;
181
+ case "meaningCloud" :
182
+ const standardisedScore = meaningCloudStandardised ( sentence . score_tag ) ;
183
+ if ( standardisedScore ) {
184
+ return { ...sentence , standardisedSentiment : [ standardisedScore ] } ;
185
+ } else {
186
+ return { ...sentence , standardisedSentiment : [ ] } ;
187
+ }
188
+ default :
189
+ return { ...sentence , standardisedSentiment : [ ] } ;
190
+ }
191
+ } ) ;
192
+ return standardisedData ;
193
+ }
194
+
195
+ function ibmStandardised ( tones ) {
196
+ let sentimentArray = [ ] ;
197
+ tones . forEach ( toneData => {
198
+ switch ( toneData . tone_id ) {
199
+ case "anger" :
200
+ if ( ! sentimentArray . includes ( "NEGATIVE" ) ) {
201
+ sentimentArray = [ ...sentimentArray , "NEGATIVE" ] ;
202
+ }
203
+ case "fear" :
204
+ if ( ! sentimentArray . includes ( "NEGATIVE" ) ) {
205
+ sentimentArray = [ ...sentimentArray , "NEGATIVE" ] ;
206
+ }
207
+ case "sadness" :
208
+ if ( ! sentimentArray . includes ( "NEGATIVE" ) ) {
209
+ sentimentArray = [ ...sentimentArray , "NEGATIVE" ] ;
210
+ }
211
+ case "analytical" :
212
+ if ( ! sentimentArray . includes ( "NEUTRAL" ) ) {
213
+ sentimentArray = [ ...sentimentArray , "NEUTRAL" ] ;
214
+ }
215
+ case "tentative" :
216
+ if ( ! sentimentArray . includes ( "NEUTRAL" ) ) {
217
+ sentimentArray = [ ...sentimentArray , "NEUTRAL" ] ;
218
+ }
219
+ case "joy" :
220
+ if ( ! sentimentArray . includes ( "POSITIVE" ) ) {
221
+ sentimentArray = [ ...sentimentArray , "POSITIVE" ] ;
222
+ }
223
+ case "confident" :
224
+ if ( ! sentimentArray . includes ( "POSITIVE" ) ) {
225
+ sentimentArray = [ ...sentimentArray , "POSITIVE" ] ;
226
+ }
227
+ default :
228
+ break ;
229
+ }
230
+ } ) ;
231
+ return sentimentArray ;
232
+ }
233
+
234
+ function meaningCloudStandardised ( score_tag ) {
235
+ switch ( score_tag ) {
236
+ case "P+" :
237
+ return "POSITIVE" ;
238
+ case "P" :
239
+ return "POSITIVE" ;
240
+ case "NEU" :
241
+ return "NEUTRAL" ;
242
+ case "N+" :
243
+ return "NEGATIVE" ;
244
+ default :
245
+ break ;
246
+ }
247
+ }
248
+
61
249
function addHeadings ( { title, fullArticle, standfirst } ) {
62
250
const titleContent = document . querySelector ( ".title-content" ) ;
63
251
titleContent . innerHTML = title ;
@@ -97,6 +285,7 @@ function sentenceFormatter(sentenceData) {
97
285
const sentencesContainer = document . querySelector ( ".sentences-container" ) ;
98
286
andSentenceElement ( match , sentencesContainer ) ;
99
287
} ) ;
288
+ return matches ;
100
289
}
101
290
102
291
function andSentenceElement ( match , sentencesContainer ) {
0 commit comments