<s> from operator import attrgetter <EOL> import logging <EOL> import refo <EOL> from iepy . extraction . rules import generate_tokens_to_match , compile_rule <EOL> logger = logging . getLogger ( __name__ ) <EOL> class RuleBasedCore ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , relation , rules , verbosity = <NUM_LIT:0> ) : <EOL> self . relation = relation <EOL> self . rules = sorted ( rules , key = attrgetter ( "<STR_LIT>" ) , reverse = True ) <EOL> self . learnt = { } <EOL> self . verbosity = verbosity <EOL> def start ( self ) : <EOL> """<STR_LIT>""" <EOL> self . rule_regexes = [ <EOL> ( compile_rule ( rule , self . relation ) , rule . answer ) for rule in self . rules <EOL> ] <EOL> def predict ( self , candidates ) : <EOL> """<STR_LIT>""" <EOL> logger . info ( '<STR_LIT>' ) <EOL> predicted = { } <EOL> for i , evidence in enumerate ( candidates ) : <EOL> match = self . match ( evidence ) <EOL> predicted [ evidence ] = match if match is not None else False <EOL> if self . verbosity > <NUM_LIT:0> : <EOL> if ( i + <NUM_LIT:1> ) % <NUM_LIT:1000> == <NUM_LIT:0> : <EOL> logger . info ( '<STR_LIT>' . format ( i + <NUM_LIT:1> ) ) <EOL> return predicted <EOL> def add_answer ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def process ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> @ property <EOL> def questions ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ ] <EOL> def match ( self , evidence ) : <EOL> tokens_to_match = generate_tokens_to_match ( evidence ) <EOL> for regex , answer in self . rule_regexes : <EOL> match = refo . match ( regex , tokens_to_match ) <EOL> if match : <EOL> return answer </s>
