Repository: stephantul/reach

Issue:
Fix bug: adding <UNK> resets dtype
Loading a .vec file with a given dtype (e.g., float32) using an OOV UNK token silently resets the dtype to the system default (usually float64).

Small fix

Discussion:

Patch:
diff --git a/reach/reach.py b/reach/reach.py
index 59fb490..13d55a9 100644
--- a/reach/reach.py
+++ b/reach/reach.py
@@ -215,7 +215,7 @@ class Reach(object):
 
         if unk_word is not None:
             if unk_word not in items:
-                unk_vec = np.zeros((1, vectors.shape[1]))
+                unk_vec = np.zeros((1, vectors.shape[1]), dtype=desired_dtype)
                 vectors = np.concatenate([unk_vec, vectors], 0)
                 items = [unk_word] + items
                 unk_index = 0


Test patch:
diff --git a/tests/test_io.py b/tests/test_io.py
index 95d13d2..7851300 100644
--- a/tests/test_io.py
+++ b/tests/test_io.py
@@ -64,9 +64,13 @@ class TestLoad(unittest.TestCase):
             instance = Reach.load(tempfile.name, unk_word=None)
             self.assertEqual(instance.unk_index, None)
 
-            instance = Reach.load(tempfile.name, unk_word="[UNK]")
+            desired_dtype = "float32"
+            instance = Reach.load(
+                tempfile.name, unk_word="[UNK]", desired_dtype=desired_dtype
+            )
             self.assertEqual(instance.unk_index, 0)
             self.assertEqual(instance.items["[UNK]"], instance.unk_index)
+            self.assertEqual(instance.vectors.dtype, desired_dtype)
 
             instance = Reach.load(tempfile.name, unk_word="splinter")
             self.assertEqual(instance.unk_index, 2)