Class: Glaemscribe::API::Mode

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Mode

Returns a new instance of Mode.



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/api/mode.rb', line 40

def initialize(name)
  @name               = name
  @errors             = []
  @warnings           = []
  @supported_charsets = {}
  @options            = {}
 
  @pre_processor    = TranscriptionPreProcessor.new(self)
  @processor        = TranscriptionProcessor.new(self)
  @post_processor   = TranscriptionPostProcessor.new(self)
end

Instance Attribute Details

#authorsObject

Returns the value of attribute authors.



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

def authors
  @authors
end

#default_charsetObject

Returns the value of attribute default_charset.



38
39
40
# File 'lib/api/mode.rb', line 38

def default_charset
  @default_charset
end

#errorsObject

Returns the value of attribute errors.



27
28
29
# File 'lib/api/mode.rb', line 27

def errors
  @errors
end

#human_nameObject

Returns the value of attribute human_name.



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

def human_name
  @human_name
end

#languageObject

Returns the value of attribute language.



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

def language
  @language
end

#nameObject

Returns the value of attribute name.



30
31
32
# File 'lib/api/mode.rb', line 30

def name
  @name
end

#optionsObject

Returns the value of attribute options.



35
36
37
# File 'lib/api/mode.rb', line 35

def options
  @options
end

#post_processorObject

Returns the value of attribute post_processor.



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

def post_processor
  @post_processor
end

#pre_processorObject

Returns the value of attribute pre_processor.



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

def pre_processor
  @pre_processor
end

#processorObject

Returns the value of attribute processor.



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

def processor
  @processor
end

#supported_charsetsObject

Returns the value of attribute supported_charsets.



37
38
39
# File 'lib/api/mode.rb', line 37

def supported_charsets
  @supported_charsets
end

#versionObject

Returns the value of attribute version.



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

def version
  @version
end

#warningsObject

Returns the value of attribute warnings.



28
29
30
# File 'lib/api/mode.rb', line 28

def warnings
  @warnings
end

#writingObject

Returns the value of attribute writing.



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

def writing
  @writing
end

Instance Method Details

#finalize(options = {}) ⇒ Object



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
# File 'lib/api/mode.rb', line 60

def finalize(options={})
        
  # Hash: option_name => value_name
  trans_options = {}
  
  # Build default options
  @options.each { |oname, o|
    trans_options[oname] = o.default_value_name
  }   

  # Push user options
  options.each { |oname, valname|
    # Check if option exists
    opt = @options[oname]
    next if(!opt)
      
    val = opt.value_for_value_name(valname)
    next if val == nil
      
    trans_options[oname] = valname
  }
    
  trans_options_converted = {}
 
  # Do a conversion to values space
  trans_options.each{ |oname,valname| 
    trans_options_converted[oname] = @options[oname].value_for_value_name(valname)
  }
     
  @pre_processor.finalize(trans_options_converted)
  @post_processor.finalize(trans_options_converted)
  @processor.finalize(trans_options_converted)
  
  self
end

#inspectObject



56
57
58
# File 'lib/api/mode.rb', line 56

def inspect
  to_s
end

#to_sObject



52
53
54
# File 'lib/api/mode.rb', line 52

def to_s
  " <Mode #{name}: Language '#{language}', Writing '#{writing}', Human Name '#{human_name}', Authors '#{authors}', Version '#{version}'> "
end

#transcribe(content, charset = nil) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/api/mode.rb', line 96

def transcribe(content, charset = nil)
  charset = default_charset if !charset
  return false, "*** No charset usable for transcription. Failed!" if !charset
  
  ret = content.lines.map{ |l| 
    l = l.strip # Clean the lines
    l = @pre_processor.apply(l)
    l = @processor.apply(l, charset)
    l = @post_processor.apply(l)
  }.join("\n")
  
  return true, ret
end