Skip to content

Commit

Permalink
fixed #from_json in TransitionTable and TransitionMatrix
Browse files Browse the repository at this point in the history
  • Loading branch information
mccallofthewild committed Sep 29, 2017
1 parent 1a47878 commit cab0ae9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
14 changes: 13 additions & 1 deletion spec/TransitionMatrix_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe Markov::TransitionMatrix do
true.should eq true
end

it "#to_json, #from_json" do
it "#to_json, #from_json with String" do
t = Markov::TransitionMatrix(String).new
t.add "I"
t.add "just"
Expand All @@ -18,6 +18,18 @@ describe Markov::TransitionMatrix do
t["I"].should eq t_j["I"]
end

it "#to_json, #from_json with Int32" do
t = Markov::TransitionMatrix(Int32).new
t.add 1
t.add 2
t.add 3
t.add 4
j_t = t.to_json
puts j_t
t_j = Markov::TransitionMatrix(Int32).from_json j_t
t[2].should eq t_j[2]
end

it "#add" do
t = Markov::TransitionMatrix(String).new
t.add "hey"
Expand Down
12 changes: 11 additions & 1 deletion spec/TransitionTable_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe Markov::TransitionTable do
is_transition_matrix.should eq(true)
end

it "#to_json, #from_json" do
it "#to_json, #from_json with strings" do
string_array = %w(some say the world will end in fire)
normal_init_table = Markov::TransitionTable(String).new
normal_init_table.fill string_array
Expand All @@ -73,6 +73,16 @@ describe Markov::TransitionTable do
from_json_init_table["some"].should eq normal_init_table["some"]
end

it "#to_json, #from_json with integers" do
int_array = [0,1,2,3,4,5]
normal_init_table = Markov::TransitionTable(Int32).new
normal_init_table.fill int_array

normal_init_table_json = normal_init_table.to_json
from_json_init_table = Markov::TransitionTable(Int32).from_json normal_init_table_json
from_json_init_table[1].should eq normal_init_table[1]
end

it "#reset" do
movie_one = %w(the great gatsby)
movie_two = %w(great expectations)
Expand Down
4 changes: 3 additions & 1 deletion src/markov/TransitionMatrix.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ module Markov
if pull.kind == :null
pull.read_next
else
hash[key] = V.new(pull) # V is the value type, as in `Hash(K, V)`
key = LinkType == String ? %("#{key}") : key # makes String compatible for json parsing
key_of_type = LinkType.from_json key
hash[key_of_type] = V.new(pull) # V is the value type, as in `Hash(K, V)`
end
end
hash
Expand Down
4 changes: 3 additions & 1 deletion src/markov/TransitionTable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ module Markov
if pull.kind == :null
pull.read_next
else
hash[key] = TransitionMatrix(LinkType).new(pull)
key = LinkType == String ? %("#{key}") : key # makes String compatible for json parsing
key_of_type = LinkType.from_json key
hash[key_of_type] = TransitionMatrix(LinkType).new(pull) # V is the value type, as in `Hash(K, V)`
end
end
hash
Expand Down

0 comments on commit cab0ae9

Please sign in to comment.