head_transitions.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/head_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 head transitions.
  18. class HeadTransitionSystem::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 heads 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. if (state.Head(i) != -1) {
  30. token->set_head(state.Head(i));
  31. } else {
  32. token->clear_head();
  33. }
  34. }
  35. }
  36. // Returns true if the head and gold head match.
  37. bool IsTokenCorrect(const ParserState &state, int index) const override {
  38. return state.GoldHead(index) == state.Head(index);
  39. }
  40. // Returns a string representation of the |state|.
  41. string ToString(const ParserState &state) const override {
  42. string str = "[";
  43. for (int i = 0; i < state.NumTokens(); ++i) {
  44. StrAppend(&str, i == 0 ? "" : " ", state.Head(i));
  45. }
  46. StrAppend(&str, "]");
  47. return str;
  48. }
  49. };
  50. ParserAction HeadTransitionSystem::GetDefaultAction(
  51. const ParserState &state) const {
  52. return state.Next();
  53. }
  54. ParserAction HeadTransitionSystem::GetNextGoldAction(
  55. const ParserState &state) const {
  56. if (state.EndOfInput()) {
  57. LOG(ERROR) << "Oracle called on invalid state: " << state.ToString();
  58. return 0;
  59. }
  60. const int current = state.Next();
  61. const int head = state.GoldHead(current);
  62. return head == -1 ? current : head;
  63. }
  64. void HeadTransitionSystem::PerformActionWithoutHistory(
  65. ParserAction action, ParserState *state) const {
  66. CHECK(IsAllowedAction(action, *state)) << "Illegal action " << action
  67. << " at state: " << state->ToString();
  68. const int current = state->Next();
  69. if (action == current) action = -1; // self connect = root
  70. VLOG(2) << "Adding arc: " << current << " <- " << action;
  71. state->AddArc(current, action, 0);
  72. state->Advance();
  73. }
  74. bool HeadTransitionSystem::IsAllowedAction(ParserAction action,
  75. const ParserState &state) const {
  76. if (state.EndOfInput()) return false;
  77. return action >= 0 && action < state.sentence().token_size();
  78. }
  79. bool HeadTransitionSystem::IsFinalState(const ParserState &state) const {
  80. return state.EndOfInput();
  81. }
  82. string HeadTransitionSystem::ActionAsString(ParserAction action,
  83. const ParserState &state) const {
  84. if (!IsAllowedAction(action, state)) return StrCat("INVALID:", action);
  85. const auto &sentence = state.sentence();
  86. const int current = state.Next();
  87. return StrCat(action == current ? "ROOT" : sentence.token(action).word(),
  88. "->", sentence.token(current).word());
  89. }
  90. ParserTransitionState *HeadTransitionSystem::NewTransitionState(
  91. bool training_mode) const {
  92. return new State();
  93. }
  94. REGISTER_TRANSITION_SYSTEM("heads", HeadTransitionSystem);
  95. } // namespace syntaxnet