Files
omnibot/lib/contrib/markov/markov.ex
Alek Ratzloff 522c62a55c Finish plugin and routing overhaul, there's a new model in town:
* Plugins all derive from Omnibot.Plugin. There still is a base plugin,
  in case we want to have another plugin backend instead of a GenServer
* All plugins are monitored by a unique Plugin.Supervisor, which is in
  turn started by the PluginSupervisor (yes this is confusing, yes it
  needs to be renamed)
* Any other auxiliary child processes may be started through the
  Plugin.children/1 function.
* By default, plugins have a CfgState process which is an Agent that
  keeps track of the plugin's configuration and state
* Plugin API is now called through the GenServer backend for better
  synchronicity.
* Very few changes to the front-facing Plugin API, which is nice

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-07-14 15:05:00 -07:00

65 lines
1.6 KiB
Elixir

defmodule Omnibot.Contrib.Markov do
use Omnibot.Plugin
alias Omnibot.Contrib.Markov.Chain
require Logger
@default_config path: "markov", order: 2, save_every: 5 * 60
@impl true
def children(cfg) do
[{Task, fn ->
Stream.timer(cfg[:save_every] * 1000)
|> Stream.cycle()
|> Stream.each(fn _ -> save_chains() end)
|> Stream.run()
end}]
end
@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
# TODO
Logger.info("Saved markov chains")
end
end