Class: NHKore::Article

Inherits:
Object
  • Object
show all
Defined in:
lib/nhkore/article.rb

Overview

Author:

  • Jonathan Bradley Whited

Since:

  • 0.2.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeArticle

Returns a new instance of Article.

Since:

  • 0.2.0



31
32
33
34
35
36
37
38
39
40
# File 'lib/nhkore/article.rb', line 31

def initialize
  super()

  @datetime = nil
  @futsuurl = nil
  @sha256 = nil
  @title = nil
  @url = nil
  @words = {}
end

Instance Attribute Details

#datetimeObject

Since:

  • 0.2.0



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

def datetime
  @datetime
end

#futsuurlObject

Since:

  • 0.2.0



25
26
27
# File 'lib/nhkore/article.rb', line 25

def futsuurl
  @futsuurl
end

#sha256Object

Since:

  • 0.2.0



26
27
28
# File 'lib/nhkore/article.rb', line 26

def sha256
  @sha256
end

#titleObject

Since:

  • 0.2.0



27
28
29
# File 'lib/nhkore/article.rb', line 27

def title
  @title
end

#urlObject

Since:

  • 0.2.0



28
29
30
# File 'lib/nhkore/article.rb', line 28

def url
  @url
end

#wordsObject (readonly)

Since:

  • 0.2.0



29
30
31
# File 'lib/nhkore/article.rb', line 29

def words
  @words
end

Class Method Details

.load_data(key, hash) ⇒ Object

Since:

  • 0.2.0



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/nhkore/article.rb', line 76

def self.load_data(key,hash)
  words = hash[:words]

  article = Article.new

  article.datetime = hash[:datetime]
  article.futsuurl = hash[:futsuurl]
  article.sha256 = hash[:sha256]
  article.title = hash[:title]
  article.url = hash[:url]

  words&.each() do |k,h|
    k = k.to_s # Change from a symbol
    article.words[k] = Word.load_data(k,h)
  end

  return article
end

Instance Method Details

#add_word(word, use_freq: false) ⇒ Object

Why does this not look up the kanji/kana only and then update the other kana/kanji part appropriately?

  • There are some words like 行って. Without the kana, it’s difficult to determine what kana it should be. Should it be いって or おこなって?

  • Similarly, if we just have いって, should this be 行って or 言って?

  • Therefore, if we only have the kanji or only have the kana, we don’t try to populate the other value.

Since:

  • 0.2.0



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/nhkore/article.rb', line 49

def add_word(word,use_freq: false)
  curr_word = words[word.key]

  if curr_word.nil?
    words[word.key] = word
    curr_word = word
  else
    curr_word.freq += (use_freq ? word.freq : 1)

    curr_word.defn = word.defn if word.defn.to_s.length > curr_word.defn.to_s.length
    curr_word.eng = word.eng if word.eng.to_s.length > curr_word.eng.to_s.length
  end

  return curr_word
end

#encode_with(coder) ⇒ Object

Since:

  • 0.2.0



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

def encode_with(coder)
  # Order matters.

  coder[:datetime] = @datetime.nil? ? @datetime : @datetime.iso8601
  coder[:title] = @title
  coder[:url] = @url.nil? ? nil : @url.to_s
  coder[:futsuurl] = @futsuurl.nil? ? nil : @futsuurl.to_s
  coder[:sha256] = @sha256
  coder[:words] = @words
end

#to_s(mini: false) ⇒ Object

Since:

  • 0.2.0



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/nhkore/article.rb', line 113

def to_s(mini: false)
  s = ''.dup

  s << "'#{@url}':"
  s << "\n  datetime: '#{@datetime}'"
  s << "\n  title:    '#{@title}'"
  s << "\n  url:      '#{@url}'"
  s << "\n  futsuurl: '#{@futsuurl}'"
  s << "\n  sha256:   '#{@sha256}'"

  if !mini
    s << "\n  words:"
    @words.each do |key,word|
      s << "\n    #{word}"
    end
  end

  return s
end