markov_words 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.travis.yml +5 -0
- data/.yardopts +1 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +30 -0
- data/LICENSE.txt +21 -0
- data/README.md +113 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/markov_words/version.rb +4 -0
- data/lib/markov_words.rb +228 -0
- data/markov_words.gemspec +29 -0
- metadata +130 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dc71ce66384d32bf99139082bb0f2fbab72ee52f
|
4
|
+
data.tar.gz: fbec0f216daf93b08b00ff81a2c42f9abb3f5a57
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 265076226e5499b53c74e85fc52189d5e919de3bb6974e592b1640a1812ddf7e2c002ad4abe82b86fb60d4c5041aa608c0d588d62359c565ca4cb980c2b6e60f
|
7
|
+
data.tar.gz: 72cf232ed14d019465451d769f04d9c67c4a6f1ac992a6acdf23d1b2f3adf10d569c3ba8f92c57895fde94cb53e6138d999ccbfeef40bf8ddda2521c49577527
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--markup markdown
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at dmerand@explo.org. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
markov_words (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
coderay (1.1.2)
|
10
|
+
method_source (0.9.0)
|
11
|
+
minitest (5.10.3)
|
12
|
+
pry (0.11.3)
|
13
|
+
coderay (~> 1.1.0)
|
14
|
+
method_source (~> 0.9.0)
|
15
|
+
rake (10.5.0)
|
16
|
+
yard (0.9.12)
|
17
|
+
|
18
|
+
PLATFORMS
|
19
|
+
ruby
|
20
|
+
|
21
|
+
DEPENDENCIES
|
22
|
+
bundler (~> 1.16)
|
23
|
+
markov_words!
|
24
|
+
minitest (~> 5.0)
|
25
|
+
pry (~> 0.11)
|
26
|
+
rake (~> 10.0)
|
27
|
+
yard (~> 0.6)
|
28
|
+
|
29
|
+
BUNDLED WITH
|
30
|
+
1.16.0
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Exploration School
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
# MarkovWords
|
2
|
+
|
3
|
+
At [EXPLO](https://www.explo.org), we often have a need for specific vocabulary-generators. For example, we might want to make a [password generator](http://lab.explo.org/password), or a Harry Potter house-generator, or some such thing.
|
4
|
+
|
5
|
+
As it turns out, [Markov Chains](http://www.thagomizer.com/blog/2017/11/07/markov-models.html) are an excellent way to create specific vocabularies by "training" a model against a set of words to determine common combinations.
|
6
|
+
|
7
|
+
While there are [quite](https://github.com/dabrorius/markov-noodles) a [few](https://github.com/dabrorius/markov-noodles) [wonderful](https://github.com/imikimi/literate_randomizer) Ruby libraries that do this, they all focus either on _actual_ English words, or on creating random _sentences_ but not words. We created this library to do the same thing, but with words, hence the name `MarkovWords`.
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'markov_words'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install markov_words
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
Basic usage is as follows:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
require 'markov_words'
|
33
|
+
|
34
|
+
words = MarkovWords::Words.new
|
35
|
+
# returns a random word
|
36
|
+
puts words.word
|
37
|
+
```
|
38
|
+
|
39
|
+
You might prefer using a number of n-grams (letter combinations being tracked) higher than the default number (which is 2). We've found that the higher you go, the more accurate words tend to sound, at the expense of having to generate a much larger database of n-gram => letter correspondences. In the case of the default `/usr/share/dict` file, `gram_size = 1` yields a roughly `3.2Kb` database size; `gram_size = 2` yields `18Kb`. Once you get up to `gram_size = 8`, you're into ~`30MB` territory, which slows things down a bit.
|
40
|
+
|
41
|
+
To set gram_size:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
words = MarkovWords::words.new(gram_size: 8)
|
45
|
+
# Will take a while the first time, while the database is created.
|
46
|
+
puts words.word
|
47
|
+
```
|
48
|
+
|
49
|
+
### Dictionary
|
50
|
+
|
51
|
+
By default, `MarkovWords` will use the system dictionary located (on Macs) in `/usr/share/dict/words`. You can change this setting:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
# eg to generate random proper names instead of English-sounding words.
|
55
|
+
words = MarkovWords::Words.new(corpus_file: '/usr/share/dict/propernames')
|
56
|
+
```
|
57
|
+
|
58
|
+
This is pretty great, because it means that if you have a dictionary to emulate, you can make words that sound like anything!
|
59
|
+
|
60
|
+
### Data Storage
|
61
|
+
|
62
|
+
`MarkovWords` stores its database of n-gram concurrences in `Marshal`'ed text files on disk and loads it into memory when necessary. You can control the location of the data file with:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
# eg to store the data in /tmp/markov.data
|
66
|
+
words = MarkovWords::Words.new(data_file: /tmp/markov.data)
|
67
|
+
```
|
68
|
+
|
69
|
+
### Caching
|
70
|
+
|
71
|
+
Because calculation can get slow, especially at high n-gram sizess, `MarkovWords` will cache 100 words into a `words_ngramsize.cache` file by default. If you want to control caching, you can adjust caching parameters eg:
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
# For no caching whatsoever
|
75
|
+
words = MarkovWords::Words.new(cache: false)
|
76
|
+
|
77
|
+
# To change the number of pre-computed/stored words to 1000:
|
78
|
+
words = MarkovWords::Words.new(cache_size: 1000)
|
79
|
+
|
80
|
+
# To change the location of the cache file to /tmp/markov.cache:
|
81
|
+
words = MarkovWords::Words.new(cache_file: '/tmp/markov.cache')
|
82
|
+
|
83
|
+
# Of course, options can be combined:
|
84
|
+
words = MarkovWords::Words.new(
|
85
|
+
cache_file: '/tmp/markov.cache',
|
86
|
+
cache_size: 1000
|
87
|
+
)
|
88
|
+
```
|
89
|
+
|
90
|
+
Lastly, you can "top off" the cache to make sure it's full with:
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
words = MarkovWords::Words.new
|
94
|
+
words.refresh_cache
|
95
|
+
```
|
96
|
+
|
97
|
+
## Development
|
98
|
+
|
99
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
100
|
+
|
101
|
+
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).
|
102
|
+
|
103
|
+
## Contributing
|
104
|
+
|
105
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/markov_words. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
106
|
+
|
107
|
+
## License
|
108
|
+
|
109
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
110
|
+
|
111
|
+
## Code of Conduct
|
112
|
+
|
113
|
+
Everyone interacting in the MarkovWords project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/exploration/markov_words/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "markov_words"
|
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__)
|
data/bin/setup
ADDED
data/lib/markov_words.rb
ADDED
@@ -0,0 +1,228 @@
|
|
1
|
+
require "markov_words/version"
|
2
|
+
require 'securerandom'
|
3
|
+
|
4
|
+
# @author Donald L. Merand
|
5
|
+
# A nice library for generating random words (not sentences) using Markov
|
6
|
+
# chains.
|
7
|
+
module MarkovWords
|
8
|
+
|
9
|
+
# This class takes care of word generation, caching, and data storage.
|
10
|
+
class Words
|
11
|
+
# Perform caching? Defaults to true.
|
12
|
+
attr :cache
|
13
|
+
# File location where you want to store the cache
|
14
|
+
attr :cache_file
|
15
|
+
# How many words you want to store in the cache?
|
16
|
+
attr :cache_size
|
17
|
+
# Your dictionary of words. Defaults to /usr/share/dict/words.
|
18
|
+
attr :corpus_file
|
19
|
+
# Where should your database be stored on disk?
|
20
|
+
attr :data_file
|
21
|
+
# The database of "grams" (word/count combinations), stored on the disk and
|
22
|
+
# loaded into this variable in memory when generating words.
|
23
|
+
attr :grams
|
24
|
+
# Number of n-grams to compute for your database. Defaults to 2
|
25
|
+
attr :gram_size
|
26
|
+
# Max generated word length. Defaults to 16
|
27
|
+
attr :max_length
|
28
|
+
# Minimum generated word length. Defaults to 3. NOTE: If your corpus size
|
29
|
+
# is very small (<1000 words or so), it's hard to guarantee a min_length
|
30
|
+
# because so many n-grams will have no association, which terminates word
|
31
|
+
# generation.
|
32
|
+
attr :min_length
|
33
|
+
|
34
|
+
# Create a new "Words" object
|
35
|
+
# @param opts [Hash] options sent to the object. Any of the object
|
36
|
+
# attributes (eg `:cache_file` or `:gram_size`) are valid parameters to
|
37
|
+
# add to the `opts` hash.
|
38
|
+
# @return [Words] A `MarkovWords::Words` object.
|
39
|
+
def initialize(opts = {})
|
40
|
+
@gram_size = opts.fetch :gram_size, 2
|
41
|
+
@max_length = opts.fetch :max_length, 16
|
42
|
+
@min_length = opts.fetch :min_length, 3
|
43
|
+
|
44
|
+
@cache = opts.fetch :cache, true
|
45
|
+
@cache_file = opts.fetch :cache_file,
|
46
|
+
"tmp/markov_words_#{@gram_size}.cache"
|
47
|
+
@cache_size = opts.fetch :cache_size, 70
|
48
|
+
@corpus_file = opts.fetch :corpus_file,
|
49
|
+
'/usr/share/dict/words'
|
50
|
+
@data_file = opts.fetch :data_file,
|
51
|
+
"tmp/markov_words_#{@gram_size}.data"
|
52
|
+
@grams = nil
|
53
|
+
end
|
54
|
+
|
55
|
+
# "Top off" the cache of stored words, and ensure that it's at
|
56
|
+
# `@cache_size`. If `@cache` is set to `false`, returns an empty array.
|
57
|
+
# @return [Array<String>] All words in the cache.
|
58
|
+
def refresh_cache
|
59
|
+
if @cache
|
60
|
+
words_array = load_from_file(@cache_file) || []
|
61
|
+
|
62
|
+
while words_array.length < @cache_size
|
63
|
+
words_array << generate_word
|
64
|
+
end
|
65
|
+
|
66
|
+
save_to_file(@cache_file, words_array)
|
67
|
+
words_array
|
68
|
+
else
|
69
|
+
[]
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# Generate a new word, or return one from the cache if available.
|
74
|
+
# @return [String] The word.
|
75
|
+
def word
|
76
|
+
if @cache
|
77
|
+
load_word_from_cache
|
78
|
+
else
|
79
|
+
generate_word
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
def contains_vowel?(ary)
|
86
|
+
if ary.length < 2
|
87
|
+
true
|
88
|
+
else
|
89
|
+
ary.take(2).join.match(/[aeiou]/)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
# Generates an English (by default)- sounding word.
|
94
|
+
def generate_word
|
95
|
+
set_grams if @grams.nil?
|
96
|
+
|
97
|
+
gram = ''
|
98
|
+
gram_array = []
|
99
|
+
|
100
|
+
# The word must be a random length, between @min and @max
|
101
|
+
desired_length = 0
|
102
|
+
until desired_length >= @min_length
|
103
|
+
desired_length = SecureRandom.rand(@max_length)
|
104
|
+
end
|
105
|
+
|
106
|
+
# Set initial array of chars, which is taken from the @grams key list. must
|
107
|
+
# contain a vowel in the first 2 chars (unless @gram_size == 1 in which
|
108
|
+
# case any letter).
|
109
|
+
all_grams_array = @grams.to_a
|
110
|
+
gram_min_length = @gram_size < @min_length ? @gram_size : @min_length
|
111
|
+
until gram_array.length >= gram_min_length && contains_vowel?(gram_array)
|
112
|
+
gram_array = all_grams_array.sample[0].chars
|
113
|
+
end
|
114
|
+
|
115
|
+
until gram_array.join.length == desired_length || gram.nil?
|
116
|
+
# grab last @gram_size (or possibly fewer if the array is too small)
|
117
|
+
# elements from the current gram_array, to use as the next key.
|
118
|
+
gal = gram_array.length
|
119
|
+
current_gram_size = gal >= @gram_size ? @gram_size : gal
|
120
|
+
key = gram_array[-current_gram_size..-1].join
|
121
|
+
|
122
|
+
gram = pick_random_char(@grams[key])
|
123
|
+
gram_array << gram
|
124
|
+
end
|
125
|
+
|
126
|
+
gram_array.join
|
127
|
+
end
|
128
|
+
|
129
|
+
def generate_words_array
|
130
|
+
@cache_size.times.map { generate_word }
|
131
|
+
end
|
132
|
+
|
133
|
+
def load_from_file(file)
|
134
|
+
result = nil
|
135
|
+
if File.exist?(file)
|
136
|
+
File.open(file, 'r') {|f| result = Marshal.load(f)}
|
137
|
+
end
|
138
|
+
result
|
139
|
+
end
|
140
|
+
|
141
|
+
def load_word_from_cache
|
142
|
+
words_array = load_from_file(@cache_file)
|
143
|
+
if words_array.nil? || words_array.empty?
|
144
|
+
words_array = generate_words_array
|
145
|
+
end
|
146
|
+
|
147
|
+
word = words_array.pop
|
148
|
+
save_to_file(@cache_file, words_array)
|
149
|
+
|
150
|
+
word
|
151
|
+
end
|
152
|
+
|
153
|
+
# Generate a MarkovWords corpus from a datafile, with a given size of n-gram.
|
154
|
+
# Returns a hash of "grams", which are a map of a letter to the frequency of
|
155
|
+
# the letters that follow it, eg: {"c" => {"a" => 1, "b" => 2}}
|
156
|
+
def markov_corpus(file, gram_size)
|
157
|
+
grams = {}
|
158
|
+
|
159
|
+
# Corpus contains a list of words, separated by newlines
|
160
|
+
File.foreach(file) do |word|
|
161
|
+
word = word.downcase.gsub(/-/, '')
|
162
|
+
gram_size.downto(1) do |current_size|
|
163
|
+
word.chars.each_cons(current_size + 1) do |gram|
|
164
|
+
first = gram[0..current_size - 1].join
|
165
|
+
second = gram[current_size]
|
166
|
+
|
167
|
+
unless first.empty? || second.empty? || is_line_ending?(second)
|
168
|
+
update_count(grams, first, second)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
grams
|
175
|
+
end
|
176
|
+
|
177
|
+
# Given a hash in the format: {"c" => {"a" => 1, "b" => 2}}, grab a random
|
178
|
+
# element from the values hash, accurate to the distribution of counts.
|
179
|
+
# In the example hash above, "a" would have a 33% chance of being chosen,
|
180
|
+
# while "b" would have a 66% chance (1/2 ratio).
|
181
|
+
def pick_random_char(counts_hash = {})
|
182
|
+
if counts_hash.nil?
|
183
|
+
return nil
|
184
|
+
else
|
185
|
+
total = counts_hash.values.sum
|
186
|
+
pick_num = SecureRandom.rand(total)
|
187
|
+
counter = 0
|
188
|
+
counts_hash.each do |char, count|
|
189
|
+
counter += count
|
190
|
+
return char if counter >= pick_num
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
def is_line_ending?(word)
|
196
|
+
word.include?("\n")
|
197
|
+
end
|
198
|
+
|
199
|
+
# Marshal a Ruby object to file storage
|
200
|
+
def save_to_file(file, data)
|
201
|
+
File.open(file, 'wb') {|f| Marshal.dump(data, f)}
|
202
|
+
end
|
203
|
+
|
204
|
+
def set_grams
|
205
|
+
if File.exist? @data_file
|
206
|
+
@grams = load_from_file(@data_file)
|
207
|
+
else
|
208
|
+
@grams = markov_corpus(@corpus_file, @gram_size)
|
209
|
+
save_to_file(@data_file, @grams)
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
# Given a @grams entry, update the count of "second" in "first"
|
214
|
+
#
|
215
|
+
# Example:
|
216
|
+
# update_count({"a" => {"b" => 1}}, "a", "b")
|
217
|
+
# => {"a" => {"b" => 2}}
|
218
|
+
def update_count(grams, first, second)
|
219
|
+
grams[first] = {} if grams[first].nil?
|
220
|
+
if grams[first][second].nil?
|
221
|
+
grams[first][second] = 1
|
222
|
+
else
|
223
|
+
grams[first][second] += 1
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
end
|
228
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "markov_words/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "markov_words"
|
8
|
+
spec.version = MarkovWords::VERSION
|
9
|
+
spec.authors = ["Donald Merand"]
|
10
|
+
spec.email = ["dmerand@explo.org"]
|
11
|
+
|
12
|
+
spec.summary = %q{Generate words (not sentences) using Markov-chain techniques.}
|
13
|
+
spec.description = %q{It's often nice to have random English-sounding words, eg. for password generators. This library uses Markov-chain techniques on words, as opposed to many others which focus on sentences. }
|
14
|
+
spec.homepage = "https://github.com/exploration/markov_words"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
27
|
+
spec.add_development_dependency "pry", "~> 0.11"
|
28
|
+
spec.add_development_dependency "yard", "~> 0.6"
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: markov_words
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Donald Merand
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-12-06 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: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.11'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.11'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: yard
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.6'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.6'
|
83
|
+
description: 'It''s often nice to have random English-sounding words, eg. for password
|
84
|
+
generators. This library uses Markov-chain techniques on words, as opposed to many
|
85
|
+
others which focus on sentences. '
|
86
|
+
email:
|
87
|
+
- dmerand@explo.org
|
88
|
+
executables: []
|
89
|
+
extensions: []
|
90
|
+
extra_rdoc_files: []
|
91
|
+
files:
|
92
|
+
- ".gitignore"
|
93
|
+
- ".travis.yml"
|
94
|
+
- ".yardopts"
|
95
|
+
- CODE_OF_CONDUCT.md
|
96
|
+
- Gemfile
|
97
|
+
- Gemfile.lock
|
98
|
+
- LICENSE.txt
|
99
|
+
- README.md
|
100
|
+
- Rakefile
|
101
|
+
- bin/console
|
102
|
+
- bin/setup
|
103
|
+
- lib/markov_words.rb
|
104
|
+
- lib/markov_words/version.rb
|
105
|
+
- markov_words.gemspec
|
106
|
+
homepage: https://github.com/exploration/markov_words
|
107
|
+
licenses:
|
108
|
+
- MIT
|
109
|
+
metadata: {}
|
110
|
+
post_install_message:
|
111
|
+
rdoc_options: []
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
requirements: []
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 2.6.13
|
127
|
+
signing_key:
|
128
|
+
specification_version: 4
|
129
|
+
summary: Generate words (not sentences) using Markov-chain techniques.
|
130
|
+
test_files: []
|