Markov chains appear to be training correctly

* Markov chains record words correctly from a single line to the end of
  their line with a couple of exceptions.
* Start working on using ETS for storing markov chains, and saving it as
  a DETS periodically

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-07-08 17:25:26 -07:00
parent 1a73a62a2c
commit 9c69ca7b72
4 changed files with 43 additions and 24 deletions

View File

@@ -4,6 +4,10 @@ defmodule Omnibot.Contrib.Markov.Chain do
@enforce_keys [:order] @enforce_keys [:order]
defstruct order: 2, chain: [] defstruct order: 2, chain: []
def train(chain, line) when is_binary(line) do
train(chain, line |> String.split(~r/\s+/))
end
def train(chain, words) when is_list(words) do def train(chain, words) when is_list(words) do
order = chain.order order = chain.order
@@ -12,22 +16,12 @@ defmodule Omnibot.Contrib.Markov.Chain do
|> Enum.reduce(chain, &case Enum.split(&1, order) do |> Enum.reduce(chain, &case Enum.split(&1, order) do
{words, []} -> if length(words) == order, {words, []} -> if length(words) == order,
# Null case for the chain; this is an "end" state # Null case for the chain; this is an "end" state
do: add_weight(&2, words, nil) do: add_weight(&2, words, nil),
# else: TODO ? train [a, nil] -> b ? else: &2 # TODO ? train [a, nil] -> b ?
{words, [next]} -> {words, [next]} -> add_weight(&2, words, next)
add_weight(&2, words, next) end)
end
)
end end
#def lookup(%Chain {chain: chain, order: order}, key) do
# if length(key) != order, do: raise(ArgumentError, message: "invalid key (length #{length(key)} vs. order #{order})")
# case Util.binary_search(chain, key) do
# {_index, value} -> value[word]
# nil -> nil
# end
#end
def add_weight(%Chain {chain: chain, order: order}, key, word, increment \\ 1) do def add_weight(%Chain {chain: chain, order: order}, key, word, increment \\ 1) do
if length(key) != order, do: raise(ArgumentError, message: "invalid key (length #{length(key)} vs. order #{order})") if length(key) != order, do: raise(ArgumentError, message: "invalid key (length #{length(key)} vs. order #{order})")
chain = case Enum.find_index(chain, fn {listkey, _} -> listkey == key end) do chain = case Enum.find_index(chain, fn {listkey, _} -> listkey == key end) do

View File

@@ -3,20 +3,44 @@ defmodule Omnibot.Contrib.Markov do
alias Omnibot.Contrib.Markov.Chain alias Omnibot.Contrib.Markov.Chain
@default_config path: :"wordbot.ets", order: 2 @default_config path: "markov", order: 2
@impl true @impl true
def on_init(cfg) do def on_init(cfg) do
# Create the markov database # Create the markov database
path = if is_atom(cfg[:path]), path = String.to_atom(cfg[:path])
do: cfg[:path], :ets.new(path, [:public])
else: String.to_atom(cfg[:path])
{:ok, db} = :dets.open_file(path)
db
end end
@impl true @impl true
def on_channel_msg(_irc, _channel, _nick, msg) do def on_channel_msg(_irc, channel, nick, msg) do
_words = String.split(msg, ~r/\s+/) train(channel, nick, msg)
end
def train(channel, user, msg) do
chain = (user_chain(channel, user) || create_user_chain(channel, user))
|> Chain.train(msg)
true = update_user_chain(channel, user, chain)
end
def user_chain(channel, user) do
db = state()
case :ets.lookup(db, {channel, user}) do
[] -> nil
[{{^channel, ^user}, chains}] -> chains
end
end
def update_user_chain(channel, user, chain) do
db = state()
case user_chain(channel, user) do
nil -> :ets.insert_new(db, {{channel, user}, chain})
chain -> :ets.insert(db, {{channel, user}, chain})
end
end
defp create_user_chain(channel, user) do
true = update_user_chain(channel, user, %Chain{order: cfg()[:order]})
user_chain(channel, user)
end end
end end

View File

@@ -10,7 +10,9 @@ defmodule Omnibot.Supervisor do
@impl true @impl true
def init(:ok) do def init(:ok) do
{_, bindings} = Code.eval_file("omnibot.exs")
{_, bindings} = System.get_env("OMNIBOT_CFG", "omnibot.exs")
|> Code.eval_file()
cfg = bindings[:config] cfg = bindings[:config]
children = [ children = [

View File

@@ -25,7 +25,6 @@ defmodule MarkovChainTest do
] ]
chain = chain |> Chain.add_weight(["foo", "bar"], "qux") chain = chain |> Chain.add_weight(["foo", "bar"], "qux")
assert chain.chain == [ assert chain.chain == [
{["foo", "bar"], %{"baz" => 3, "qux" => 1}} {["foo", "bar"], %{"baz" => 3, "qux" => 1}}
] ]