From ad8e0187e63078cc53a308853cc6ffdbe08b05ce Mon Sep 17 00:00:00 2001
From: Franck Dary <franck.dary@lis-lab.fr>
Date: Sat, 13 Jun 2020 17:40:42 +0200
Subject: [PATCH] Throws error if asked to read empty w2v file

---
 common/src/Dict.cpp             | 3 +++
 torch_modules/src/Submodule.cpp | 3 +++
 2 files changed, 6 insertions(+)

diff --git a/common/src/Dict.cpp b/common/src/Dict.cpp
index cab960b..12d7bff 100644
--- a/common/src/Dict.cpp
+++ b/common/src/Dict.cpp
@@ -246,6 +246,9 @@ void Dict::loadWord2Vec(std::filesystem::path & path)
 
   std::fclose(file);
 
+  if (firstLine)
+    util::myThrow(fmt::format("file '{}' is empty", path.string()));
+
   setState(originalState);
 }
 
diff --git a/torch_modules/src/Submodule.cpp b/torch_modules/src/Submodule.cpp
index e58ff28..ea63b99 100644
--- a/torch_modules/src/Submodule.cpp
+++ b/torch_modules/src/Submodule.cpp
@@ -60,6 +60,9 @@ void Submodule::loadPretrainedW2vEmbeddings(torch::nn::Embedding & embeddings, s
 
   std::fclose(file);
 
+  if (firstLine)
+    util::myThrow(fmt::format("file '{}' is empty", path.string()));
+
   getDict().setState(originalState);
 }
 
-- 
GitLab