def test_rule_that_not_matches(self): @rule(True) def test_rule(Subject, Object): return Subject + Object + Token("something here") pipeline = RuleBasedCore(self.person_date_relation, self._candidates, [test_rule]) pipeline.start() facts = pipeline.known_facts() candidate = self._candidates[0] self.assertFalse(facts[candidate])
def test_rule_that_matches(self): @rule(True) def test_rule(Subject, Object): anything = Question(Star(Any())) return Subject + Token("(") + Object + Token("-") + anything pipeline = RuleBasedCore(self.person_date_relation, self._candidates, [test_rule]) pipeline.start() facts = pipeline.known_facts() candidate = self._candidates[0] self.assertTrue(facts[candidate])
def test_rule_with_negative_answer(self): @rule(False) def test_rule(Subject, Object): anything = Question(Star(Any())) return Subject + Token("(") + Object + Token("-") + anything pipeline = RuleBasedCore(self.person_date_relation, self._candidates, [test_rule]) pipeline.start() facts = pipeline.known_facts() candidate = self._candidates[0] self.assertFalse(facts[candidate])
def __call__(self, config): # Prepare data if self.data is None or self.relname != config["relation"]: self.relname = config["relation"] self.relation = iepy.data.models.Relation.objects.get( name=config["relation"]) candidates = CEM.candidates_for_relation(self.relation) self.data = CEM.labels_for(self.relation, candidates, CEM.conflict_resolution_newest_wins) self.evidences = [] self.labels = [] for evidence, label in self.data.items(): if label is not None: self.labels.append(label) self.evidences.append(evidence) if not self.data: raise NotEnoughLabeledData( "There is no labeled data for training!") result = { "dataset_size": len(self.data), "start_time": time.time(), } # Load rules in the config if config["rules"] == "<all>": rules = self.rules.values() else: for rule_name in config["rules"]: if rule_name not in self.rules.keys(): raise RuleNotFound(rule_name) rules = [ rule for rule_name, rule in self.rules.items() if rule_name in config["rules"] ] # Run the rule based pipeline pipeline = RuleBasedCore(self.relation, self.evidences, rules) pipeline.start() matched = pipeline.known_facts() predicted_labels = [e in matched for e in self.evidences] # Evaluate prediction result.update( result_dict_from_predictions(self.evidences, self.labels, predicted_labels)) return result
def test_empty_rules(self): pipeline = RuleBasedCore(self.person_date_relation, self._candidates, []) pipeline.start() facts = pipeline.known_facts() self.assertEqual(len(facts), 0)
from iepy.extraction.rules_core import RuleBasedCore from iepy.data import models from iepy.data.db import CandidateEvidenceManager import rules if __name__ == u'__main__': logging.basicConfig( level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s") relation = models.Relation.objects.get(name=rules.RELATION) # Load rules rules = [] for attr_name in dir(rules): attr = getattr(rules, attr_name) if hasattr(attr, '__call__'): # is callable if hasattr(attr, "is_rule") and attr.is_rule: rules.append(attr) # Load evidences evidences = CandidateEvidenceManager.candidates_for_relation(relation) # Run the pipeline iextractor = RuleBasedCore(relation, evidences, rules) iextractor.start() facts = iextractor.known_facts() print(facts)