Module: KoremuStringFunctions
Overview
KoremuStringFunctions
The following functions are either available through the KoremuString class, or can be included in other classes. For example, maybe the most natural use would be by including them in the String class. You can include them by typing this in your initialization:
class String; include KoremuStringFunctions; end
This way, any arbitrary string can be treated as a KoremuString:
puts 'fefifofu'.to_ki # 27494288
puts 'fefifofu'.to_ka # [13, 14, 15, 16]
If you choose not to extend String, you can use KoremuString as a regular class, initializing it with the string in question:
ks = KoremuString.new('fefifofu')
Constant Summary
Constants included from Koremutake
Koremutake::Phonemes, Koremutake::Vowels
Instance Method Summary collapse
-
#to_ka ⇒ Object
Returns the KoremuArray representation of the KoremuString.
-
#to_ki ⇒ Object
Returns the numeric representation of the KoremuString as a KoremuFixnum (why
to_ki? as an analogy to Ruby’s ownto_i, which returns a Fixnum). -
#tokenize ⇒ Object
Returns an array (not a KoremuArray - A regular Array) where each of the elements is one of the tokens of the string.
-
#valid? ⇒ Boolean
Check if the given KoremuString is valid, returning true or false.
Instance Method Details
#to_ka ⇒ Object
Returns the KoremuArray representation of the KoremuString.
123 124 125 126 127 128 129 |
# File 'lib/koremu.rb', line 123 def to_ka rel = phonemes_to_nums arr = self.tokenize.map { |phoneme| rel[phoneme] } arr = [0] if arr.empty? KoremuArray.new(arr) end |
#to_ki ⇒ Object
Returns the numeric representation of the KoremuString as a KoremuFixnum (why to_ki? as an analogy to Ruby’s own to_i, which returns a Fixnum)
118 119 120 |
# File 'lib/koremu.rb', line 118 def to_ki self.to_ka.to_ki end |
#tokenize ⇒ Object
Returns an array (not a KoremuArray - A regular Array) where each of the elements is one of the tokens of the string. When tokenizing, any particle which does not conform a Koremutake syllable will be dropped (with a warning being issued), thus:
irb(main):017:0> Koremutake.new('KoremuFixnum').tokenize
Invalid Koremutake phoneme 'XNU'
=> ["KO", "RE", "MU", "FI"]
Please refer to the WHAT IS VALID KOREMUTAKE section in the general documentation for koremu.rb for further information.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/koremu.rb', line 66 def tokenize str = self || '' rel = phonemes_to_nums buf = '' res = [] str.upcase.split(//).each do |chr| buf << chr next unless Vowels.include?(chr) if Phonemes.include?(buf) res << buf else warn "Invalid Koremutake phoneme '#{buf}'" end buf = '' end if str.size > 0 and ! Vowels.include?(str[-1,1].upcase) warn "The received string does not end in a valid syllable: #{str[-1,1].upcase}" end if res[0] == Phonemes[0] and res.size > 1 warn "Dropping leading zero (#{Phonemes[0]}) syllables from #{res} (#{res.size})" res.shift while res[0] == Phonemes[0] end res end |
#valid? ⇒ Boolean
Check if the given KoremuString is valid, returning true or false. If it is not valid, it can still be used - Non-recognized characters will just be dropped, so:
puts KoremuString.new('Koremu Take').to_ki # 10610353957
puts KoremuString.new('Koremu Take').to_ki.to_ks # KOREMUTAKE
However, a syllable should be atomic - It should not be split:
puts KoremuString.new('Korem Utake').to_ki.to_ks # KORETAKE
Note that the ‘BA’ particle has a particularity - If it is at the beginning of the string, it should be dropped, as it means 0. Keep in mind Koremutake is mainly a way to represent numbers.
-
‘BABABABABABE’ == 1 == ‘BE’. Therefore, a string starting
-
with ‘BA’ and with more characters is considered valid.
110 111 112 113 |
# File 'lib/koremu.rb', line 110 def valid? return false if self.upcase[0,2] == 'BA' and self.size > 2 self.tokenize.join == self.upcase end |