Class: YaLoremJa::WordResource

Inherits:
Object
  • Object
show all
Defined in:
lib/ya_lorem_ja/word_resource.rb

Overview

base class of word resource

Direct Known Subclasses

Kazehakase

Constant Summary collapse

DEFAULT_LINE_BREAK =

default line break character of sentence of paragraph

"\n"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(char_count_range, word_count_range, sentence_count_range) ⇒ WordResource

Returns a new instance of WordResource.

Parameters:

  • char_count_range (Range)

    range of character count in a word

  • word_count_range (Range)

    range of word count in sentence

  • sentence_count_range (Range)

    renage of sentence count in paragraph



58
59
60
61
62
63
64
65
66
67
# File 'lib/ya_lorem_ja/word_resource.rb', line 58

def initialize(char_count_range, word_count_range, sentence_count_range)
  # 単語の文字数範囲の設定
  @char_count_range_in_a_word = char_count_range
  # 文の単語数範囲の設定      
  @word_count_range_in_a_sentence = word_count_range
  # 段落の文数範囲の設定
  @sentence_count_range_in_a_paragraph = sentence_count_range
  # 改行文字
  @line_break = DEFAULT_LINE_BREAK
end

Instance Attribute Details

#char_count_range_in_a_wordObject (readonly)

Returns the value of attribute char_count_range_in_a_word.



51
52
53
# File 'lib/ya_lorem_ja/word_resource.rb', line 51

def char_count_range_in_a_word
  @char_count_range_in_a_word
end

#line_breakObject

Returns the value of attribute line_break.



52
53
54
# File 'lib/ya_lorem_ja/word_resource.rb', line 52

def line_break
  @line_break
end

#sentence_count_range_in_a_paragraphObject (readonly)

Returns the value of attribute sentence_count_range_in_a_paragraph.



51
52
53
# File 'lib/ya_lorem_ja/word_resource.rb', line 51

def sentence_count_range_in_a_paragraph
  @sentence_count_range_in_a_paragraph
end

#sentence_end_charsObject (readonly)

Returns the value of attribute sentence_end_chars.



51
52
53
# File 'lib/ya_lorem_ja/word_resource.rb', line 51

def sentence_end_chars
  @sentence_end_chars
end

#sentence_mapObject (readonly)

Returns the value of attribute sentence_map.



51
52
53
# File 'lib/ya_lorem_ja/word_resource.rb', line 51

def sentence_map
  @sentence_map
end

#word_count_range_in_a_sentenceObject (readonly)

Returns the value of attribute word_count_range_in_a_sentence.



51
52
53
# File 'lib/ya_lorem_ja/word_resource.rb', line 51

def word_count_range_in_a_sentence
  @word_count_range_in_a_sentence
end

Class Method Details

.register(resource_name) ⇒ Object

register word resource



154
155
156
# File 'lib/ya_lorem_ja/word_resource.rb', line 154

def register(resource_name)
  ::YaLoremJa::WordResources.register(resource_name, self)
end

Instance Method Details

#paragraphString

return a random paragraph from word resource

Returns:

  • (String)

    sentence



124
125
126
# File 'lib/ya_lorem_ja/word_resource.rb', line 124

def paragraph
  paragraphs(1)
end

#paragraphs(total) ⇒ String

return random paragraphs from word resource

Parameters:

  • total (Fixnum)

    count of paragraph

Returns:

  • (String)

    paragraph



132
133
134
135
136
137
138
139
# File 'lib/ya_lorem_ja/word_resource.rb', line 132

def paragraphs(total)
  list = []
  total.times do
    sentence_count = rand(sentence_count_range_in_a_paragraph)
    list << self.sentences(sentence_count)
  end
  return list.join(@line_break * 2)
end

#sentenceString

return a random sentence from word resource

Returns:

  • (String)

    sentence



98
99
100
# File 'lib/ya_lorem_ja/word_resource.rb', line 98

def sentence
  sentences(1)
end

#sentence_map_keysObject



69
70
71
# File 'lib/ya_lorem_ja/word_resource.rb', line 69

def sentence_map_keys
  @sentence_map_keys ||= sentence_map.keys.sort.reverse
end

#sentences(total) ⇒ String

return rondom sentences from word resource

Parameters:

  • total (Fixnum)

    count of sentence

Returns:

  • (String)

    words



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ya_lorem_ja/word_resource.rb', line 107

def sentences(total)
  list = []
  total.times do
    word_count = rand(word_count_range_in_a_sentence)
    sentence_len = word_count.times.inject(0){ |sum| sum + rand(char_count_range_in_a_word) }
    sentence_key = bsearch(sentence_map_keys, sentence_len)
    unless sentence_key
      sentence_key = sentence_map.keys.min
    end
    list << sentence_map[sentence_key].sample
  end
  return list.join(@line_break)
end

#wordString

return a random word from word resource

Returns:

  • (String)

    word



76
77
78
# File 'lib/ya_lorem_ja/word_resource.rb', line 76

def word
  words(1)
end

#words(num_of_word) ⇒ String

return rondom words from word resource

Parameters:

  • total (Fixnum)

    count of word

Returns:

  • (String)

    words



85
86
87
88
89
90
91
92
93
# File 'lib/ya_lorem_ja/word_resource.rb', line 85

def words(num_of_word)
  target_len = num_of_word.times.inject(0){ |sum| sum + rand(@char_count_range_in_a_word) }
  sentence_key = bsearch(sentence_map_keys, target_len)
  unless sentence_key
    sentence_key = sentence_map.keys.min
  end
  wk_word = sentence_map[sentence_key].sample
  return wk_word.sub(/[#{ sentence_end_chars.join() }]$/, "")      
end