wilco 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
+ SHA1:
3
+ metadata.gz: 4abe85f8ee956fb31c5921eb690491c6c334d8db
4
+ data.tar.gz: 6ece65e0205e83fe3d2182576230ac35324a6f63
5
+ SHA512:
6
+ metadata.gz: 182f7f6608020497e9bb13c06c21afcbaa39047924db36df33ab8b8e7be902364fdced52eadb1e9af1e82650188cef6e5622a42d65a5aa86954da27daa2ae709
7
+ data.tar.gz: 8b0cd3723a398f49dbe718327c9e67e6ba8dd68e41903b72a2fe603449e2d6770db6cec53bed00d0ae3dc0be72d126fdcd40c00ac905d84aeae342d5f75ce91c
@@ -0,0 +1 @@
1
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,29 @@
1
+ # https://github.com/bbatsov/rubocop/blob/master/config/default.yml
2
+ AllCops:
3
+ TargetRubyVersion: 2.3
4
+
5
+ # Don't check binstubs or gemfile
6
+ Exclude:
7
+ - 'bin/**/*'
8
+ - 'Gemfile'
9
+
10
+ # Don't restrict length of modules/files
11
+ Metrics/ModuleLength:
12
+ Enabled: false
13
+
14
+ # Allow braces for blocks that mainly return a value, including RSpec lets and
15
+ # subjects
16
+ Style/BlockDelimiters:
17
+ EnforcedStyle: semantic
18
+
19
+ # Don't require a class comment
20
+ Style/Documentation:
21
+ Enabled: false
22
+
23
+ # Always use double-quoted string literals
24
+ Style/StringLiterals:
25
+ EnforcedStyle: double_quotes
26
+
27
+ # Require trailing commas for multiline arrays/hashes
28
+ Style/TrailingCommaInLiteral:
29
+ EnforcedStyleForMultiline: comma
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+ source 'https://rubygems.org'
3
+
4
+ ruby '2.3.1'
5
+
6
+ gemspec
7
+
8
+ gem 'rspec'
9
+ gem 'pry'
10
+ gem 'highline'
11
+
12
+ gem 'rack'
13
+ gem 'puma'
14
+ gem 'facebook-messenger'
15
+ gem 'redis'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Josh Justice
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 @@
1
+ web: bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}
@@ -0,0 +1,38 @@
1
+ # Wilco
2
+
3
+ A Ruby conversation engine for powering natural-language conversations, for example in chatbots.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'wilco'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install wilco
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bundle install` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ 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).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/wilco.
34
+
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+ require "bundler/gem_tasks"
3
+ task default: :spec
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
4
+ require 'sample'
5
+ require 'highline'
6
+
7
+ bot = Sample::Chatbot.new
8
+ cli = HighLine.new
9
+
10
+ class FakeStore
11
+ def initialize
12
+ @hash = {}
13
+ end
14
+
15
+ def set(key, value)
16
+ @hash[key] = value
17
+ end
18
+
19
+ def get(key)
20
+ @hash[key]
21
+ end
22
+ end
23
+ store = FakeStore.new
24
+
25
+ message = cli.ask(bot.greet)
26
+ until message == ""
27
+ response = nil
28
+ # store and load data to simulate server
29
+ Sample::Chatbot.persisted(store, "sender_id") do |bot2|
30
+ response = bot2.respond_to_message(message)
31
+ end
32
+
33
+ message = cli.ask(response)
34
+ end
35
+ puts bot.goodbye
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "wilco"
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
data/bot.rb ADDED
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+ require "facebook/messenger"
3
+ require "redis"
4
+ require_relative "lib/sample"
5
+
6
+ include Facebook::Messenger
7
+
8
+ Facebook::Messenger.configure do |config|
9
+ config.access_token = ENV["FACEBOOK_ACCESS_TOKEN"]
10
+ config.app_secret = ENV["FACEBOOK_APP_SECRET"]
11
+ config.verify_token = ENV["FACEBOOK_VERIFY_TOKEN"]
12
+ end
13
+
14
+ Facebook::Messenger::Thread.set(
15
+ setting_type: "greeting",
16
+ greeting: { text: Sample::Chatbot.new.greet }
17
+ )
18
+
19
+ Facebook::Messenger::Subscriptions.subscribe
20
+
21
+ redis = Redis.new
22
+
23
+ Bot.on :message do |message|
24
+ request = message.text
25
+
26
+ response = nil
27
+ Sample::Chatbot.persisted(redis, message.sender["id"]) do |chatbot|
28
+ response = chatbot.respond_to_message(request)
29
+ end
30
+
31
+ Bot.deliver(
32
+ recipient: message.sender,
33
+ message: { text: response }
34
+ )
35
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ require "facebook/messenger"
3
+ require_relative "bot"
4
+
5
+ run Facebook::Messenger::Server
File without changes
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ require "wilco/version"
3
+ require "wilco/chatbot"
4
+ require "wilco/command"
5
+ require "wilco/parser"
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+ require "pry"
3
+ require "yaml"
4
+
5
+ module Wilco
6
+ class Chatbot
7
+ def self.persisted(store, id)
8
+ bot = new.load_from(store, id)
9
+ yield(bot)
10
+ bot.save_to(store, id)
11
+ end
12
+
13
+ def initialize(options = {})
14
+ @commands = options.fetch(:commands)
15
+ @messages = options.fetch(:messages)
16
+ @parser = options.fetch(:parser, Parser.new)
17
+ end
18
+
19
+ def greet
20
+ "#{t(:greeting)} #{t(:command_prompt)} #{t(:suggestions)}"
21
+ end
22
+
23
+ def goodbye
24
+ t(:valediction)
25
+ end
26
+
27
+ def respond_to_message(message)
28
+ if current_command
29
+ identify_param(message)
30
+ else
31
+ identify_command(message)
32
+ end
33
+ end
34
+
35
+ def save_to(store, id)
36
+ serialized_command = YAML.dump(current_command)
37
+ store.set(serialization_key(id), serialized_command)
38
+ end
39
+
40
+ def load_from(store, id)
41
+ serialized_command = store.get(serialization_key(id))
42
+ @current_command = YAML.load(serialized_command) if serialized_command
43
+ self
44
+ end
45
+
46
+ private
47
+
48
+ attr_reader :commands, :messages, :current_command, :parser
49
+
50
+ def t(key)
51
+ messages[key]
52
+ end
53
+
54
+ def serialization_key(id)
55
+ "intent_chatbot_#{id}"
56
+ end
57
+
58
+ def identify_command(message)
59
+ if (@current_command = command_for(message))
60
+ respond_to_current_command_state
61
+ else
62
+ did_not_understand_command
63
+ end
64
+ end
65
+
66
+ def did_not_understand_command
67
+ "#{t(:did_not_understand)} #{t(:command_prompt)} #{t(:suggestions)}"
68
+ end
69
+
70
+ def did_not_understand_param
71
+ "#{t(:did_not_understand)} " \
72
+ "#{current_command.request_first_missing_param}"
73
+ end
74
+
75
+ def identify_param(message)
76
+ if current_command.extract_param(message)
77
+ respond_to_current_command_state
78
+ elsif (param_name = current_command.find_param_to_switch_to(message))
79
+ current_command.request_param(param_name)
80
+ else
81
+ did_not_understand_param
82
+ end
83
+ end
84
+
85
+ def respond_to_current_command_state
86
+ if current_command.missing_params?
87
+ current_command.request_first_missing_param
88
+ else
89
+ response = current_command.execute
90
+ @current_command = nil
91
+ "#{response} #{t(:next_command_prompt)}"
92
+ end
93
+ end
94
+
95
+ def command_for(message)
96
+ command = command_instances.max_by { |c| c.likelihood(message) }
97
+ command.parse_message(message) if command.likelihood(message).positive?
98
+ end
99
+
100
+ def command_instances
101
+ @command_instances ||= commands.map { |command_class|
102
+ command_class.new(parser)
103
+ }
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+ module Wilco
3
+ class Command
4
+ def initialize(parser)
5
+ @parser = parser
6
+ end
7
+
8
+ def likelihood(message)
9
+ @likelihood = parser.command_likelihood(tokens, message)
10
+ end
11
+
12
+ def parse_message(message)
13
+ @param_values = parser.extract_params(tokens, message)
14
+ self
15
+ end
16
+
17
+ def missing_params?
18
+ !first_missing_param.nil?
19
+ end
20
+
21
+ def request_param(new_param)
22
+ @current_param = new_param
23
+ switch_param_message
24
+ end
25
+
26
+ def request_first_missing_param
27
+ @current_param = first_missing_param
28
+ missing_param_message
29
+ end
30
+
31
+ def extract_param(message)
32
+ param = params[current_param] # TODO: rename to param name
33
+ new_param_values = parser.extract_params([param], message)
34
+ param_values.merge!(new_param_values) if new_param_values[param[:name]]
35
+ end
36
+
37
+ def find_param_to_switch_to(message)
38
+ parser.find_param_name(tokens, message)
39
+ end
40
+
41
+ def execute
42
+ confirmation_message
43
+ end
44
+
45
+ private
46
+
47
+ attr_reader :parser, :param_values, :current_param
48
+
49
+ def first_missing_param
50
+ param_values.keys.find { |key| param_values[key].nil? }
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+ require "pry"
3
+
4
+ module Wilco
5
+ class Parser
6
+ def command_likelihood(tokens, message)
7
+ words = tokenize_message(message)
8
+ static_tokens(tokens).count do |token|
9
+ match_static_token_in_message(token, words)
10
+ end
11
+ end
12
+
13
+ def extract_params(tokens, message)
14
+ params = {}
15
+ words = tokenize_message(message)
16
+ param_tokens(tokens).each do |token|
17
+ params[token[:name]] = extract_param_from_message(token, words)
18
+ end
19
+ params
20
+ end
21
+
22
+ def find_param_name(tokens, message)
23
+ words = tokenize_message(message)
24
+ token = param_tokens(tokens).find { |t|
25
+ extract_param_name_from_message(t, words)
26
+ }
27
+ token[:name] if token
28
+ end
29
+
30
+ private
31
+
32
+ attr_accessor :params
33
+
34
+ def static_tokens(tokens)
35
+ tokens.select { |t| t.is_a?(Array) }
36
+ end
37
+
38
+ def param_tokens(tokens)
39
+ tokens.select { |t| t.is_a?(Hash) }
40
+ end
41
+
42
+ def tokenize_message(message)
43
+ simplify_string(message.strip).split(/\s+/)
44
+ end
45
+
46
+ def simplify_string(string)
47
+ string.downcase.gsub(/[^\w\s]+/, "")
48
+ end
49
+
50
+ def match_static_token_in_message(token, words)
51
+ token.each do |token_option|
52
+ return words if words.include?(token_option)
53
+ end
54
+ false
55
+ end
56
+
57
+ def extract_param_from_message(token, words)
58
+ value = token[:values].find { |synonyms|
59
+ words_include_a_synonym?(words, synonyms)
60
+ }
61
+ value[0] if value
62
+ end
63
+
64
+ def extract_param_name_from_message(token, words)
65
+ words.include?(token[:name].to_s) # TODO: store as a string originally
66
+ end
67
+
68
+ def words_include_a_synonym?(words, synonyms)
69
+ synonyms.any? { |synonym| words.include?(simplify_string(synonym)) }
70
+ end
71
+
72
+ def set_param(token, value)
73
+ params[token[:name]] = value
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module Wilco
3
+ VERSION = "0.1.0"
4
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+ # coding: utf-8
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "wilco/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "wilco"
9
+ spec.version = Wilco::VERSION
10
+ spec.authors = ["Josh Justice"]
11
+ spec.email = ["me@codingitwrong.com"]
12
+
13
+ spec.summary = "A Ruby conversation engine"
14
+ spec.homepage = "https://github.com/CodingItWrong/wilco"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`
18
+ .split("\x0")
19
+ .reject { |f| f.match(%r{^(sample|spec)/}) }
20
+ spec.bindir = "bin"
21
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.12"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wilco
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Josh Justice
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-08-12 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.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
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
+ description:
42
+ email:
43
+ - me@codingitwrong.com
44
+ executables:
45
+ - chat
46
+ - console
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".gitignore"
51
+ - ".rspec"
52
+ - ".rubocop.yml"
53
+ - Gemfile
54
+ - LICENSE.txt
55
+ - Procfile
56
+ - README.md
57
+ - Rakefile
58
+ - bin/chat
59
+ - bin/console
60
+ - bot.rb
61
+ - config.ru
62
+ - lib/.gitkeep
63
+ - lib/wilco.rb
64
+ - lib/wilco/chatbot.rb
65
+ - lib/wilco/command.rb
66
+ - lib/wilco/parser.rb
67
+ - lib/wilco/version.rb
68
+ - wilco.gemspec
69
+ homepage: https://github.com/CodingItWrong/wilco
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.5.1
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: A Ruby conversation engine
93
+ test_files: []