txt2speech 0.9 → 1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/Gemfile +2 -2
- data/Gemfile.lock +4 -3
- data/README.md +3 -1
- data/Rakefile +1 -1
- data/examples/rss_reader.rb +2 -2
- data/lib/txt2speech.rb +1 -1
- data/lib/txt2speech/speech.rb +71 -63
- data/lib/txt2speech/version.rb +3 -3
- data/spec/speech_spec.rb +13 -8
- data/txt2speech.gemspec +17 -17
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 62d739323d097d547cf6d5f31896445be172bde5
|
4
|
+
data.tar.gz: dd9a36baf44ade2da2ba4b05a1ec50b18f05b2d3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 228907e50e2691db3347f5b60390bf6b972660cbb592ef8b6b241e7dc3be77b7e7d293564da692d267f3eff768554f1e97518bbdbcdf5c10b89668c92110a2a7
|
7
|
+
data.tar.gz: ae28be2efbb1eee95cf0b8b0e1929f9b50f3886295a4046a1ca5b7ee180435f2ed14aa903800b8d3b752a73d3f378b95781ff6b3bdf6dc291f76483be70eb035
|
data/.gitignore
ADDED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
GEM
|
2
2
|
remote: https://rubygems.org/
|
3
3
|
specs:
|
4
|
-
bundle (0.0.1)
|
5
|
-
bundler
|
6
4
|
diff-lcs (1.2.5)
|
7
5
|
rake (10.4.2)
|
8
6
|
rspec (3.3.0)
|
@@ -23,6 +21,9 @@ PLATFORMS
|
|
23
21
|
ruby
|
24
22
|
|
25
23
|
DEPENDENCIES
|
26
|
-
|
24
|
+
bundler
|
27
25
|
rake
|
28
26
|
rspec
|
27
|
+
|
28
|
+
BUNDLED WITH
|
29
|
+
1.12.5
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
[![Build Status](https://travis-ci.org/rudkovskyi/txt2speech.svg?branch=master)](https://travis-ci.org/rudkovsky/txt2speech)
|
2
2
|
|
3
|
-
|
3
|
+
txt2speech is a very simple library that by using Google Translate undocumented API allow you convert text to speech.
|
4
4
|
|
5
5
|
```
|
6
6
|
bin/txt2speech -r README.md -f -o example.mpg
|
@@ -23,3 +23,5 @@ or you can load a text file to read it
|
|
23
23
|
=> #<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">
|
24
24
|
2.1.2 :007 > f.save("out.mpg")
|
25
25
|
```
|
26
|
+
|
27
|
+
The differences between Google and Bing - [How Google and Bing protects their API](https://rudk.ws/2016/10/23/how-google-and-bing-protects-their-api/)
|
data/Rakefile
CHANGED
data/examples/rss_reader.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require_relative '../lib/txt2speech'
|
2
2
|
require 'rss'
|
3
3
|
|
4
|
-
rss = RSS::Parser.parse('http://
|
4
|
+
rss = RSS::Parser.parse('http://feeds.feedburner.com/TechCrunch/startups', false)
|
5
5
|
|
6
6
|
f = Txt2Speech::Speech.new(rss.items.map{|item| item.title}.join('.'))
|
7
|
-
f.save("rss_reader.mpg")
|
7
|
+
f.save("rss_reader.mpg")
|
data/lib/txt2speech.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require_relative 'txt2speech/speech'
|
1
|
+
require_relative 'txt2speech/speech'
|
data/lib/txt2speech/speech.rb
CHANGED
@@ -1,63 +1,71 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
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
|
-
|
1
|
+
require 'net/http'
|
2
|
+
|
3
|
+
module Txt2Speech
|
4
|
+
class Speech
|
5
|
+
GOOGLE_TRANSLATE_URL = 'http://translate.google.com/translate_tts'.freeze
|
6
|
+
|
7
|
+
attr_accessor :text, :lang
|
8
|
+
|
9
|
+
def initialize(text, lang = 'en')
|
10
|
+
@text = text
|
11
|
+
@lang = lang
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.load(file_path, lang = 'en')
|
15
|
+
f = File.open(file_path)
|
16
|
+
new f.read.encode('UTF-16be', invalid: :replace, replace: '?').encode('UTF-8'), lang
|
17
|
+
end
|
18
|
+
|
19
|
+
def save(file_path)
|
20
|
+
uri = URI(GOOGLE_TRANSLATE_URL)
|
21
|
+
|
22
|
+
response = []
|
23
|
+
|
24
|
+
sentences = text.split(/[,.\r\n]/i)
|
25
|
+
sentences.reject!(&:empty?)
|
26
|
+
sentences.map! { |t| divide(t.strip) }.flatten!
|
27
|
+
|
28
|
+
sentences.each_with_index do |q, _idx|
|
29
|
+
uri.query = URI.encode_www_form(
|
30
|
+
ie: 'UTF-8',
|
31
|
+
q: q,
|
32
|
+
tl: lang,
|
33
|
+
total: sentences.length,
|
34
|
+
idx: 0,
|
35
|
+
textlen: q.length,
|
36
|
+
client: 'tw-ob',
|
37
|
+
prev: 'input'
|
38
|
+
)
|
39
|
+
|
40
|
+
res = Net::HTTP.get_response(uri)
|
41
|
+
|
42
|
+
next unless res.is_a?(Net::HTTPSuccess)
|
43
|
+
|
44
|
+
response << res.body.force_encoding(Encoding::UTF_8)
|
45
|
+
end
|
46
|
+
|
47
|
+
File.open(file_path, 'wb') do |f|
|
48
|
+
f.write response.join
|
49
|
+
return f.path
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def divide(text)
|
56
|
+
return text if text.length < 150
|
57
|
+
|
58
|
+
attempts = text.length / 150.0
|
59
|
+
starts = 0
|
60
|
+
arr = []
|
61
|
+
|
62
|
+
attempts.ceil.times do
|
63
|
+
ends = starts + 150
|
64
|
+
arr << text[starts...ends]
|
65
|
+
starts = ends
|
66
|
+
end
|
67
|
+
|
68
|
+
arr
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/lib/txt2speech/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module Txt2Speech
|
2
|
-
|
3
|
-
end
|
1
|
+
module Txt2Speech
|
2
|
+
VERSION = '1.1'.freeze
|
3
|
+
end
|
data/spec/speech_spec.rb
CHANGED
@@ -4,25 +4,30 @@ require './lib/txt2speech'
|
|
4
4
|
|
5
5
|
RSpec.describe Txt2Speech::Speech, '#new' do
|
6
6
|
context 'with english text' do
|
7
|
-
let!(:output_path) {
|
7
|
+
let!(:output_path) { '/tmp/txt2speech.mp4' }
|
8
8
|
|
9
9
|
it 'converts string to the audio file' do
|
10
|
-
@speech = Txt2Speech::Speech.new(
|
10
|
+
@speech = Txt2Speech::Speech.new('Hello World')
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'converts string longer than 150 character into the audio file' do
|
14
|
+
@speech = Txt2Speech::Speech.new('Super long string abcdef' * 10)
|
11
15
|
end
|
12
16
|
|
13
17
|
it 'load a file and save it to audio file' do
|
14
|
-
File.open(
|
15
|
-
f.write(
|
18
|
+
File.open('/tmp/txt2speech.txt', 'w+') do |f|
|
19
|
+
f.write('Hello World from file')
|
16
20
|
end
|
17
|
-
|
21
|
+
|
22
|
+
@speech = Txt2Speech::Speech.load('/tmp/txt2speech.txt')
|
18
23
|
end
|
19
24
|
|
20
25
|
after(:each) do
|
21
26
|
@speech.save(output_path)
|
22
|
-
expect(File.
|
27
|
+
expect(File.exist?(output_path)).to eq(true)
|
23
28
|
expect(File.size(output_path)).to be > 0
|
24
|
-
[output_path,
|
25
|
-
FileUtils.rm(f) if File.
|
29
|
+
[output_path, '/tmp/txt2speech.txt'].each do |f|
|
30
|
+
FileUtils.rm(f) if File.exist?(f)
|
26
31
|
end
|
27
32
|
end
|
28
33
|
end
|
data/txt2speech.gemspec
CHANGED
@@ -1,17 +1,17 @@
|
|
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 = '
|
9
|
-
s.summary =
|
10
|
-
s.description =
|
11
|
-
s.authors = [
|
12
|
-
s.email = 'rrubyist@gmail.com'
|
13
|
-
s.files = `git ls-files`.split("\n")
|
14
|
-
s.executables = ['txt2speech']
|
15
|
-
s.homepage = 'http://github.com/rudkovsky/txt2speech'
|
16
|
-
s.license = 'MIT'
|
17
|
-
end
|
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 = Time.now.strftime('%Y-%m-%d')
|
9
|
+
s.summary = 'txt2speech'
|
10
|
+
s.description = 'txt2speech is a very simple library that by using Google Translate undocumented API allow you convert text to speech.'
|
11
|
+
s.authors = ['Viacheslav Rudkovskyi']
|
12
|
+
s.email = 'rrubyist@gmail.com'
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
s.executables = ['txt2speech']
|
15
|
+
s.homepage = 'http://github.com/rudkovsky/txt2speech'
|
16
|
+
s.license = 'MIT'
|
17
|
+
end
|
metadata
CHANGED
@@ -1,23 +1,24 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: txt2speech
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '
|
4
|
+
version: '1.1'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Viacheslav Rudkovskyi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-08-01 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
14
|
-
|
13
|
+
description: txt2speech is a very simple library that by using Google Translate undocumented
|
14
|
+
API allow you convert text to speech.
|
15
15
|
email: rrubyist@gmail.com
|
16
16
|
executables:
|
17
17
|
- txt2speech
|
18
18
|
extensions: []
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
|
+
- ".gitignore"
|
21
22
|
- ".rspec"
|
22
23
|
- ".travis.yml"
|
23
24
|
- Gemfile
|
@@ -53,7 +54,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
53
54
|
version: '0'
|
54
55
|
requirements: []
|
55
56
|
rubyforge_project:
|
56
|
-
rubygems_version: 2.
|
57
|
+
rubygems_version: 2.6.10
|
57
58
|
signing_key:
|
58
59
|
specification_version: 4
|
59
60
|
summary: txt2speech
|