Class: Mixer::Mixuj
- Inherits:
-
Object
- Object
- Mixer::Mixuj
- Defined in:
- lib/mixer/mixuj.rb
Instance Attribute Summary collapse
-
#dl ⇒ Object
private.
-
#dl_max ⇒ Object
private.
-
#dl_min ⇒ Object
private.
-
#error ⇒ Object
private.
-
#input ⇒ Object
private.
-
#output ⇒ Object
private.
Instance Method Summary collapse
-
#initialize(string) ⇒ Mixuj
constructor
A new instance of Mixuj.
- #max_min(tab) ⇒ Object
- #mixuj(s1) ⇒ Object
- #valid(string) ⇒ Object
Constructor Details
#initialize(string) ⇒ Mixuj
Returns a new instance of Mixuj.
7 8 9 10 11 12 |
# File 'lib/mixer/mixuj.rb', line 7 def initialize(string) @input = string @dl=string.size @dl_max=string.size @dl_min=string.size end |
Instance Attribute Details
#dl ⇒ Object
private
5 6 7 |
# File 'lib/mixer/mixuj.rb', line 5 def dl @dl end |
#dl_max ⇒ Object
private
5 6 7 |
# File 'lib/mixer/mixuj.rb', line 5 def dl_max @dl_max end |
#dl_min ⇒ Object
private
5 6 7 |
# File 'lib/mixer/mixuj.rb', line 5 def dl_min @dl_min end |
#error ⇒ Object
private
5 6 7 |
# File 'lib/mixer/mixuj.rb', line 5 def error @error end |
#input ⇒ Object
private
5 6 7 |
# File 'lib/mixer/mixuj.rb', line 5 def input @input end |
#output ⇒ Object
private
5 6 7 |
# File 'lib/mixer/mixuj.rb', line 5 def output @output end |
Instance Method Details
#max_min(tab) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/mixer/mixuj.rb', line 60 def max_min(tab) min=-1 max=-1 tab.each do |i| if max==-1 or (i.length)>max max=i.length end if min==-1 or (i.length)<min min=i.length end end return max,min end |
#mixuj(s1) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/mixer/mixuj.rb', line 76 def mixuj(s1) valid(s1) if not valid(s1) return error else # SPRAWDZANIE paragrafow if s1.include? "\n\n" tab_wyc=s1.split("\n\n") tab_wyc.shuffle! tab_wyc.each do |j| j=j+"\n\n" end tab_wyc[tab_wyc.size-1].chomp! s1="" tab_wyc.each do |j| s1=s1+j end end # SPRAWDZANIE [ ] until s1.slice(/\[.+\]/).nil? wyc=s1.slice(/\[.+\]/) @dl=dl-wyc.size @dl_max=dl_max-wyc.size @dl_min=dl_min-wyc.size poz=s1.index(wyc) s1.slice!(/\[.+\]/) wyc=wyc[1..-2] begin tab_wyc=wyc.split("|") tab_wyc.shuffle! wyc="" tab_wyc.each do |j| wyc=wyc+j end @dl=dl+wyc.size @dl_max=dl_max+wyc.size @dl_min=dl_min+wyc.size s1.insert(poz, wyc) rescue puts @error="Blad: #{$!}" end end # SPRAWDZANIE { } until s1.slice(/{[^}]+}/).nil? wyc=s1.slice(/{[^{.+}]+}/) @dl=dl-wyc.size @dl_max=dl_max-wyc.size @dl_min=dl_min-wyc.size poz=s1.index(wyc) s1.slice!(/{[^{.+}]+}/) wyc=wyc[1..-2] begin if wyc[-1, 1]=="|" tab_wyc=wyc.split("|") tab_wyc[tab_wyc.size]="" else tab_wyc=wyc.split("|") end tmp_min,tmp_max = max_min(tab_wyc) wyc=tab_wyc[rand(tab_wyc.size())] #random synonimu @dl=dl+wyc.size @dl_max=dl_max+tmp_max @dl_min=dl_min+tmp_min s1.insert(poz, wyc) rescue puts @error="Blad: #{$!}" end end wynik="" wynik=s1 @output=wynik return output #,dl,dl_max,dl_min end end |
#valid(string) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/mixer/mixuj.rb', line 14 def valid(string) s1=string il_zam=0 il_dom=0 il_zam2=0 il_dom2=0 il_zam=s1.count"{" il_dom=s1.count"}" il_zam2=s1.count"[" il_dom2=s1.count"]" if s1.empty? @error="Brak tekstu" return false end if il_zam != il_dom @error="Niezgodnosc w ilosci nawiasow { }" return false end if il_zam2 != il_dom2 @error="Niezgodnosc w ilosci nawiasow [ ]" return false end if il_zam==0 and il_zam2==0 and s1.include? "|" @error="wystapilo | pomimmo bralu nawiasow [] lub {}" return false end if s1.include? "{}" @error="pusty { }" return false end if s1.include? "[]" @error="pusty [ ]" return false end #if not s1.slice(/{*[^{]*}*\|.*{/).nil? #(/{[^{]+}+|.+{/ # puts s1.slice(/{*[^{]*}*\|.*{/) # @error="wystapilo | przed po za {}" # return false #end return true end |