-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter.ts
More file actions
105 lines (93 loc) · 3.71 KB
/
Chapter.ts
File metadata and controls
105 lines (93 loc) · 3.71 KB
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
// ----------------------------------------------------------------
// PURPOSE:
// This library implements the Chapter class.
//
// Models:
// This directory contiains the model of the data of this application.
// ----------------------------------------------------------------
import { SerializedChapter } from '../serializers/Chapter'
import { Language } from '../enums/Language'
import { NumberedModel } from '../interfaces/Model'
import { Source } from './Source'
import { Story } from './Story'
import { Text } from './Text'
import { Topic } from './Topic'
import { Log } from '../utils/Logging'
import { Yaml } from '../utils/Yaml'
export class Chapter implements NumberedModel {
public readonly number: Text = new Text()
public readonly title: Text = new Text()
public readonly summary: Text = new Text()
public stories: Array<Story> = new Array<Story>()
// String serializers.
public toString(): string {
return `<${(this as any).constructor.name}: ${this.title.get()}>`
}
// Number getter and setter.
public getNumber(): string {
return this.number.get(Language.ALL)
}
public setNumber(...numbers: Array<string>): void {
this.number.set(Language.ALL, numbers.join('.'))
}
// Extracts Topics from Stories.
public getTopics(): Array<Topic> {
let set: Set<string> = new Set<string>()
return this.stories.reduce(
(accumulator, story) => accumulator.concat(story.topics),
[]
).filter(
topic => !set.has(topic.title.get()) && set.add(topic.title.get())
) || []
}
// Extracts Sources from Stories.
public getSources(): Array<Source> {
let set: Set<string> = new Set<string>()
return this.stories.reduce(
(accumulator, story) => accumulator.concat(story.sources),
[]
).filter(
source => source.title !== undefined && !set.has(source.title.get()) && set.add(source.title.get())
) || []
}
// ----------------------------------------------------------------
// PREPROCESSING STEP:
// Preprocessing simply refers to perform series of operations to
// transform or change data and cache the results before actually
// using them. That includes:
// - Data Cleaning.
// - Dimensionality Reduction.
// - Feature Engineering.
// - Sampling Data.
// - Data Transformation.
// - Imbalanced Data.
// ----------------------------------------------------------------
public async build(): Promise<void> {
Log.info('Preprocessing Chapter', this)
await this.summary.build()
await Promise.all(this.stories.map(async x => await x.build()))
}
// ----------------------------------------------------------------
// JSON SERIALIZERS:
// The main purpose is to save the state of an object in order to
// be able to recreate it when needed. The reverse process is called
// deserialization
// ----------------------------------------------------------------
public serialise(): SerializedChapter {
return {
"Type": (this as any).constructor.name,
"Title": this.title.serialise(),
"Summary": this.summary.serialise(),
"Topics": this.getTopics()?.map(topic => topic.serialise()) || [],
"Stories": this.stories?.map(story => story.serialise()) || [],
}
}
public deserialise(data: SerializedChapter): void {
if (data) {
Log.info('Unserialising Chapter', data)
this.title.deserialise(data.Title)
this.summary.deserialise(data.Summary)
this.stories = data.Stories?.map((x, index) => <Story>Yaml.deserialise(x))
}
}
}