laowaihua 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/README +25 -3
  2. data/lib/lao_wai_hua.rb +45 -7
  3. metadata +2 -2
data/README CHANGED
@@ -1,13 +1,35 @@
1
1
  LaoWaiHua
2
- =========
2
+ ======
3
3
 
4
4
  A "Lorem Ipsum" generator for Chinese (simplified)
5
5
 
6
- Example
7
- =======
6
+ taken from the tongue-twister
7
+
8
+ 一位爷爷他姓顾,
9
+ 上街打醋又买布。
10
+ 买了布,打了醋,
11
+ 回头看见鹰抓兔。
12
+ 放下布,搁下醋,
13
+ 上前去追鹰和兔,
14
+ 飞了鹰,跑了兔。
15
+ 打翻醋,醋湿布
16
+
17
+ yi wei ye ye ta xing gu
18
+ shang jie da cu you mai bu
19
+ mai le bu, da le cu
20
+ hui tou kan jian ying zhuan tu
21
+ fang xia bu, ge xia cu
22
+ shang qian qu zhui ying he tu
23
+ fei le ying, pao le tu
24
+ da fan cu, cu shi bu.
8
25
 
26
+ Example
27
+ ======
9
28
  >> require 'laowaihua'
10
29
 
11
30
  >> LaoWaiHua(25)
12
31
  => "一位爷爷他姓顾,上街打醋又买布。买了布,打了醋,回"
32
+
33
+ >> LaoWaiHua.random(20)
34
+ => "跑抓跑,姓打,追顾布顾,位翻。上兔醋顾。"
13
35
 
data/lib/lao_wai_hua.rb CHANGED
@@ -11,14 +11,52 @@ module LaoWaiHua
11
11
  打 翻 醋 , 醋 湿 布
12
12
  )
13
13
 
14
- def self.generate(length=WORDS.length)
15
- words = []
16
- while words.length < length
17
- words += LaoWaiHua::WORDS[0, length]
18
- end
19
- words.join("")
20
- end
14
+ PUNCTUATION = %w( , 。 )
15
+
16
+ WORDS_WITHOUT_PUNCTUATION = WORDS.select{|word| !PUNCTUATION.include?(word)}
17
+
18
+ class << self
19
+
20
+ # return a string of *length* taken from the provided tongue twister
21
+ # defaults to the full length of the text
22
+ def generate(length=WORDS.length)
23
+ words = []
24
+ while words.length < length
25
+ words += WORDS[0, length]
26
+ end
27
+ words.join("")
28
+ end
21
29
 
30
+ # return a random word (excluding punctuation) from our text
31
+ def random_word()
32
+ WORDS_WITHOUT_PUNCTUATION[rand(WORDS_WITHOUT_PUNCTUATION.length)]
33
+ end
34
+
35
+ # return a string or random words of the provided *length*
36
+ def random(length)
37
+ words = []
38
+ next_punctuation = nil
39
+
40
+ 1.upto(length) do
41
+ next_punctuation ||= rand(3)+2
42
+ if next_punctuation == 0
43
+ next_punctuation = nil
44
+ words << random_punctuation
45
+ else
46
+ next_punctuation -= 1
47
+ words << random_word
48
+ end
49
+ end
50
+ words.join("")
51
+ end
52
+
53
+ protected
54
+
55
+ def random_punctuation()
56
+ PUNCTUATION[rand(PUNCTUATION.length)]
57
+ end
58
+
59
+ end
22
60
  end
23
61
 
24
62
  def LaoWaiHua(*args)
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Matthew Rudy