markov-rb 0.1.2 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 43f0df95ad9d0cb2ff124309b03f6e43ca0b7154
4
- data.tar.gz: 04a1e05951dad32b6b492bbdd416776cc03296b6
3
+ metadata.gz: beb0322e06d4010dae371a3cd34c164ce95ef01b
4
+ data.tar.gz: 41c6c2364bb409bd3203e7f4e2d14f5a83678dfe
5
5
  SHA512:
6
- metadata.gz: d5d942cea36addfe1066390b1422849a388f4a4783c206c009aa069212fcea59f859fbc6ee1b07dd4d57ca923020537ac864ccd5e7618b2cecbb145dc41bbf21
7
- data.tar.gz: 829adcdf8c3d877d683d94eb8c7f9df32889546189cf2d51f18509d976e2290d4216052cf824ad4db026ecf1671ee59219f7e39a420322f890411ef777b9e464
6
+ metadata.gz: 44495e7e61d8e00bec997938378e401d66f735a5637079e127efd240a8091b87693edfa78c3118798ed9a8adfe8be027534f6767c72d0ef27b18f5c0ad3d9818
7
+ data.tar.gz: 0784cbaf902d855161d3132d10b4d5e1273a0eb905bd683a6ded5f3f224a1db471505d095830253466bc01a75321050943df942c79ed4d5211d58da402be3bc0
File without changes
File without changes
@@ -1,7 +1,35 @@
1
1
  require 'ruby-progressbar'
2
+ require 'oj'
2
3
 
3
4
  require 'markov/version'
4
5
  require 'markov/transition_matrix'
5
6
  require 'markov/statistics/random_event'
6
7
 
7
8
  require 'markov/chain'
9
+
10
+ module Markov
11
+ def self.generate(thing)
12
+ chain_for(thing).generate_word!.strip
13
+ end
14
+
15
+ private
16
+ def self.chains
17
+ @chains ||= {}
18
+ end
19
+
20
+ def self.chain_for(thing)
21
+ chains[thing] ||= load_chain(thing)
22
+ end
23
+
24
+ def self.load_chain(thing)
25
+ analysis_file = File.join(
26
+ File.expand_path(File.dirname(__FILE__)),
27
+ '..',
28
+ 'data',
29
+ "#{thing}.json"
30
+ )
31
+
32
+ chain_data = File.read(analysis_file)
33
+ Oj.load(chain_data)
34
+ end
35
+ end
@@ -72,15 +72,7 @@ module Markov
72
72
  word = nil
73
73
 
74
74
  until max && (generated_count +=1)>=max
75
- if new_words
76
- word = @words.first
77
- until !@words.include?(word.chomp) && word.chomp.match(/^[A-Z][a-z]+$/)
78
- word = (generate_word!) #.chomp #(context: generated_text)
79
- end
80
- @words.push(word.chomp)
81
- else
82
- word = generate_word!(context: generated_text)
83
- end
75
+ word = generate_word!(context: new_words ? ["\n"] : generated_text)
84
76
 
85
77
  if show
86
78
  word.chars.each do |ch|
@@ -97,10 +89,24 @@ module Markov
97
89
  end
98
90
 
99
91
  def generate_word!(context: ["\n"])
92
+ if new_words
93
+ word = @words.first
94
+ until !@words.include?(word.chomp) && word.chomp.match(/^[A-Z][a-z]+$/)
95
+ word = (generate_any_word!) #.chomp #(context: generated_text)
96
+ end
97
+ @words.push(word.chomp)
98
+ word
99
+ else
100
+ generate_any_word!(context: context) #generated_text)
101
+ end
102
+ end
103
+
104
+ private
105
+ def generate_any_word!(context: ["\n"])
100
106
  generated_chars = []
101
107
  char = ''
102
108
  until char.match(/[ .,;!?\n]/)
103
- char = predict_next_state(context+generated_chars)
109
+ char = predict_next_state(context+generated_chars)
104
110
  generated_chars.push(char)
105
111
  end
106
112
  generated_chars.join
@@ -1,4 +1,4 @@
1
1
  module Markov
2
2
  # markov version
3
- VERSION = "0.1.2"
3
+ VERSION = "0.1.3"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: markov-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Weissman
@@ -203,8 +203,8 @@ files:
203
203
  - bin/markov
204
204
  - data/architecture.json
205
205
  - data/architecture.txt
206
- - data/cities.json
207
206
  - data/cities.txt
207
+ - data/city.json
208
208
  - data/frankenstein.txt
209
209
  - data/iliad.txt
210
210
  - data/jabberwocky.json
@@ -216,7 +216,7 @@ files:
216
216
  - data/metamorphosis.txt
217
217
  - data/milton.txt
218
218
  - data/moby-dick.txt
219
- - data/names.json
219
+ - data/name.json
220
220
  - data/names.txt
221
221
  - data/phil.txt
222
222
  - data/shakespeare.txt