Class: NHKore::Sifter

Inherits:
Object
  • Object
show all
Includes:
Fileable
Defined in:
lib/nhkore/sifter.rb

Constant Summary collapse

DEFAULT_DIR =
Util::CORE_DIR
DEFAULT_FUTSUU_FILENAME =
'sift_nhk_news_web_regular'
DEFAULT_YASASHII_FILENAME =
'sift_nhk_news_web_easy'
DEFAULT_FUTSUU_FILE =
build_file(DEFAULT_FUTSUU_FILENAME)
DEFAULT_YASASHII_FILE =
build_file(DEFAULT_YASASHII_FILENAME)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Fileable

included, #save_file

Constructor Details

#initialize(news) ⇒ Sifter

Returns a new instance of Sifter.



37
38
39
40
41
42
43
# File 'lib/nhkore/sifter.rb', line 37

def initialize(news)
  @articles = news.articles.values.dup
  @caption = nil
  @filters = {}
  @ignores = {}
  @output = nil
end

Instance Attribute Details

#articlesObject

Returns the value of attribute articles.



31
32
33
# File 'lib/nhkore/sifter.rb', line 31

def articles
  @articles
end

#captionObject

Returns the value of attribute caption.



32
33
34
# File 'lib/nhkore/sifter.rb', line 32

def caption
  @caption
end

#filtersObject

Returns the value of attribute filters.



33
34
35
# File 'lib/nhkore/sifter.rb', line 33

def filters
  @filters
end

#ignoresObject

Returns the value of attribute ignores.



34
35
36
# File 'lib/nhkore/sifter.rb', line 34

def ignores
  @ignores
end

#outputObject

Returns the value of attribute output.



35
36
37
# File 'lib/nhkore/sifter.rb', line 35

def output
  @output
end

Class Method Details

.build_file(filename) ⇒ Object



24
25
26
# File 'lib/nhkore/sifter.rb', line 24

def self.build_file(filename)
  return File.join(DEFAULT_DIR,filename)
end

Instance Method Details

#build_headerObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/nhkore/sifter.rb', line 45

def build_header
  header = []

  header << 'Frequency' unless @ignores[:freq]
  header << 'Word' unless @ignores[:word]
  header << 'Kana' unless @ignores[:kana]
  header << 'English' unless @ignores[:eng]
  header << 'Definition' unless @ignores[:defn]

  return header
end

#build_rows(words) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/nhkore/sifter.rb', line 57

def build_rows(words)
  rows = words.map do |word|
    build_word_row(word)
  end

  return rows
end

#build_word_row(word) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/nhkore/sifter.rb', line 65

def build_word_row(word)
  row = []

  row << word.freq unless @ignores[:freq]
  row << word.word unless @ignores[:word]
  row << word.kana unless @ignores[:kana]
  row << word.eng unless @ignores[:eng]
  row << word.defn unless @ignores[:defn]

  return row
end

#compare_empty_str(str1, str2) ⇒ Object



360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/nhkore/sifter.rb', line 360

def compare_empty_str(str1,str2)
  has_str1 = !Util.empty_web_str?(str1)
  has_str2 = !Util.empty_web_str?(str2)

  if has_str1 && !has_str2
    return -1 # Bubble word1 to top
  elsif !has_str1 && has_str2
    return 1 # Bubble word2 to top
  end

  return 0 # Further comparison needed
end

#filter?(article) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'lib/nhkore/sifter.rb', line 77

def filter?(article)
  return false if @filters.empty?

  datetime_filter = @filters[:datetime]
  title_filter = @filters[:title]
  url_filter = @filters[:url]

  if !datetime_filter.nil?
    datetime = article.datetime

    return true if datetime.nil? ||
                   datetime < datetime_filter[:from] || datetime > datetime_filter[:to]
  end

  if !title_filter.nil?
    title = article.title.to_s
    title = Util.unspace_web_str(title) if title_filter[:unspace]
    title = title.downcase if title_filter[:uncase]

    return true unless title.include?(title_filter[:filter])
  end

  if !url_filter.nil?
    url = article.url.to_s
    url = Util.unspace_web_str(url) if url_filter[:unspace]
    url = url.downcase if url_filter[:uncase]

    return true unless url.include?(url_filter[:filter])
  end

  return false
