lita-markov-blabber 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,12 +1,12 @@
1
- require "lita"
1
+ require 'lita'
2
2
 
3
3
  Lita.load_locales Dir[File.expand_path(
4
- File.join("..", "..", "locales", "*.yml"), __FILE__
4
+ File.join('..', '..', 'locales', '*.yml'), __FILE__
5
5
  )]
6
6
 
7
- require "lita/handlers/markov_blabber"
7
+ require 'lita/handlers/markov_blabber'
8
8
 
9
9
  Lita::Handlers::MarkovBlabber.template_root File.expand_path(
10
- File.join("..", "..", "templates"),
11
- __FILE__
10
+ File.join('..', '..', 'templates'),
11
+ __FILE__
12
12
  )
@@ -1,30 +1,28 @@
1
1
  require 'lita/markov_brain'
2
+ require 'pry'
2
3
 
3
4
  module Lita
4
5
  module Handlers
5
6
  class MarkovBlabber < Handler
7
+ DEFAULT_INPUTS_PATH = File.join __dir__, '..', '..', '..', 'dict'
6
8
 
7
- on :loaded, :on_loaded
8
- on :unhandled_message, :blabber
9
+ config :markov_inputs_path, default: DEFAULT_INPUTS_PATH
9
10
 
10
- def on_loaded(payload)
11
- brain.load_dictionaries
12
- end
11
+ on :unhandled_message, :blabber
13
12
 
14
- def brain
15
- self.class.brain
13
+ def blabber(payload)
14
+ payload.fetch(:message).reply gibberish
16
15
  end
17
16
 
18
- def blabber(payload)
17
+ def gibberish
19
18
  n = rand(5..20)
20
19
  gibberish = brain.generate_n_words n
21
-
22
- payload.fetch(:message).reply gibberish
23
20
  end
24
21
 
25
- # Save a class-wide
26
- def self.brain
27
- @brain ||= Lita::MarkovBrain.new
22
+ private
23
+
24
+ def brain
25
+ @@brain ||= Lita::MarkovBrain.new(inputs_path: config.markov_inputs_path)
28
26
  end
29
27
 
30
28
  Lita.register_handler(self)
@@ -1,31 +1,40 @@
1
1
  require 'marky_markov'
2
2
 
3
3
  class Lita::MarkovBrain
4
- def initialize
5
- @dictionary ||= ::MarkyMarkov::TemporaryDictionary.new
6
- end
4
+ NoBrainInputsFound = Class.new(StandardError)
7
5
 
8
- attr_reader :dictionary
6
+ def initialize(inputs_path:)
7
+ @inputs_path = File.absolute_path(inputs_path)
8
+ @dictionary = ::MarkyMarkov::TemporaryDictionary.new
9
+ load_brain!
10
+ end
9
11
 
10
- def inputs_path
11
- File.join __dir__, '..', '..', 'dict', '*.txt'
12
+ def generate_n_words(n)
13
+ dictionary.generate_n_words(n)
12
14
  end
13
15
 
14
- def load_dictionaries
15
- Dir[inputs_path].each do |file|
16
+ private
17
+
18
+ attr_reader :dictionary, :inputs_path
19
+
20
+ def load_brain!
21
+ return unless dictionary.dictionary.empty?
22
+ text_files_path = inputs_path + '/*.txt'
23
+
24
+ files = Dir[text_files_path]
25
+ raise NoBrainInputsFound, "No markov input files found at [#{text_files_path}]" if files.none?
26
+
27
+ Dir[text_files_path].each do |file|
16
28
  load_dictionary(file)
17
29
  end
18
30
  end
19
31
 
32
+
20
33
  def load_dictionary(path)
21
34
  logger.debug "Loading Markov input text at: [#{path}]"
22
35
  dictionary.parse_file path
23
36
  end
24
37
 
25
- def generate_n_words(n)
26
- dictionary.generate_n_words(n)
27
- end
28
-
29
38
  def logger
30
39
  Lita.logger
31
40
  end
@@ -1,25 +1,25 @@
1
1
  Gem::Specification.new do |spec|
