whole_sentence_features_test.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/whole_sentence_features.h"
  13. #include <memory>
  14. #include "syntaxnet/feature_extractor.h"
  15. #include "syntaxnet/parser_features.h"
  16. #include "syntaxnet/parser_state.h"
  17. #include "syntaxnet/sentence.pb.h"
  18. #include "syntaxnet/task_context.h"
  19. #include "syntaxnet/term_frequency_map.h"
  20. #include "syntaxnet/workspace.h"
  21. #include "tensorflow/core/platform/test.h"
  22. namespace syntaxnet {
  23. namespace {
  24. // Testing rig for exercising whole-sentence features, forwarded through parser
  25. // features.
  26. class WholeSentenceFeaturesTest : public ::testing::Test {
  27. protected:
  28. // Initializes the feature extractor from the |spec|.
  29. void Init(const string &spec) {
  30. extractor_.Parse(spec);
  31. extractor_.Setup(&context_);
  32. extractor_.Init(&context_);
  33. extractor_.RequestWorkspaces(&registry_);
  34. workspaces_.Reset(registry_);
  35. state_.reset(new ParserState(&sentence_, nullptr /* no transition state */,
  36. &label_map_));
  37. extractor_.Preprocess(&workspaces_, state_.get());
  38. }
  39. // Checks that the whole-sentence feature fired with the expected value.
  40. // Assumes Init() has been called.
  41. void ExpectValue(string value) {
  42. FeatureVector result;
  43. extractor_.ExtractFeatures(workspaces_, *state_, &result);
  44. ASSERT_EQ(result.size(), 1);
  45. EXPECT_EQ(value, result.type(0)->GetFeatureValueName(result.value(0)));
  46. }
  47. Token *AddToken() { return sentence_.add_token(); }
  48. TermFrequencyMap label_map_;
  49. TaskContext context_;
  50. WorkspaceRegistry registry_;
  51. ParserFeatureExtractor extractor_;
  52. Sentence sentence_;
  53. WorkspaceSet workspaces_;
  54. std::unique_ptr<ParserState> state_;
  55. };
  56. TEST_F(WholeSentenceFeaturesTest, SentenceLengthEmpty) {
  57. Init("sentence.length");
  58. ExpectValue("0");
  59. }
  60. TEST_F(WholeSentenceFeaturesTest, SentenceLengthPopulated) {
  61. AddToken()->set_word("test");
  62. AddToken()->set_word("test");
  63. AddToken()->set_word("test");
  64. Init("sentence.length");
  65. ExpectValue("3");
  66. }
  67. TEST_F(WholeSentenceFeaturesTest, SentenceLengthClipped) {
  68. AddToken()->set_word("test");
  69. AddToken()->set_word("test");
  70. AddToken()->set_word("test");
  71. Init("sentence.length(max-length=1)");
  72. ExpectValue("1");
  73. }
  74. } // namespace
  75. } // namespace syntaxnet