Skip to content

Commit

Permalink
Removed dependency on base64 gem for serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Dec 29, 2024
1 parent 1ac80be commit bbda2c3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.5.1 (unreleased)

- Removed dependency on `base64` gem for serialization

## 0.5.0 (2024-10-22)

- Changed dataset directory to match XDG Base Directory Specification
Expand Down
11 changes: 4 additions & 7 deletions lib/disco/recommender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ def inspect
end

def to_json
require "base64"
require "json"

obj = {
Expand All @@ -270,8 +269,8 @@ def to_json
item_ids: @item_map.keys,
rated: @user_map.map { |_, u| (@rated[u] || {}).keys },
global_mean: @global_mean,
user_factors: Base64.strict_encode64(@user_factors.to_binary),
item_factors: Base64.strict_encode64(@item_factors.to_binary),
user_factors: [@user_factors.to_binary].pack("m0"),
item_factors: [@item_factors.to_binary].pack("m0"),
factors: @factors,
epochs: @epochs,
verbose: @verbose
Expand Down Expand Up @@ -432,16 +431,14 @@ def to_dataset(dataset)
end

def json_load(obj)
require "base64"

@implicit = obj["implicit"]
@user_map = obj["user_ids"].map.with_index.to_h
@item_map = obj["item_ids"].map.with_index.to_h
@rated = obj["rated"].map.with_index.to_h { |r, i| [i, r.to_h { |v| [v, true] }] }
@global_mean = obj["global_mean"].to_f
@factors = obj["factors"].to_i
@user_factors = Numo::SFloat.from_binary(Base64.strict_decode64(obj["user_factors"]), [@user_map.size, @factors])
@item_factors = Numo::SFloat.from_binary(Base64.strict_decode64(obj["item_factors"]), [@item_map.size, @factors])
@user_factors = Numo::SFloat.from_binary(obj["user_factors"].unpack1("m0"), [@user_map.size, @factors])
@item_factors = Numo::SFloat.from_binary(obj["item_factors"].unpack1("m0"), [@item_map.size, @factors])
@epochs = obj["epochs"].to_i
@verbose = obj["verbose"]

Expand Down

0 comments on commit bbda2c3

Please sign in to comment.