Files
omnibot/lib/contrib/markov/markov.ex
Alek Ratzloff 04a4c740bd Add allchain and implement "all", "emulate" commands for markov plugin
* Markov allchain is generated from all markov chains in the save
  directory for the given channel. This omits starting processes for
  known markov chains.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-07-17 20:08:18 -07:00

98 lines
2.7 KiB
Elixir

defmodule Omnibot.Contrib.Markov do
use Omnibot.Plugin
alias Omnibot.{Contrib.Markov.ChainServer, Util}
require Logger
@default_config save_dir: "markov", order: 2, save_every: 5 * 60, ignore: []
@registry __MODULE__.Registry
@supervisor __MODULE__.ChainSupervisor
@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},
{Registry, keys: :unique, name: @registry},
{DynamicSupervisor, name: @supervisor, strategy: :one_for_one},
]
end
command "!markov", ["force"] do
reply = chain_server(channel, nick) |> ChainServer.generate()
Irc.send_to(irc, channel, "#{nick}: #{reply}")
end
command "!markov", ["emulate", emulate] do
reply = chain_server(channel, emulate) |> ChainServer.generate()
Irc.send_to(irc, channel, "#{nick}: #{reply}")
end
command "!markov", ["all"] do
reply = chain_server(channel, :all) |> ChainServer.generate()
Irc.send_to(irc, channel, "#{nick}: #{reply}")
end
command "!markov", ["status"] do
Irc.send_to(irc, channel, "TODO")
end
def save_dir() do
cfg()[:save_dir]
end
def train(channel, user, msg) do
server = chain_server(channel, user)
ChainServer.train(server, msg)
end
def chain_servers() do
# See https://hexdocs.pm/elixir/Registry.html#select/2-examples to understand what the hell is going on here
# (it just selects the PID of all chain_server processes)
for {pid} <- Registry.select(@registry, [{{:_, :"$1", :_}, [], [{{:"$1"}}]}]),
do: pid
end
def chain_server(channel, user) do
case Registry.lookup(@registry, {channel, user}) do
[] -> start_chain_server!(channel, user)
[{pid, _} | _] -> pid
end
end
defp start_chain_server!(channel, user) do
{:ok, chain} = start_chain_server(channel, user)
chain
end
defp start_chain_server(channel, user) do
DynamicSupervisor.start_child(
@supervisor,
{ChainServer, channel: channel, user: user, name: {:via, Registry, {@registry, {channel, user}}}}
)
end
def save_chains() do
start = Util.now_unix()
Logger.debug("Saving markov chains")
chain_servers() |> Enum.each(&ChainServer.save/1)
stop = Util.now_unix()
Logger.info("Saved markov chains in #{stop - start} seconds")
end
@impl true
def on_channel_msg(_irc, channel, nick, msg) do
# self-messages are already ignored, so just check the configured ignore-list
filter = nick in cfg()[:ignore]
|| (String.trim(msg) |> String.starts_with?("!"))
if !filter do
train(channel, nick, msg)
end
end
end