def query_adjustment_to_action_sequence(
      self,
      query_adjustment,
      answer_words: Sequence[str],
      title_words: Sequence[str],
      question_words: Sequence[str],
      document_words: Sequence[str],
      tokenize_fn: Callable[[str], Sequence[str]],
      fallback_term_type: Optional[str] = None) -> Tuple[bool, Sequence[int]]:
    """Get the action sequence from a single QueryAdjustment."""

    def _term_type_from_action_sequence(action_sequence):
      """Get the term_type from a step action sequence."""
      for action in action_sequence:
        lhs = str(self.productions()[action].lhs())
        if lhs in ['_Wa_', '_Wt_', '_Wq_', '_Wd_']:
          return lhs
      return None

    query_addon = []
    query_addon.extend([query_adjustment.operator.value])
    if query_adjustment.operator != Operator.APPEND:
      query_addon.extend([query_adjustment.field.value])
    query_addon.extend(tokenize_fn(query_adjustment.term))

    # Find the vocabulary the word came from.
    if query_adjustment.term in answer_words:
      term_type = '_Wa_'
    elif query_adjustment.term in title_words:
      term_type = '_Wt_'
    elif query_adjustment.term in question_words:
      term_type = '_Wq_'
    elif query_adjustment.term in document_words:
      term_type = '_Wd_'
    else:
      # The term is not in one of the legal vocabularies.
      if fallback_term_type:
        logging.warning(
            "Couldn't find term type for term '%s'. Using fallback term type '%s'",
            query_adjustment.term, fallback_term_type)
        term_type = fallback_term_type
      else:
        return False, []

    parser = nltk.parse.BottomUpChartParser(grammar=self)
    for parsed_step in parser.parse(query_addon + ['[stop]']):
      action_sequence = [
          self.production_to_action(production)
          for production in parsed_step.productions()
      ]
      if term_type == _term_type_from_action_sequence(action_sequence):
        return True, action_sequence[:-1]

    # Couldn't find a parse
    return False, []
Get the action sequence from a single QueryAdjustment.
language/search_agents/muzero/state_tree.py