Skip to content

Commit

Permalink
Compile and fix warning for regex in SQL action seq formatting (allen…
Browse files Browse the repository at this point in the history
…ai#1864)

* compile regex

* replace the other regex too
  • Loading branch information
kl2806 authored Oct 4, 2018
1 parent 5172c85 commit 8236624
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions allennlp/semparse/contexts/sql_context_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from parsimonious.nodes import Node, NodeVisitor
from parsimonious.grammar import Grammar

WHITESPACE_REGEX = re.compile(" wsp |wsp | wsp| ws |ws | ws")

def format_grammar_string(grammar_dictionary: Dict[str, List[str]]) -> str:
"""
Formats a dictionary of production rules into the string format expected
Expand Down Expand Up @@ -98,7 +100,7 @@ def format_action(nonterminal: str,

else:
right_hand_side = right_hand_side.lstrip("(").rstrip(")")
child_strings = [token for token in re.split(" wsp |wsp | wsp| ws |ws | ws|", right_hand_side) if token]
child_strings = [token for token in WHITESPACE_REGEX.split(right_hand_side) if token]
child_strings = [tok.upper() if tok.upper() in keywords_to_uppercase else tok for tok in child_strings]
return f"{nonterminal} -> [{', '.join(child_strings)}]"

Expand Down Expand Up @@ -175,8 +177,7 @@ def add_action(self, node: Node) -> None:
else:
child_right_side_string = child.expr._as_rhs().lstrip("(").rstrip(")") # pylint: disable=protected-access
child_right_side_list = [tok for tok in
re.split(" wsp |wsp | wsp| ws |ws | ws|",
child_right_side_string) if tok]
WHITESPACE_REGEX.split(child_right_side_string) if tok]
child_right_side_list = [tok.upper() if tok.upper() in
self.keywords_to_uppercase else tok
for tok in child_right_side_list]
Expand Down

0 comments on commit 8236624

Please sign in to comment.