hiq 0.2.0 → 0.2.1
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 +4 -4
- data/lib/hiq/domain/line_composer.rb +63 -0
- data/lib/hiq/domain/word_checker.rb +40 -0
- data/lib/hiq/exceptions.rb +35 -0
- data/lib/hiq/haiku.rb +36 -0
- data/lib/hiq/syllabizable.rb +24 -0
- data/lib/hiq/version.rb +5 -0
- data/lib/hiq.rb +6 -6
- metadata +10 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f0b32efd71e97bff2d927e2011ba99ba7953473f48e4c1006009109526589777
|
4
|
+
data.tar.gz: bc77a0486a393cbe5dfbce67cd7fbed4b34c7ae74e55a48a370d38fff54ef330
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e52601838f38c0880481672f69884b46a1be9b55779ee86de15fc277cfea20e1e104ca6985c316488c2b3a8a2d3edcb73cf7abc91a25f65c941703e9eae2774b
|
7
|
+
data.tar.gz: ee1910c58e9c0458d281dcf476bcf90b3a74b0cf7797a94e1b2437f5f4fc35aace762db310dd78448167930867cbba411f00134dead04706eb500d49e4342ad0
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Hiq
|
4
|
+
# service responsible for composing a single line of Haiku
|
5
|
+
class LineComposer
|
6
|
+
include Syllabizable
|
7
|
+
|
8
|
+
attr_reader :line, :target_syllable_length
|
9
|
+
|
10
|
+
def self.call(line, target_syllable_length:)
|
11
|
+
new(line, target_syllable_length: target_syllable_length).call
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(line, target_syllable_length:)
|
15
|
+
@line = line
|
16
|
+
@target_syllable_length = target_syllable_length
|
17
|
+
end
|
18
|
+
|
19
|
+
def call
|
20
|
+
return line if syllable_gap_size.zero?
|
21
|
+
|
22
|
+
filled_line
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def syllable_gap_size
|
28
|
+
target_syllable_length - line.map { |word| syllables(word) }.sum
|
29
|
+
end
|
30
|
+
|
31
|
+
def filled_line
|
32
|
+
return [fillers[target_syllable_length].sample] if line.empty?
|
33
|
+
|
34
|
+
[interpolated_fillers[target_syllable_length][syllable_gap_size].gsub('X', line.first)]
|
35
|
+
end
|
36
|
+
|
37
|
+
def interpolated_fillers
|
38
|
+
{
|
39
|
+
5 => {
|
40
|
+
1 => 'the X',
|
41
|
+
2 => 'ghastly X',
|
42
|
+
3 => 'shadow of X',
|
43
|
+
4 => 'X of my joy fade'
|
44
|
+
},
|
45
|
+
7 => {
|
46
|
+
1 => 'a X',
|
47
|
+
2 => 'the pale X',
|
48
|
+
3 => 'X in silence',
|
49
|
+
4 => 'X ends the autumn',
|
50
|
+
5 => 'A spark of X above',
|
51
|
+
6 => 'the sad facade of X torn'
|
52
|
+
}
|
53
|
+
}
|
54
|
+
end
|
55
|
+
|
56
|
+
def fillers
|
57
|
+
{
|
58
|
+
5 => ['empty riverside', 'tears flow in the rain', 'cheerful summer\'s charm'],
|
59
|
+
7 => ['the vain wind of summer blows', 'gentle breeze upon a mountain', 'a frog jumps into a well']
|
60
|
+
}
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Hiq
|
4
|
+
# service for checking whether haiku can be constructed out of those words
|
5
|
+
class WordChecker
|
6
|
+
include Syllabizable
|
7
|
+
|
8
|
+
attr_reader :words
|
9
|
+
|
10
|
+
def self.validate!(words)
|
11
|
+
new(words).validate!
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(words)
|
15
|
+
@words = words
|
16
|
+
end
|
17
|
+
|
18
|
+
def validate!
|
19
|
+
ensure_word_length
|
20
|
+
ensure_word_composition
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def ensure_word_composition
|
26
|
+
raise WordsCompositionError if syllable_count.select { |count| count > 5 }.size > 1
|
27
|
+
raise WordsCompositionError if syllable_count.select { |count| count == 5 }.size > 3
|
28
|
+
end
|
29
|
+
|
30
|
+
def ensure_word_length
|
31
|
+
raise TooManyWordsError if words.size > 3
|
32
|
+
raise WordTooLongError if syllable_count.any? { |count| count > 7 }
|
33
|
+
raise WordsTooLongError if syllable_count.sum > 22
|
34
|
+
end
|
35
|
+
|
36
|
+
def syllable_count
|
37
|
+
@syllable_count ||= words.map { |word| syllables(word) }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Hiq
|
4
|
+
# Basic haiku error
|
5
|
+
class WordLengthError < StandardError
|
6
|
+
end
|
7
|
+
|
8
|
+
# limiting number of words to 3
|
9
|
+
class TooManyWordsError < StandardError
|
10
|
+
def failure_message
|
11
|
+
'The API is currently limited to 3 words'
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
# Exception for when one of the words is too long to fit into haiku
|
16
|
+
class WordTooLongError < WordLengthError
|
17
|
+
def failure_message
|
18
|
+
'At least one of the words provided exceeds 7 syllables - haiku with such word is impossible'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Exception for when word composition is unattainable
|
23
|
+
class WordsCompositionError < WordLengthError
|
24
|
+
def failure_message
|
25
|
+
'The words provided cannot meet the haiku composition requirement due to their length'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Exception for when too many syllables are provided by the user
|
30
|
+
class WordsTooLongError < WordLengthError
|
31
|
+
def failure_message
|
32
|
+
'Words provided exceed 22 syllables - haiku needs to be 22 syllables long'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/hiq/haiku.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Hiq
|
4
|
+
# Haiku generating object
|
5
|
+
class Haiku
|
6
|
+
include Syllabizable
|
7
|
+
|
8
|
+
attr_reader :words
|
9
|
+
|
10
|
+
def initialize(words)
|
11
|
+
@words = words.sort_by(&:size)
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_s
|
15
|
+
haiku_lines.map { |l| l.join(' ') }.join("\n")
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def haiku_lines
|
21
|
+
WordChecker.validate!(words)
|
22
|
+
|
23
|
+
[5, 7, 5].zip(line_templates).map do |syllabes, line|
|
24
|
+
LineComposer.call(line, target_syllable_length: syllabes)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def line_templates
|
29
|
+
lines = [[], [], []]
|
30
|
+
words.each do |word|
|
31
|
+
lines.detect { |l| l.size == lines.map(&:size).min } << word
|
32
|
+
end
|
33
|
+
lines
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Hiq
|
4
|
+
# syllabization functionality
|
5
|
+
module Syllabizable
|
6
|
+
def syllables(word)
|
7
|
+
word.downcase.scan(/[aeiouy]/).size - diphtongs(word) - triphtongs(word) - silent_e_count(word)
|
8
|
+
end
|
9
|
+
|
10
|
+
def diphtongs(word)
|
11
|
+
word.downcase.scan(/(?:[^aeiouy]|^)([aeiouy][aeiouy])(?:[^aeiouy]|$)/).size
|
12
|
+
end
|
13
|
+
|
14
|
+
def triphtongs(word)
|
15
|
+
word.downcase.scan(/(?:[^aeiouy]|^)([aeiouy][aeiouy][aeiouy])([^aeiouy]|$)/).size
|
16
|
+
end
|
17
|
+
|
18
|
+
def silent_e_count(word)
|
19
|
+
return 0 if word == 'the'
|
20
|
+
|
21
|
+
word.downcase[-1] == 'e' ? 1 : 0
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/hiq/version.rb
ADDED
data/lib/hiq.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'version'
|
4
|
-
require 'syllabizable'
|
5
|
-
require 'exceptions'
|
6
|
-
require 'domain/word_checker'
|
7
|
-
require 'domain/line_composer'
|
8
|
-
require 'haiku'
|
3
|
+
require 'hiq/version'
|
4
|
+
require 'hiq/syllabizable'
|
5
|
+
require 'hiq/exceptions'
|
6
|
+
require 'hiq/domain/word_checker'
|
7
|
+
require 'hiq/domain/line_composer'
|
8
|
+
require 'hiq/haiku'
|
9
9
|
|
10
10
|
# haiku main module
|
11
11
|
module Hiq
|
metadata
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hiq
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jan Taras
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
date: 2022-03-20 00:00:00.000000000 Z
|
@@ -87,11 +87,17 @@ extensions: []
|
|
87
87
|
extra_rdoc_files: []
|
88
88
|
files:
|
89
89
|
- lib/hiq.rb
|
90
|
+
- lib/hiq/domain/line_composer.rb
|
91
|
+
- lib/hiq/domain/word_checker.rb
|
92
|
+
- lib/hiq/exceptions.rb
|
93
|
+
- lib/hiq/haiku.rb
|
94
|
+
- lib/hiq/syllabizable.rb
|
95
|
+
- lib/hiq/version.rb
|
90
96
|
homepage: https://github.com/TarasJan/hiq
|
91
97
|
licenses:
|
92
98
|
- MIT
|
93
99
|
metadata: {}
|
94
|
-
post_install_message:
|
100
|
+
post_install_message:
|
95
101
|
rdoc_options: []
|
96
102
|
require_paths:
|
97
103
|
- lib
|
@@ -107,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
107
113
|
version: '0'
|
108
114
|
requirements: []
|
109
115
|
rubygems_version: 3.0.3.1
|
110
|
-
signing_key:
|
116
|
+
signing_key:
|
111
117
|
specification_version: 4
|
112
118
|
summary: Hiq - a Haiku generator
|
113
119
|
test_files: []
|