Add merge/1 and merge/2 to Markov.Chain

merge/1 takes a nonempty list of markov chains to merge

merge/2 takes two markov chains of the same order and sums their weights

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-07-17 20:07:02 -07:00
parent 6f625c21ca
commit 9dc1033cd2
2 changed files with 51 additions and 17 deletions

View File

@@ -67,4 +67,19 @@ defmodule MarkovChainTest do
["foo", "bar"] => %{"baz" => 3, "qux" => 1},
}
end
test "chain merge works correctly" do
chain1 = %Chain {order: 2}
|> Chain.add_weight(["foo", "bar"], "baz")
chain2 = %Chain {order: 2}
|> Chain.add_weight(["foo", "bar"], "baz")
|> Chain.add_weight(["bar", "baz"], "qux")
merged = Chain.merge(chain1, chain2)
assert merged.chain == %{
["foo", "bar"] => %{"baz" => 2},
["bar", "baz"] => %{"qux" => 1},
}
end
end