diff options
author | href <href@random.sh> | 2018-02-23 14:39:28 +0100 |
---|---|---|
committer | href <href@random.sh> | 2018-02-23 14:39:28 +0100 |
commit | 5f203a9a3db09251c86f812b00bbafd482657def (patch) | |
tree | 0c1a07d30ea35c66fb91b55b888759dc8ee2ed33 /lib/lsg_irc/txt_handler.ex | |
parent | :) (diff) |
…
Diffstat (limited to '')
-rw-r--r-- | lib/lsg_irc/txt_handler.ex | 42 |
1 files changed, 39 insertions, 3 deletions
diff --git a/lib/lsg_irc/txt_handler.ex b/lib/lsg_irc/txt_handler.ex index f946092..efe2b68 100644 --- a/lib/lsg_irc/txt_handler.ex +++ b/lib/lsg_irc/txt_handler.ex @@ -1,5 +1,6 @@ defmodule LSG.IRC.TxtHandler do alias LSG.IRC.UserTrack + require Logger @moduledoc """ # [txt](/irc/txt) @@ -27,12 +28,13 @@ defmodule LSG.IRC.TxtHandler do GenServer.start_link(__MODULE__, [client]) end - defstruct client: nil, triggers: %{}, rw: true, locks: nil + defstruct client: nil, triggers: %{}, rw: true, locks: nil, markov: nil def init([client]) do dets_locks_filename = (LSG.data_path() <> "/" <> "txtlocks.dets") |> String.to_charlist {:ok, locks} = :dets.open_file(dets_locks_filename, []) - state = %__MODULE__{client: client, locks: locks} + {:ok, markov} = ExChain.MarkovModel.start_link + state = %__MODULE__{client: client, locks: locks, markov: markov} ExIRC.Client.add_handler(client, self()) {:ok, %__MODULE__{state | triggers: load()}} end @@ -118,6 +120,25 @@ defmodule LSG.IRC.TxtHandler do {:noreply, state} end + def handle_info({:received, "~txt", _, chan}, state) do + case ExChain.SentenceGenerator.create_filtered_sentence(state.markov) do + {:ok, line, _, _} -> + ExIRC.Client.msg(state.client, :privmsg, chan, line) + error -> + Logger.error "Txt Markov error: "<>inspect error + end + {:noreply, state} + end + def handle_info({:received, "~txt "<>complete, _, chan}, state) do + case ExChain.SentenceGenerator.complete_sentence(state.markov, complete) do + {line, _} -> + ExIRC.Client.msg(state.client, :privmsg, chan, line) + error -> + Logger.error "Txt Markov error: "<>inspect error + end + {:noreply, state} + end + def handle_info({:received, "!"<>trigger, _, chan}, state) do {trigger, opts} = clean_trigger(trigger) line = get_random(state.triggers, trigger, opts) @@ -174,7 +195,19 @@ defmodule LSG.IRC.TxtHandler do end end + def handle_info(:reload_markov, state=%__MODULE__{triggers: triggers, markov: markov}) do + all_data = triggers + |> Enum.map(fn({_, data}) -> + for {line, _idx} <- data, do: line + end) + |> List.flatten + for l <- all_data, do: IO.puts(l) + + populate = ExChain.MarkovModel.populate_model(markov, all_data) + IO.puts "populated markov: #{inspect(populate)}" + {:noreply, state} + end def handle_info(msg, state) do {:noreply, state} @@ -182,7 +215,7 @@ defmodule LSG.IRC.TxtHandler do # Load/Reloads text files from disk defp load() do - Path.wildcard(directory() <> "/*.txt") + triggers = Path.wildcard(directory() <> "/*.txt") |> Enum.reduce(%{}, fn(path, m) -> file = Path.basename(path) [key, "txt"] = String.split(file, ".", parts: 2) @@ -201,6 +234,9 @@ defmodule LSG.IRC.TxtHandler do end) |> Enum.sort |> Enum.into(Map.new) + + send(self(), :reload_markov) + triggers end defp dump(trigger, data) do |