tts_based_on_google 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ .idea/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tts_based_on_google.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Petr Brazdil
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # TTSBasedOnGoogle
2
+
3
+ This Ruby gem provides Text-to-speech functionality in many languages. Result can be stored in MP3 file. Based on technologies from Google.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'tts_based_on_google'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install tts_based_on_google
18
+
19
+ ## Usage
20
+
21
+ Basic example:
22
+
23
+ file = File.new("test.mp3", "w+")
24
+ TextToSpeech.new("Hi guys, hope you will love it!", :en, file).save_to_file
25
+
26
+ Or you don't have to specify instance of file, it will automatically write into Tempfile, so if you can just send it to output, try something like this:
27
+
28
+ speech = TextToSpeech.new("Hi guys, home you will love it!", :en).save_to_file
29
+ send_file(speech.path, :type => 'audio/mp3', :disposition => 'inline')
30
+
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,85 @@
1
+ require "tts_based_on_google/version"
2
+ require 'net/http'
3
+ require 'tempfile'
4
+ require 'uri'
5
+
6
+ class TextToSpeech
7
+ URL_HOST = "translate.google.com"
8
+ URL_PATH = lambda { |text, lang| "/translate_tts?ie=UTF-8&q=#{URI.escape(text)}&tl=#{lang}" }
9
+
10
+ def initialize(text, lang = :en, file = Tempfile.new("tts_mp3"))
11
+ @full_text = escape_text(text)
12
+ @lang = lang.to_s if lang.class == Symbol
13
+ @file = file
14
+ end
15
+
16
+ def save_to_file
17
+ convert_to_speech
18
+
19
+ @file
20
+ end
21
+
22
+
23
+ private
24
+ def convert_to_speech
25
+ str = ""
26
+ parts = [] # each part's size has to be lower then 100 chars, in order to get working with google
27
+
28
+ splitted = split_text_into_parts
29
+
30
+ # join sentences or words into parts that has lower size then 100
31
+ until splitted.empty?
32
+ # if text's size will be bigger then 100, we first need to download speech for this text, then reset it
33
+ # google don't support TTS for texts bigger then 100 chars
34
+ if (str + splitted[0]).size > 100
35
+ parts << str
36
+ str = ""
37
+ else
38
+ str += splitted[0]
39
+ str += " "
40
+ splitted.delete_at(0)
41
+ end
42
+ end
43
+
44
+ parts << str
45
+
46
+ download_speech(parts)
47
+ end
48
+
49
+ def split_text_into_parts
50
+ splitted = []
51
+
52
+ # let's split by sentences
53
+ @full_text.split(".").each do |sentence|
54
+ #if sentence is bigger, then split by words
55
+ if sentence.size >= 100
56
+ sentence.split(" ").each { |word| splitted << word }
57
+ else
58
+ splitted << sentence + "."
59
+ end
60
+ end
61
+ splitted
62
+ end
63
+
64
+ def download_speech(parts)
65
+ #binding.pry
66
+ Net::HTTP.start(URL_HOST) do |http|
67
+ open(@file.path, "ab") do |file|
68
+ parts.each do |part|
69
+ response = http.get(URL_PATH.call(part, @lang))
70
+ file.write(response.body)
71
+ end
72
+ end
73
+ end
74
+ end
75
+
76
+ def escape_text(text)
77
+ text.squeeze!(".")
78
+ text.gsub!("\n", " ")
79
+ text.gsub!("\r", " ")
80
+ text.squeeze!(" ")
81
+ text
82
+ end
83
+ end
84
+
85
+
@@ -0,0 +1,3 @@
1
+ module TTSBasedOnGoogle
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tts_based_on_google/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "tts_based_on_google"
8
+ gem.version = TTSBasedOnGoogle::VERSION
9
+ gem.authors = ["Petr Brazdil"]
10
+ gem.email = ["p.brazdil@gmail.com"]
11
+ gem.description = %q{Provides Text-to-speech functionality in many languages. Results can be stored into MP3 file. Based on technologies from Google.}
12
+ gem.summary = %q{Provides Text-to-speech functionality in many languages. Results can be stored into MP3 file.}
13
+ gem.homepage = "https://github.com/pbrazdil/tts_based_on_google"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tts_based_on_google
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Petr Brazdil
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-02 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Provides Text-to-speech functionality in many languages. Results can
15
+ be stored into MP3 file. Based on technologies from Google.
16
+ email:
17
+ - p.brazdil@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/text_to_speech.rb
28
+ - lib/tts_based_on_google/version.rb
29
+ - tts_based_on_google.gemspec
30
+ homepage: https://github.com/pbrazdil/tts_based_on_google
31
+ licenses: []
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 1.8.24
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: Provides Text-to-speech functionality in many languages. Results can be stored
54
+ into MP3 file.
55
+ test_files: []
56
+ has_rdoc: