head_transitions_test.cc 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 <memory>
  13. #include "syntaxnet/base.h"
  14. #include "syntaxnet/parser_state.h"
  15. #include "syntaxnet/parser_transitions.h"
  16. #include "syntaxnet/sentence.pb.h"
  17. #include "syntaxnet/task_context.h"
  18. #include "syntaxnet/term_frequency_map.h"
  19. #include "tensorflow/core/platform/test.h"
  20. namespace syntaxnet {
  21. namespace {
  22. const char kSentence[] = R"(
  23. text: 'I saw a man with a telescope.'
  24. token { word: 'I' start: 0 end: 0 tag: 'PRP' category: 'PRON'
  25. head: 1 label: 'nsubj' break_level: NO_BREAK }
  26. token { word: 'saw' start: 2 end: 4 tag: 'VBD' category: 'VERB'
  27. label: 'ROOT' break_level: SPACE_BREAK }
  28. token { word: 'a' start: 6 end: 6 tag: 'DT' category: 'DET'
  29. head: 3 label: 'det' break_level: SPACE_BREAK }
  30. token { word: 'man' start: 8 end: 10 tag: 'NN' category: 'NOUN'
  31. head: 1 label: 'dobj' break_level: SPACE_BREAK }
  32. token { word: 'with' start: 12 end: 15 tag: 'IN' category: 'ADP'
  33. head: 1 label: 'prep' break_level: SPACE_BREAK }
  34. token { word: 'a' start: 17 end: 17 tag: 'DT' category: 'DET'
  35. head: 6 label: 'det' break_level: SPACE_BREAK }
  36. token { word: 'telescope' start: 19 end: 27 tag: 'NN' category: 'NOUN'
  37. head: 4 label: 'pobj' break_level: SPACE_BREAK }
  38. token { word: '.' start: 28 end: 28 tag: '.' category: '.'
  39. head: 1 label: 'p' break_level: NO_BREAK }
  40. )";
  41. class HeadTransitionSystemTest : public ::testing::Test {
  42. public:
  43. HeadTransitionSystemTest() {
  44. transition_system_->Setup(&context_);
  45. transition_system_->Init(&context_);
  46. CHECK(TextFormat::ParseFromString(kSentence, &sentence_));
  47. for (auto &token : sentence_.token()) label_map_.Increment(token.label());
  48. state_.reset(new ParserState(
  49. &sentence_, transition_system_->NewTransitionState(true), &label_map_));
  50. }
  51. protected:
  52. TermFrequencyMap label_map_;
  53. TaskContext context_;
  54. std::unique_ptr<ParserTransitionSystem> transition_system_{
  55. ParserTransitionSystem::Create("heads")};
  56. Sentence sentence_;
  57. std::unique_ptr<ParserState> state_;
  58. };
  59. TEST_F(HeadTransitionSystemTest, Characteristics) {
  60. EXPECT_EQ(1, transition_system_->NumActionTypes());
  61. EXPECT_EQ(1, transition_system_->NumActions(10));
  62. }
  63. TEST_F(HeadTransitionSystemTest, GoldParsesCorrectly) {
  64. LOG(INFO) << "Initial parser state: " << state_->ToString();
  65. while (!transition_system_->IsFinalState(*state_)) {
  66. ParserAction action = transition_system_->GetNextGoldAction(*state_);
  67. EXPECT_TRUE(transition_system_->IsAllowedAction(action, *state_));
  68. LOG(INFO) << "Performing action: "
  69. << transition_system_->ActionAsString(action, *state_);
  70. transition_system_->PerformActionWithoutHistory(action, state_.get());
  71. LOG(INFO) << "Parser state: " << state_->ToString();
  72. }
  73. for (int i = 0; i < state_->NumTokens(); ++i) {
  74. EXPECT_EQ(state_->GoldHead(i), state_->Head(i));
  75. }
  76. }
  77. } // namespace
  78. } // namespace syntaxnet