wordz 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d9398d7849bb37dc06bb92379aa7307a0485982b336b36c25533b6b056b54ae4
4
+ data.tar.gz: 16fe44f437cddd6474217c28c75c779af9ecea2134e03bcf4ef25ce52e50a9d4
5
+ SHA512:
6
+ metadata.gz: 4eed3849b297e853e4a684fdcc6b04411161456b82575d5a0c908497d85d994b4d06ecd9c1bb5f2ca5669926e751c00ee1056602388d2af4816480a33219cb5a
7
+ data.tar.gz: 956f2313746a6ee3de6c10fdcb261739d0aa8252481ad57fef4ced2a8e4be0c7908fff10d49d975f3b29d62ddde1f4d7f5c7b4dc442276de00af489fe2143ea0
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ .rspec_status
11
+ Gemfile.lock
12
+ .ruby-version
13
+ .ruby-gemset
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Michael Hoffman
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.
@@ -0,0 +1,3 @@
1
+ # Wordz
2
+
3
+ A minimalist generative grammar library. For use in bots and other mischief.
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "wordz"
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(__FILE__)
@@ -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
@@ -0,0 +1,9 @@
1
+ require "wordz/version"
2
+
3
+ WORDZ_APP_PATH = File.expand_path("../", __FILE__) + "/wordz"
4
+
5
+ module Wordz
6
+ autoload(:PostProcessor, "#{WORDZ_APP_PATH}/post_processor.rb")
7
+ end
8
+
9
+ Dir["#{WORDZ_APP_PATH}/**/*.rb"].each { |file| require file }
@@ -0,0 +1,92 @@
1
+ require "json"
2
+
3
+ module Wordz
4
+ class Generator
5
+ include PostProcessor
6
+
7
+ attr_reader :subjects, :output, :grammar
8
+
9
+ ROOT_NODE = "<root>".freeze
10
+
11
+ def initialize(grammar = {}, subjects: {})
12
+ @output = []
13
+ @subjects = subjects
14
+ @grammar = grammar
15
+ end
16
+
17
+ def generate(root_node = ROOT_NODE)
18
+ post_process(generate_list(root_node))
19
+ end
20
+
21
+ private
22
+
23
+ def build_instruction(str)
24
+ Wordz::Instruction.new(str)
25
+ end
26
+
27
+ def generate_list(key)
28
+ instruction_lists = grammar[key]
29
+
30
+ if instruction_lists
31
+ instruction_lists.sample.each(&method(:evaluate_instruction))
32
+ output
33
+ else
34
+ raise UndefinedMacroError, "Grammar does not contain key #{key}."
35
+ end
36
+ end
37
+
38
+ def evaluate_instruction(instruction_text)
39
+ instruction = build_instruction(instruction_text)
40
+
41
+ if instruction.macro?
42
+ evaluate_macro(instruction)
43
+ elsif instruction.method?
44
+ evaluate_method(instruction)
45
+ else
46
+ evaluate_literal(instruction)
47
+ end
48
+ end
49
+
50
+ def evaluate_literal(instruction)
51
+ with_probability(instruction) do
52
+ output << instruction.content
53
+ end
54
+ end
55
+
56
+ def evaluate_macro(instruction)
57
+ with_probability(instruction) do
58
+ generate_list(instruction.content)
59
+ end
60
+ end
61
+
62
+ def evaluate_method(instruction)
63
+ with_probability(instruction) do
64
+ receiver = extract_method_receiver(instruction)
65
+ method_name = extract_method_name(instruction)
66
+ output << receiver.send(method_name)
67
+ end
68
+ end
69
+
70
+ def extract_method_receiver(method_instruction)
71
+ receiver_name = spit_method_instruction(method_instruction).first.to_sym
72
+ subjects[receiver_name] || raise(
73
+ UndefinedReceiverError, "No subject specified for #{receiver_name.inspect}."
74
+ )
75
+ end
76
+
77
+ def extract_method_name(method_instruction)
78
+ spit_method_instruction(method_instruction)[1]
79
+ end
80
+
81
+ def spit_method_instruction(method_instruction)
82
+ method_instruction.content.split("#").reject(&:empty?)
83
+ end
84
+
85
+ def with_probability(instruction)
86
+ yield if instruction.probability >= Kernel.rand
87
+ end
88
+
89
+ class UndefinedReceiverError < RuntimeError; end
90
+ class UndefinedMacroError < RuntimeError; end
91
+ end
92
+ end
@@ -0,0 +1,40 @@
1
+ module Wordz
2
+ class Instruction
3
+ attr_reader :probability, :content
4
+
5
+ PROBABILITY_PATTERN = /(\|0\.[0-9]+)/
6
+ MACRO_PATTERN = /^<.+>$/
7
+ METHOD_PATTERN = /^\#.+\#.+\#$/
8
+
9
+ def initialize(str)
10
+ @content = extract_content(str)
11
+ @probability = extract_probability(str)
12
+ end
13
+
14
+ def macro?
15
+ content.match?(MACRO_PATTERN)
16
+ end
17
+
18
+ def method?
19
+ content.match?(METHOD_PATTERN)
20
+ end
21
+
22
+ private
23
+
24
+ def extract_probability(str)
25
+ if prob_match_data(str)
26
+ prob_match_data(str).captures.last.delete("|")
27
+ else
28
+ 1
29
+ end.to_f
30
+ end
31
+
32
+ def extract_content(str)
33
+ prob_match_data(str) ? prob_match_data(str).pre_match : str
34
+ end
35
+
36
+ def prob_match_data(str)
37
+ PROBABILITY_PATTERN.match(str)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,37 @@
1
+ module Wordz
2
+ module PostProcessor
3
+ NO_SPACE_BEFORE = %w(. , ! ?).freeze
4
+ POST_PROCESSED_PHRASES = {
5
+ "$INDEF_ARTICLE".freeze => :indefinite_article,
6
+ }.freeze
7
+
8
+ private
9
+
10
+ def post_process(phrase_list)
11
+ sentence_join(replace_ppps(phrase_list))
12
+ end
13
+
14
+ def indefinite_article(phrase_list, i)
15
+ next_phrase = phrase_list[i + 1]
16
+ vowel_initial?(next_phrase) ? "an" : "a"
17
+ end
18
+
19
+ def replace_ppps(phrase_list)
20
+ phrase_list.each_with_index.map do |phrase, i|
21
+ method_name = POST_PROCESSED_PHRASES[phrase]
22
+ method_name ? send(method_name, phrase_list, i) : phrase
23
+ end
24
+ end
25
+
26
+ def sentence_join(phrase_list)
27
+ phrase_list.reduce("") do |accum_text, phrase|
28
+ prefix = NO_SPACE_BEFORE.include?(phrase) ? "" : " "
29
+ accum_text << (prefix + phrase)
30
+ end.strip.squeeze(" ")
31
+ end
32
+
33
+ def vowel_initial?(str)
34
+ str[0] =~ /[aeiou]/i
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module Wordz
2
+ VERSION = "0.1.0".freeze
3
+ end
@@ -0,0 +1,23 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "wordz/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "wordz"
7
+ spec.version = Wordz::VERSION
8
+ spec.authors = ["Michael Hoffman"]
9
+ spec.email = ["michael.s.hoffman@gmail.com"]
10
+
11
+ spec.summary = "A minimalist generative grammar library."
12
+ spec.homepage = "https://github.com/hoffm/wordz"
13
+ spec.license = "MIT"
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
+
20
+ spec.add_development_dependency "bundler", "~> 1.16"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "rspec", "~> 3.0"
23
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wordz
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Hoffman
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-12-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - michael.s.hoffman@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/wordz.rb
71
+ - lib/wordz/generator.rb
72
+ - lib/wordz/instruction.rb
73
+ - lib/wordz/post_processor.rb
74
+ - lib/wordz/version.rb
75
+ - wordz.gemspec
76
+ homepage: https://github.com/hoffm/wordz
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.7.3
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: A minimalist generative grammar library.
100
+ test_files: []