ruffini 1.0.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/ruffini.rb +83 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6519cb513cd013150c56ca7fa91443125d05b96b
4
+ data.tar.gz: f2f81a4cae431c0bc0db05c5faadd119257d01b6
5
+ SHA512:
6
+ metadata.gz: 473fef94858087638e273c1ae13d10f064da156c029af784942e3cb10c82d1a780aacc56980ff338ef8ced96dc420e93ced2bd78520a0d9a673078188bcb571a
7
+ data.tar.gz: 9eb2286d6be30d4b06be96b0d5a5ec20d87e4f7e174be12575f9ce41e8a89c0ca18a0d9aa803a07898a559c41e4acb02c29720eef213ea5f3d5927b5fee11651
data/lib/ruffini.rb ADDED
@@ -0,0 +1,83 @@
1
+ require 'zlib'
2
+
3
+ ##
4
+ # Module containing the Markov chain generator class.
5
+ module Ruffini
6
+ VERSION = '1.0.0'
7
+
8
+ ##
9
+ # This class provides a markov chain text generator.
10
+ class Markov
11
+ ##
12
+ # Creates a new markov text generator with a given +depth+.
13
+ #
14
+ # If no +depth+ is provided, the constructor defaults to a depth of 2.
15
+ #
16
+ # If a +filename+ is provided, the constructor reads the dictionary from a file.
17
+ def initialize(depth = 2, filename = nil)
18
+ @DEPTH = depth
19
+ if filename.nil?
20
+ @store = {}
21
+ else
22
+ @store = Marshal.load(Zlib::Inflate.inflate(File.read(filename)))
23
+ end
24
+ end
25
+
26
+ ##
27
+ # Parses a given +string+ and adds its words to the markov dictionary.
28
+ def parse!(string)
29
+ words = string.split.map(&:downcase)
30
+ for index in 0 .. (words.length - (@DEPTH + 1))
31
+ if @store[ words[index..index+(@DEPTH - 1)] ].nil?
32
+ @store[ words[index..index+(@DEPTH - 1)] ] = [words[index+@DEPTH]]
33
+ else
34
+ @store[ words[index..index+(@DEPTH - 1)] ].push words[index+@DEPTH]
35
+ end
36
+ end
37
+ return @store
38
+ end
39
+
40
+ ##
41
+ # Convenience method to parse an entire file by +filename+.
42
+ def parse_file!(filename)
43
+ self.parse!(File.read(filename))
44
+ end
45
+
46
+ ##
47
+ # Generates +count+ words using the dictionary, optionally starting from the last
48
+ # n words of +start+, if +start+ is provided (accepts Array or String).
49
+ def generate(count, start = nil)
50
+ output = []
51
+
52
+ if !start.nil?
53
+ if start.class == String
54
+ key = start.downcase.split.last(@DEPTH)
55
+ else
56
+ key = start.last(@DEPTH)
57
+ end
58
+ else
59
+ key = @store.keys.shuffle.first
60
+ end
61
+
62
+ key.map { |word| output.push word }
63
+
64
+ count.times {
65
+ if @store[key].nil?
66
+ else
67
+ word = @store[key].shuffle.first
68
+ output.push word
69
+ key = key.last(@DEPTH - 1).push word
70
+ end
71
+ }
72
+
73
+ output.join(" ")
74
+ end
75
+
76
+ ##
77
+ # Serializes and compresses the markov dictionary to +filename+.
78
+ # The file is stored as a zlib-compressed, marshalled Hash.
79
+ def save!(filename)
80
+ File.write(filename, Zlib::Deflate.deflate(Marshal.dump(@store)))
81
+ end
82
+ end
83
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruffini
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Rose
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-01-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: minimalist markov text library with a high degree of user control
14
+ email: rose@lain.org.uk
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/ruffini.rb
20
+ homepage: https://github.com/ralsei/ruffini
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.6.14
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: minimalist markov text library
44
+ test_files: []