txt2speech 0.4 → 0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -2
- data/bin/txt2speech +1 -1
- data/lib/txt2speech/application.rb +26 -15
- data/lib/txt2speech/speech.rb +63 -0
- data/lib/txt2speech/version.rb +1 -1
- data/lib/txt2speech.rb +1 -1
- data/txt2speech.gemspec +2 -1
- metadata +5 -4
- data/lib/txt2speech/txt2speech.rb +0 -61
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1c40146f701737a59b26858ab5b3c182e6ba1754
|
4
|
+
data.tar.gz: 0c1a11ee5ad703f43ce661ff74011bc12d38ab2b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f3d4e27f81e74aaeb595992dcfd2c687f34934551a47258e726ebb76365e4c030b57fb573ca36ee91adba0e0d0500c7c41feb20f9464e184e7e0f220e71d39b
|
7
|
+
data.tar.gz: 15b8190faa7d946ecee9f95e3ec437ad82d60862c9c73de215c0b48b36efdff40c60d661f0440c680d909261a1aa2d73510d1cebe669c2c2bea26aef7779c3e2
|
data/README.md
CHANGED
@@ -4,12 +4,18 @@ txt2Speech lifehack of using Google Translate. Allow you to convert text into sp
|
|
4
4
|
bin/txt2speech -r README.md -f -o example.mp3
|
5
5
|
```
|
6
6
|
|
7
|
+
to change language to read
|
8
|
+
|
9
|
+
```
|
10
|
+
bin/txt2speech -r examples/de.txt -f -l de
|
11
|
+
```
|
12
|
+
|
7
13
|
### To use library directly in your application just install gem and run
|
8
14
|
|
9
15
|
```
|
10
16
|
2.1.2 :001 > require 'txt2speech'
|
11
17
|
=> 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"
|
18
|
+
2.1.2 :010 > f = Txt2Speech::Speech.new "Hello I am Google robot! Nice to meet you, hope you'll enjoy this script and will fork it"
|
13
19
|
=> #<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
20
|
2.1.2 :011 > f.save('out.mp3')
|
15
21
|
```
|
@@ -17,7 +23,7 @@ bin/txt2speech -r README.md -f -o example.mp3
|
|
17
23
|
or you can load a text file to read it
|
18
24
|
|
19
25
|
```
|
20
|
-
2.1.2 :006 > f = Txt2Speech.load "README.md"
|
26
|
+
2.1.2 :006 > f = Txt2Speech::Speech.load "README.md"
|
21
27
|
=> #<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
28
|
2.1.2 :007 > f.save("out.mp3")
|
23
29
|
```
|
data/bin/txt2speech
CHANGED
@@ -1,22 +1,33 @@
|
|
1
1
|
require 'optparse'
|
2
|
-
require_relative "txt2speech"
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
OptionParser.new do |opts|
|
7
|
-
opts.banner = "Usage: txt2speech [options]"
|
3
|
+
require_relative 'speech'
|
4
|
+
require_relative 'version'
|
8
5
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
6
|
+
module Txt2Speech
|
7
|
+
class Application
|
8
|
+
def initialize
|
9
|
+
options = {}
|
10
|
+
OptionParser.new do |opts|
|
11
|
+
opts.banner = "Usage: txt2speech [options]"
|
14
12
|
|
15
|
-
|
13
|
+
opts.on("-o", "--output [String]", String, "Output file") {|o| options[:out] = o }
|
14
|
+
opts.on("-r", "--read [String]", String, "Read text") {|o| options[:read] = o}
|
15
|
+
opts.on("-f", "--file", "From file") {|o| options[:file] = o }
|
16
|
+
opts.on("-l", "--lang [String]", String, "Language of text") {|o| options[:lang] = o }
|
17
|
+
opts.on("-v", "--version", "Show version") {|o| options[:version] = Txt2Speech::VERSION }
|
18
|
+
end.parse!
|
16
19
|
|
17
|
-
|
18
|
-
|
20
|
+
if options[:version]
|
21
|
+
puts options[:version]
|
22
|
+
else
|
23
|
+
out = options[:out].to_s.length > 0 ? options[:out] : "out.mp3"
|
19
24
|
|
20
|
-
|
21
|
-
|
25
|
+
attrs = [options[:read], options[:lang] || "en"]
|
26
|
+
attrs.unshift(options[:file] ? :load : :new)
|
27
|
+
|
28
|
+
f = Txt2Speech::Speech.send(*attrs)
|
29
|
+
f.save(out)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
22
33
|
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'net/http'
|
4
|
+
|
5
|
+
module Txt2Speech
|
6
|
+
class Speech
|
7
|
+
GOOGLE_TRANSLATE_URL = 'http://translate.google.com/translate_tts'
|
8
|
+
|
9
|
+
attr_accessor :text, :lang
|
10
|
+
|
11
|
+
def initialize(text, lang = 'en')
|
12
|
+
@text = text
|
13
|
+
@lang = lang
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.load(file_path, lang = 'en')
|
17
|
+
f = File.open(file_path)
|
18
|
+
self.new f.read.encode("UTF-16be", :invalid=>:replace, :replace=>"?").encode('UTF-8'), lang
|
19
|
+
end
|
20
|
+
|
21
|
+
def save(file_path)
|
22
|
+
uri = URI(GOOGLE_TRANSLATE_URL)
|
23
|
+
|
24
|
+
response = []
|
25
|
+
ar = text.split(/[,.\r\n]/i)
|
26
|
+
ar.reject! {|t| t.empty? }
|
27
|
+
ar.map! {|t| divide(t) }.flatten!
|
28
|
+
ar.map! {|t| t.strip }
|
29
|
+
|
30
|
+
puts ar.inspect
|
31
|
+
|
32
|
+
ar.each_with_index do |q, idx|
|
33
|
+
uri.query = URI.encode_www_form({ ie: 'UTF-8', q: q, tl: lang, total: ar.length, idx: idx+1, textlen: q.length, prev: 'input' })
|
34
|
+
res = Net::HTTP.get_response(uri)
|
35
|
+
|
36
|
+
response << res.body.force_encoding(Encoding::UTF_8) if res.is_a?(Net::HTTPSuccess)
|
37
|
+
end
|
38
|
+
|
39
|
+
File.open(file_path, 'wb') do |f|
|
40
|
+
f.write response.join
|
41
|
+
return f.path
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def divide(text)
|
48
|
+
return text if text.length < 99
|
49
|
+
|
50
|
+
attempts = text.length / 99.0
|
51
|
+
starts = 0
|
52
|
+
ar = []
|
53
|
+
|
54
|
+
attempts.ceil.times do
|
55
|
+
ends = starts + 99
|
56
|
+
ar << text[starts...ends]
|
57
|
+
starts = ends
|
58
|
+
end
|
59
|
+
|
60
|
+
ar
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/lib/txt2speech/version.rb
CHANGED
data/lib/txt2speech.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require_relative 'txt2speech/
|
1
|
+
require_relative 'txt2speech/speech'
|
data/txt2speech.gemspec
CHANGED
@@ -5,12 +5,13 @@ require 'txt2speech/version'
|
|
5
5
|
Gem::Specification.new do |s|
|
6
6
|
s.name = 'txt2speech'
|
7
7
|
s.version = Txt2Speech::VERSION
|
8
|
-
s.date = '
|
8
|
+
s.date = '2014-12-17'
|
9
9
|
s.summary = "txt2speech"
|
10
10
|
s.description = "txt2Speech lifehack of using Google Translate. Allow you to convert text into speech."
|
11
11
|
s.authors = ["Jared Levitz"]
|
12
12
|
s.email = 'jared.levitz@gmail.com'
|
13
13
|
s.files = `git ls-files`.split("\n")
|
14
|
+
s.executables = ['txt2speech']
|
14
15
|
s.homepage = 'http://github.com/rudkovsky/txt2speech'
|
15
16
|
s.license = 'MIT'
|
16
17
|
end
|
metadata
CHANGED
@@ -1,19 +1,20 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: txt2speech
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.5'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jared Levitz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-12-17 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: txt2Speech lifehack of using Google Translate. Allow you to convert text
|
14
14
|
into speech.
|
15
15
|
email: jared.levitz@gmail.com
|
16
|
-
executables:
|
16
|
+
executables:
|
17
|
+
- txt2speech
|
17
18
|
extensions: []
|
18
19
|
extra_rdoc_files: []
|
19
20
|
files:
|
@@ -24,7 +25,7 @@ files:
|
|
24
25
|
- examples/ru.txt
|
25
26
|
- lib/txt2speech.rb
|
26
27
|
- lib/txt2speech/application.rb
|
27
|
-
- lib/txt2speech/
|
28
|
+
- lib/txt2speech/speech.rb
|
28
29
|
- lib/txt2speech/version.rb
|
29
30
|
- txt2speech.gemspec
|
30
31
|
homepage: http://github.com/rudkovsky/txt2speech
|
@@ -1,61 +0,0 @@
|
|
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'), lang
|
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
|