def optional(item, reverse=False): from parser.parser import or_, empty a = empty() b = item if reverse: a, b = b, a return or_(a, b)
def repeatable(item, reverse=False): from parser.parser import forward, or_, rule temp = forward() a = rule(item, temp) b = item if reverse: a, b = b, a return temp.define(or_(a, b))
def max_bound(item, count, reverse=False): from parser.parser import rule, or_ if count == 1: return item else: a = rule(item, max_bound(item, count - 1, reverse)) b = item if reverse: a, b = b, a return or_(a, b)
def test_predicate(): tokenizer = MorphTokenizer() predicate = or_(normalized('московским'), and_(gram('NOUN'), not_(gram('femn')))) context = Context(tokenizer) predicate = predicate.activate(context) tokens = tokenizer('московский зоопарк') values = [predicate(_) for _ in tokens] assert values == [True, True] tokens = tokenizer('московская погода') values = [predicate(_) for _ in tokens] assert values == [True, False]
def repeatable_optional(item, reverse_repeatable=False, reverse_optional=False): from parser.parser import forward, or_, rule, empty temp = forward() a = empty() b = rule(item, temp) c = item if reverse_repeatable: b, c = c, b if reverse_optional: a, b, c = b, c, a return temp.define(or_(a, b, c))
def test_or(): assert_bnf(or_(rule('a'), rule('b')).named('A'), "A -> 'a' | 'b'")