cathy 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.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.standard.yml +3 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +21 -0
- data/README.md +97 -0
- data/Rakefile +10 -0
- data/cathy.gemspec +43 -0
- data/examples/chatty_cathy.rb +83 -0
- data/lib/cathy/version.rb +5 -0
- data/lib/cathy.rb +178 -0
- data/sig/cathy.rbs +4 -0
- metadata +63 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 9425cecd1a8abc352a76ef5b5ec49d3b65ac6c76f9b8dea21b951e4878bf21b6
|
|
4
|
+
data.tar.gz: 64f5cc0e99a06355c793e965feac34f8bdfc3acdb6bcdd9160f8810815533bd5
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 7a030833f9b6002e6e08535906cd70bb38e6e829fb746b90ebd9935a948063a8b4f7ae579ea321fcb81ace712ace13a1090f62746666c7f8cbcb4292442dc47e
|
|
7
|
+
data.tar.gz: 7fce4ce32cee23ecf651a66fe17d27c69e1390f9898fcba40fa5383437d190cff1ef68554b6228f71be10703df097128ada237b836f7ffe0ad80eefc9c2e8eeb
|
data/.rspec
ADDED
data/.standard.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
|
6
|
+
|
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
|
8
|
+
|
|
9
|
+
## Our Standards
|
|
10
|
+
|
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
|
12
|
+
|
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
|
18
|
+
|
|
19
|
+
Examples of unacceptable behavior include:
|
|
20
|
+
|
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
|
22
|
+
advances of any kind
|
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
24
|
+
* Public or private harassment
|
|
25
|
+
* Publishing others' private information, such as a physical or email
|
|
26
|
+
address, without their explicit permission
|
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
28
|
+
professional setting
|
|
29
|
+
|
|
30
|
+
## Enforcement Responsibilities
|
|
31
|
+
|
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
|
33
|
+
|
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
|
35
|
+
|
|
36
|
+
## Scope
|
|
37
|
+
|
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
|
39
|
+
|
|
40
|
+
## Enforcement
|
|
41
|
+
|
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at dfhougthon@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
|
43
|
+
|
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
|
45
|
+
|
|
46
|
+
## Enforcement Guidelines
|
|
47
|
+
|
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
|
49
|
+
|
|
50
|
+
### 1. Correction
|
|
51
|
+
|
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
|
53
|
+
|
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
|
55
|
+
|
|
56
|
+
### 2. Warning
|
|
57
|
+
|
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
|
59
|
+
|
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
|
61
|
+
|
|
62
|
+
### 3. Temporary Ban
|
|
63
|
+
|
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
|
65
|
+
|
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
|
67
|
+
|
|
68
|
+
### 4. Permanent Ban
|
|
69
|
+
|
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
|
71
|
+
|
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
|
73
|
+
|
|
74
|
+
## Attribution
|
|
75
|
+
|
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
|
78
|
+
|
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
|
80
|
+
|
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
|
82
|
+
|
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 dfhoughton
|
|
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,97 @@
|
|
|
1
|
+
# Cathy
|
|
2
|
+
|
|
3
|
+
`Cathy` facilitates modeling a weighted probability distribution from which one can draw a sample. Say you
|
|
4
|
+
want to pick a realistic weather forecast and you know there's one sunny day for every two cloudy days and one
|
|
5
|
+
rainy day for every two sunny days. `Cathy` will let you model this. But more importantly, `Cathy` will let you
|
|
6
|
+
modify the probabilities on the fly (if you don't need an updatable distribution, I suggest you try
|
|
7
|
+
[`pick_me_too`](https://github.com/dfhoughton/pick_me_too), which is more efficient in this case).
|
|
8
|
+
|
|
9
|
+
## Synopsis
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
require 'cathy'
|
|
13
|
+
|
|
14
|
+
# make a new distribution
|
|
15
|
+
cathy = Cathy.new
|
|
16
|
+
|
|
17
|
+
# teach cathy some things
|
|
18
|
+
10.times { cathy.add :foo }
|
|
19
|
+
5.times { cathy.add :bar }
|
|
20
|
+
2.times { cathy.add :plugh }
|
|
21
|
+
|
|
22
|
+
# ask cathy for a sample
|
|
23
|
+
10.times.map { cathy.pick } # => [:foo, :foo, :bar, :foo, :foo, :foo, :foo, :foo, :plugh, :foo]
|
|
24
|
+
|
|
25
|
+
# teach cathy more things!
|
|
26
|
+
20.times { cathy.add :plugh }
|
|
27
|
+
|
|
28
|
+
# another sample
|
|
29
|
+
10.times.map { cathy.pick } # => [:plugh, :plugh, :foo, :bar, :foo, :bar, :plugh, :plugh, :foo, :plugh]
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Installation
|
|
33
|
+
|
|
34
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
35
|
+
|
|
36
|
+
$ bundle add cathy
|
|
37
|
+
|
|
38
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
39
|
+
|
|
40
|
+
$ gem install cathy
|
|
41
|
+
|
|
42
|
+
## Usage
|
|
43
|
+
|
|
44
|
+
See [here](https://github.com/dfhoughton/cathy/blob/main/examples/chatty_cathy.rb) for an example.
|
|
45
|
+
|
|
46
|
+
### `Cathy#new`
|
|
47
|
+
|
|
48
|
+
Make an updatable probability distribution. In this state if you ask for a sample it will only give you `nil`.
|
|
49
|
+
|
|
50
|
+
### `Cathy#add(thing)`
|
|
51
|
+
|
|
52
|
+
Increase the frequency of `thing` by one.
|
|
53
|
+
|
|
54
|
+
### `Cathy#pick`
|
|
55
|
+
|
|
56
|
+
Draw a sample from the distribution given the frequencies seen so far.
|
|
57
|
+
|
|
58
|
+
### `Cathy#counts`
|
|
59
|
+
|
|
60
|
+
Returns a map from things to their frequencies.
|
|
61
|
+
|
|
62
|
+
This is basically a serialization method.
|
|
63
|
+
|
|
64
|
+
### `Cathy.from_counts`
|
|
65
|
+
|
|
66
|
+
Build a distribution from a map from things to their frequencies.
|
|
67
|
+
|
|
68
|
+
This is basically a deserialization method.
|
|
69
|
+
|
|
70
|
+
## Contributing
|
|
71
|
+
|
|
72
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cathy. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/cathy/blob/main/CODE_OF_CONDUCT.md).
|
|
73
|
+
|
|
74
|
+
## License
|
|
75
|
+
|
|
76
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
77
|
+
|
|
78
|
+
## Code of Conduct
|
|
79
|
+
|
|
80
|
+
Everyone interacting in the Cathy project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/cathy/blob/main/CODE_OF_CONDUCT.md).
|
|
81
|
+
|
|
82
|
+
## Efficiency
|
|
83
|
+
|
|
84
|
+
My aim in writing this has been to be lean on memory usage and to use as few mathematical operations and fetches from memory as possible
|
|
85
|
+
when picking things. The underlying data structure is a [heap](https://en.wikipedia.org/wiki/Heap_(data_structure)), which allows drawing
|
|
86
|
+
a sample in sub-logarithmic time in the typical case using only integer subtraction, and comparison. Updating is similarly efficient.
|
|
87
|
+
|
|
88
|
+
## Name
|
|
89
|
+
|
|
90
|
+
Cathy is named after [Chatty Cathy](https://en.wikipedia.org/wiki/Chatty_Cathy). Why? Because the only time I wanted to have
|
|
91
|
+
this was when I made a chat bot for my kids to play with.
|
|
92
|
+
|
|
93
|
+
Also, naming is hard.
|
|
94
|
+
|
|
95
|
+
## Acknowledgements
|
|
96
|
+
|
|
97
|
+
My wife Paula and son Jude have been exceedingly tolerant of my messing about with this silliness. Also, my co-workers at [Green River](https://www.greenriver.com/) have humored me.
|
data/Rakefile
ADDED
data/cathy.gemspec
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "lib/cathy/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "cathy"
|
|
7
|
+
spec.version = Cathy::VERSION
|
|
8
|
+
spec.authors = ["David F. Houghton"]
|
|
9
|
+
spec.email = ["dfhougthon@gmail.com"]
|
|
10
|
+
|
|
11
|
+
spec.summary = "an updatable weighted probability distribution model"
|
|
12
|
+
spec.description = <<~DESCRIPTION.strip.gsub(/\s+/, " ")
|
|
13
|
+
Cathy lets you model an updatable weighted probability distribution which you can draw from.
|
|
14
|
+
For example, suppose you want to randomly pull cards from a deck which consists only of
|
|
15
|
+
aces, jacks, queens, and kings, with twice as many jacks as aces, twice as many queens as jacks,
|
|
16
|
+
and twice as many kings as queens. Cathy will model this for you. And you can change the frequency
|
|
17
|
+
of each card on the fly.
|
|
18
|
+
DESCRIPTION
|
|
19
|
+
spec.homepage = "https://github.com/dfhoughton/cathy"
|
|
20
|
+
spec.license = "MIT"
|
|
21
|
+
spec.required_ruby_version = ">= 2.6.0"
|
|
22
|
+
|
|
23
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
24
|
+
spec.metadata["source_code_uri"] = "https://github.com/dfhoughton/cathy"
|
|
25
|
+
spec.metadata["changelog_uri"] = "https://github.com/dfhoughton/cathy/blob/main/CHANGELOG.md"
|
|
26
|
+
|
|
27
|
+
# Specify which files should be added to the gem when it is released.
|
|
28
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
29
|
+
spec.files = Dir.chdir(__dir__) do
|
|
30
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
|
31
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
spec.bindir = "exe"
|
|
35
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
|
36
|
+
spec.require_paths = ["lib"]
|
|
37
|
+
|
|
38
|
+
# Uncomment to register a new dependency of your gem
|
|
39
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
|
40
|
+
|
|
41
|
+
# For more information and examples about making a new gem, check out our
|
|
42
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
|
43
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
require "cathy"
|
|
2
|
+
|
|
3
|
+
# a chat bot that takes a long, long time to warm up
|
|
4
|
+
# all Cathy learns is the probability a given word begins a sentences
|
|
5
|
+
# and the probability that a given word follows the two preceding words
|
|
6
|
+
# a "word" to Cathy is whatever is between two spaces
|
|
7
|
+
class ChattyCathy
|
|
8
|
+
# the easy way to begin a conversation
|
|
9
|
+
def self.chat!
|
|
10
|
+
new.chat
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
private
|
|
14
|
+
|
|
15
|
+
def initialize
|
|
16
|
+
@starters = Cathy.new
|
|
17
|
+
@bigrams = {}
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def chat
|
|
21
|
+
greet
|
|
22
|
+
|
|
23
|
+
loop do
|
|
24
|
+
text = listen
|
|
25
|
+
|
|
26
|
+
if ["", "bye", "goodbye", "quit"].include?(text.downcase.gsub(/\W+/, ""))
|
|
27
|
+
farewell
|
|
28
|
+
break
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
ponder(text)
|
|
32
|
+
|
|
33
|
+
respond
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def greet
|
|
38
|
+
puts <<~GREETING
|
|
39
|
+
Hi, I'm Chatty Cathy! Let's have a chat!
|
|
40
|
+
|
|
41
|
+
You tell me something. Press enter when you're done. Then I'll
|
|
42
|
+
respond. If you want to quit, respond with "bye", "goodbye",
|
|
43
|
+
or "quit", or just press enter without saying anything at all.
|
|
44
|
+
|
|
45
|
+
I will learn to chat from what you tell me. At first I will only know
|
|
46
|
+
how to repeat what you say. The more you repeat yourself the less I
|
|
47
|
+
will repeat myself. But it will take me a l-o-o-o-n-g time to start being original.
|
|
48
|
+
GREETING
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def listen
|
|
52
|
+
puts
|
|
53
|
+
print "You: "
|
|
54
|
+
$stdin.readline
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def ponder(text)
|
|
58
|
+
words = [:start] + text.strip.split(/\s+/) + [:end]
|
|
59
|
+
@starters.add(words[1])
|
|
60
|
+
(0...words.length - 2).each do |i|
|
|
61
|
+
*bigram, word = words[i...i + 3]
|
|
62
|
+
(@bigrams[bigram] ||= Cathy.new).add word
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def respond
|
|
67
|
+
words = [w1 = @starters.pick]
|
|
68
|
+
w2 = @bigrams[[:start, w1]].pick
|
|
69
|
+
while w2 != :end
|
|
70
|
+
words << w2
|
|
71
|
+
w3 = @bigrams[[w1, w2]].pick
|
|
72
|
+
w1 = w2
|
|
73
|
+
w2 = w3
|
|
74
|
+
end
|
|
75
|
+
puts
|
|
76
|
+
puts "Cathy: #{words.join " "}"
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def farewell
|
|
80
|
+
puts
|
|
81
|
+
puts "Cathy: It's been good chatting with you!"
|
|
82
|
+
end
|
|
83
|
+
end
|
data/lib/cathy.rb
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "cathy/version"
|
|
4
|
+
|
|
5
|
+
##
|
|
6
|
+
# Cathy models an updatable weighted probability distribution. It keeps track of the frequency of things
|
|
7
|
+
# added to it. Then you can pick one of the things added to it with a probability proportional to the number
|
|
8
|
+
# of times it has been added.
|
|
9
|
+
class Cathy
|
|
10
|
+
|
|
11
|
+
##
|
|
12
|
+
# Create a new distribution. In its initial state, it will always pick nil.
|
|
13
|
+
def initialize
|
|
14
|
+
@heap = []
|
|
15
|
+
@total = 0
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
##
|
|
19
|
+
# pick an item according to the frequency with which items were added
|
|
20
|
+
def pick
|
|
21
|
+
return if @heap.empty?
|
|
22
|
+
|
|
23
|
+
amount = rand 0...@total
|
|
24
|
+
@heap[0].find(amount)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
##
|
|
28
|
+
# Increment the frequency of this item by one
|
|
29
|
+
def add(item)
|
|
30
|
+
@total += 1
|
|
31
|
+
if (i = index(item))
|
|
32
|
+
child = @heap[i]
|
|
33
|
+
child.increment!
|
|
34
|
+
loop do
|
|
35
|
+
return if child.index.zero?
|
|
36
|
+
|
|
37
|
+
# rebalance the heap if necessary
|
|
38
|
+
parent = child.parent
|
|
39
|
+
if parent.count < child.count
|
|
40
|
+
i = child.index
|
|
41
|
+
child.move!(parent.index)
|
|
42
|
+
parent.move!(i)
|
|
43
|
+
else
|
|
44
|
+
parent.clear_parents!
|
|
45
|
+
break
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
else
|
|
49
|
+
# new items require minimal work
|
|
50
|
+
i = @heap.length
|
|
51
|
+
child = Item.new(item, @heap, i)
|
|
52
|
+
@heap << child
|
|
53
|
+
child.clear_parents!
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
##
|
|
58
|
+
# Returns a map from the items added to their frequencies.
|
|
59
|
+
def counts
|
|
60
|
+
@heap.to_h { |i| [i.item, i.count] }
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
##
|
|
64
|
+
# Generates a distribution from a map from items to frequencies.
|
|
65
|
+
def self.from_counts(counts)
|
|
66
|
+
heap = []
|
|
67
|
+
counts.each_with_index do |(item, count), i|
|
|
68
|
+
c = Item.new(item, heap, i, count)
|
|
69
|
+
heap << c
|
|
70
|
+
loop do
|
|
71
|
+
break if c.index.zero?
|
|
72
|
+
|
|
73
|
+
p = c.parent
|
|
74
|
+
if c.count > p.count
|
|
75
|
+
idx = c.index
|
|
76
|
+
c.move!(p.index)
|
|
77
|
+
p.move!(idx)
|
|
78
|
+
else
|
|
79
|
+
break
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
total = counts.values.sum
|
|
84
|
+
new.tap do |obj|
|
|
85
|
+
obj.instance_variable_set :@heap, heap
|
|
86
|
+
obj.instance_variable_set :@total, total
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
private
|
|
91
|
+
|
|
92
|
+
# because the heap tends to be sorted from most common to least, a linear
|
|
93
|
+
# search is reasonably efficient
|
|
94
|
+
#
|
|
95
|
+
# this returns the index of an item in the heap
|
|
96
|
+
def index(item)
|
|
97
|
+
@heap.index { |c| c.item == item }
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# a container for things added
|
|
101
|
+
#
|
|
102
|
+
# items memoize useful counts
|
|
103
|
+
class Item
|
|
104
|
+
def initialize(item, heap, index, count = 1)
|
|
105
|
+
@heap = heap
|
|
106
|
+
@index = index
|
|
107
|
+
@count = count
|
|
108
|
+
@item = item
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# find an item in this subtree given an "amount"
|
|
112
|
+
def find(amount)
|
|
113
|
+
if amount < left_count
|
|
114
|
+
left.find(amount)
|
|
115
|
+
elsif amount < non_right
|
|
116
|
+
item
|
|
117
|
+
else
|
|
118
|
+
right&.find(amount - non_right) || item
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
attr_reader :item, :count, :heap, :index
|
|
123
|
+
|
|
124
|
+
def parent
|
|
125
|
+
heap[(index - 1) / 2] if index.positive?
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def left
|
|
129
|
+
(@left ||= [heap[index * 2 + 1]])[0]
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def right
|
|
133
|
+
(@right ||= [heap[index * 2 + 2]])[0]
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def sum
|
|
137
|
+
@sum ||= non_right + right_count
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def left_count
|
|
141
|
+
@left_count ||= left&.sum.to_i
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def right_count
|
|
145
|
+
@right_count ||= right&.sum.to_i
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def non_right
|
|
149
|
+
@non_right ||= left_count + count
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# move in the heap
|
|
153
|
+
def move!(new_index)
|
|
154
|
+
@index = new_index
|
|
155
|
+
heap[new_index] = self
|
|
156
|
+
clear!
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# increment the frequency count
|
|
160
|
+
def increment!
|
|
161
|
+
@count += 1
|
|
162
|
+
clear!
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# clear memoized information
|
|
166
|
+
def clear!
|
|
167
|
+
@left_count = @right_count = @non_right = @sum = @left = @right = nil
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# clear memoization up the ancestor path
|
|
171
|
+
def clear_parents!
|
|
172
|
+
if (p = parent)
|
|
173
|
+
p.clear!
|
|
174
|
+
p.clear_parents!
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
end
|
data/sig/cathy.rbs
ADDED
metadata
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: cathy
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- David F. Houghton
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2022-10-01 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Cathy lets you model an updatable weighted probability distribution which
|
|
14
|
+
you can draw from. For example, suppose you want to randomly pull cards from a deck
|
|
15
|
+
which consists only of aces, jacks, queens, and kings, with twice as many jacks
|
|
16
|
+
as aces, twice as many queens as jacks, and twice as many kings as queens. Cathy
|
|
17
|
+
will model this for you. And you can change the frequency of each card on the fly.
|
|
18
|
+
email:
|
|
19
|
+
- dfhougthon@gmail.com
|
|
20
|
+
executables: []
|
|
21
|
+
extensions: []
|
|
22
|
+
extra_rdoc_files: []
|
|
23
|
+
files:
|
|
24
|
+
- ".rspec"
|
|
25
|
+
- ".standard.yml"
|
|
26
|
+
- CHANGELOG.md
|
|
27
|
+
- CODE_OF_CONDUCT.md
|
|
28
|
+
- Gemfile
|
|
29
|
+
- LICENSE.txt
|
|
30
|
+
- README.md
|
|
31
|
+
- Rakefile
|
|
32
|
+
- cathy.gemspec
|
|
33
|
+
- examples/chatty_cathy.rb
|
|
34
|
+
- lib/cathy.rb
|
|
35
|
+
- lib/cathy/version.rb
|
|
36
|
+
- sig/cathy.rbs
|
|
37
|
+
homepage: https://github.com/dfhoughton/cathy
|
|
38
|
+
licenses:
|
|
39
|
+
- MIT
|
|
40
|
+
metadata:
|
|
41
|
+
homepage_uri: https://github.com/dfhoughton/cathy
|
|
42
|
+
source_code_uri: https://github.com/dfhoughton/cathy
|
|
43
|
+
changelog_uri: https://github.com/dfhoughton/cathy/blob/main/CHANGELOG.md
|
|
44
|
+
post_install_message:
|
|
45
|
+
rdoc_options: []
|
|
46
|
+
require_paths:
|
|
47
|
+
- lib
|
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
49
|
+
requirements:
|
|
50
|
+
- - ">="
|
|
51
|
+
- !ruby/object:Gem::Version
|
|
52
|
+
version: 2.6.0
|
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
54
|
+
requirements:
|
|
55
|
+
- - ">="
|
|
56
|
+
- !ruby/object:Gem::Version
|
|
57
|
+
version: '0'
|
|
58
|
+
requirements: []
|
|
59
|
+
rubygems_version: 3.3.7
|
|
60
|
+
signing_key:
|
|
61
|
+
specification_version: 4
|
|
62
|
+
summary: an updatable weighted probability distribution model
|
|
63
|
+
test_files: []
|