<gh_stars>0
import random
from num2words import num2words


def replace_tokens_all(strings, game_dim, n_samples_per_string, lang='en'):
    """
    Given set of possible strings generated by CFG, generates n_samples_per_string
    training points where x and y are bounded by game_dim. Replaces <Y>, <X> tokens
    in strings with numbers in word format.

    :param strings: iterable(str)
    :param game_dim: (int) dimension of board, assumes square board
    :param n_samples_per_string: (int)
    :return: list[
        input string (e.g. "go two up and ten left"),
        [x, y] (e.g. [2, 10]
    ]
    """
    training_set = []
    for string in strings:
        # Modify sign based on left, right
        x_sign, y_sign = _get_signs(string)

        # sample a different set of coordinates for each
        for i in range(n_samples_per_string):
            x, y = random.randint(1, game_dim), random.randint(1, game_dim)
            string_i = _replace_tokens(string, y, x, lang=lang)
            training_set.append([string_i, x * x_sign, y * y_sign])

    return training_set


def _replace_tokens(string, y, x, lang='en'):
    """
    Assumes tokens <Y> and <X> to mean change in y, x respectively.
    Replaces tokens with args y and x.
    :param string: (str) string containing at least one of <Y> and <X>.
    :param y: (int) positive integer
    :param x: (int) positive integer
    :param lang: 'en' or 'fr'
    :return: (str) string with <Y> replaced by y in words and <X> by x in words.
    """
    assert y > 0 and x > 0
    y_words = num2words(y, lang=lang)
    x_words = num2words(x, lang=lang)
    return string.replace('<Y>', y_words).replace('<X>', x_words)


def _get_signs(string):
    """
    Modifies the x, y integer coordinates with direction or multiplication by 0.
    :param string: (str)
    :return:
    """
    x, y = 0, 0
    if 'up' in string or 'montez' in string:
        y = 1
    elif 'down' in string or 'descendez' in string:
        y = -1

    if 'right' in string or 'droite' in string:
        x = 1
    elif 'left' in string or 'gauche' in string:
        x = -1

    return x, y
import random
from num2words import num2words


def replace_tokens_all(strings, game_dim, n_samples_per_string, lang='en'):
    """
    Given set of possible strings generated by CFG, generates n_samples_per_string
    training points where x and y are bounded by game_dim. Replaces <Y>, <X> tokens
    in strings with numbers in word format.

    :param strings: iterable(str)
    :param game_dim: (int) dimension of board, assumes square board
    :param n_samples_per_string: (int)
    :return: list[
        input string (e.g. "go two up and ten left"),
        [x, y] (e.g. [2, 10]
    ]
    """
    training_set = []
    for string in strings:
        # Modify sign based on left, right
        x_sign, y_sign = _get_signs(string)

        # sample a different set of coordinates for each
        for i in range(n_samples_per_string):
            x, y = random.randint(1, game_dim), random.randint(1, game_dim)
            string_i = _replace_tokens(string, y, x, lang=lang)
            training_set.append([string_i, x * x_sign, y * y_sign])

    return training_set


def _replace_tokens(string, y, x, lang='en'):
    """
    Assumes tokens <Y> and <X> to mean change in y, x respectively.
    Replaces tokens with args y and x.
    :param string: (str) string containing at least one of <Y> and <X>.
    :param y: (int) positive integer
    :param x: (int) positive integer
    :param lang: 'en' or 'fr'
    :return: (str) string with <Y> replaced by y in words and <X> by x in words.
    """
    assert y > 0 and x > 0
    y_words = num2words(y, lang=lang)
    x_words = num2words(x, lang=lang)
    return string.replace('<Y>', y_words).replace('<X>', x_words)


def _get_signs(string):
    """
    Modifies the x, y integer coordinates with direction or multiplication by 0.
    :param string: (str)
    :return:
    """
    x, y = 0, 0
    if 'up' in string or 'montez' in string:
        y = 1
    elif 'down' in string or 'descendez' in string:
        y = -1

    if 'right' in string or 'droite' in string:
        x = 1
    elif 'left' in string or 'gauche' in string:
        x = -1

    return x, y
Given set of possible strings generated by CFG, generates n_samples_per_string
     training points where x and y are bounded by game_dim. Replaces <Y>, <X> tokens
     in strings with numbers in word format.
 
     :param strings: iterable(str)
     :param game_dim: (int) dimension of board, assumes square board
     :param n_samples_per_string: (int)
     :return: list[
         input string (e.g. "go two up and ten left"),
         [x, y] (e.g. [2, 10]
     ] # Modify sign based on left, right # sample a different set of coordinates for each Assumes tokens <Y> and <X> to mean change in y, x respectively.
     Replaces tokens with args y and x.
     :param string: (str) string containing at least one of <Y> and <X>.
     :param y: (int) positive integer
     :param x: (int) positive integer
     :param lang: 'en' or 'fr'
     :return: (str) string with <Y> replaced by y in words and <X> by x in words. Modifies the x, y integer coordinates with direction or multiplication by 0.
     :param string: (str)
     :return: