once_transitions.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/base.h"
  13. #include "syntaxnet/parser_state.h"
  14. #include "syntaxnet/parser_transitions.h"
  15. using tensorflow::strings::StrCat;
  16. namespace syntaxnet {
  17. namespace {
  18. // A transition system that has exactly one action and performs exactly one
  19. // action per state, regardless of sentence length.
  20. class OnceTransitionSystem : public ParserTransitionSystem {
  21. public:
  22. // A transition state that allows exactly one transition.
  23. class State : public ParserTransitionState {
  24. public:
  25. // Implements TransitionState.
  26. State *Clone() const override { return new State(*this); }
  27. void Init(ParserState *state) override {}
  28. bool IsTokenCorrect(const ParserState &state, int index) const override {
  29. return true;
  30. }
  31. string ToString(const ParserState &state) const override {
  32. return StrCat("done=", done() ? "true" : "false");
  33. }
  34. // Records that a transition has been performed.
  35. void PerformAction() { done_ = true; }
  36. // Returns true if no more transitions are allowed.
  37. bool done() const { return done_; }
  38. private:
  39. bool done_ = false; // true if no more transitions are allowed
  40. };
  41. // Implements ParserTransitionSystem.
  42. int NumActionTypes() const override { return 1; }
  43. int NumActions(int num_labels) const override { return 1; }
  44. ParserAction GetDefaultAction(const ParserState &state) const override {
  45. DCHECK(!IsFinalState(state));
  46. return 0;
  47. }
  48. ParserAction GetNextGoldAction(const ParserState &state) const override {
  49. return GetDefaultAction(state);
  50. }
  51. void PerformActionWithoutHistory(ParserAction action,
  52. ParserState *state) const override {
  53. DCHECK(!IsFinalState(*state));
  54. static_cast<State *>(state->mutable_transition_state())->PerformAction();
  55. }
  56. bool IsAllowedAction(ParserAction action,
  57. const ParserState &state) const override {
  58. return action == 0 && !IsFinalState(state);
  59. }
  60. bool IsFinalState(const ParserState &state) const override {
  61. return static_cast<const State *>(state.transition_state())->done();
  62. }
  63. string ActionAsString(ParserAction action,
  64. const ParserState &state) const override {
  65. return StrCat("action:", action);
  66. }
  67. ParserTransitionState *NewTransitionState(bool training_mode) const override {
  68. return new State();
  69. }
  70. bool IsDeterministicState(const ParserState &state) const override {
  71. return true; // all states have only one action
  72. }
  73. };
  74. REGISTER_TRANSITION_SYSTEM("once", OnceTransitionSystem);
  75. } // namespace
  76. } // namespace syntaxnet