Class: GoogleTts::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/google_tts/parser.rb

Constant Summary collapse

MAX_LENGTH =
100
SPACE =
URI.escape(" ")

Instance Method Summary collapse

Instance Method Details

#paragraphs(text = "") ⇒ Object



9
10
11
12
13
14
# File 'lib/google_tts/parser.rb', line 9

def paragraphs(text = "")
  paragraphs = text.split(/(?<=[\.\?\!])/)
  paragraphs.map do |p|
    p.strip
  end
end

#sentences(text = "") ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/google_tts/parser.rb', line 16

def sentences(text = "")
  text = URI.escape(text)
  return [text] if text.length < MAX_LENGTH

  tokens = paragraphs(remove_extra_spaces(text))
  tokens.flat_map do |token| 
    next_partial(token) {
      comma_setences = token.split(',').flat_map do |subtoken|
        subtoken = remove_extra_spaces("#{subtoken},")
        next_partial(subtoken) { 
          accumulate(subtoken, SPACE)
        }
      end
      comma_setences[-1] = custom_strip(comma_setences.last, ',')
      comma_setences
   }
  end
end