parser_features_test.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* Copyright 2016 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/parser_features.h"
  13. #include <string>
  14. #include "syntaxnet/feature_extractor.h"
  15. #include "syntaxnet/parser_state.h"
  16. #include "syntaxnet/populate_test_inputs.h"
  17. #include "syntaxnet/sentence.pb.h"
  18. #include "syntaxnet/task_context.h"
  19. #include "syntaxnet/task_spec.pb.h"
  20. #include "syntaxnet/term_frequency_map.h"
  21. #include "syntaxnet/utils.h"
  22. #include "syntaxnet/workspace.h"
  23. #include "tensorflow/core/lib/strings/strcat.h"
  24. #include "tensorflow/core/platform/test.h"
  25. namespace syntaxnet {
  26. // Feature extractor for the transition parser based on a parser state object.
  27. typedef FeatureExtractor<ParserState, int> ParserIndexFeatureExtractor;
  28. // Test fixture for parser features.
  29. class ParserFeatureFunctionTest : public ::testing::Test {
  30. protected:
  31. // Sets up a parser state.
  32. void SetUp() override {
  33. // Prepare a document.
  34. const char *kTaggedDocument =
  35. "text: 'I saw a man with a telescope.' "
  36. "token { word: 'I' start: 0 end: 0 tag: 'PRP' category: 'PRON'"
  37. " break_level: NO_BREAK head: 1 } "
  38. "token { word: 'saw' start: 2 end: 4 tag: 'VBD' category: 'VERB'"
  39. " break_level: SPACE_BREAK } "
  40. "token { word: 'a' start: 6 end: 6 tag: 'DT' category: 'DET'"
  41. " break_level: SPACE_BREAK head: 3 } "
  42. "token { word: 'man' start: 8 end: 10 tag: 'NN' category: 'NOUN'"
  43. " break_level: SPACE_BREAK head: 1 } "
  44. "token { word: 'with' start: 12 end: 15 tag: 'IN' category: 'ADP'"
  45. " break_level: SPACE_BREAK head: 1 } "
  46. "token { word: 'a' start: 17 end: 17 tag: 'DT' category: 'DET'"
  47. " break_level: SPACE_BREAK head: 6 } "
  48. "token { word: 'telescope' start: 19 end: 27 tag: 'NN' category: 'NOUN'"
  49. " break_level: SPACE_BREAK head: 4 } "
  50. "token { word: '.' start: 28 end: 28 tag: '.' category: '.'"
  51. " break_level: NO_BREAK head: 1 }";
  52. CHECK(TextFormat::ParseFromString(kTaggedDocument, &sentence_));
  53. creators_ = PopulateTestInputs::Defaults(sentence_);
  54. // Prepare a label map. By adding labels in lexicographic order we make sure
  55. // the term indices stay the same after sorting (which happens when the
  56. // label map is saved to disk).
  57. label_map_.Increment("NULL");
  58. label_map_.Increment("ROOT");
  59. label_map_.Increment("det");
  60. label_map_.Increment("dobj");
  61. label_map_.Increment("nsubj");
  62. label_map_.Increment("p");
  63. label_map_.Increment("pobj");
  64. label_map_.Increment("prep");
  65. creators_.Add("label-map", "text", "", [this](const string &filename) {
  66. label_map_.Save(filename);
  67. });
  68. // Prepare a parser state.
  69. state_.reset(new ParserState(&sentence_, nullptr /* no transition state */,
  70. &label_map_));
  71. }
  72. // Prepares a feature for computations.
  73. string ExtractFeature(const string &feature_name) {
  74. context_.mutable_spec()->mutable_input()->Clear();
  75. context_.mutable_spec()->mutable_output()->Clear();
  76. feature_extractor_.reset(new ParserFeatureExtractor());
  77. feature_extractor_->Parse(feature_name);
  78. feature_extractor_->Setup(&context_);
  79. creators_.Populate(&context_);
  80. feature_extractor_->Init(&context_);
  81. feature_extractor_->RequestWorkspaces(&registry_);
  82. workspaces_.Reset(registry_);
  83. feature_extractor_->Preprocess(&workspaces_, state_.get());
  84. FeatureVector result;
  85. feature_extractor_->ExtractFeatures(workspaces_, *state_, &result);
  86. return result.type(0)->GetFeatureValueName(result.value(0));
  87. }
  88. std::unique_ptr<ParserState> state_;
  89. Sentence sentence_;
  90. WorkspaceSet workspaces_;
  91. TermFrequencyMap label_map_;
  92. PopulateTestInputs::CreatorMap creators_;
  93. TaskContext context_;
  94. WorkspaceRegistry registry_;
  95. std::unique_ptr<ParserFeatureExtractor> feature_extractor_;
  96. };
  97. TEST_F(ParserFeatureFunctionTest, TagFeatureFunction) {
  98. state_->Push(-1);
  99. state_->Push(0);
  100. EXPECT_EQ("PRP", ExtractFeature("input.tag"));
  101. EXPECT_EQ("VBD", ExtractFeature("input(1).tag"));
  102. EXPECT_EQ("<OUTSIDE>", ExtractFeature("input(10).tag"));
  103. EXPECT_EQ("PRP", ExtractFeature("stack(0).tag"));
  104. EXPECT_EQ("<ROOT>", ExtractFeature("stack(1).tag"));
  105. }
  106. TEST_F(ParserFeatureFunctionTest, LabelFeatureFunction) {
  107. // Construct a partial dependency tree.
  108. state_->AddArc(0, 1, 4);
  109. state_->AddArc(1, -1, 1);
  110. state_->AddArc(2, 3, 2);
  111. state_->AddArc(3, 1, 3);
  112. state_->AddArc(5, 6, 2);
  113. state_->AddArc(6, 4, 6);
  114. state_->AddArc(7, 1, 5);
  115. // Test the feature function.
  116. EXPECT_EQ(label_map_.GetTerm(4), ExtractFeature("input.label"));
  117. EXPECT_EQ("ROOT", ExtractFeature("input(1).label"));
  118. EXPECT_EQ(label_map_.GetTerm(2), ExtractFeature("input(2).label"));
  119. // Push artifical root token onto the stack. This triggers the wrapped <ROOT>
  120. // value, rather than indicating a token with the label "ROOT" (which may or
  121. // may not be the artificial root token.)
  122. state_->Push(-1);
  123. EXPECT_EQ("<ROOT>", ExtractFeature("stack.label"));
  124. }
  125. TEST_F(ParserFeatureFunctionTest, GoldHeadFeatureFunction) {
  126. EXPECT_EQ("1", ExtractFeature("input.gold-head"));
  127. EXPECT_EQ("", ExtractFeature("input(1).gold-head")); // extracts -1
  128. EXPECT_EQ("3", ExtractFeature("input(2).gold-head"));
  129. EXPECT_EQ("1", ExtractFeature("input(3).gold-head"));
  130. EXPECT_EQ("1", ExtractFeature("input(4).gold-head"));
  131. EXPECT_EQ("6", ExtractFeature("input(5).gold-head"));
  132. EXPECT_EQ("4", ExtractFeature("input(6).gold-head"));
  133. EXPECT_EQ("1", ExtractFeature("input(7).gold-head"));
  134. }
  135. } // namespace syntaxnet