Class: Glaemscribe::API::Fragment

Inherits:
Object
  • Object
show all
Defined in:
lib/api/fragment.rb

Constant Summary collapse

EQUIVALENCE_SEPARATOR =
","
EQUIVALENCE_RX_OUT =
/(\(.*?\))/
EQUIVALENCE_RX_IN =
/\((.*?)\)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#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
97
# 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 }.reject{ |eq| eq == '' }
  equivalences = equivalences.map{ |eq|           
    eq =~ EQUIVALENCE_RX_IN
    if $1
      eq = $1.split(EQUIVALENCE_SEPARATOR,-1).map{ |elt| 
        elt = elt.strip
        elt.split(/\s/).map{ |leaf| finalize_fragment_leaf(leaf) }
      }
    else
      eq = [eq.split(/\s/).map{ |leaf| finalize_fragment_leaf(leaf) }] # 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]
    
  # ((eq0 x eq1) x eq2) x eq3 ) ... )))))
  (equivalences.length-1).times { |i|
    prod  = res.product(equivalences[i+1]).map{ |x,y| x+y}
    res   = prod
  }
  
  @combinations = res
end

Instance Attribute Details

#combinationsObject (readonly)

Returns the value of attribute combinations.



34
35
36
# File 'lib/api/fragment.rb', line 34

def combinations
  @combinations
end

#modeObject (readonly)

Returns the value of attribute mode.



33
34
35
# File 'lib/api/fragment.rb', line 33

def mode
  @mode
end

#ruleObject (readonly)

Returns the value of attribute rule.



32
33
34
# File 'lib/api/fragment.rb', line 32

def rule
  @rule
end

#sheafObject (readonly)

Returns the value of attribute sheaf.



31
32
33
# File 'lib/api/fragment.rb', line 31

def sheaf
  @sheaf
end

Instance Method Details

#dst?Boolean

Returns:

  • (Boolean)


37
# File 'lib/api/fragment.rb', line 37

def dst?; @sheaf.dst?; end

#finalize_fragment_leaf(leaf) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/api/fragment.rb', line 99

def finalize_fragment_leaf(leaf)
  if src?
    
    # Replace {UNI_XXXX} by its value to allow any unicode char to be found in the transcription tree
    leaf = leaf.gsub(RuleGroup::UNICODE_VAR_NAME_REGEXP_OUT) { |cap_var|
      unival = $1
      new_char = [unival.hex].pack("U")
      new_char = "\u0001" if new_char == '_'
      new_char
    }

    # Replace '_' (word boundary) by '\u0000' to allow 
    # the real underscore to be used in the transcription tree
    # (Do it after replacing the uni_xxx vars because they have underscores inside)
    leaf = leaf.gsub(WORD_BOUNDARY_LANG, WORD_BOUNDARY_TREE) 
    leaf = leaf.gsub("\u0001","_")        
  end

  leaf
end

#pObject



120
121
122
123
124
125
126
# File 'lib/api/fragment.rb', line 120

def p
  ret = "---- " + @expression + "\n"
  @combinations.each{ |c|
    ret += "------ " + c.inspect + "\n"
  }
  ret
end

#src?Boolean

Returns:

  • (Boolean)


36
# File 'lib/api/fragment.rb', line 36

def src?; @sheaf.src?; end