forked from ratchagarn/Natural-Language-Generation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjitta_example.coffee
430 lines (406 loc) · 11.2 KB
/
jitta_example.coffee
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
{NaturalLanguage} = require "./src/main"
input = require './resources/input2.json'
NL = new NaturalLanguage input.data
# Jitta Line
NL.addType "jittaline", {
setAttrs: (data) ->
data.oldNumber = @getNumber(data.oldData) if(typeof data.oldData != 'undefined')
data.newNumber = @getNumber(data.newData)
data
getDifference: (data) ->
if(typeof data.oldData != 'undefined')
data.newNumber - data.oldNumber
else
'na'
getNumber: (data) ->
percentIndex = data.indexOf("%")
status = data.substring(percentIndex + 2, percentIndex + 7)
number = data.substring(0, percentIndex)
number = number * -1 if status is "Below"
number
getStatus: (data) ->
percentIndex = data.indexOf("%")
data.substring(percentIndex + 2, percentIndex + 7)
getDisplayInfo: (data) ->
precision = data.precision
result = {}
result.title = "the price"
result.newData = data.newData.toLowerCase()
result.newNumber = Math.abs(data.newNumber.toFixed(precision))
result.newLine = if data.newNumber < 0 then 'below' else 'above'
if(typeof data.oldData != 'undefined')
result.oldData = data.oldData.toLowerCase()
result.oldNumber = Math.abs(data.oldNumber.toFixed(precision))
result.oldLine = if data.oldNumber < 0 then 'below' else 'above'
difference = data.newNumber - data.oldNumber
result.difference = Math.abs(difference.toFixed(if difference.toFixed(0) == 0 then 2 else precision))
result
}
# Price
NL.addType "price", {
getDifference: (data) ->
if(typeof data.oldData != 'undefined')
((data.newData - data.oldData)/data.oldData)*100
else
'na'
getDisplayInfo: (data) ->
precision = data.precision
result = {}
result.title = "the " + data.title.toLowerCase()
if(typeof data.oldData != 'undefined')
percentDiff = Math.abs(data.difference)
result.oldData = data.oldData.toFixed(precision) + " " + data.currency
result.differencePrice = Math.abs(data.newData - data.oldData).toFixed(precision) + " " + data.currency
result.difference = percentDiff.toFixed(if percentDiff.toFixed(0) == 0 then 2 else precision) + "%"
result.newData = data.newData.toFixed(precision) + " " + data.currency
result
}
# Jitta Score
NL.addType "score", {
getDisplayInfo: (data) ->
precision = data.precision
result = {}
result.title = data.title.charAt(0).toUpperCase() + data.title.slice(1).toLowerCase()
if(typeof data.oldData != 'undefined')
result.oldData = data.oldData.toFixed(precision)
result.difference = Math.abs(data.difference).toFixed(precision)
result.newData = data.newData.toFixed(precision)
result
}
# Type: Loss Chance
NL.addType "loss", {
getDisplayInfo: (data) ->
precision = data.precision
result = {}
result.title = data.title.toLowerCase()
result.newData = data.newData.toFixed(precision) + "%"
if(typeof data.oldData != 'undefined')
result.oldData = data.oldData.toFixed(precision) + "%"
absoluteDifference = Math.abs(data.difference)
precision = 2 if absoluteDifference.toFixed(0) == "0"
result.difference = absoluteDifference.toFixed(precision) + "%"
result
}
# Type: Jitta Signs
NL.addType "sign", {
words: {
"Revenue and Earning": {
"Consistent": "+1",
"Consistent Growth": "+2",
"Consistently High Growth": "+3",
"-": "0",
"Revenue loss detected in the past years": "-2",
"Earning loss detected in the past years": "-2",
"Revenue Declined": "-1",
"Earning Declined": "-1"
},
"Operating Margin": {
"-": "0",
"Consistent": "+1",
"Expansion": "+2",
"Inconsistent": "-1",
"Declined": "-2"
},
"Debt Level": {
"-": "0",
"Low .*": "+1",
"No .*": "+2",
"High .* in the past 5 years": "-1",
"High .*": "-2",
"Very High .*": "-3"
},
"Recent Business Performance":{
"-": "0",
"Earning decline detected in the last 4 quarters": "-3",
"Earning declined in the last quarter": "-1",
"Earning declined in the last year": "-2"
},
"Return on Equity": {
"-": "0",
"Consistently High": "+2"
},
"Dividend Payout": {
"-": "0",
"Every year": "+1",
"Increasing Every Year": "+2"
},
"Share Repurchase": {
"-": "0",
"Every year": "+2"
},
"CapEx": {
"-": "0",
"Very Low": "+2",
"Very High": "-2"
},
"Recent IPO": {
"-": "0",
"Less than 3 years": "+1"
},
"New Share Issued": {
"-": "0",
"More than 50% in 5 years": "-2"
}
},
setAttrs: (data) ->
data.newScore = @getScore(data.title, data.newData)
if(typeof data.oldData != "undefined")
data.oldScore = @getScore(data.title, data.oldData)
if(data.newScore == '0')
data.hidden = true
data
getDisplayInfo: (data) ->
precision = data.precision
result = {}
result.title = data.title.toLowerCase()
result.title = "CapEx" if data.title == "CapEx"
result.newData = data.newData.toLowerCase()
if(typeof data.oldData != "undefined")
result.oldData = data.oldData.toLowerCase()
result
getScore: (title, data) ->
for item of @words[title]
pattern = new RegExp(item, "g");
if pattern.test(data)
return @words[title][item]
return null
getDifference: (data) ->
if(typeof data.oldData != "undefined")
parseInt(data.newScore) - parseInt(data.oldScore)
else
"na"
}
# Type: Error
NL.addType "error", {
getDisplayInfo: (data) ->
result = {}
result.title = data.title
result.newData = data.newData.toLowerCase()
result
}
# Sentence: Jitta Line
NL.addSentence "jittaline", {
simpleSentences: {
"above_above": {
"0": [
"{title} has changed from {oldNumber}% to {newData}"
],
"1": [
"{title} has slightly changed from {oldNumber}% to {newData}",
"{title} has slightly raised from {oldNumber}% to {newData}"
],
"2": [
"{title} has changed from {oldNumber}% to {newData}"
],
"3": [
"{title} has raised from {oldNumber}% to {newData}",
"{title} has improved from {oldNumber}% to {newData}"
],
"-1": [
"{title} has slightly decreased from {oldNumber}% to {newData}"
],
"-2": [
"{title} has fallen to {newData}",
"{title} has decreased from {oldNumber}% to {newData}"
],
"-3": [
"{title} has changed from {oldNumber}% to {newData}"
]
},
"above_below": {
"0": [
"{title} has fallen from {oldData} to {newData}"
],
"-3": [
"{title} has fallen from {oldData} to {newData}"
],
"-2": [
"{title} has fallen from {oldData} to {newData}"
],
"-1": [
"{title} has fallen from {oldData} to {newData}"
]
},
"below_above": {
"0": [
"{title} has improved from {oldData} to {newData}"
],
"1": [
"{title} has improved from {oldData} to {newData}"
],
"2": [
"{title} has improved from {oldData} to {newData}"
],
"3": [
"{title} has improved from {oldData} to {newData}"
]
},
"below_below": {
"0": [
"{title} has changed from {oldNumber}% to {newData}"
],
"1": [
"{title} has slightly changed from {oldNumber}% to {newData}",
"{title} has slightly upgraded from {oldNumber}% to {newData}"
],
"2": [
"{title} has changed from {oldNumber}% to {newData}",
"{title} has upgraded from {oldNumber}% to {newData}"
],
"3": [
"{title} has improved from {oldNumber}% to {newData}",
"{title} has jumped from {oldNumber}% to {newData}"
],
"-1": [
"{title} has slightly decreased from {oldNumber}% to {newData}"
],
"-2": [
"{title} has fallen to {newData}"
],
"-3": [
"{title} has decreased from {oldNumber}% to {newData}"
]
}
},
getSimpleSentenceList: (data, simpleSentences) ->
if typeof data.displayInfo.oldLine != 'undefined'
group = data.displayInfo.oldLine + "_" + data.displayInfo.newLine
@simpleSentences[group][data.level]
else
simpleSentences.default.na
}
# Sentence: Earning
NL.addSentence "earning", {
simpleSentences: {
"+1": [
"{title} is still {newData}"
],
"+2": [
"{newData} in {title}"
],
"+3": [
"{newData} in {title}"
],
"-2": [
"{newData}"
],
"-1": [
"{newData}"
],
"0": [
"No update in {title}"
]
},
getSimpleSentenceList: (data, simpleSentences) ->
@simpleSentences[data.newScore]
}
# Sentence: Operating
NL.addSentence "operating", {
simpleSentences: {
"na": [
"{title} is {newData}"
],
"positive": [
"{title} is {newData}"
],
"neutral": [
"{title} is still {newData}"
],
"negative": [
"{title} is {newData}"
]
},
getSimpleSentenceList: (data, simpleSentences) ->
@simpleSentences[data.levelType]
}
# Sentence: Debt
NL.addSentence "debt", {
simpleSentences: {
"all": [
"there is {newData}"
]
},
getSimpleSentenceList: (data, simpleSentences) ->
@simpleSentences.all
}
# Sentence: Return on Equity
NL.addSentence "roe", {
simpleSentences: {
"+2": {
"+2": [
"{title} is still {newData}"
]
},
"0": {
"+2": [
"{title} is {newData}"
]
}
},
getSimpleSentenceList: (data, simpleSentences) ->
oldScore = if typeof data.oldScore == 'undefined' then 0 else data.oldScore
if(@simpleSentences[oldScore] && @simpleSentences[oldScore][data.newScore])
@simpleSentences[oldScore][data.newScore]
else
["Error #{data.title}"]
}
# Sentence: Dividend Payout
NL.addSentence "dividend", {
simpleSentences: {
"positive": [
"{title} is {newData}"
],
"neutral": [
"{title} is still {newData}"
],
"negative": [
"{title} is {newData}"
],
"na": [
"{title} is {newData}"
]
},
getSimpleSentenceList: (data, simpleSentences) ->
if(@simpleSentences[data.levelType])
@simpleSentences[data.levelType]
else
["Error #{data.title}"]
}
# Sentence: CapEx
NL.addSentence "capex", {
simpleSentences: {
"na": [
"{title} is {newData}"
],
"positive": [
"{title} is {newData}"
],
"neutral": [
"{title} is still {newData}"
],
"negative": [
"{title} is {newData}"
]
},
getSimpleSentenceList: (data, simpleSentences) ->
@simpleSentences[data.levelType]
}
# Sentence: Repurchase
NL.addSentence "repurchase", {
simpleSentences: {
"+2": {
"+2": [
"there is still {title} {newData}"
]
},
"0": {
"+2": [
"there is {title} {newData}"
]
}
}
getSimpleSentenceList: (data, simpleSentences) ->
oldScore = if typeof data.oldScore == "undefined" then 0 else data.oldScore
@simpleSentences[oldScore][data.newScore]
}
console.log NL.generate()