arc_standard_transitions_test.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 <memory>
  13. #include <string>
  14. #include "syntaxnet/parser_state.h"
  15. #include "syntaxnet/parser_transitions.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 <gmock/gmock.h>
  23. #include "tensorflow/core/lib/core/status.h"
  24. #include "tensorflow/core/platform/env.h"
  25. #include "tensorflow/core/platform/test.h"
  26. namespace syntaxnet {
  27. class ArcStandardTransitionTest : public ::testing::Test {
  28. public:
  29. ArcStandardTransitionTest()
  30. : transition_system_(ParserTransitionSystem::Create("arc-standard")) {}
  31. protected:
  32. // Creates a label map and a tag map for testing based on the given
  33. // document and initializes the transition system appropriately.
  34. void SetUpForDocument(const Sentence &document) {
  35. input_label_map_ = context_.GetInput("label-map", "text", "");
  36. transition_system_->Setup(&context_);
  37. PopulateTestInputs::Defaults(document).Populate(&context_);
  38. label_map_.Load(TaskContext::InputFile(*input_label_map_),
  39. 0 /* minimum frequency */,
  40. -1 /* maximum number of terms */);
  41. transition_system_->Init(&context_);
  42. }
  43. // Creates a cloned state from a sentence in order to test that cloning
  44. // works correctly for the new parser states.
  45. ParserState *NewClonedState(Sentence *sentence) {
  46. ParserState state(sentence, transition_system_->NewTransitionState(
  47. true /* training mode */),
  48. &label_map_);
  49. return state.Clone();
  50. }
  51. // Performs gold transitions and check that the labels and heads recorded
  52. // in the parser state match gold heads and labels.
  53. void GoldParse(Sentence *sentence) {
  54. ParserState *state = NewClonedState(sentence);
  55. LOG(INFO) << "Initial parser state: " << state->ToString();
  56. while (!transition_system_->IsFinalState(*state)) {
  57. ParserAction action = transition_system_->GetNextGoldAction(*state);
  58. EXPECT_TRUE(transition_system_->IsAllowedAction(action, *state));
  59. LOG(INFO) << "Performing action: "
  60. << transition_system_->ActionAsString(action, *state);
  61. transition_system_->PerformActionWithoutHistory(action, state);
  62. LOG(INFO) << "Parser state: " << state->ToString();
  63. }
  64. for (int i = 0; i < sentence->token_size(); ++i) {
  65. EXPECT_EQ(state->GoldLabel(i), state->Label(i));
  66. EXPECT_EQ(state->GoldHead(i), state->Head(i));
  67. }
  68. delete state;
  69. }
  70. // Always takes the default action, and verifies that this leads to
  71. // a final state through a sequence of allowed actions.
  72. void DefaultParse(Sentence *sentence) {
  73. ParserState *state = NewClonedState(sentence);
  74. LOG(INFO) << "Initial parser state: " << state->ToString();
  75. while (!transition_system_->IsFinalState(*state)) {
  76. ParserAction action = transition_system_->GetDefaultAction(*state);
  77. EXPECT_TRUE(transition_system_->IsAllowedAction(action, *state));
  78. LOG(INFO) << "Performing action: "
  79. << transition_system_->ActionAsString(action, *state);
  80. transition_system_->PerformActionWithoutHistory(action, state);
  81. LOG(INFO) << "Parser state: " << state->ToString();
  82. }
  83. delete state;
  84. }
  85. TaskContext context_;
  86. TaskInput *input_label_map_ = nullptr;
  87. TermFrequencyMap label_map_;
  88. std::unique_ptr<ParserTransitionSystem> transition_system_;
  89. };
  90. TEST_F(ArcStandardTransitionTest, SingleSentenceDocumentTest) {
  91. string document_text;
  92. Sentence document;
  93. TF_CHECK_OK(ReadFileToString(
  94. tensorflow::Env::Default(),
  95. "syntaxnet/testdata/document", &document_text));
  96. LOG(INFO) << "see doc\n:" << document_text;
  97. CHECK(TextFormat::ParseFromString(document_text, &document));
  98. SetUpForDocument(document);
  99. GoldParse(&document);
  100. DefaultParse(&document);
  101. }
  102. } // namespace syntaxnet