Move markov to be a supervisor plugin
This is probably going to be necessary to avoid race conditions in the ETS storage Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
50
lib/contrib/markov/bot.ex
Normal file
50
lib/contrib/markov/bot.ex
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
defmodule Omnibot.Contrib.Markov.Bot do
|
||||||
|
use Omnibot.Plugin
|
||||||
|
alias Omnibot.Contrib.Markov.Chain
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def on_init(_cfg) do
|
||||||
|
# Create the markov database
|
||||||
|
path = String.to_atom(cfg()[:path])
|
||||||
|
{:ok, db} = :dets.open_file(path, [:named_table])
|
||||||
|
chains = :ets.new(:markov_chains, [:public])
|
||||||
|
:dets.to_ets(db, chains)
|
||||||
|
:dets.close(db)
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def on_channel_msg(_irc, channel, nick, msg) do
|
||||||
|
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
|
||||||
|
|
||||||
|
def save_chains() do
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,46 +1,32 @@
|
|||||||
defmodule Omnibot.Contrib.Markov do
|
defmodule Omnibot.Contrib.Markov do
|
||||||
use Omnibot.Plugin
|
use Omnibot.Plugin.Base
|
||||||
|
alias Omnibot.Contrib.Markov
|
||||||
|
use Supervisor
|
||||||
|
|
||||||
alias Omnibot.Contrib.Markov.Chain
|
@default_config path: "markov", order: 2, save_every: 5 * 60
|
||||||
|
|
||||||
@default_config path: "markov", order: 2
|
def start_link(opts) do
|
||||||
|
Supervisor.start_link(__MODULE__, opts[:cfg], opts)
|
||||||
@impl true
|
|
||||||
def on_init(cfg) do
|
|
||||||
# Create the markov database
|
|
||||||
path = String.to_atom(cfg[:path])
|
|
||||||
:ets.new(path, [:public])
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def on_channel_msg(_irc, channel, nick, msg) do
|
def init(cfg) do
|
||||||
train(channel, nick, msg)
|
children = [
|
||||||
|
{Markov.Bot, cfg: cfg, name: Omnibot.Contrib.Markov.Bot},
|
||||||
|
{Task, fn -> save_loop(cfg) end}
|
||||||
|
]
|
||||||
|
Supervisor.init(children, strategy: :one_for_all)
|
||||||
end
|
end
|
||||||
|
|
||||||
def train(channel, user, msg) do
|
defp save_loop(cfg) do
|
||||||
chain = (user_chain(channel, user) || create_user_chain(channel, user))
|
save_every = cfg[:save_every]
|
||||||
|> Chain.train(msg)
|
Process.sleep(save_every * 1000)
|
||||||
true = update_user_chain(channel, user, chain)
|
Markov.Bot.save_chains()
|
||||||
end
|
end
|
||||||
|
|
||||||
def user_chain(channel, user) do
|
@impl true
|
||||||
db = state()
|
def on_msg(irc, msg), do: Markov.Bot.on_msg(irc, msg)
|
||||||
case :ets.lookup(db, {channel, user}) do
|
|
||||||
[] -> nil
|
|
||||||
[{{^channel, ^user}, chains}] -> chains
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def update_user_chain(channel, user, chain) do
|
@impl true
|
||||||
db = state()
|
def on_channel_msg(irc, channel, nick, msg), do: Markov.Bot.on_channel_msg(irc, channel, nick, msg)
|
||||||
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
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
defmodule Omnibot.Contrib.Wordbot.Bot do
|
defmodule Omnibot.Contrib.Wordbot.Bot do
|
||||||
use Omnibot.Plugin.Base
|
use Omnibot.Plugin
|
||||||
use Omnibot.Plugin.GenServer
|
|
||||||
|
|
||||||
alias Omnibot.{Contrib.Wordbot, Irc, State, Util}
|
alias Omnibot.{Contrib.Wordbot, Irc, State, Util}
|
||||||
require Logger
|
require Logger
|
||||||
|
|||||||
Reference in New Issue
Block a user