diff options
author | Jordan Bracco <href@random.sh> | 2022-12-20 02:19:42 +0000 |
---|---|---|
committer | Jordan Bracco <href@random.sh> | 2022-12-20 19:29:41 +0100 |
commit | 9958e90eb5eb5a2cc171c40860745e95a96bd429 (patch) | |
tree | b49cdb1d0041b9c0a81a14950d38c0203896f527 /lib/plugins/txt | |
parent | Rename to Nola (diff) |
Actually do not prefix folders with nola_ refs T77
Diffstat (limited to 'lib/plugins/txt')
-rw-r--r-- | lib/plugins/txt/markov.ex | 9 | ||||
-rw-r--r-- | lib/plugins/txt/markov_native.ex | 33 | ||||
-rw-r--r-- | lib/plugins/txt/markov_py_markovify.ex | 39 |
3 files changed, 81 insertions, 0 deletions
diff --git a/lib/plugins/txt/markov.ex b/lib/plugins/txt/markov.ex new file mode 100644 index 0000000..b47666c --- /dev/null +++ b/lib/plugins/txt/markov.ex @@ -0,0 +1,9 @@ +defmodule Nola.Plugins.Txt.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/plugins/txt/markov_native.ex b/lib/plugins/txt/markov_native.ex new file mode 100644 index 0000000..aa6b454 --- /dev/null +++ b/lib/plugins/txt/markov_native.ex @@ -0,0 +1,33 @@ +defmodule Nola.Plugins.Txt.MarkovNative do + @behaviour Nola.Plugins.Txt.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/plugins/txt/markov_py_markovify.ex b/lib/plugins/txt/markov_py_markovify.ex new file mode 100644 index 0000000..f79ed47 --- /dev/null +++ b/lib/plugins/txt/markov_py_markovify.ex @@ -0,0 +1,39 @@ +defmodule Nola.Plugins.Txt.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.Plugins.Txt.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.Plugins.Txt, []) + |> Keyword.get(:py_markovify, []) + + {Keyword.get(env, :python, "python3"), Keyword.get(env, :script, default_script)} + end + + + +end |