-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstories_transformer.rb
82 lines (64 loc) · 1.41 KB
/
stories_transformer.rb
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
# ruby stories_transformer path_to_web_file
require 'json'
class Sentence
def initialize(text, start, endTime)
@text = text
@start = start
@end = endTime
end
def as_json(options={})
{
text: @text,
start: @start,
"end": @end
}
end
def to_json(*options)
as_json(*options).to_json(*options)
end
end
class Sentences
def initialize(cs, uk)
@cs = cs
@uk = uk
end
def as_json(options={})
{
cs: @cs,
uk: @uk
}
end
def to_json(*options)
as_json(*options).to_json(*options)
end
end
class Timeline
def initialize(timeline)
@timeline = timeline
end
def as_json(options={})
{
timeline: @timeline
}
end
def to_json(*options)
as_json(*options).to_json(*options)
end
end
filePath = ARGV[0]
stringData = File.read filePath
data = JSON.parse(stringData)
output = Array.new
data.each do |item|
object = Sentences.new(
Sentence.new(item["main"], item["start_cs"], item["end_cs"]),
Sentence.new(item["uk"], item["start_uk"], item["end_uk"])
)
output.append(object)
end
timeline = Timeline.new(output)
puts timeline.to_json
File.open("#{File.basename(filePath, ".*")}-transformed.json", "w") do |f|
f.write(timeline.to_json)
end
puts "end"