2
- spec.name = "lita-markov-blabber"
3
- spec.version = "0.2.0"
4
- spec.authors = ["Daniel J. Pritchett"]
5
- spec.email = ["dpritchett@gmail.com"]
6
- spec.description = "Nonsensical nearly-human fallback responses for your lita bot"
7
- spec.summary = "Nonsensical nearly-human fallback responses for your lita bot"
8
- spec.homepage = "https://github.com/dpritchett/lita-markov-blabber"
9
- spec.license = "MIT"
10
- spec.metadata = { "lita_plugin_type" => "handler" }
2
+ spec.name = 'lita-markov-blabber'
3
+ spec.version = '0.3.0'
4
+ spec.authors = ['Daniel J. Pritchett']
5
+ spec.email = ['dpritchett@gmail.com']
6
+ spec.description = 'Nonsensical nearly-human fallback responses for your lita bot'
7
+ spec.summary = 'Nonsensical nearly-human fallback responses for your lita bot'
8
+ spec.homepage = 'https://github.com/dpritchett/lita-markov-blabber'
9
+ spec.license = 'MIT'
10
+ spec.metadata = { 'lita_plugin_type' => 'handler' }
11
11
 
12
- spec.files = `git ls-files`.split($/)
12
+ spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
13
13
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
14
14
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
- spec.require_paths = ["lib"]
15
+ spec.require_paths = ['lib']
16
16
 
17
- spec.add_runtime_dependency "lita", ">= 4.7"
18
- spec.add_runtime_dependency "marky_markov", "~> 0.3.5"
17
+ spec.add_runtime_dependency 'lita', '>= 4.7'
18
+ spec.add_runtime_dependency 'marky_markov', '~> 0.3.5'
19
19
 
20
- spec.add_development_dependency "bundler", "~> 1.3"
21
- spec.add_development_dependency "pry-byebug"
22
- spec.add_development_dependency "rake"
23
- spec.add_development_dependency "rack-test"
24
- spec.add_development_dependency "rspec", ">= 3.0.0"
20
+ spec.add_development_dependency 'bundler', '~> 1.3'
21
+ spec.add_development_dependency 'pry-byebug'
22
+ spec.add_development_dependency 'rack-test'
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'rspec', '>= 3.0.0'
25
25
  end
