ncode-syosetu-polly 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6346aa4443a206e89c17bece44edb858d78e85ed
4
+ data.tar.gz: 2f174b100f12d0aa9b29e7cba44b89e4f7f1ddcc
5
+ SHA512:
6
+ metadata.gz: e7c0757a85aa3a42f7822dff747b517d773f9d86ed1e57ebd40e3dc3665fa2ea0643086ffcc43e12fcb10b3e07030e0c72dcf5efe9fd3b7b17154ceaa8bb2bfe
7
+ data.tar.gz: 8aca908e2c6f5cf34d44178e743171cb56720d2dd41372aae1c5294702820cc8d66b6a056683f285460f64ebef24b063d9486efbee95d9a4260c7d4f72d43aba
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ncode-syosetu-polly.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 hogelog
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Ncode::Syosetu::Polly
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ncode/syosetu/polly`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ncode-syosetu-polly'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ncode-syosetu-polly
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ncode-syosetu-polly.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ncode/syosetu/polly"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/ncode2mp3 ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
4
+ require 'ncode-syosetu-polly'
5
+
6
+ if ARGV.size == 0
7
+ puts "#$0 ncode episode_nubmer"
8
+ exit
9
+ end
10
+
11
+ ncode, episode_number = ARGV
12
+
13
+ episode = NcodeSyosetu.client.get_episode(ncode, "dummy_title", episode_number)
14
+
15
+ builder = NcodeSyosetu::Builder::Polly.new
16
+ builder.write_episode(episode, "#{ncode}-#{episode_number}.mp3")
@@ -0,0 +1,3 @@
1
+ require "ncode-syosetu-core"
2
+ require "ncode-syosetu-ssml"
3
+ require "ncode_syosetu/builder/polly"
@@ -0,0 +1,101 @@
1
+ require "aws-sdk-polly"
2
+ require "sanitize"
3
+ require "nokogiri"
4
+
5
+ module NcodeSyosetu
6
+ module Builder
7
+ class Polly
8
+ POLLY_TEXT_LENGTH_LIMIT = 1500
9
+
10
+ attr_reader :sample_rate, :client
11
+
12
+ def initialize(options={})
13
+ options[:region] ||= "us-west-2"
14
+ @sample_rate = options.delete(:sample_rate) || "16000"
15
+ @client = Aws::Polly::Client.new(options)
16
+ end
17
+
18
+ def write_episode(episode, path)
19
+ tmp_files = []
20
+ ssmls = split_ssml(episode.body_ssml.gsub("\n", "")).map{|body_ssml| create_ssml(body_ssml) }
21
+
22
+ ssmls.each_with_index do |ssml, i|
23
+ dirname = File.dirname(path)
24
+ basename = File.basename(path, ".mp3")
25
+
26
+ File.write(File.join(dirname, "#{basename}-#{i}.ssml"), ssml)
27
+ tmp_path = File.join(dirname, "#{basename}-#{i}.mp3")
28
+ client.synthesize_speech(
29
+ response_target: tmp_path,
30
+ output_format: "mp3",
31
+ sample_rate: sample_rate,
32
+ text: ssml,
33
+ text_type: "ssml",
34
+ voice_id: "Mizuki",
35
+ )
36
+ tmp_files << tmp_path
37
+ end
38
+ File.open(path, "wb") do |file|
39
+ tmp_files.each do |tmp_path|
40
+ File.open(tmp_path, "rb") do |tmp_file|
41
+ IO.copy_stream(tmp_file, file)
42
+ end
43
+ end
44
+ file.flush
45
+ end
46
+ end
47
+
48
+ def create_ssml(body_ssml)
49
+ <<-XML
50
+ <?xml version="1.0"?>
51
+ <speak version="1.1" xmlns="http://www.w3.org/2001/10/synthesis" xml:lang="ja">
52
+ #{body_ssml}
53
+ </speak>
54
+ XML
55
+ end
56
+
57
+ def split_ssml(body_ssml)
58
+ ssml = tweak_ssml(body_ssml)
59
+ doc = Nokogiri::XML.parse("<root>#{ssml}</root>")
60
+ elements = doc.root.children
61
+
62
+ buffer = StringIO.new
63
+ text_count = 0
64
+ results = []
65
+ while elements.size > 0 do
66
+ element = elements.shift
67
+
68
+ unless element.is_a?(Nokogiri::XML::Text) || element.is_a?(String)
69
+ buffer.print(element.to_s)
70
+ next
71
+ end
72
+
73
+ text = element.to_s
74
+
75
+ if text.size > POLLY_TEXT_LENGTH_LIMIT
76
+ elements = text.chars.each_slice(POLLY_TEXT_LENGTH_LIMIT).map(&:join) + elements
77
+ next
78
+ end
79
+
80
+ if (text_count + text.size) > POLLY_TEXT_LENGTH_LIMIT
81
+ results << buffer.string
82
+ buffer = StringIO.new
83
+ text_count = 0
84
+ end
85
+ buffer.print(element.to_s)
86
+ text_count += text.size
87
+ end
88
+ results << buffer.string if buffer.size > 0
89
+
90
+ results
91
+ end
92
+
93
+ def tweak_ssml(body_ssml)
94
+ body_ssml.
95
+ gsub("<p>", "").
96
+ gsub("</p>", '<break strength="strong"/>').
97
+ gsub(/([」】)』])/, '\1<break strength="strong"/>')
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ GEM_VERSION = File.read("../ncode-syosetu-core/lib/ncode_syosetu/version.rb") =~ /"(.+)"/ && $1
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "ncode-syosetu-polly"
6
+ spec.version = GEM_VERSION
7
+ spec.authors = ["hogelog"]
8
+ spec.email = ["konbu.komuro@gmail.com"]
9
+
10
+ spec.summary = %q{Ncode syosetu to speech by Amazon Polly}
11
+ spec.homepage = "https://github.com/hogelog/ncode-syosetu"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
15
+ f.match(%r{^(test|spec|features)/})
16
+ end
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "ncode-syosetu-core", GEM_VERSION
22
+ spec.add_dependency "ncode-syosetu-ssml", GEM_VERSION
23
+ spec.add_dependency "aws-sdk-polly", "1.0.0.rc2"
24
+ spec.add_dependency "sanitize", "~> 4.4"
25
+ spec.add_dependency "nokogiri", ">= 1.6"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.13"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ spec.add_development_dependency "pry"
31
+ end
metadata ADDED
@@ -0,0 +1,181 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ncode-syosetu-polly
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - hogelog
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ncode-syosetu-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: ncode-syosetu-ssml
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.2.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: aws-sdk-polly
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.0.0.rc2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.0.rc2
55
+ - !ruby/object:Gem::Dependency
56
+ name: sanitize
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.4'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: nokogiri
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '1.6'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '1.6'
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.13'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.13'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '10.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '10.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '3.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '3.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description:
140
+ email:
141
+ - konbu.komuro@gmail.com
142
+ executables:
143
+ - ncode2mp3
144
+ extensions: []
145
+ extra_rdoc_files: []
146
+ files:
147
+ - Gemfile
148
+ - LICENSE.txt
149
+ - README.md
150
+ - Rakefile
151
+ - bin/console
152
+ - bin/setup
153
+ - exe/ncode2mp3
154
+ - lib/ncode-syosetu-polly.rb
155
+ - lib/ncode_syosetu/builder/polly.rb
156
+ - ncode-syosetu-polly.gemspec
157
+ homepage: https://github.com/hogelog/ncode-syosetu
158
+ licenses:
159
+ - MIT
160
+ metadata: {}
161
+ post_install_message:
162
+ rdoc_options: []
163
+ require_paths:
164
+ - lib
165
+ required_ruby_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ required_rubygems_version: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ requirements: []
176
+ rubyforge_project:
177
+ rubygems_version: 2.6.8
178
+ signing_key:
179
+ specification_version: 4
180
+ summary: Ncode syosetu to speech by Amazon Polly
181
+ test_files: []