def test_parsing_with_no_localized_grammars(self): mgr = EvaluableParseManager(self.symbol_table, Grammar()) parse_tree1 = mgr.parse('message == "2009-07-13"') parse_tree2 = mgr.parse('message == "2009-07-13"', None) expected_tree = EvaluableParseTree(Equal(String("Hello world"), String("2009-07-13"))) eq_(parse_tree1, parse_tree2) eq_(parse_tree1, expected_tree)
def test_parsing_with_localized_grammars(self): castilian_grammar = Grammar(decimal_separator=",", thousands_separator=".") mgr = EvaluableParseManager(self.symbol_table, Grammar(), es=castilian_grammar) parse_tree = mgr.parse(u"tráfico:peatones_cruzando_calle <= 3,00", "es") expected_tree = EvaluableParseTree( LessEqual(PedestriansCrossingRoad(), Number(3.0))) eq_(parse_tree, expected_tree)
def test_adding_grammar(self): """It should be possible to add grammars after instantiation.""" castilian_grammar = Grammar(decimal_separator=",", thousands_separator=".") mgr = EvaluableParseManager(self.symbol_table, Grammar()) mgr.add_parser("es", castilian_grammar) parse_tree = mgr.parse(u'tráfico:peatones_cruzando_calle <= 3,00', "es") expected_tree = EvaluableParseTree( LessEqual(PedestriansCrossingRoad(), Number(3.0))) eq_(parse_tree, expected_tree)
def test_evaluating_expressions(self): """Managers should be able to evaluate the expressions too.""" mgr = EvaluableParseManager(self.symbol_table, Grammar()) context = {'pedestrians_crossroad': (u"gustavo", u"carla")} evaluation1 = mgr.evaluate("traffic:pedestrians_crossing_road <= 3", None, context) evaluation2 = mgr.evaluate(u'"carla" ∈ traffic:pedestrians_crossing_road', None, context) evaluation3 = mgr.evaluate("traffic:pedestrians_crossing_road > 2", None, context) ok_(evaluation1) ok_(evaluation2) assert_false(evaluation3)
def test_parsing_with_defined_grammar_but_no_available_translations(self): """ When an expression is written in an supported grammar but there are no translated bindings, the default names must be used along with the custom grammar. """ french_grammar = Grammar(decimal_separator=",", thousands_separator=".") mgr = EvaluableParseManager(self.symbol_table, Grammar(), fr=french_grammar) # French grammar is not supported: parse_tree = mgr.parse("traffic:pedestrians_crossing_road <= 3,0", "fr") expected_tree = EvaluableParseTree( LessEqual(PedestriansCrossingRoad(), Number(3))) eq_(parse_tree, expected_tree)
def test_parsing_with_undefined_grammar_but_available_translations(self): """ When an expression is written in an unsupported grammar, a parser based on the generic grammar must be created and used. The respective translated bindings must be used if available. """ log_handler = LoggingHandlerFixture() mgr = EvaluableParseManager(self.symbol_table, Grammar()) # Castilian grammar is not supported: parse_tree = mgr.parse(u"tráfico:peatones_cruzando_calle <= 3.0", "es") expected_tree = EvaluableParseTree( LessEqual(PedestriansCrossingRoad(), Number(3))) eq_(parse_tree, expected_tree) # Checking the log: info = "Generated parser for unknown grammar 'es'" ok_(info in log_handler.handler.messages['info']) log_handler.undo()
def test_parsing_with_undefined_grammar_and_no_translated_bindings(self): """ When an expression is written in an unsupported grammar, a parser based on the generic grammar must be created and used. If there are no translated bindings, the default names must be used. """ log_handler = LoggingHandlerFixture() mgr = EvaluableParseManager(self.symbol_table, Grammar()) # French grammar is not supported: parse_tree = mgr.parse("traffic:pedestrians_crossing_road <= 3.0", "fr") expected_tree = EvaluableParseTree( LessEqual(PedestriansCrossingRoad(), Number(3))) eq_(parse_tree, expected_tree) # Checking the log: info = "Generated parser for unknown grammar 'fr'" ok_(info in log_handler.handler.messages['info']) log_handler.undo()
def create_grammar(self): root_table = SymbolTable( "root", map(lambda f: Bind(f['name'], GeometryProperty(f['name'])), self.fields)) tokens = { 'not': 'not', 'eq': '==', 'ne': '!=', 'belongs_to': 'in', 'is_subset': 'are included in', 'or': "or", 'and': 'and' } grammar = Grammar(**tokens) self.parse_manager = EvaluableParseManager(root_table, grammar)
SymbolTable("month", []) ) ) from booleano.parser import Grammar new_tokens = { 'and': '&&', 'or': '||', 'belongs_to': 'in', 'is_subset': 'subset of' } english_grammar = Grammar(**new_tokens) from booleano.parser import EvaluableParseManager parse_manager = EvaluableParseManager(root_table, english_grammar) buy_parser = parse_manager.parse(buy_conditions) sell_parser = parse_manager.parse(sell_conditions) print(buy_parser) print(sell_parser) sp = StockProcessing(data_dir, date_fmt, (date_column, open_column, close_column)) sp.generate_sold_stocks() sp.buy_stocks(buy_parser, sell_parser) sa = StockAnalysis(sp.order_history, sp.owned_stocks, sp.dates_arr, sp.date_keyed, buy_conditions, sell_conditions) sa.analyze_trades("output") import os
def test_adding_existing_grammar(self): """There can't be duplicate/overlapped parsers.""" mgr = EvaluableParseManager(self.symbol_table, Grammar(), es=Grammar()) assert_raises(GrammarError, mgr.add_parser, "es", Grammar())
def greater_than(self, value, context): return context.average_rating > value def less_than(self, value, context): return context.average_rating < value # Defining the scope: symbols = SymbolTable("global", ( Bind("movie", MovieId(), nl="film", es=u"película"), ), SymbolTable("movie", ( Bind("id", MovieId(), nl="id", es="id"), Bind("title", MovieTitle(), nl="titel", es=u"título"), Bind("year", MovieYear(), nl="jaar", es=u"año"), Bind("average_rating", MovieAverageRating(), nl="gemiddelde_rating", es=u"puntuación_promedio") ), nl="film", es=u"película" ), ) # Finally, the parse manager: parse_manager = EvaluableParseManager(symbols, english_grammar, nl=dutch_grammar, es=castilian_grammar)