end

#filter_by_datetime(datetime_filter = nil, from: nil, to: nil) ⇒ Object



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
# File 'lib/nhkore/sifter.rb', line 110

def filter_by_datetime(datetime_filter = nil,from: nil,to: nil)
  if !datetime_filter.nil?
    if datetime_filter.respond_to?(:[])
      # If out-of-bounds, just nil.
      from = datetime_filter[0] if from.nil?
      to = datetime_filter[1] if to.nil?
    else
      from = datetime_filter if from.nil?
      to = datetime_filter if to.nil?
    end
  end

  from = to if from.nil?
  to = from if to.nil?

  from = Util.jst_time(from) unless from.nil?
  to = Util.jst_time(to) unless to.nil?

  datetime_filter = [from,to]

  return self if datetime_filter.flatten.compact.empty?

  @filters[:datetime] = {from: from,to: to}

  return self
end

#filter_by_title(title_filter, uncase: true, unspace: true) ⇒ Object



137
138
139
140
141
142
143
144
# File 'lib/nhkore/sifter.rb', line 137

def filter_by_title(title_filter,uncase: true,unspace: true)
  title_filter = Util.unspace_web_str(title_filter) if unspace
  title_filter = title_filter.downcase if uncase

  @filters[:title] = {filter: title_filter,uncase: uncase,unspace: unspace}

  return self
end

#filter_by_url(url_filter, uncase: true, unspace: true) ⇒ Object



146
147
148
149
150
151
152
153
# File 'lib/nhkore/sifter.rb', line 146

def filter_by_url(url_filter,uncase: true,unspace: true)
  url_filter = Util.unspace_web_str(url_filter) if unspace
  url_filter = url_filter.downcase if uncase

  @filters[:url] = {filter: url_filter,uncase: uncase,unspace: unspace}

  return self
end

#ignore(key) ⇒ Object



155
156
157
158
159
# File 'lib/nhkore/sifter.rb', line 155

def ignore(key)
  @ignores[key] = true

  return self
end

#put_csv!Object

This does not output #caption.



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/nhkore/sifter.rb', line 162

def put_csv!
  require 'csv'

  words = sift

  @output = CSV.generate(headers: :first_row,write_headers: true) do |csv|
    csv << build_header

    words.each do |word|
      csv << build_word_row(word)
    end
  end

  return @output
end

#put_html!Object



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
# File 'lib/nhkore/sifter.rb', line 178

def put_html!
  words = sift

  @output = ''.dup

  @output << "    <!DOCTYPE html>\n    <html lang=\"ja\">\n    <head>\n    <meta charset=\"utf-8\">\n    <title>NHKore</title>\n    <link rel=\"stylesheet\" href=\"https://fonts.googleapis.com/css?family=Noto+Serif+JP&amp;display=fallback\">\n    <style>\n    body {\n      background-color: #FCFBF9;\n      color: #333333;\n      font-family: 'Noto Serif JP',Verdana,sans-serif;\n    }\n    h1 {\n      color: #737373;\n    }\n    table {\n      border-collapse: collapse;\n      table-layout: fixed;\n      width: 100%;\n    }\n    tr:nth-child(even) {\n      background-color: #A5C7ED;\n    }\n    tr:hover {\n      background-color: #FFDDCA;\n    }\n    td,th {\n      border: 1px solid #333333;\n      padding: 8px;\n      text-align: left;\n    }\n    th {\n      background-color: #082A8E;\n      color: #FCFBF9;\n    }\n    td {\n      vertical-align: top;\n    }\n    td:nth-child(1) {\n      padding-right: 1em;\n      text-align: right;\n    }\n    </style>\n    </head>\n    <body>\n    <h1>NHKore</h1>\n    <h2>\#{@caption}</h2>\n    <table>\n  HTML\n\n  # If have too few or too many '<col>', invalid HTML.\n  @output << %(<col style=\"width:6em;\">\\n) unless @ignores[:freq]\n  @output << %(<col style=\"width:17em;\">\\n) unless @ignores[:word]\n  @output << %(<col style=\"width:17em;\">\\n) unless @ignores[:kana]\n  @output << %(<col style=\"width:5em;\">\\n) unless @ignores[:eng]\n  @output << \"<col>\\n\" unless @ignores[:defn] # No width for defn, fills rest of page\n\n  @output << '<tr>'\n\n  build_header.each do |h|\n    @output << \"<th>\#{h}</th>\"\n  end\n\n  @output << \"</tr>\\n\"\n\n  words.each do |word|\n    @output << '<tr>'\n\n    build_word_row(word).each do |w|\n      @output << \"<td>\#{Util.escape_html(w.to_s)}</td>\"\n    end\n\n    @output << \"</tr>\\n\"\n  end\n\n  @output << <<~HTML\n    </table>\n    </body>\n    </html>\n  HTML\n\n  return @output\nend\n"

