-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix problem with newline in vocab files #619
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -256,7 +256,7 @@ Dictionary::Gather(const GatherDictionaryArgs& args, | |
int token_id = 0; | ||
while (!vocab.eof()) { | ||
std::getline(vocab, str); | ||
if (vocab.eof()) | ||
if (vocab.eof() && str.empty()) | ||
break; | ||
|
||
boost::algorithm::trim(str); | ||
|
@@ -545,3 +545,4 @@ float Dictionary::CountTopicCoherence(const std::vector<core::Token>& tokens_to_ | |
|
||
} // namespace core | ||
} // namespace artm | ||
// vim: set ts=2 sw=2: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, with additions in |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,36 +46,48 @@ TEST(CollectionParser, UciBagOfWords) { | |
|
||
artm::MasterModelConfig master_config; | ||
::artm::MasterModel mc(master_config); | ||
artm::GatherDictionaryArgs gather_config; | ||
gather_config.set_data_path(target_folder); | ||
gather_config.set_vocab_file_path(config.vocab_file_path()); | ||
gather_config.set_dictionary_target_name("mydictionary"); | ||
mc.GatherDictionary(gather_config); | ||
|
||
::artm::GetDictionaryArgs get_dictionary_args; | ||
get_dictionary_args.set_dictionary_name("mydictionary"); | ||
auto dictionary = mc.GetDictionary(get_dictionary_args); | ||
ASSERT_EQ(dictionary.token_size(), 3); | ||
auto dictionary_checker = [&mc, &target_folder] ( | ||
const std::string &path_to_vocab, | ||
const std::string &dict_name) -> void { | ||
// first of all, we gather dictionary into the core | ||
artm::GatherDictionaryArgs gather_config; | ||
gather_config.set_data_path(target_folder); | ||
gather_config.set_vocab_file_path(path_to_vocab); | ||
gather_config.set_dictionary_target_name(dict_name); | ||
mc.GatherDictionary(gather_config); | ||
|
||
// next, we retrieve it from the core | ||
::artm::GetDictionaryArgs get_dictionary_args; | ||
get_dictionary_args.set_dictionary_name(dict_name); | ||
auto dict = mc.GetDictionary(get_dictionary_args); | ||
|
||
// now we check its consistency | ||
ASSERT_EQ(dict.token_size(), 3); | ||
|
||
EXPECT_EQ(dict.token(0), "token1"); | ||
EXPECT_EQ(dict.token(1), "token2"); | ||
EXPECT_EQ(dict.token(2), "token3"); | ||
|
||
EXPECT_EQ(dictionary.token(0), "token1"); | ||
EXPECT_EQ(dictionary.token(1), "token2"); | ||
EXPECT_EQ(dictionary.token(2), "token3"); | ||
EXPECT_EQ(dict.class_id(0), "@default_class"); | ||
EXPECT_EQ(dict.class_id(1), "@default_class"); | ||
EXPECT_EQ(dict.class_id(2), "@default_class"); | ||
|
||
EXPECT_EQ(dictionary.class_id(0), "@default_class"); | ||
EXPECT_EQ(dictionary.class_id(1), "@default_class"); | ||
EXPECT_EQ(dictionary.class_id(2), "@default_class"); | ||
EXPECT_EQ(dict.token_df(0), 1); | ||
EXPECT_EQ(dict.token_df(1), 2); | ||
EXPECT_EQ(dict.token_df(2), 2); | ||
|
||
EXPECT_EQ(dictionary.token_df(0), 1); | ||
EXPECT_EQ(dictionary.token_df(1), 2); | ||
EXPECT_EQ(dictionary.token_df(2), 2); | ||
EXPECT_EQ(dict.token_tf(0), 5); | ||
EXPECT_EQ(dict.token_tf(1), 4); | ||
EXPECT_EQ(dict.token_tf(2), 9); | ||
|
||
EXPECT_EQ(dictionary.token_tf(0), 5); | ||
EXPECT_EQ(dictionary.token_tf(1), 4); | ||
EXPECT_EQ(dictionary.token_tf(2), 9); | ||
ASSERT_APPROX_EQ(dict.token_value(0), 5.0 / 18.0); | ||
ASSERT_APPROX_EQ(dict.token_value(1), 2.0 / 9.0); | ||
ASSERT_APPROX_EQ(dict.token_value(2), 0.5); | ||
}; | ||
|
||
ASSERT_APPROX_EQ(dictionary.token_value(0), 5.0 / 18.0); | ||
ASSERT_APPROX_EQ(dictionary.token_value(1), 2.0 / 9.0); | ||
ASSERT_APPROX_EQ(dictionary.token_value(2), 0.5); | ||
dictionary_checker(config.vocab_file_path(), "default_dictionary"); | ||
dictionary_checker("../../../test_data/vocab.parser_test_no_newline.txt", "no_newline_dictionary"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
try { boost::filesystem::remove_all(target_folder); } | ||
catch (...) {} | ||
|
@@ -241,3 +253,4 @@ TEST(CollectionParser, VowpalWabbit) { | |
try { boost::filesystem::remove_all(target_folder); } | ||
catch (...) {} | ||
} | ||
// vim: set ts=2 sw=2 sts=2: |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
token1 | ||
token2 | ||
token3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!