-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.rb
executable file
·185 lines (148 loc) · 3.18 KB
/
generator.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
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
#!/usr/bin/ruby
# coding: utf-8
#
# == NAME
# generator.rb
#
# == USAGE
# ./generator.rb
#
# == DESCRIPTION
# Takes a list of kanji as input and outputs a file that can be used to
# generate a kanji of the day entry.
#
# == AUTHOR
# Douglas Perkins - https://dperkins.org
Script_dir = File.dirname(__FILE__)
require 'nokogiri'
require Script_dir + '/' + 'wordfreq'
require Script_dir + '/' + 'loader'
# Makes the details file for a given list of Kanji.
class Generator
# Makes the literal string.
def make_literal literal
s = literal
return s
end
# Makes the stroke count string.
def make_stroke_count stroke_count
s = stroke_count
return s
end
# Makes the grade string.
def make_grade grade
if grade >= 1 and grade <= 6
s = '小' + grade.to_s
elsif grade == 8
s = '中学'
else
s = ''
end
return s
end
# Makes the meanings string.
def make_meanings meanings
if meanings.size == 0
return ''
end
s = ''
meanings.each do |meaning|
s += meaning + ', '
end
s.chomp! ', '
return s
end
# Makes the onyomi readings string.
def make_onyomis readings
if readings.size == 0
return ''
end
s = ''
readings.each do |reading|
s += reading + ' '
end
s.chomp! ' '
return s
end
# Makes the kunyomi readings string.
def make_kunyomis readings
if readings.size == 0
return ''
end
s = ''
readings.each do |reading|
s += reading + ' '
end
s.chomp! ' '
return s
end
# Makes the examples string.
def make_examples examples
s = ''
examples.each do |example|
word = example.word
kana = example.kana
meaning = example.meaning
s += word + ' (' + kana + ') — ' + meaning
s += '<br />'
end
s.chomp! '<br />'
return s
end
# Makes the text for a card.
def make_card kanji
# Separates the front and back of the card.
splitter = "\t"
# The order of these lines is important.
# If they are changed, care must be taken upon import.
card = make_literal kanji.literal
card += splitter
card += make_stroke_count kanji.stroke_count
card += splitter
card += make_grade kanji.grade
card += splitter
card += make_meanings kanji.meanings
card += splitter
card += make_onyomis kanji.onyomis
card += splitter
card += make_kunyomis kanji.kunyomis
card += splitter
card += make_examples kanji.examples
card += "\n"
return card
end
# Makes the details string stores it as @details.
def make_details kanjilist
details = ""
kanjilist.each do |kanji|
details += make_card kanji
end
@details = details
end
def initialize kanjilist
puts 'Making the details ...'
make_details kanjilist
end
# Writes the contents of @details to a text file.
def write_details
file = 'details.txt'
path = Script_dir + '/' + file
puts 'Writing the details to ' + file + '...'
open(path, 'w') do |f|
f.puts @details
end
puts 'Done writing.'
end
end
def make_details
# Read all of the files and data.
$edict = Edict.new
$wordfreq = Wordfreq.new
$styler = Styler.new
$kanjidic = Kanjidic.new
$targetkanji = Targetkanji.new
# Make the details.
$generator = Generator.new($targetkanji.kanjilist)
$generator.write_details
end
make_details