txt2speech 0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +48 -0
- data/bin/txt2speech +22 -0
- data/lib/txt2speech.rb +1 -0
- data/lib/txt2speech/txt2speech.rb +61 -0
- data/lib/txt2speech/version.rb +3 -0
- data/txt2speech.gemspec +16 -0
- metadata +49 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 986ffd54fb80ffb8bb4f14bdd042a422d8b35701
|
4
|
+
data.tar.gz: 8c8589afbb071a3c1b3da059abb2d38031d74a7c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f53a293919cf7c6d767e7dbf7f9be13af2f981b589dbd0264e99b7119cb83dddbb961358b4201a6e605e2b08288a005863543d1a416dc22088de888c9a1f48ee
|
7
|
+
data.tar.gz: 66479cd198145ac3f10436f1cf7c570ba176affe25b73463f2d8660efdb4daeca2b86b3ed520d87fc7c42f6a4a7a55587495803d9392523d5da6888851d9e281
|
data/README.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
txt2Speech lifehack of using Google Translate. Allow you to convert text into speech.
|
2
|
+
|
3
|
+
```
|
4
|
+
bin/txt2speech -r README.md -f -o example.mp3
|
5
|
+
```
|
6
|
+
|
7
|
+
### To use library directly in your application just install gem and run
|
8
|
+
|
9
|
+
```
|
10
|
+
2.1.2 :001 > require 'txt2speech'
|
11
|
+
=> true
|
12
|
+
2.1.2 :010 > f = Txt2Speech.new "Hello I am Google robot! Nice to meet you, hope you'll enjoy this script and will fork it"
|
13
|
+
=> #<Txt2Speech:0x000000029a3290 @text="Hello I am Google robot! Nice to meet you, hope you'll enjoy this script and will fork it", @lang="en">
|
14
|
+
2.1.2 :011 > f.save('out.mp3')
|
15
|
+
```
|
16
|
+
|
17
|
+
or you can load a text file to read it
|
18
|
+
|
19
|
+
```
|
20
|
+
2.1.2 :006 > f = Txt2Speech.load "README.md"
|
21
|
+
=> #<Txt2Speech:0x00000002ef8b00 @text="txt2Speech is a simple ruby script that convert text to speech by using Google Translate.\r\n\r\n```\r\n2.1.2 :001 > require './txt2speech.rb'\r\n => true\r\n2.1.2 :002 > f = Txt2Speech.new \"Hello I am google! Nice to meet you\"\r\n => #<Txt2Speech:0x000000018aafd8 @text=\"Hello I am google! Nice to meet you\", @lang=\"en\">\r\n2.1.2 :003 > f.save(\"out.mp3\")\r\n => \"out.mp3\"\r\n```\r\n\r\nor you can load a text file to read it\r\n\r\n```\r\nt = Txt2Speech.load \"\#{Dir.home}/text.txt\"\r\nt.save(\"out.mp3\")\r\n```", @lang="en">
|
22
|
+
2.1.2 :007 > f.save("out.mp3")
|
23
|
+
```
|
24
|
+
|
25
|
+
Supplemental publishing:
|
26
|
+
[Text to Speech? Lifehack of using Google Translate](http://jaredlevitz.com/post/105204717375/text-to-speech-lifehack-of-using-google-translate)
|
27
|
+
|
28
|
+
The MIT License (MIT)
|
29
|
+
|
30
|
+
Copyright (c) 2014 Jared Levitz <jared.levitz@gmail.com>
|
31
|
+
|
32
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
33
|
+
of this software and associated documentation files (the "Software"), to deal
|
34
|
+
in the Software without restriction, including without limitation the rights
|
35
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
36
|
+
copies of the Software, and to permit persons to whom the Software is
|
37
|
+
furnished to do so, subject to the following conditions:
|
38
|
+
|
39
|
+
The above copyright notice and this permission notice shall be included in
|
40
|
+
all copies or substantial portions of the Software.
|
41
|
+
|
42
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
43
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
44
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
45
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
46
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
47
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
48
|
+
THE SOFTWARE.
|
data/bin/txt2speech
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'optparse'
|
4
|
+
require_relative "../lib/txt2speech"
|
5
|
+
|
6
|
+
options = {}
|
7
|
+
OptionParser.new do |opts|
|
8
|
+
opts.banner = "Usage: txt2speech [options]"
|
9
|
+
|
10
|
+
opts.on("-o", "--output [String]", String, "Output file") {|o| options[:out] = o }
|
11
|
+
opts.on("-r", "--read [String]", String, "Read text") {|o| options[:read] = o}
|
12
|
+
opts.on("-f", "--file", "From file") {|o| options[:file] = o }
|
13
|
+
opts.on("-l", "--lang", "Language of text") {|o| options[:lang] = o }
|
14
|
+
end.parse!
|
15
|
+
|
16
|
+
out = options[:out].to_s.length > 0 ? options[:out] : "out.mp3"
|
17
|
+
|
18
|
+
attrs = [options[:read], options[:lang] || "en"]
|
19
|
+
attrs.unshift(options[:file] ? :load : :new)
|
20
|
+
|
21
|
+
f = Txt2Speech.send(*attrs)
|
22
|
+
f.save(out)
|
data/lib/txt2speech.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require_relative 'txt2speech/txt2speech'
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'net/http'
|
4
|
+
|
5
|
+
class Txt2Speech
|
6
|
+
GOOGLE_TRANSLATE_URL = 'http://translate.google.com/translate_tts'
|
7
|
+
|
8
|
+
attr_accessor :text, :lang
|
9
|
+
|
10
|
+
def initialize(text, lang = 'en')
|
11
|
+
@text = text
|
12
|
+
@lang = lang
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.load(file_path, lang = 'en')
|
16
|
+
f = File.open(file_path)
|
17
|
+
self.new f.read.encode("UTF-16be", :invalid=>:replace, :replace=>"?").encode('UTF-8')
|
18
|
+
end
|
19
|
+
|
20
|
+
def save(file_path)
|
21
|
+
uri = URI(GOOGLE_TRANSLATE_URL)
|
22
|
+
|
23
|
+
response = []
|
24
|
+
ar = text.split(/[,.\r\n]/i)
|
25
|
+
ar.reject! {|t| t.empty? }
|
26
|
+
ar.map! {|t| divide(t) }.flatten!
|
27
|
+
ar.map! {|t| t.strip }
|
28
|
+
|
29
|
+
puts ar.inspect
|
30
|
+
|
31
|
+
ar.each_with_index do |q, idx|
|
32
|
+
uri.query = URI.encode_www_form({ ie: 'UTF-8', q: q, tl: lang, total: ar.length, idx: idx+1, textlen: q.length, prev: 'input' })
|
33
|
+
res = Net::HTTP.get_response(uri)
|
34
|
+
|
35
|
+
response << res.body.force_encoding(Encoding::UTF_8) if res.is_a?(Net::HTTPSuccess)
|
36
|
+
end
|
37
|
+
|
38
|
+
File.open(file_path, 'wb') do |f|
|
39
|
+
f.write response.join
|
40
|
+
return f.path
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def divide(text)
|
47
|
+
return text if text.length < 99
|
48
|
+
|
49
|
+
attempts = text.length / 99.0
|
50
|
+
starts = 0
|
51
|
+
ar = []
|
52
|
+
|
53
|
+
attempts.ceil.times do
|
54
|
+
ends = starts + 99
|
55
|
+
ar << text[starts...ends]
|
56
|
+
starts = ends
|
57
|
+
end
|
58
|
+
|
59
|
+
ar
|
60
|
+
end
|
61
|
+
end
|
data/txt2speech.gemspec
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'txt2speech/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'txt2speech'
|
7
|
+
s.version = Txt2Speech::VERSION
|
8
|
+
s.date = '2013-09-02'
|
9
|
+
s.summary = "txt2speech"
|
10
|
+
s.description = "Gem"
|
11
|
+
s.authors = ["Jared Levitz"]
|
12
|
+
s.email = 'jared.levitz@gmail.com'
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
s.homepage = 'http://github.com/rudkovsky/txt2speech'
|
15
|
+
s.license = 'MIT'
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: txt2speech
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.3'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jared Levitz
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-02 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Gem
|
14
|
+
email: jared.levitz@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- README.md
|
20
|
+
- bin/txt2speech
|
21
|
+
- lib/txt2speech.rb
|
22
|
+
- lib/txt2speech/txt2speech.rb
|
23
|
+
- lib/txt2speech/version.rb
|
24
|
+
- txt2speech.gemspec
|
25
|
+
homepage: http://github.com/rudkovsky/txt2speech
|
26
|
+
licenses:
|
27
|
+
- MIT
|
28
|
+
metadata: {}
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project:
|
45
|
+
rubygems_version: 2.2.2
|
46
|
+
signing_key:
|
47
|
+
specification_version: 4
|
48
|
+
summary: txt2speech
|
49
|
+
test_files: []
|