-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
210 additions
and
24 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,5 @@ erl_crash.dump | |
|
||
# Also ignore archive artifacts (built via "mix archive.build"). | ||
*.ez | ||
|
||
/test/temp/*.txt |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule SimpleBayes.Data do | ||
def encode(value, opts \\ []) do | ||
value | ||
|> Kernel.inspect(limit: 1_000_000_000) | ||
|> Base.encode64(opts) | ||
end | ||
|
||
def decode(value, opts \\ []) do | ||
{struct, _} = value | ||
|> Base.decode64!(opts) | ||
|> Code.eval_string() | ||
|
||
struct | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
defmodule SimpleBayes.Storage.Behaviour do | ||
@callback init(%SimpleBayes{}, Keyword.t) :: pid | ||
@callback load(Keyword.t) :: pid | ||
@callback save(pid, Keyword.t) :: {:ok, pid} | ||
@callback load(Keyword.t) :: pid | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
defmodule SimpleBayes.Storage.FileSystem do | ||
@behaviour SimpleBayes.Storage.Behaviour | ||
|
||
alias SimpleBayes.Data | ||
|
||
def init(struct, _opts) do | ||
{:ok, pid} = Agent.start_link(fn -> struct end) | ||
|
||
pid | ||
end | ||
|
||
def save(pid, struct) do | ||
File.write!(file_path(struct.opts), Data.encode(struct)) | ||
|
||
pid | ||
end | ||
|
||
def load(opts) do | ||
struct = file_path(opts) | ||
|> File.read!() | ||
|> Data.decode() | ||
|
||
init(struct, opts) | ||
end | ||
|
||
defp file_path(opts) do | ||
opts[:file_path] || opts[:storage_config][:file_path] | ||
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
%{"earmark": {:hex, :earmark, "0.2.1", "ba6d26ceb16106d069b289df66751734802777a3cbb6787026dd800ffeb850f3", [:mix], []}, | ||
"ex_doc": {:hex, :ex_doc, "0.12.0", "b774aabfede4af31c0301aece12371cbd25995a21bb3d71d66f5c2fe074c603f", [:mix], [{:earmark, "~> 0.2", [hex: :earmark, optional: false]}]}, | ||
%{"earmark": {:hex, :earmark, "1.0.1", "2c2cd903bfdc3de3f189bd9a8d4569a075b88a8981ded9a0d95672f6e2b63141", [:mix], []}, | ||
"ex_doc": {:hex, :ex_doc, "0.13.0", "aa2f8fe4c6136a2f7cfc0a7e06805f82530e91df00e2bff4b4362002b43ada65", [:mix], [{:earmark, "~> 1.0", [hex: :earmark, optional: false]}]}, | ||
"stemmer": {:hex, :stemmer, "1.0.0-beta.1", "17112a69e72953e80985068ce68a77413de7e9adfe17204f23df1a1dc69fa7de", [:mix], []}} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
defmodule SimpleBayes.Storage.FileSystemTest do | ||
use ExUnit.Case, async: true | ||
|
||
doctest SimpleBayes.Storage.FileSystem | ||
|
||
describe "file system storage" do | ||
setup do | ||
opts = [ | ||
storage: :file_system, | ||
storage_config: [file_path: "test/temp/file_sysmte_test.txt"] | ||
] | ||
|
||
SimpleBayes.init(opts) | ||
|> SimpleBayes.train(:apple, "red sweet") | ||
|> SimpleBayes.train(:apple, "green", weight: 0.5) | ||
|> SimpleBayes.train(:apple, "round", weight: 2) | ||
|> SimpleBayes.train(:banana, "sweet") | ||
|> SimpleBayes.save() | ||
|
||
SimpleBayes.load(opts) | ||
|> SimpleBayes.train(:banana, "green", weight: 0.5) | ||
|> SimpleBayes.train(:banana, "yellow long", weight: 2) | ||
|> SimpleBayes.train(:orange, "red") | ||
|> SimpleBayes.train(:orange, "yellow sweet", weight: 0.5) | ||
|> SimpleBayes.train(:orange, "round", weight: 2) | ||
|> SimpleBayes.save() | ||
|
||
{:ok, opts: opts} | ||
end | ||
|
||
test "README example on .classify", meta do | ||
result = SimpleBayes.load(meta.opts) | ||
|> SimpleBayes.classify("Maybe green maybe red but definitely round and sweet") | ||
|> Keyword.keys() | ||
|
||
assert result == [:apple, :orange, :banana] | ||
end | ||
|
||
test "README example on .classify_one", meta do | ||
result = SimpleBayes.load(meta.opts) | ||
|> SimpleBayes.classify_one("Maybe green maybe red but definitely round and sweet") | ||
|
||
assert result == :apple | ||
end | ||
end | ||
|
||
describe "file system storage with flattened configuration options" do | ||
setup do | ||
opts = [ | ||
storage: :file_system, | ||
file_path: "test/temp/file_sysmte_test.txt" | ||
] | ||
|
||
SimpleBayes.init(opts) | ||
|> SimpleBayes.train(:apple, "red sweet") | ||
|> SimpleBayes.train(:apple, "green", weight: 0.5) | ||
|> SimpleBayes.train(:apple, "round", weight: 2) | ||
|> SimpleBayes.train(:banana, "sweet") | ||
|> SimpleBayes.save() | ||
|
||
SimpleBayes.load(opts) | ||
|> SimpleBayes.train(:banana, "green", weight: 0.5) | ||
|> SimpleBayes.train(:banana, "yellow long", weight: 2) | ||
|> SimpleBayes.train(:orange, "red") | ||
|> SimpleBayes.train(:orange, "yellow sweet", weight: 0.5) | ||
|> SimpleBayes.train(:orange, "round", weight: 2) | ||
|> SimpleBayes.save() | ||
|
||
{:ok, opts: opts} | ||
end | ||
|
||
test "README example on .classify", meta do | ||
result = SimpleBayes.load(meta.opts) | ||
|> SimpleBayes.classify("Maybe green maybe red but definitely round and sweet") | ||
|> Keyword.keys() | ||
|
||
assert result == [:apple, :orange, :banana] | ||
end | ||
|
||
test "README example on .classify_one", meta do | ||
result = SimpleBayes.load(meta.opts) | ||
|> SimpleBayes.classify_one("Maybe green maybe red but definitely round and sweet") | ||
|
||
assert result == :apple | ||
end | ||
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
Empty file.