@@ -0,0 +1,100 @@
1
+ reveries—stand that man on his legs, set his feet a-going, and he will
2
+ infallibly lead you to water, if water there be in all that region.
3
+ Should you ever be athirst in the great American desert, try this
4
+ experiment, if your caravan happen to be supplied with a metaphysical
5
+ professor. Yes, as every one knows, meditation and water are wedded for
6
+ ever.
7
+
8
+ But here is an artist. He desires to paint you the dreamiest, shadiest,
9
+ quietest, most enchanting bit of romantic landscape in all the valley
10
+ of the Saco. What is the chief element he employs? There stand his
11
+ trees, each with a hollow trunk, as if a hermit and a crucifix were
12
+ within; and here sleeps his meadow, and there sleep his cattle; and up
13
+ from yonder cottage goes a sleepy smoke. Deep into distant woodlands
14
+ winds a mazy way, reaching to overlapping spurs of mountains bathed in
15
+ their hill-side blue. But though the picture lies thus tranced, and
16
+ though this pine-tree shakes down its sighs like leaves upon this
17
+ shepherd’s head, yet all were vain, unless the shepherd’s eye were
18
+ fixed upon the magic stream before him. Go visit the Prairies in June,
19
+ when for scores on scores of miles you wade knee-deep among
20
+ Tiger-lilies—what is the one charm wanting?—Water—there is not a drop
21
+ of water there! Were Niagara but a cataract of sand, would you travel
22
+ your thousand miles to see it? Why did the poor poet of Tennessee, upon
23
+ suddenly receiving two handfuls of silver, deliberate whether to buy
24
+ him a coat, which he sadly needed, or invest his money in a pedestrian
25
+ trip to Rockaway Beach? Why is almost every robust healthy boy with a
26
+ robust healthy soul in him, at some time or other crazy to go to sea?
27
+ Why upon your first voyage as a passenger, did you yourself feel such a
28
+ mystical vibration, when first told that you and your ship were now out
29
+ of sight of land? Why did the old Persians hold the sea holy? Why did
30
+ the Greeks give it a separate deity, and own brother of Jove? Surely
31
+ all this is not without meaning. And still deeper the meaning of that
32
+ story of Narcissus, who because he could not grasp the tormenting, mild
33
+ image he saw in the fountain, plunged into it and was drowned. But that
34
+ same image, we ourselves see in all rivers and oceans. It is the image
35
+ of the ungraspable phantom of life; and this is the key to it all.
36
+
37
+ Now, when I say that I am in the habit of going to sea whenever I begin
38
+ to grow hazy about the eyes, and begin to be over conscious of my
39
+ lungs, I do not mean to have it inferred that I ever go to sea as a
40
+ passenger. For to go as a passenger you must needs have a purse, and a
41
+ purse is but a rag unless you have something in it. Besides, passengers
42
+ get sea-sick—grow quarrelsome—don’t sleep of nights—do not enjoy
43
+ themselves much, as a general thing;—no, I never go as a passenger;
44
+ nor, though I am something of a salt, do I ever go to sea as a
45
+ Commodore, or a Captain, or a Cook. I abandon the glory and distinction
46
+ of such offices to those who like them. For my part, I abominate all
47
+ honorable respectable toils, trials, and tribulations of every kind
48
+ whatsoever. It is quite as much as I can do to take care of myself,
49
+ without taking care of ships, barques, brigs, schooners, and what not.
50
+ And as for going as cook,—though I confess there is considerable glory
51
+ in that, a cook being a sort of officer on ship-board—yet, somehow, I
52
+ never fancied broiling fowls;—though once broiled, judiciously
53
+ buttered, and judgmatically salted and peppered, there is no one who
54
+ will speak more respectfully, not to say reverentially, of a broiled
55
+ fowl than I will. It is out of the idolatrous dotings of the old
56
+ Egyptians upon broiled ibis and roasted river horse, that you see the
57
+ mummies of those creatures in their huge bake-houses the pyramids.
58
+
59
+ No, when I go to sea, I go as a simple sailor, right before the mast,
60
+ plumb down into the forecastle, aloft there to the royal mast-head.
61
+ True, they rather order me about some, and make me jump from spar to
62
+ spar, like a grasshopper in a May meadow. And at first, this sort of
63
+ thing is unpleasant enough. It touches one’s sense of honor,
64
+ particularly if you come of an old established family in the land, the
65
+ Van Rensselaers, or Randolphs, or Hardicanutes. And more than all, if
66
+ just previous to putting your hand into the tar-pot, you have been
67
+ lording it as a country schoolmaster, making the tallest boys stand in
68
+ awe of you. The transition is a keen one, I assure you, from a
69
+ schoolmaster to a sailor, and requires a strong decoction of Seneca and
70
+ the Stoics to enable you to grin and bear it. But even this wears off
71
+ in time.
72
+
73
+ What of it, if some old hunks of a sea-captain orders me to get a broom
74
+ and sweep down the decks? What does that indignity amount to, weighed,
75
+ I mean, in the scales of the New Testament? Do you think the archangel
76
+ Gabriel thinks anything the less of me, because I promptly and
77
+ respectfully obey that old hunks in that particular instance? Who ain’t
78
+ a slave? Tell me that. Well, then, however the old sea-captains may
79
+ order me about—however they may thump and punch me about, I have the
80
+ satisfaction of knowing that it is all right; that everybody else is
81
+ one way or other served in much the same way—either in a physical or
82
+ metaphysical point of view, that is; and so the universal thump is
83
+ passed round, and all hands should rub each other’s shoulder-blades,
84
+ and be content.
85
+
86
+ Again, I always go to sea as a sailor, because they make a point of
87
+ paying me for my trouble, whereas they never pay passengers a single
88
+ penny that I ever heard of. On the contrary, passengers themselves must
89
+ pay. And there is all the difference in the world between paying and
90
+ being paid. The act of paying is perhaps the most uncomfortable
91
+ infliction that the two orchard thieves entailed upon us. But _being
92
+ paid_,—what will compare with it? The urbane activity with which a man
93
+ receives money is really marvellous, considering that we so earnestly
94
+ believe money to be the root of all earthly ills, and that on no
95
+ account can a monied man enter heaven. Ah! how cheerfully we consign
96
+ ourselves to perdition!
97
+
98
+ Finally, I always go to sea as a sailor, because of the wholesome
99
+ exercise and pure air of the fore-castle deck. For as in this world,
100
+ head winds are far more prevalent than winds from astern (that is, if
@@ -1,19 +1,31 @@
1
- require "spec_helper"
1
+ require 'spec_helper'
2
2
 
3
3
  describe Lita::Handlers::MarkovBlabber, lita_handler: true do
4
+ let(:test_inputs_path) { File.join(__dir__, '..', '..', 'dict') }
5
+
4
6
  let(:robot) { Lita::Robot.new(registry) }
5
7
 
8
+ before do
9
+ robot.config.handlers.markov_blabber.markov_inputs_path = test_inputs_path
10
+ end
11
+
6
12
  subject { described_class.new(robot) }
7
13
 
8
- describe ':blabber' do
9
- it 'responds with a caption and an image URL' do
10
- send_message "this isn't matched"
11
- response = replies.last
12
- word_count = response.split.count
14
+ describe ':gibberish' do
15
+ it 'generates lots of words' do
16
+ result = subject.gibberish
17
+ word_count = result.split.count
13
18
  expect(word_count > 4).to be_truthy
14
19
  expect(word_count < 30).to be_truthy
15
20
  end
21
+ end
22
+
23
+ describe 'preload_brain' do
24
+ it "fills the brain's dictionary with words" do
25
+ end
26
+ end
16
27
 
28
+ describe 'blabber' do
17
29
  it 'answers arbitrary inputs' do
18
30
  lyrics = ['welcome to the jungle',
19
31
  'take me down to the paradise city',
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Lita::MarkovBrain do
4
+ let(:test_inputs_path) { File.join(__dir__, '..', '..', 'dict') }
5
+
6
+ describe 'given a short file full of words' do
7
+ subject { Lita::MarkovBrain.new(inputs_path: test_inputs_path) }
8
+
9
+ it "can generate n words on demand" do
10
+ n = rand(1..100)
11
+ result = subject.generate_n_words(n)
12
+
13
+ expect(result.split.count).to eq(n)
14
+ end
15
+
16
+ end
17
+
18
+ end
@@ -1,5 +1,5 @@
1
- require "lita-markov-blabber"
2
- require "lita/rspec"
1
+ require 'lita-markov-blabber'
2
+ require 'lita/rspec'
3
3
 
4
4
  # A compatibility mode is provided for older plugins upgrading from Lita 3. Since this plugin
5
5
  # was generated with Lita 4, the compatibility mode should be left disabled.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-markov-blabber
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel J. Pritchett
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-20 00:00:00.000000000 Z
11
+ date: 2018-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lita
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: rake
70
+ name: rack-test
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
@@ -81,7 +81,7 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: rack-test
84
+ name: rake
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
@@ -120,12 +120,15 @@ files:
120
120
  - README.md
121
121
  - Rakefile
122
122
  - dict/2701-0.txt
123
+ - dict/gutenberg-moby-dick.txt
123
124
  - lib/lita-markov-blabber.rb
124
125
  - lib/lita/handlers/markov_blabber.rb
125
126
  - lib/lita/markov_brain.rb
126
127
  - lita-markov-blabber.gemspec
127
128
  - locales/en.yml
129
+ - spec/dict/moby_dick_sample.txt
128
130
  - spec/lita/handlers/markov_blabber_spec.rb
131
+ - spec/lita/markov_brain_spec.rb
129
132
  - spec/spec_helper.rb
130
133
  - templates/.gitkeep
131
134
  homepage: https://github.com/dpritchett/lita-markov-blabber
@@ -154,5 +157,7 @@ signing_key:
154
157
  specification_version: 4
155
158
  summary: Nonsensical nearly-human fallback responses for your lita bot
156
159
  test_files:
160
+ - spec/dict/moby_dick_sample.txt
157
161
  - spec/lita/handlers/markov_blabber_spec.rb
162
+ - spec/lita/markov_brain_spec.rb
158
163
  - spec/spec_helper.rb