head_transitions.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #ifndef SYNTAXNET_HEAD_TRANSITIONS_H_
  13. #define SYNTAXNET_HEAD_TRANSITIONS_H_
  14. #include "syntaxnet/parser_state.h"
  15. #include "syntaxnet/parser_transitions.h"
  16. namespace syntaxnet {
  17. // Head transition system. Predicts the syntactic heads of a sentence directly.
  18. //
  19. // For a sentence with N tokens, actions are interpreted as follows:
  20. //
  21. // For input pointer at position i:
  22. //
  23. // Action A == i : Add a root arc to token i.
  24. // Action A != i : Add an arc A -> i.
  25. //
  26. // Note that in nlp_saft.Document, root arcs are token.head() == -1, whereas
  27. // here, we use a self-loop to represent roots.
  28. class HeadTransitionSystem : public ParserTransitionSystem {
  29. public:
  30. class State; // defined in the .cc file
  31. // Returns 1 for number of actions. This is because each action should be
  32. // scored separately; e.g. instead of a fixed output set, we have a single
  33. // scoring function.
  34. int NumActionTypes() const override { return 1; }
  35. int NumActions(int num_labels) const override { return 1; }
  36. // Returns the default action, which is to assign itself as root.
  37. ParserAction GetDefaultAction(const ParserState &state) const override;
  38. // Returns the next gold head for a given state according to the underlying
  39. // annotated sentence.
  40. ParserAction GetNextGoldAction(const ParserState &state) const override;
  41. // Returns true if the action is allowed in a given parser state.
  42. bool IsAllowedAction(ParserAction action,
  43. const ParserState &state) const override;
  44. // Performs the specified action on a given parser state, without adding the
  45. // action to the state's history.
  46. void PerformActionWithoutHistory(ParserAction action,
  47. ParserState *state) const override;
  48. // Returns true if the state is at the end of the input.
  49. bool IsFinalState(const ParserState &state) const override;
  50. // Returns a string representation of a parser action.
  51. string ActionAsString(ParserAction action,
  52. const ParserState &state) const override;
  53. // Returns a new transition state to be used to enhance the parser state.
  54. ParserTransitionState *NewTransitionState(bool training_mode) const override;
  55. // Returns false, since no states are deterministic.
  56. bool IsDeterministicState(const ParserState &state) const override {
  57. return false;
  58. }
  59. };
  60. } // namespace syntaxnet
  61. #endif // SYNTAXNET_HEAD_TRANSITIONS_H_