def test_different_instances_with_same_content_are_equal(self):
     """ If two different instances has the same content are equal. """
     value = (
         grammar.SingleTerm('terminal1'),
         'terminal2'
     )
     self.assertEquals(
         grammar.Or(*value),
         grammar.Or(*value),
     )
 def test_if_values_in_value_are_tuples_not_all_weights_are_numbers(self):
     """ If value is a tuple of less than 2 terms, it raises an error. """
     with self.assertRaises(ValueError):
         grammar.Or((
             (grammar.SingleTerm('terminal'), 2),
             (grammar.SingleTerm('terminal'), '8'),
         ))
 def test_not_all_values_in_value_tuple_are_not_term_string_or_tuple(self):
     """ If value is a tuple of less than 2 terms, it raises an error. """
     invalid_values = (
         (grammar.SingleTerm('terminal'), 'a nice dog', 9),
         (1, 2, 3, 4, 5),
     )
     for value in invalid_values:
         with self.assertRaises(ValueError):
             grammar.Or(value)
 def test_value_is_a_tuple_of_less_than_two_terms(self):
     """ If value is a tuple of less than 2 terms, it raises an error. """
     invalid_values = (
         tuple(),
         grammar.SingleTerm('terminal'),
     )
     for value in invalid_values:
         with self.assertRaises(ValueError):
             grammar.Or(value)
    def test_correct_construction_without_weights(self):
        """ If value correct without weights, term is correctly initialized. """
        value = (
            grammar.SingleTerm('t1'),
            't2',
        )

        or_connector = grammar.Or(*value)
        self.assertEquals(value[0], or_connector.value[0])
        self.assertEquals(grammar.SingleTerm(value[1]), or_connector.value[1])
        self.assertEquals(tuple(1 for _ in value), or_connector.weights)
    def test_correct_construction_with_weights(self):
        """ If value correct with weights, term is correctly initialized. """
        value_and_weights = (
            (grammar.SingleTerm('t1'), 1),
            ('t2', 3),
        )
        value = tuple(t[0] for t in value_and_weights)
        weights = tuple(t[1] for t in value_and_weights)

        or_connector = grammar.Or(*value_and_weights)
        self.assertEquals(value[0], or_connector.value[0])
        self.assertEquals(grammar.SingleTerm(value[1]), or_connector.value[1])
        self.assertEquals(weights, or_connector.weights)
 def test_value_is_not_a_tuple(self):
     """ If value is not a tuple, it raises an error. """
     with self.assertRaises(ValueError):
         grammar.Or(grammar.SingleTerm('terminal'))
Beispiel #8
0
from pynetics.gggp import grammar

awesome_grammar = grammar.Grammar(
    start_symbol='frase',
    productions=[
        grammar.Production(
            'frase',
            grammar.And(
                grammar.Multiplier('sujeto', upper=1), 'predicado',
                grammar.Multiplier(
                    grammar.And('conjuncion',
                                grammar.Multiplier('sujeto', upper=1),
                                'predicado'), )),
        ),
        grammar.Production('conjuncion', grammar.Or(('y', 4), ('o', 6))),
        grammar.Production('sujeto', grammar.Or('sujeto_masc', 'sujeto_fem')),
        grammar.Production(
            'sujeto_masc',
            grammar.Or(
                grammar.And('el', 'nombre_comun_masc'),
                grammar.And(grammar.Multiplier('el', upper=1),
                            'nombre_propio_masc'),
            )),
        grammar.Production(
            'sujeto_fem',
            grammar.Or(
                grammar.And('la', 'nombre_comun_fem'),
                grammar.And(grammar.Multiplier('la', upper=1),
                            'nombre_propio_fem'),
            )),