Class: StringSyntax::Parser
- Inherits:
-
Object
- Object
- StringSyntax::Parser
- Defined in:
- lib/strsyntax/strsyntax.rb
Constant Summary collapse
- C =
/^([bcdfghjklmnpqrstvwxyz])/
- V =
/^([aeiou])/
- CV =
/^([bcdfghjklmnpqrstvwxyz][aeiou])/
- CVC =
/^([bcdfghjklmnpqrstvwxyz][aeiou][bcdfghjklmnpqrstvwxyz])/
- VC =
/^([aeiou][bcdfghjklmnpqrstvwxyz])/
- VCV =
/^([aeiou][bcdfghjklmnpqrstvwxyz][aeiou])/
- SCHEMA_TEMPLATES =
{ C => :c, V => :v, CV => :cv, CVC => :cvc, VC => :vc, VCV => :vcv }
Class Method Summary collapse
-
.parse(s) ⇒ Object
Returns an array containing the components of the string in terms of consonant and vowel groupings: CVC, VCV, CV, VC, C, V.
- .schema_from_string(s) ⇒ Object
- .schema_from_template(template) ⇒ Object
-
.score(s) ⇒ Object
Return the number of :c and :v components in the structure.
- .split(s, structure = parse( s )) ⇒ Object
- .template_from_schema(schema) ⇒ Object
Class Method Details
.parse(s) ⇒ Object
Returns an array containing the components of the string in terms of consonant and vowel groupings: CVC, VCV, CV, VC, C, V.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/strsyntax/strsyntax.rb', line 28 def self.parse( s ) s = s.downcase structure = case s when CVC [ parse_subpart( CVC, s[0,3], s[(3..-1)] ), parse_subpart( CV, s[0,2], s[(2..-1)] ) ] when CV [ parse_subpart( CV, s[0,2], s[(2..-1)] ), parse_subpart( C, s[0,1], s[(1..-1)] ) ] when C [ parse_subpart( C, s[0,1], s[(1..-1)] ) ] when VCV [ parse_subpart( VCV, s[0,3], s[(3..-1)] ), parse_subpart( VC, s[0,2], s[(2..-1)] ) ] when VC [ parse_subpart( VC, s[0,2], s[(2..-1)] ), parse_subpart( V, s[0,1], s[(1..-1)] ) ] when V [ parse_subpart( V, s[0,1], s[(1..-1)] ) ] else [] end # Return the sub-structure containing the least number of stray :c and :v structure.sort_by { |s| score( s ) }.first end |
.schema_from_string(s) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/strsyntax/strsyntax.rb', line 89 def self.schema_from_string( s ) case s when CVC :cvc when CV :cv when VCV :vcv when VC :vc when C :c when V :v end end |
.schema_from_template(template) ⇒ Object
66 67 68 |
# File 'lib/strsyntax/strsyntax.rb', line 66 def self.schema_from_template( template ) SCHEMA_TEMPLATES[template] end |
.score(s) ⇒ Object
Return the number of :c and :v components in the structure
53 54 55 |
# File 'lib/strsyntax/strsyntax.rb', line 53 def self.score( s ) s.inject( 0 ) { |sum, e| e == :c || e == :v ? sum + 1 : sum } end |
.split(s, structure = parse( s )) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/strsyntax/strsyntax.rb', line 57 def self.split( s, structure = parse( s ) ) templates = structure.map { |schema| template_from_schema( schema ) } templates.map do |template| match_data = template.match( s ) s = match_data.post_match match_data[1] end end |
.template_from_schema(schema) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/strsyntax/strsyntax.rb', line 70 def self.template_from_schema( schema ) case schema when :cvc CVC when :vcv VCV when :cv CV when :vc VC when :c C when :v V else raise "Unknown schema: #{schema}" end end |