Class: Aozora

Inherits:
Object
  • Object
show all
Defined in:
lib/aozora/base.rb,
lib/aozora/version.rb

Overview

ダミーテキストを生成するためのクラスです。青空文庫などの著作権フリーな文書をもとに整形された文字列を生成します。

Authors

Tsukuru Tanimichi

Version

0.1.0

Copyright

MIT License

License

2013 Tsukuru Tanimichi

Constant Summary collapse

TITLES =

テキストの種類として指定できるSymbolのリスト

lambda {
  titles = Array.new
  titles.push :kokoro
  titles.push :bocchan
  titles.push :wagahaiwa_nekodearu
  titles.push :sanshiro
  titles.push :hashire_merosu
  titles.push :pandorano_hako
  titles.push :viyon
  titles.push :joseito
  titles.push :ningen_shikkaku
  titles.push :lemon
  titles.push :sangetsuki
  titles.push :watashino_kojinshugi
  titles.push :london
  titles.push :lorem_ipsum
  titles.push :happy_prince
  titles.push :christmas_carol
  titles.push :__TEST_DATA__ # for test only
  return titles
}.call
EN_TITLES =

英文のテキストのリスト

lambda {
  en_titles = Array.new
  en_titles.push :lorem_ipsum
  en_titles.push :happy_prince
  en_titles.push :christmas_carol
  return en_titles
}.call
VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title = :kokoro, length = 100) ⇒ Aozora

Aozoraクラスのインスタンスを生成する。

title

使用するテキストの種類を設定する

length

テキストの長さを設定する



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/aozora/base.rb', line 52

def initialize(title=:kokoro, length=100)
  validate_tilte(title)
  validate_length(length)

  if title.kind_of? Numeric
    title = TITLES[title]
  end
  @text = read_file(title, length)
  @toggles = {:dots => false, :paragraph => false}
  if EN_TITLES.include? title
    @lang = :en
  else
    @lang = :ja
  end
  return self
end

Class Method Details

.titlesObject

テキストの種類として指定できるSymbolおよび番号の対応表を標準出力に表示します。



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/aozora/base.rb', line 79

def self.titles
  titles = String.new
  titles += "+--+-------------------------+\n"
  titles += "| n| symbol                  |\n"
  titles += "+--+-------------------------+\n"
  # __TEST_DATA__.txtを含めない
  (0..(TITLES.size-2)).each do |i| 
    num_s = sprintf("%2d",i)
    sym_s = sprintf("%-25s",TITLES[i].inspect)
    titles += "|#{num_s}|#{sym_s}|\n"
  end
  titles += "+--+-------------------------+\n"
  print titles
end

Instance Method Details

#alpha(option) ⇒ Object

テキストに含まれるアルファベットを半角や全角に変換します。引数に:halfまたは“half”を渡すとテキスト内のすべてのアルファベットが半角に変換されます。:fullまたは“full”を渡すと全角に変換されます。

option

オプションを指定します。:half, “half”, :full, “full” のうちいずれかを指定して下さい。



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/aozora/base.rb', line 101

def alpha(option)
  case option
  when :half, "half"
    @text.tr!("a-z","a-z")
    @text.tr!("A-Z","A-Z")
    return self
  when :full, "full"
    @text.tr!("a-z","a-z")
    @text.tr!("A-Z","A-Z")
    return self
  end
  raise 'option must be :half, "half", :full, or "full"'
end

#digit(option) ⇒ Object

テキストに含まれる数字を半角や全角に変換します。引数に:halfまたは“half”を渡すとテキスト内のすべての数字が半角に変換されます。:fullまたは“full”を渡すと全角に変換されます。

option

オプションを指定します。:half, “half”, :full, “full” のうちいずれかを指定して下さい。



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/aozora/base.rb', line 122

def digit(option)
  case option
  when :half, "half"
    @text.tr!("0-9","0-9")
    return self
  when :full, "full"
    @text.tr!("0-9","0-9")
    return self
  end
  raise 'option must be :half, "half", :full, or "full"'
end

#dotsObject

ダミーテキストの末尾に、文章の途中であることを示す「…」を付します。



190
191
192
193
194
195
196
197
# File 'lib/aozora/base.rb', line 190

def dots
  # 既にdotsが実行済みだった場合は何もしない
  return self if @toggles[:dots]

  @text += "…"
  @toggles[:dots] = true
  return self
end

#paragraph(p_length = 40, options = {}) ⇒ Object

テキストをパラグラフ化します。

p_length

パラグラフごとのおおまかな文字数を指定します。

options

パラグラフに関するオプションをHashで受け取ります。=> true を渡された場合、パラグラフの冒頭にスペースが入ります。=> true を渡された場合、パラグラフの冒頭にスペースが入ります。



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/aozora/base.rb', line 163

def paragraph(p_length=40, options={})
  # 既にparagraphが実行済みだった場合は何もしない
  return self if @toggles[:paragraph]
  @toggles[:paragraph] = true

  div_paragraph(p_length)

  if options[:space_head]
    if @lang == :en
      @text = "    "+@text
      @text.gsub!(/\n/,"\n    ")
    else
      @text = " "+@text
      @text.gsub!(/\n/,"\n ")
    end
  end

  if options[:blank_line]
    @text.gsub!(/\n/,"\n\n")
  end

  return self
end

#sign(option) ⇒ Object

テキストに含まれる記号を半角や全角に変換します。引数に:halfまたは“half”を渡すとテキスト内のすべての記号が半角に変換されます。:fullまたは“full”を渡すと全角に変換されます。

option

オプションを指定します。:half, “half”, :full, “full” のうちいずれかを指定して下さい。



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/aozora/base.rb', line 141

def sign(option)
  full_sign = "!#$%&()*+,ー./:;<=>?@[]^_`{|}〜’\"
  half_sign = "!#\$%&()*+,-./:;<=>?@[]^_`{|}~'\\"
  case option
  when :half, "half"
    @text.tr!(full_sign, half_sign)
    return self
  when :full, "full"
    @text.tr!(half_sign, full_sign)
    return self
  end
  raise 'option must be :half, "half", :full, or "full"'
end

#textObject

Aozorato_sの別名です。生成されたダミーテキストを文字列として返します。



73
# File 'lib/aozora/base.rb', line 73

def text; to_s end

#to_sObject

生成されたダミーテキストを文字列として返します。



202
# File 'lib/aozora/base.rb', line 202

def to_s; @text.to_s end