label_transitions.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/label_transitions.h"
  13. #include "syntaxnet/base.h"
  14. using tensorflow::strings::StrAppend;
  15. using tensorflow::strings::StrCat;
  16. namespace syntaxnet {
  17. // Parser transition state for label transitions.
  18. class LabelTransitionSystem::State : public ParserTransitionState {
  19. public:
  20. // Returns a copy of this state.
  21. State *Clone() const override { return new State(*this); }
  22. // Does nothing; no need for additional initialization.
  23. void Init(ParserState *state) override {}
  24. // Copies the selected labels to the |sentence|.
  25. void AddParseToDocument(const ParserState &state, bool rewrite_root_labels,
  26. Sentence *sentence) const override {
  27. for (int i = 0; i < state.NumTokens(); ++i) {
  28. Token *token = sentence->mutable_token(i);
  29. token->set_label(state.LabelAsString(state.Label(i)));
  30. if (rewrite_root_labels && state.Head(i) == -1) {
  31. token->set_label(state.LabelAsString(state.RootLabel()));
  32. }
  33. }
  34. }
  35. // Returns true if the label and gold label match.
  36. bool IsTokenCorrect(const ParserState &state, int index) const override {
  37. return state.GoldLabel(index) == state.Label(index);
  38. }
  39. // Returns a string representation of the |state|.
  40. string ToString(const ParserState &state) const override {
  41. string str = "[";
  42. for (int i = 0; i < state.NumTokens(); ++i) {
  43. StrAppend(&str, i == 0 ? "" : " ", state.LabelAsString(state.Label(i)));
  44. }
  45. StrAppend(&str, "]");
  46. return str;
  47. }
  48. };
  49. ParserAction LabelTransitionSystem::GetDefaultAction(
  50. const ParserState &state) const {
  51. return state.RootLabel();
  52. }
  53. ParserAction LabelTransitionSystem::GetNextGoldAction(
  54. const ParserState &state) const {
  55. if (state.EndOfInput()) {
  56. LOG(ERROR) << "Oracle called on invalid state: " << state.ToString();
  57. return 0;
  58. }
  59. const int current = state.Next();
  60. return state.GoldLabel(current);
  61. }
  62. void LabelTransitionSystem::PerformActionWithoutHistory(
  63. ParserAction action, ParserState *state) const {
  64. const int current = state->Next();
  65. const int head = state->GoldHead(current);
  66. CHECK(IsAllowedAction(action, *state))
  67. << "Illegal action " << action << " (root label " << state->RootLabel()
  68. << ") with current=" << current << " and head=" << head
  69. << " at state: " << state->ToString() << "\ndocument:\n"
  70. << state->sentence().DebugString();
  71. VLOG(2) << "Adding arc: " << action << " (" << current << " <- " << head
  72. << ")";
  73. state->AddArc(current, head, action);
  74. state->Advance();
  75. }
  76. bool LabelTransitionSystem::IsAllowedAction(ParserAction action,
  77. const ParserState &state) const {
  78. if (state.EndOfInput()) return false;
  79. if (action < 0 || action >= state.NumLabels()) return false;
  80. const int head = state.GoldHead(state.Next());
  81. const bool is_root_head = head < 0;
  82. const bool is_root_label = action == state.RootLabel();
  83. // The root label is allowed iff the head is the root.
  84. return is_root_head == is_root_label;
  85. }
  86. bool LabelTransitionSystem::IsFinalState(const ParserState &state) const {
  87. return state.EndOfInput();
  88. }
  89. string LabelTransitionSystem::ActionAsString(ParserAction action,
  90. const ParserState &state) const {
  91. if (!IsAllowedAction(action, state)) return StrCat("INVALID:", action);
  92. const auto &sentence = state.sentence();
  93. const int current = state.Next();
  94. const int head = state.GoldHead(current);
  95. return StrCat(state.LabelAsString(action), "(",
  96. sentence.token(current).word(), "<-",
  97. head == -1 ? "ROOT" : sentence.token(head).word(), ")");
  98. }
  99. ParserTransitionState *LabelTransitionSystem::NewTransitionState(
  100. bool training_mode) const {
  101. return new State();
  102. }
  103. REGISTER_TRANSITION_SYSTEM("labels", LabelTransitionSystem);
  104. } // namespace syntaxnet