class VoiceBase::JSON
Attributes
words[W]
Public Class Methods
new(word_array = nil)
click to toggle source
# File lib/voicebase/json.rb, line 48 def initialize(word_array = nil) raise StandardError, "Must be initialized with words." if word_array.is_a?(Array) && !words.all? {|w| w.is_a?(VoiceBase::JSON::Word)} @words = word_array || [] end
parse(input, options = {})
click to toggle source
# File lib/voicebase/json.rb, line 9 def parse(input, options = {}) @debug = options.fetch(:debug, false) if input.is_a?(String) parse_string(input) elsif input.is_a?(::File) parse_file(input) else raise "Invalid input. Expected a String or File, got #{input.class.name}." end end
Private Class Methods
parse_file(json_file)
click to toggle source
# File lib/voicebase/json.rb, line 22 def parse_file(json_file) parse_string ::File.open(json_file, 'rb') { |f| json_file.read } end
parse_string(json_string_data)
click to toggle source
# File lib/voicebase/json.rb, line 26 def parse_string(json_string_data) result = new json_hash_data = ::JSON.parse(json_string_data) raise ParseError, "Invalid format" unless json_hash_data.is_a?(Array) json_hash_data.each_with_index do |word_hash, index| word = Word.new(word_hash) result.words << word unless word.empty? %w(p c s e w).each do |field| if word.send(field).nil? word.error = "#{index}, Invalid formatting of #{field}, [#{word_hash[field]}]" $stderr.puts word.error if @debug end end end result end
Public Instance Methods
each(&block)
click to toggle source
# File lib/voicebase/json.rb, line 61 def each(&block) @words.each {|word| block.call(word)} end
errors()
click to toggle source
# File lib/voicebase/json.rb, line 57 def errors @words.map {|w| w.error if w.error}.compact end
gt(start_time)
click to toggle source
# File lib/voicebase/json.rb, line 65 def gt(start_time) VoiceBase::JSON.new(select {|w| w.start_time > start_time}) end
gteq(start_time)
click to toggle source
# File lib/voicebase/json.rb, line 69 def gteq(start_time) VoiceBase::JSON.new(select {|w| w.start_time >= start_time}) end
lt(start_time)
click to toggle source
# File lib/voicebase/json.rb, line 73 def lt(start_time) VoiceBase::JSON.new(select {|w| w.start_time < start_time}) end
lteq(start_time)
click to toggle source
# File lib/voicebase/json.rb, line 77 def lteq(start_time) VoiceBase::JSON.new(select {|w| w.start_time <= start_time}) end
to_a()
click to toggle source
# File lib/voicebase/json.rb, line 81 def to_a @words end
to_json()
click to toggle source
# File lib/voicebase/json.rb, line 85 def to_json map {|w| w.to_hash}.to_json end
words()
click to toggle source
# File lib/voicebase/json.rb, line 53 def words @words ||= [] end