hiq 0.2.0 → 0.2.3
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/filler_library.rb +39 -0
- data/lib/hiq/domain/line_composer.rb +38 -0
- data/lib/hiq/domain/word_checker.rb +40 -0
- data/lib/hiq/exceptions.rb +35 -0
- data/lib/hiq/haiku.rb +38 -0
- data/lib/hiq/syllabizable.rb +24 -0
- data/lib/hiq/version.rb +5 -0
- data/lib/hiq.rb +7 -6
- metadata +12 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 01f5f0694196e980cd36d86d7c3995d05a72548230a37b513570ee0c5faed998
|
4
|
+
data.tar.gz: b67a7324ae0deb31451a7797ba27af9127ef743669259743a7c7e4a6ef0553ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '0803d7976ba63d812f742c4d400cce9e1b61e182ea93c925023d08ed3b1dad520e653f808ddac05056ac89de994701f0c921f999d8fb2edb607005c19fa77b9b'
|
7
|
+
data.tar.gz: 62b8b18edca28bec99356dfc502c904f9dcb1c9aa125ca4d0fdd3e8e0e8abf0f9117db13a822cf892a926d42b7b8caa876920bfd69faf1a9b8d7902fc51884f7
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Hiq
|
4
|
+
# Provided of filler for lines of haiku
|
5
|
+
class FillerLibrary
|
6
|
+
def self.interpolated_fillers
|
7
|
+
{
|
8
|
+
1 => ['the X', 'a X'],
|
9
|
+
2 => ['ghastly X', 'the pale X'],
|
10
|
+
3 => ['shadow of X', 'X in silence'],
|
11
|
+
4 => ['X of my joy fade', 'X ends the autumn'],
|
12
|
+
5 => ['A spark of X above'],
|
13
|
+
6 => ['the sad facade of X torn']
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.fillers
|
18
|
+
{
|
19
|
+
5 => ['empty riverside', 'tears flow in the rain', 'cheerful summer\'s charm'],
|
20
|
+
7 => ['the vain wind of summer blows', 'gentle breeze upon a mountain', 'a frog jumps into a well']
|
21
|
+
}
|
22
|
+
end
|
23
|
+
|
24
|
+
attr_reader :ifillers, :cfillers
|
25
|
+
|
26
|
+
def initialize
|
27
|
+
@ifillers = FillerLibrary.interpolated_fillers
|
28
|
+
@cfillers = FillerLibrary.fillers
|
29
|
+
end
|
30
|
+
|
31
|
+
def get_const_filler(filler_length)
|
32
|
+
cfillers[filler_length].delete(cfillers[filler_length].sample)
|
33
|
+
end
|
34
|
+
|
35
|
+
def get_interpolated_filler(filler_length)
|
36
|
+
ifillers[filler_length].delete(ifillers[filler_length].sample)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,38 @@
|
|
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, :library
|
9
|
+
|
10
|
+
def self.call(line, target_syllable_length:, library:)
|
11
|
+
new(line, target_syllable_length: target_syllable_length, library: library).call
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(line, target_syllable_length:, library:)
|
15
|
+
@line = line
|
16
|
+
@target_syllable_length = target_syllable_length
|
17
|
+
@library = library
|
18
|
+
end
|
19
|
+
|
20
|
+
def call
|
21
|
+
return line if syllable_gap_size.zero?
|
22
|
+
|
23
|
+
filled_line
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def syllable_gap_size
|
29
|
+
target_syllable_length - line.map { |word| syllables(word) }.sum
|
30
|
+
end
|
31
|
+
|
32
|
+
def filled_line
|
33
|
+
return [library.get_const_filler(target_syllable_length)] if line.empty?
|
34
|
+
|
35
|
+
[library.get_interpolated_filler(syllable_gap_size).gsub('X', line.first)]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
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,38 @@
|
|
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
|
+
library = FillerLibrary.new
|
24
|
+
|
25
|
+
[5, 7, 5].zip(line_templates).map do |syllabes, line|
|
26
|
+
LineComposer.call(line, target_syllable_length: syllabes, library: library)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def line_templates
|
31
|
+
lines = [[], [], []]
|
32
|
+
words.each do |word|
|
33
|
+
lines.detect { |l| l.size == lines.map(&:size).min } << word
|
34
|
+
end
|
35
|
+
lines
|
36
|
+
end
|
37
|
+
end
|
38
|
+
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,12 @@
|
|
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/
|
8
|
-
require '
|
3
|
+
require 'hiq/version'
|
4
|
+
require 'hiq/syllabizable'
|
5
|
+
require 'hiq/exceptions'
|
6
|
+
require 'hiq/domain/word_checker'
|
7
|
+
require 'hiq/domain/filler_library'
|
8
|
+
require 'hiq/domain/line_composer'
|
9
|
+
require 'hiq/haiku'
|
9
10
|
|
10
11
|
# haiku main module
|
11
12
|
module Hiq
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jan Taras
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-03-
|
11
|
+
date: 2022-03-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: byebug
|
@@ -87,11 +87,18 @@ extensions: []
|
|
87
87
|
extra_rdoc_files: []
|
88
88
|
files:
|
89
89
|
- lib/hiq.rb
|
90
|
+
- lib/hiq/domain/filler_library.rb
|
91
|
+
- lib/hiq/domain/line_composer.rb
|
92
|
+
- lib/hiq/domain/word_checker.rb
|
93
|
+
- lib/hiq/exceptions.rb
|
94
|
+
- lib/hiq/haiku.rb
|
95
|
+
- lib/hiq/syllabizable.rb
|
96
|
+
- lib/hiq/version.rb
|
90
97
|
homepage: https://github.com/TarasJan/hiq
|
91
98
|
licenses:
|
92
99
|
- MIT
|
93
100
|
metadata: {}
|
94
|
-
post_install_message:
|
101
|
+
post_install_message:
|
95
102
|
rdoc_options: []
|
96
103
|
require_paths:
|
97
104
|
- lib
|
@@ -107,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
107
114
|
version: '0'
|
108
115
|
requirements: []
|
109
116
|
rubygems_version: 3.0.3.1
|
110
|
-
signing_key:
|
117
|
+
signing_key:
|
111
118
|
specification_version: 4
|
112
119
|
summary: Hiq - a Haiku generator
|
113
120
|
test_files: []
|