Method: Glaemscribe::API::Fragment#initialize
- Defined in:
- lib/api/fragment.rb
#initialize(sheaf, expression) ⇒ Fragment
Should pass a fragment expression, e.g. : “h(a|ä)(i|ï)”
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 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 93 94 95 96 |
# File 'lib/api/fragment.rb', line 45 def initialize(sheaf, expression) @sheaf = sheaf @mode = sheaf.mode @rule = sheaf.rule @expression = expression # Split the fragment, turn it into an array of arrays, e.g. [[h],[a,ä],[i,ï]] equivalences = expression.split(EQUIVALENCE_RX_OUT).map{ |eq| eq.strip } equivalences = equivalences.map{ |eq| eq =~ EQUIVALENCE_RX_IN if $1 eq = $1.split(EQUIVALENCE_SEPARATOR,-1).map{ |elt| elt = elt.strip elt.split(/\s/) } else eq = [eq.split(/\s/)] # This equivalence has only one possibility end } equivalences = [[[""]]] if equivalences.empty? # In the case of a destination fragment, check that all symbols used are found # in the charsets used by the mode if dst? mode = @sheaf.mode equivalences.each{ |eq| eq.each{ |member| member.each{ |token| next if token.empty? # NULL case mode.supported_charsets.each{ |charset_name, charset| symbol = charset[token] if !symbol @rule.errors << "Symbol #{token} not found in charset '#{charset.name}'!" return end } } } } end # Calculate all combinations for this fragment (productize the array of arrays) res = equivalences[0] (equivalences.length-1).times { |i| prod = res.product(equivalences[i+1]).map{ |x,y| x+y} res = prod } @combinations = res end |