txt2speech 0.4 → 0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 29f22ea642c96606c65fa0e64154b808bc13b22a
4
- data.tar.gz: 5c96e6a371d72adad45ff90c08c5bd0396a4cc57
3
+ metadata.gz: 1c40146f701737a59b26858ab5b3c182e6ba1754
4
+ data.tar.gz: 0c1a11ee5ad703f43ce661ff74011bc12d38ab2b
5
5
  SHA512:
6
- metadata.gz: a653682d999950007c93257ea8bc369f10a6ab1512468453a16b5a5a36e7cda8404b43554d16de4f8e6bcb67cec43308efa9b5f0e2dd73a56eeb17f203034e9b
7
- data.tar.gz: 2e0dcb388c9ae65aa458ceffb986337d2d8bfe22775a0d4fa3fd306635be69e62c2763d986e697e57a48d8717eb8c421e89e6945926d3645f61872360f0994ae
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
@@ -2,4 +2,4 @@
2
2
 
3
3
  require_relative "../lib/txt2speech/application"
4
4
 
5
- Application.new
5
+ Txt2Speech::Application.new
@@ -1,22 +1,33 @@
1
1
  require 'optparse'
2
- require_relative "txt2speech"
3
2
 
4
- class Application
5
- options = {}
6
- OptionParser.new do |opts|
7
- opts.banner = "Usage: txt2speech [options]"
3
+ require_relative 'speech'
4
+ require_relative 'version'
8
5
 
9
- opts.on("-o", "--output [String]", String, "Output file") {|o| options[:out] = o }
10
- opts.on("-r", "--read [String]", String, "Read text") {|o| options[:read] = o}
11
- opts.on("-f", "--file", "From file") {|o| options[:file] = o }
12
- opts.on("-l", "--lang [String]", String, "Language of text") {|o| options[:lang] = o }
13
- end.parse!
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
- out = options[:out].to_s.length > 0 ? options[:out] : "out.mp3"
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
- attrs = [options[:read], options[:lang] || "en"]
18
- attrs.unshift(options[:file] ? :load : :new)
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
- f = Txt2Speech.send(*attrs)
21
- f.save(out)
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
@@ -1,3 +1,3 @@
1
1
  module Txt2Speech
2
- VERSION = '0.4'
2
+ VERSION = '0.5'
3
3
  end
data/lib/txt2speech.rb CHANGED
@@ -1 +1 @@
1
- require_relative 'txt2speech/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 = '2013-09-02'
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'
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: 2013-09-02 00:00:00.000000000 Z
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/txt2speech.rb
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