txt2speech 0.5 → 0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +28 -0
- data/README.md +9 -7
- data/Rakefile +3 -0
- data/bin/txt2speech +1 -1
- data/examples/rss_reader.rb +7 -0
- data/examples/ru.txt +0 -0
- data/lib/txt2speech.rb +0 -0
- data/lib/txt2speech/application.rb +23 -23
- data/lib/txt2speech/speech.rb +58 -58
- data/lib/txt2speech/version.rb +2 -2
- data/spec/spec_helper.rb +96 -0
- data/spec/speech_spec.rb +28 -0
- data/txt2speech.gemspec +3 -3
- metadata +11 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0ce8d7e048b1003c3d8f94aa85984a180b68f63f
|
4
|
+
data.tar.gz: e27c25c110173405d91b89bda9a3a92beeb1503e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1842088ccd9542de7a369b07ee74e3c331ce36ebb0992ab60da85b70f001077fbc4d2b374f5f6947f5a2d9cf158e41b993906caf912a3ab3e7c44b0fea1c7c82
|
7
|
+
data.tar.gz: 44b558edc9cd828ac08c55e4269675dc275777d64f1301ecfc30f3e6e00a6e7a5f2990fab586762591a2a51d3c7ab4616fd96a4c6ce3ee0af66cbc36116ac798
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
GEM
|
2
|
+
remote: https://rubygems.org/
|
3
|
+
specs:
|
4
|
+
bundle (0.0.1)
|
5
|
+
bundler
|
6
|
+
diff-lcs (1.2.5)
|
7
|
+
rake (10.4.2)
|
8
|
+
rspec (3.3.0)
|
9
|
+
rspec-core (~> 3.3.0)
|
10
|
+
rspec-expectations (~> 3.3.0)
|
11
|
+
rspec-mocks (~> 3.3.0)
|
12
|
+
rspec-core (3.3.1)
|
13
|
+
rspec-support (~> 3.3.0)
|
14
|
+
rspec-expectations (3.3.0)
|
15
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
16
|
+
rspec-support (~> 3.3.0)
|
17
|
+
rspec-mocks (3.3.1)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.3.0)
|
20
|
+
rspec-support (3.3.0)
|
21
|
+
|
22
|
+
PLATFORMS
|
23
|
+
ruby
|
24
|
+
|
25
|
+
DEPENDENCIES
|
26
|
+
bundle
|
27
|
+
rake
|
28
|
+
rspec
|
data/README.md
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/rudkovsky/txt2speech.svg)](https://travis-ci.org/rudkovsky/txt2speech)
|
2
|
+
|
1
3
|
txt2Speech lifehack of using Google Translate. Allow you to convert text into speech.
|
2
4
|
|
3
5
|
```
|
4
|
-
bin/txt2speech -r README.md -f -o example.
|
6
|
+
bin/txt2speech -r README.md -f -o example.mpg
|
5
7
|
```
|
6
8
|
|
7
9
|
to change language to read
|
@@ -16,16 +18,16 @@ bin/txt2speech -r examples/de.txt -f -l de
|
|
16
18
|
2.1.2 :001 > require 'txt2speech'
|
17
19
|
=> true
|
18
20
|
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"
|
19
|
-
=> #<Txt2Speech:
|
20
|
-
2.1.2 :011 > f.save('out.
|
21
|
+
=> #<Txt2Speech::Speech:0x000000022314f8 @text="Hello I am Google robot! Nice to meet you, hope you'll enjoy this script and will fork it", @lang="en">
|
22
|
+
2.1.2 :011 > f.save('out.mpg')
|
21
23
|
```
|
22
24
|
|
23
25
|
or you can load a text file to read it
|
24
26
|
|
25
27
|
```
|
26
28
|
2.1.2 :006 > f = Txt2Speech::Speech.load "README.md"
|
27
|
-
=> #<Txt2Speech:
|
28
|
-
2.1.2 :007 > f.save("out.
|
29
|
+
=> #<Txt2Speech::Speech:0x000000022314f8 @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.mpg\")\r\n => \"out.mpg\"\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.mpg\")\r\n```", @lang="en">
|
30
|
+
2.1.2 :007 > f.save("out.mpg")
|
29
31
|
```
|
30
32
|
|
31
33
|
Supplemental publishing:
|
@@ -33,7 +35,7 @@ Supplemental publishing:
|
|
33
35
|
|
34
36
|
The MIT License (MIT)
|
35
37
|
|
36
|
-
Copyright (c) 2014
|
38
|
+
Copyright (c) 2014 Viacheslav Rudkovskyi <rrubyist@gmail.com>
|
37
39
|
|
38
40
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
39
41
|
of this software and associated documentation files (the "Software"), to deal
|
@@ -51,4 +53,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
51
53
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
52
54
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
53
55
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
54
|
-
THE SOFTWARE.
|
56
|
+
THE SOFTWARE.
|
data/Rakefile
ADDED
data/bin/txt2speech
CHANGED
data/examples/ru.txt
CHANGED
File without changes
|
data/lib/txt2speech.rb
CHANGED
File without changes
|
@@ -4,30 +4,30 @@ require_relative 'speech'
|
|
4
4
|
require_relative 'version'
|
5
5
|
|
6
6
|
module Txt2Speech
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
7
|
+
class Application
|
8
|
+
def initialize
|
9
|
+
options = {}
|
10
|
+
OptionParser.new do |opts|
|
11
|
+
opts.banner = "Usage: txt2speech [options]"
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
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!
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
20
|
+
if options[:version]
|
21
|
+
puts options[:version]
|
22
|
+
else
|
23
|
+
out = options[:out].to_s.length > 0 ? options[:out] : "out.mpg"
|
24
24
|
|
25
|
-
|
26
|
-
|
25
|
+
attrs = [options[:read], options[:lang] || "en"]
|
26
|
+
attrs.unshift(options[:file] ? :load : :new)
|
27
27
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
end
|
28
|
+
f = Txt2Speech::Speech.send(*attrs)
|
29
|
+
f.save(out)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/txt2speech/speech.rb
CHANGED
@@ -3,61 +3,61 @@
|
|
3
3
|
require 'net/http'
|
4
4
|
|
5
5
|
module Txt2Speech
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
end
|
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: 0, textlen: q.length, client: 't', 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 < 150
|
49
|
+
|
50
|
+
attempts = text.length / 150.0
|
51
|
+
starts = 0
|
52
|
+
ar = []
|
53
|
+
|
54
|
+
attempts.ceil.times do
|
55
|
+
ends = starts + 150
|
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
@@ -1,3 +1,3 @@
|
|
1
1
|
module Txt2Speech
|
2
|
-
VERSION = '0.
|
3
|
-
end
|
2
|
+
VERSION = '0.7'
|
3
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
4
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
5
|
+
# files.
|
6
|
+
#
|
7
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
8
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
9
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
10
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
11
|
+
# a separate helper file that requires the additional dependencies and performs
|
12
|
+
# the additional setup, and require it from the spec files that actually need
|
13
|
+
# it.
|
14
|
+
#
|
15
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
16
|
+
# users commonly want.
|
17
|
+
#
|
18
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
19
|
+
RSpec.configure do |config|
|
20
|
+
# rspec-expectations config goes here. You can use an alternate
|
21
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
22
|
+
# assertions if you prefer.
|
23
|
+
config.expect_with :rspec do |expectations|
|
24
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
25
|
+
# and `failure_message` of custom matchers include text for helper methods
|
26
|
+
# defined using `chain`, e.g.:
|
27
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
28
|
+
# # => "be bigger than 2 and smaller than 4"
|
29
|
+
# ...rather than:
|
30
|
+
# # => "be bigger than 2"
|
31
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
32
|
+
end
|
33
|
+
|
34
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
35
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
36
|
+
config.mock_with :rspec do |mocks|
|
37
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
38
|
+
# a real object. This is generally recommended, and will default to
|
39
|
+
# `true` in RSpec 4.
|
40
|
+
mocks.verify_partial_doubles = true
|
41
|
+
end
|
42
|
+
|
43
|
+
# The settings below are suggested to provide a good initial experience
|
44
|
+
# with RSpec, but feel free to customize to your heart's content.
|
45
|
+
=begin
|
46
|
+
# These two settings work together to allow you to limit a spec run
|
47
|
+
# to individual examples or groups you care about by tagging them with
|
48
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
49
|
+
# get run.
|
50
|
+
config.filter_run :focus
|
51
|
+
config.run_all_when_everything_filtered = true
|
52
|
+
|
53
|
+
# Allows RSpec to persist some state between runs in order to support
|
54
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
55
|
+
# you configure your source control system to ignore this file.
|
56
|
+
config.example_status_persistence_file_path = "spec/examples.txt"
|
57
|
+
|
58
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
59
|
+
# recommended. For more details, see:
|
60
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
61
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
62
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
63
|
+
config.disable_monkey_patching!
|
64
|
+
|
65
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
66
|
+
# be too noisy due to issues in dependencies.
|
67
|
+
config.warnings = true
|
68
|
+
|
69
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
70
|
+
# file, and it's useful to allow more verbose output when running an
|
71
|
+
# individual spec file.
|
72
|
+
if config.files_to_run.one?
|
73
|
+
# Use the documentation formatter for detailed output,
|
74
|
+
# unless a formatter has already been configured
|
75
|
+
# (e.g. via a command-line flag).
|
76
|
+
config.default_formatter = 'doc'
|
77
|
+
end
|
78
|
+
|
79
|
+
# Print the 10 slowest examples and example groups at the
|
80
|
+
# end of the spec run, to help surface which specs are running
|
81
|
+
# particularly slow.
|
82
|
+
config.profile_examples = 10
|
83
|
+
|
84
|
+
# Run specs in random order to surface order dependencies. If you find an
|
85
|
+
# order dependency and want to debug it, you can fix the order by providing
|
86
|
+
# the seed, which is printed after each run.
|
87
|
+
# --seed 1234
|
88
|
+
config.order = :random
|
89
|
+
|
90
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
91
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
92
|
+
# test failures related to randomization by passing the same `--seed` value
|
93
|
+
# as the one that triggered the failure.
|
94
|
+
Kernel.srand config.seed
|
95
|
+
=end
|
96
|
+
end
|
data/spec/speech_spec.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'fileutils'
|
3
|
+
require './lib/txt2speech'
|
4
|
+
|
5
|
+
RSpec.describe Txt2Speech::Speech, '#new' do
|
6
|
+
context 'with english text' do
|
7
|
+
let!(:output_path) { "/tmp/txt2speech.mp4" }
|
8
|
+
|
9
|
+
it 'converts string to the audio file' do
|
10
|
+
@speech = Txt2Speech::Speech.new("Hello World")
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'load a file and save it to audio file' do
|
14
|
+
File.open("/tmp/txt2speech.txt", "w+") do |f|
|
15
|
+
f.write("Hello World from file")
|
16
|
+
end
|
17
|
+
@speech = Txt2Speech::Speech.load("/tmp/txt2speech.txt")
|
18
|
+
end
|
19
|
+
|
20
|
+
after(:each) do
|
21
|
+
@speech.save(output_path)
|
22
|
+
expect(File.exists?(output_path)).to eq(true)
|
23
|
+
[output_path, "/tmp/txt2speech.txt"].each do |f|
|
24
|
+
FileUtils.rm(f) if File.exists?(f)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/txt2speech.gemspec
CHANGED
@@ -8,10 +8,10 @@ Gem::Specification.new do |s|
|
|
8
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
|
-
s.authors = ["
|
12
|
-
s.email = '
|
11
|
+
s.authors = ["Viacheslav Rudkovskyi"]
|
12
|
+
s.email = 'rrubyist@gmail.com'
|
13
13
|
s.files = `git ls-files`.split("\n")
|
14
14
|
s.executables = ['txt2speech']
|
15
15
|
s.homepage = 'http://github.com/rudkovsky/txt2speech'
|
16
16
|
s.license = 'MIT'
|
17
|
-
end
|
17
|
+
end
|
metadata
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: txt2speech
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.7'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- Viacheslav Rudkovskyi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
@@ -12,21 +12,29 @@ 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
|
-
email:
|
15
|
+
email: rrubyist@gmail.com
|
16
16
|
executables:
|
17
17
|
- txt2speech
|
18
18
|
extensions: []
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
|
+
- ".rspec"
|
22
|
+
- ".travis.yml"
|
23
|
+
- Gemfile
|
24
|
+
- Gemfile.lock
|
21
25
|
- README.md
|
26
|
+
- Rakefile
|
22
27
|
- bin/txt2speech
|
23
28
|
- examples/de.txt
|
24
29
|
- examples/en.txt
|
30
|
+
- examples/rss_reader.rb
|
25
31
|
- examples/ru.txt
|
26
32
|
- lib/txt2speech.rb
|
27
33
|
- lib/txt2speech/application.rb
|
28
34
|
- lib/txt2speech/speech.rb
|
29
35
|
- lib/txt2speech/version.rb
|
36
|
+
- spec/spec_helper.rb
|
37
|
+
- spec/speech_spec.rb
|
30
38
|
- txt2speech.gemspec
|
31
39
|
homepage: http://github.com/rudkovsky/txt2speech
|
32
40
|
licenses:
|