Class: Glaemscribe::API::Fragment
Constant Summary collapse
- EQUIVALENCE_SEPARATOR =
","
- EQUIVALENCE_RX_OUT =
/(\(.*?\))/
- EQUIVALENCE_RX_IN =
/\((.*?)\)/
Instance Attribute Summary collapse
-
#combinations ⇒ Object
readonly
Returns the value of attribute combinations.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#rule ⇒ Object
readonly
Returns the value of attribute rule.
-
#sheaf ⇒ Object
readonly
Returns the value of attribute sheaf.
Instance Method Summary collapse
- #dst? ⇒ Boolean
-
#initialize(sheaf, expression) ⇒ Fragment
constructor
Should pass a fragment expression, e.g.
- #p ⇒ Object
- #src? ⇒ Boolean
Constructor Details
permalink #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 |
Instance Attribute Details
permalink #combinations ⇒ Object (readonly)
Returns the value of attribute combinations.
34 35 36 |
# File 'lib/api/fragment.rb', line 34 def combinations @combinations end |
permalink #mode ⇒ Object (readonly)
Returns the value of attribute mode.
33 34 35 |
# File 'lib/api/fragment.rb', line 33 def mode @mode end |
Instance Method Details
permalink #dst? ⇒ Boolean
37 |
# File 'lib/api/fragment.rb', line 37 def dst?; @sheaf.dst?; end |
permalink #p ⇒ Object
[View source]
99 100 101 102 103 104 105 |
# File 'lib/api/fragment.rb', line 99 def p ret = "---- " + @expression + "\n" @combinations.each{ |c| ret += "------ " + c.inspect + "\n" } ret end |
permalink #src? ⇒ Boolean
36 |
# File 'lib/api/fragment.rb', line 36 def src?; @sheaf.src?; end |