#put_json!Object



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
# File 'lib/nhkore/sifter.rb', line 268

def put_json!
  require 'json'

  words = sift

  @output = ''.dup

  @output << "    {\n    \"caption\": \#{JSON.generate(@caption)},\n    \"header\": \#{JSON.generate(build_header)},\n    \"words\": [\n  JSON\n\n  if !words.empty?\n    0.upto(words.length - 2) do |i|\n      @output << \"  \#{JSON.generate(build_word_row(words[i]))},\\n\"\n    end\n\n    @output << \"  \#{JSON.generate(build_word_row(words[-1]))}\\n\"\n  end\n\n  @output << \"]\\n}\\n\"\n\n  return @output\nend\n"

#put_yaml!Object



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
# File 'lib/nhkore/sifter.rb', line 295

def put_yaml!
  require 'psychgus'

  words = sift

  yaml = {
    caption: @caption,
    header: build_header,
    words: build_rows(words),
  }

  header_styler = Class.new do
    include Psychgus::Styler

    def style_sequence(sniffer,node)
      parent = sniffer.parent

      if !parent.nil? && parent.node.respond_to?(:value) && parent.value == 'header'
        node.style = Psychgus::SEQUENCE_FLOW
      end
    end
  end

  # Put each Word on one line (flow/inline style).
  @output = Util.dump_yaml(yaml,flow_level: 4,stylers: header_styler.new)

  return @output
end

#siftObject



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
# File 'lib/nhkore/sifter.rb', line 324

def sift
  result = Article.new

  @articles.each do |article|
    next if filter?(article)

    article.words.each_value do |word|
      # TODO: Try to remove garbage data better.
      next if word.word.length < 2
      next if word.freq <= 1
      next if word.word =~ /\p{Latin}|[[:digit:]]/

      result.add_word(word,use_freq: true)
    end
  end

  words = result.words.values

  words.sort! do |word1,word2|
    # Order by freq DESC (most frequent words to top).
    i = (word2.freq <=> word1.freq)

    # Order by !defn.empty, word ASC, !kana.empty, kana ASC, defn.len DESC, defn ASC.
    i = compare_empty_str(word1.defn,word2.defn) if i == 0 # Favor words that have definitions
    i = (word1.word.to_s <=> word2.word.to_s) if i == 0
    i = compare_empty_str(word1.kana,word2.kana) if i == 0 # Favor words that have kana
    i = (word1.kana.to_s <=> word2.kana.to_s) if i == 0
    i = (word2.defn.to_s.length <=> word1.defn.to_s.length) if i == 0 # Favor longer definitions
    i = (word1.defn.to_s <=> word2.defn.to_s) if i == 0

    i
  end

  return words
end

#to_sObject



373
374
375
# File 'lib/nhkore/sifter.rb', line 373

def to_s
  return @output.to_s
end