Created
March 28, 2019 15:25
-
-
Save jarsen/25510929a470c1aedda1fe87b7d8fa94 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MarkovChain do | |
def create_chain(source, ngram_length) do | |
source | |
|> Stream.chunk_every(ngram_length + 1, 1) | |
|> Stream.map(fn chunk -> Enum.split(chunk, ngram_length) end) | |
|> Stream.scan(%{}, fn {ngram, word}, acc -> | |
Map.update(acc, ngram, word, fn val -> val ++ word end) | |
end) | |
end | |
def next_element(chain, preceding) do | |
chain | |
|> Map.get(preceding, []) | |
|> Enum.random() | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MarkovChainTest do | |
use ExUnit.Case | |
doctest MarkovChain | |
describe "create_chain" do | |
test "returns correct map for names" do | |
words = "Tom Bombadil" |> String.graphemes() | |
assert %{ | |
["a"] => ["d"], | |
[" "] => ["B"], | |
["B"] => ["o"], | |
["T"] => ["o"], | |
["b"] => ["a"], | |
["d"] => ["i"], | |
["i"] => ["l"], | |
["l"] => [], | |
["m"] => [" ", "b"], | |
["o"] => ["m", "m"] | |
} == MarkovChain.create_chain(words, 1) |> Enum.to_list() |> List.last() | |
end | |
test "returns correct map for words" do | |
words = "a long time ago in a galaxy far far away" |> String.split() | |
assert %{ | |
["a"] => ["long", "galaxy"], | |
["long"] => ["time"], | |
["time"] => ["ago"], | |
["ago"] => ["in"], | |
["in"] => ["a"], | |
["galaxy"] => ["far"], | |
["far"] => ["far", "away"], | |
["away"] => [] | |
} == MarkovChain.create_chain(words, 1) |> Enum.to_list() |> List.last() | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment