diff options
author | Jordan Bracco <href@random.sh> | 2022-12-20 00:21:54 +0000 |
---|---|---|
committer | Jordan Bracco <href@random.sh> | 2022-12-20 19:29:41 +0100 |
commit | 2d83df8b32bff7f0028923bb5b64dc0b55f20d03 (patch) | |
tree | 1207e67b5b15f540963db05e7be89f3ca950e724 /lib/nola_plugins/txt_plugin | |
parent | Nola rename, the end. pt 6. Refs T77. (diff) |
Nola rename: The Big Move, Refs T77
Diffstat (limited to 'lib/nola_plugins/txt_plugin')
-rw-r--r-- | lib/nola_plugins/txt_plugin/markov.ex | 9 | ||||
-rw-r--r-- | lib/nola_plugins/txt_plugin/markov_native.ex | 33 | ||||
-rw-r--r-- | lib/nola_plugins/txt_plugin/markov_py_markovify.ex | 39 |
3 files changed, 81 insertions, 0 deletions
diff --git a/lib/nola_plugins/txt_plugin/markov.ex b/lib/nola_plugins/txt_plugin/markov.ex new file mode 100644 index 0000000..2e30dfa --- /dev/null +++ b/lib/nola_plugins/txt_plugin/markov.ex @@ -0,0 +1,9 @@ +defmodule Nola.IRC.TxtPlugin.Markov do + + @type state :: any() + @callback start_link() :: {:ok, state()} + @callback reload(content :: Map.t, state()) :: any() + @callback sentence(state()) :: {:ok, String.t} | {:error, String.t} + @callback complete_sentence(state()) :: {:ok, String.t} | {:error, String.t} + +end diff --git a/lib/nola_plugins/txt_plugin/markov_native.ex b/lib/nola_plugins/txt_plugin/markov_native.ex new file mode 100644 index 0000000..4c403c2 --- /dev/null +++ b/lib/nola_plugins/txt_plugin/markov_native.ex @@ -0,0 +1,33 @@ +defmodule Nola.IRC.TxtPlugin.MarkovNative do + @behaviour Nola.IRC.TxtPlugin.Markov + + def start_link() do + ExChain.MarkovModel.start_link() + end + + def reload(data, markov) do + data = data + |> Enum.map(fn({_, data}) -> + for {line, _idx} <- data, do: line + end) + |> List.flatten + + ExChain.MarkovModel.populate_model(markov, data) + :ok + end + + def sentence(markov) do + case ExChain.SentenceGenerator.create_filtered_sentence(markov) do + {:ok, line, _, _} -> {:ok, line} + error -> error + end + end + + def complete_sentence(sentence, markov) do + case ExChain.SentenceGenerator.complete_sentence(markov, sentence) do + {line, _} -> {:ok, line} + error -> error + end + end + +end diff --git a/lib/nola_plugins/txt_plugin/markov_py_markovify.ex b/lib/nola_plugins/txt_plugin/markov_py_markovify.ex new file mode 100644 index 0000000..b610ea8 --- /dev/null +++ b/lib/nola_plugins/txt_plugin/markov_py_markovify.ex @@ -0,0 +1,39 @@ +defmodule Nola.IRC.TxtPlugin.MarkovPyMarkovify do + + def start_link() do + {:ok, nil} + end + + def reload(_data, _markov) do + :ok + end + + def sentence(_) do + {:ok, run()} + end + + def complete_sentence(sentence, _) do + {:ok, run([sentence])} + end + + defp run(args \\ []) do + {binary, script} = script() + args = [script, Path.expand(Nola.IRC.TxtPlugin.directory()) | args] + IO.puts "Args #{inspect args}" + case MuonTrap.cmd(binary, args) do + {response, 0} -> response + {response, code} -> "error #{code}: #{response}" + end + end + + defp script() do + default_script = to_string(:code.priv_dir(:nola)) <> "/irc/txt/markovify.py" + env = Application.get_env(:nola, Nola.IRC.TxtPlugin, []) + |> Keyword.get(:py_markovify, []) + + {Keyword.get(env, :python, "python3"), Keyword.get(env, :script, default_script)} + end + + + +end |