def test_type_errors(): F = fact('F', ['a']) RULE = rule( 'a', eq('1').interpretation( custom(int) ) ).interpretation( F.a ) parser = Parser(RULE) match = parser.match('a 1') with pytest.raises(TypeError): match.fact F = fact('F', ['a']) RULE = rule( 'a', eq('1').interpretation( custom(int) ) ).interpretation( custom(str) ) parser = Parser(RULE) match = parser.match('a 1') with pytest.raises(TypeError): match.fact
def test_type_errors(): F = fact('F', ['a']) RULE = rule( 'a', eq('1').interpretation( custom(int) ) ).interpretation( F.a ) parser = Parser(RULE) match = parser.match('a 1') with pytest.raises(TypeError): match.fact F = fact('F', ['a']) RULE = rule( 'a', eq('1').interpretation( custom(int) ) ).interpretation( custom(str) ) parser = Parser(RULE) match = parser.match('a 1') with pytest.raises(TypeError): match.fact
def test_rule_custom(): RULE = rule( '3', '.', '14' ).interpretation( custom(float) ) parser = Parser(RULE) match = parser.match('3.14') assert match.fact == 3.14
def test_rule_custom(): RULE = rule( '3', '.', '14' ).interpretation( custom(float) ) parser = Parser(RULE) match = parser.match('3.14') assert match.fact == 3.14
def test_rule_custom_attribute(): F = fact('F', ['a']) RULE = rule('1').interpretation(custom(int)).interpretation( F.a).interpretation(F) parser = Parser(RULE) match = parser.match('1') record = match.fact assert record == F(a=1) assert record.spans == [(0, 1)] assert record.as_json == {'a': 1}
def test_rule_custom_custom(): MAPPING = {'a': 1} RULE = rule( 'A' ).interpretation( custom(str.lower).custom(MAPPING.get) ) parser = Parser(RULE) match = parser.match('A') assert match.fact == 1
def make_rule_from_station(title: str) -> Rule: title = title.replace('1', '').replace('2', '').lower().strip() phrase = [] for token in title.split(' '): word = Abbrs.get(token) if Abbrs.is_abbr(token) \ else normalized(token).interpretation(meaning.const(token)) phrase.append(word.interpretation(Array.element)) phrase = rule(*phrase).means(Array).interpretation( meaning.custom(lambda p: Restore.get(' '.join(p.element)))).means( StationTitle.value) if Synonyms.has(title): synonym = Synonyms.get(title).interpretation( meaning.custom(lambda p: Restore.get(p))).means(StationTitle.value) return or_(synonym, phrase) return phrase
def test_rule_custom_custom(): MAPPING = {'a': 1} RULE = rule( 'A' ).interpretation( custom(str.lower).custom(MAPPING.get) ) parser = Parser(RULE) match = parser.match('A') assert match.fact == 1
def test_rule_attribute_custom(): F = fact('F', ['a']) RULE = rule( '1' ).interpretation( F.a ).interpretation( custom(int) ) parser = Parser(RULE) match = parser.match('1') assert match.fact == 1
def test_rule_attribute_custom(): F = fact('F', ['a']) RULE = rule( '1' ).interpretation( F.a ).interpretation( custom(int) ) parser = Parser(RULE) match = parser.match('1') assert match.fact == 1
def test_rule_custom_attribute(): F = fact('F', ['a']) RULE = rule( '1' ).interpretation( custom(int) ).interpretation( F.a ).interpretation( F ) parser = Parser(RULE) match = parser.match('1') record = match.fact assert record == F(a=1) assert record.spans == [(0, 1)] assert record.as_json == {'a': 1}
METRO_WORD = or_( rule(caseless('м'), '.'), rule(normalized('метро')), ) __quotes = "„“”‚‘’'\"" LEFT_QUOTE = in_("«" + __quotes) RIGHT_QUOTE = in_("»" + __quotes) STATION = rule( STATION_WORD.optional(), METRO_WORD.optional(), LEFT_QUOTE.optional(), STATION_TITLE.interpretation( meaning.custom(lambda p: p.value)).interpretation(Station.name), rule( eq('-').optional(), LIST_OF_NUMERALS.interpretation(Station.num), ).optional(), RIGHT_QUOTE.optional(), ).interpretation(Station) LIST_OF_STATIONS = rule( STATION.means(Array.element), rule( in_caseless('и,-'), STATION.means(Array.element), ).repeatable().optional(), ).interpretation(Array).interpretation(meaning.custom(lambda p: p.element))
from yargy import or_, rule from yargy.interpretation import attribute, fact import yargy.interpretation as meaning from yargy.predicates import caseless, gram, in_caseless, normalized from .station import FROM_STATION_TO_STATION, LIST_OF_STATIONS, STATION Transfer = fact('Transfer', [attribute('to', default=[])]) TRANSFER = rule( gram('ADJF').optional(), # пешеходный normalized('переход'), or_( FROM_STATION_TO_STATION.interpretation(Transfer.to), rule( or_(caseless('на'), caseless('между'), caseless('с')).optional(), LIST_OF_STATIONS.interpretation(Transfer.to)), ).optional(), ).interpretation(Transfer) StationAndTransfer = fact('StationAndTransfer', ['station', 'transfer']) STATION_AND_TRANSFER = rule( STATION.interpretation(StationAndTransfer.station), rule( in_caseless('и,'), TRANSFER.interpretation(meaning.custom(lambda p: p.to)).interpretation( StationAndTransfer.transfer), ).optional()).interpretation(StationAndTransfer)
Nums = fact('Nums', [attribute('values').repeatable()]) __literals = { 'один': 1, 'два': 2, 'три': 3, 'четыре': 4, 'пять': 5, 'шесть': 6, 'семь': 7, 'восемь': 8, 'девять': 9, } LITERAL = dictionary(__literals).means( interp.normalized().custom(__literals.get)) CONJ_NUMS = in_caseless('-и,') NUMERAL = or_(*[eq(str(i)) for i in __literals.values()]).means(interp.custom(int)) # вестибюль 1 и 2 LIST_OF_NUMERALS = connect(NUMERAL.means(Nums.values), CONJ_NUMS) \ .means(Nums).means(meaning.custom(lambda p: list(sorted(set(p.values))))) # первый и второй вестибюли LIST_OF_LITERALS = connect(LITERAL.means(Nums.values), CONJ_NUMS) \ .means(Nums).means(meaning.custom(lambda p: list(sorted(set(p.values)))))
tires_vendors_path = 'tires.vendors.json' # xls_path = '06.07.18 ДАКАР Уфа.xls' xls_path = 'Прайс_Колобокс_Шины_2018-07-07 (XLS).xls' data_list = xls_to_list(xls_path) # FACTS Tire = fact('Tire', [ 'vendor', 'width', 'profile', 'diameter', 'max_speed_index', 'max_load_index', 'season', 'spikes' ]) Vendor = fact('Vendor', ['id', 'name']) # HELPERS SEP = in_({'-', '/', '|', ':', ';', '.'}) NUM = type_('INT') INT = NUM.interpretation(interp.custom(int)) FLOAT = rule(NUM.repeatable(), in_({',', '.'}), NUM, NUM.optional()).interpretation(interp.custom(to_float)) # TIRE_VENDORS VENDORS_NAME, VENDORS_ID = get_vendor_dict(tires_vendors_path) VENDOR = rule( caseless_pipeline(VENDORS_NAME).interpretation( Vendor.name.normalized().custom( VENDORS_NAME.get))).interpretation(Vendor) # TIRE_HELPERS DIAMETER_WITH_LETTER = rule(NUM, or_(eq('С'), eq('C')).optional()) STRUCTURE = or_( rule(or_(INT, FLOAT), SEP, or_(INT, FLOAT), SEP, or_(INT, DIAMETER_WITH_LETTER)),
from .common import Array # Working = fact('Working', [attribute('to', default=[])]) Type = fact('Type', [attribute('value', default=[]), 'status', attribute('inversed', default=False)]) ENTER_EXIT_WORD = rule( or_(caseless('на'), caseless('для')).optional(), morph_pipeline(['вход', 'выход']).interpretation(Type.value), ).interpretation(Type) # и на вход и на выход ON_ENTER_AND_EXIT = rule( caseless('и').optional(), ENTER_EXIT_WORD .interpretation(meaning.custom(lambda p: p.value)) .interpretation(Array.element), # caution: misses comma, I don't know why rule( caseless('и').optional(), ENTER_EXIT_WORD .interpretation(meaning.custom(lambda p: p.value)) .interpretation(Array.element), ).optional(), ).interpretation(Array) IS_WORKING_OPEN_CLOSED_DICT = { 'работать': 'работает', 'открытый': 'открыт', 'закрытый': 'закрыт' }