char_ngram_string_extractor_test.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Copyright 2017 Google Inc. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. #include "syntaxnet/char_ngram_string_extractor.h"
  13. #include <set>
  14. #include <string>
  15. #include "syntaxnet/task_context.h"
  16. #include <gmock/gmock.h>
  17. #include "tensorflow/core/platform/test.h"
  18. namespace syntaxnet {
  19. namespace {
  20. TEST(CharNgramStringExtractorTest, GetConfigId) {
  21. // The 0'th extractor is default-configured; others differ in one of the four
  22. // configuration settings.
  23. std::vector<CharNgramStringExtractor> extractors(5);
  24. extractors[1].set_min_length(2);
  25. extractors[2].set_max_length(4);
  26. extractors[3].set_add_terminators(true);
  27. extractors[4].set_mark_boundaries(true);
  28. // Assert that all config IDs are unique.
  29. std::set<string> ids;
  30. for (CharNgramStringExtractor &extractor : extractors) {
  31. extractor.Setup(TaskContext());
  32. const string id = extractor.GetConfigId();
  33. EXPECT_TRUE(ids.emplace(id).second) << "Duplicate id: " << id;
  34. }
  35. EXPECT_EQ(extractors.size(), ids.size());
  36. }
  37. // Returns the character n-grams extracted from the |word| based on the
  38. // configuration settings.
  39. std::multiset<string> ExtractCharNgramSet(const string &word,
  40. const int min_length,
  41. const int max_length,
  42. const bool add_terminators,
  43. const bool mark_boundaries) {
  44. std::multiset<string> ngrams;
  45. CharNgramStringExtractor extractor;
  46. extractor.set_min_length(min_length);
  47. extractor.set_max_length(max_length);
  48. extractor.set_add_terminators(add_terminators);
  49. extractor.set_mark_boundaries(mark_boundaries);
  50. extractor.Setup(TaskContext());
  51. extractor.Extract(word, [&](const string &ngram) { ngrams.insert(ngram); });
  52. return ngrams;
  53. }
  54. TEST(CharNgramStringExtractorTest, Normal) {
  55. const std::multiset<string> expected = {"h", "he", "hel", "e", "el", "ell",
  56. "l", "ll", "llo", "l", "lo", "o"};
  57. EXPECT_EQ(expected, ExtractCharNgramSet("hello", 1, 3, false, false));
  58. }
  59. TEST(CharNgramStringExtractorTest, MinLength) {
  60. const std::multiset<string> expected = {"he", "hel", "el", "ell",
  61. "ll", "llo", "lo"};
  62. EXPECT_EQ(expected, ExtractCharNgramSet("hello", 2, 3, false, false));
  63. }
  64. TEST(CharNgramStringExtractorTest, SpaceInMiddle) {
  65. const std::multiset<string> expected = {"h", "he", "e", "l", "lo", "o"};
  66. EXPECT_EQ(expected, ExtractCharNgramSet("he lo", 1, 3, false, false));
  67. }
  68. TEST(CharNgramStringExtractorTest, AddTerminators) {
  69. const std::multiset<string> expected = {"^", "^h", "^he", "h", "he", "hel",
  70. "e", "el", "ell", "l", "ll", "llo",
  71. "l", "lo", "lo$", "o", "o$", "$"};
  72. EXPECT_EQ(expected, ExtractCharNgramSet("hello", 1, 3, true, false));
  73. }
  74. TEST(CharNgramStringExtractorTest, MarkBoundaries) {
  75. const std::multiset<string> expected = {"^ h", "^ he", "^ hel", "e",
  76. "el", "ell", "l", "ll",
  77. "llo $", "l", "lo $", "o $"};
  78. EXPECT_EQ(expected, ExtractCharNgramSet("hello", 1, 3, false, true));
  79. }
  80. TEST(CharNgramStringExtractorTest, MarkBoundariesSingleton) {
  81. const std::multiset<string> expected = {"^ h $"};
  82. EXPECT_EQ(expected, ExtractCharNgramSet("h", 1, 3, false, true));
  83. }
  84. TEST(CharNgramStringExtractorTest, MarkBoundariesLeadingSpace) {
  85. const std::multiset<string> expected = {"e", "el", "ell", "l", "ll",
  86. "llo $", "l", "lo $", "o $"};
  87. EXPECT_EQ(expected, ExtractCharNgramSet(" ello", 1, 3, false, true));
  88. }
  89. TEST(CharNgramStringExtractorTest, MarkBoundariesTrailingSpace) {
  90. const std::multiset<string> expected = {"^ h", "^ he", "^ hel", "e", "el",
  91. "ell", "l", "ll", "l"};
  92. EXPECT_EQ(expected, ExtractCharNgramSet("hell ", 1, 3, false, true));
  93. }
  94. TEST(CharNgramStringExtractorTest, MarkBoundariesLeadingAndTrailingSpace) {
  95. const std::multiset<string> expected = {"e", "el", "ell", "l", "ll", "l"};
  96. EXPECT_EQ(expected, ExtractCharNgramSet(" ell ", 1, 3, false, true));
  97. }
  98. } // namespace
  99. } // namespace syntaxnet