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

ChuumonNoOoiRyouriten, 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



64
65
66
67
68
69
70
71
72
73
# File 'lib/ya_lorem_ja/word_resource.rb', line 64

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.



57
58
59
# File 'lib/ya_lorem_ja/word_resource.rb', line 57

def char_count_range_in_a_word
  @char_count_range_in_a_word
end

#line_breakObject

Returns the value of attribute line_break.



58
59
60
# File 'lib/ya_lorem_ja/word_resource.rb', line 58

def line_break
  @line_break
end

#sentence_count_range_in_a_paragraphObject (readonly)

Returns the value of attribute sentence_count_range_in_a_paragraph.



57
58
59
# File 'lib/ya_lorem_ja/word_resource.rb', line 57

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.



57
58
59
# File 'lib/ya_lorem_ja/word_resource.rb', line 57

def sentence_end_chars
  @sentence_end_chars
end

#sentence_mapObject (readonly)

Returns the value of attribute sentence_map.



57
58
59
# File 'lib/ya_lorem_ja/word_resource.rb', line 57

def sentence_map
  @sentence_map
end

#word_count_range_in_a_sentenceObject (readonly)

Returns the value of attribute word_count_range_in_a_sentence.



57
58
59
# File 'lib/ya_lorem_ja/word_resource.rb', line 57

def word_count_range_in_a_sentence
  @word_count_range_in_a_sentence
end

Class Method Details

.register(resource_name) ⇒ Object

register word resource



162
163
164
# File 'lib/ya_lorem_ja/word_resource.rb', line 162

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

Instance Method Details

#paragraph(opts) ⇒ String

return a random paragraph from word resource

Returns:

  • (String)

    sentence



130
131
132
# File 'lib/ya_lorem_ja/word_resource.rb', line 130

def paragraph(opts)
  paragraphs(1, opts)
end

#paragraphs(total, opts) ⇒ String

return random paragraphs from word resource

Parameters:

  • total (Fixnum)

    count of paragraph

Returns:

  • (String)

    paragraph



138
139
140
141
142
143
144
145
146
147
# File 'lib/ya_lorem_ja/word_resource.rb', line 138

def paragraphs(total, opts)
  list = []
  total.times do
    sentence_count = rand(sentence_count_range_in_a_paragraph)
    start_sep = opts[:start_sep].to_s
    end_sep = opts[:end_sep].to_s
    list << start_sep + self.sentences(sentence_count) + end_sep
  end
  return list.join()
end

#sentenceString

return a random sentence from word resource

Returns:

  • (String)

    sentence



104
105
106
# File 'lib/ya_lorem_ja/word_resource.rb', line 104

def sentence
  sentences(1)
end

#sentence_map_keysObject



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

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



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ya_lorem_ja/word_resource.rb', line 113

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



82
83
84
# File 'lib/ya_lorem_ja/word_resource.rb', line 82

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



91
92
93
94
95
96
97
98
99
# File 'lib/ya_lorem_ja/word_resource.rb', line 91

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