def test_052_relation_dict(self): """using the defaultdict functionality to create a relation_dict""" RS1 = relation_dict() k = (5, 123423) # referring to non-existant entry creats empty entry at that key self.assertEqual( RS1[ k ] , relation( RS1.heading, [] ) ) self.assertEqual( RS1, { k: relation( RS1.heading, [] ) } )
def test_053_relation_dict(self): """additive behavior of empty relation_dict""" RS1 = relation_dict() k = (5, 123423) RS1[ k ] += (1, 2, 3) # simply accessing a non-existant slot creates a new empty relation self.assertEqual( RS1[ k ] , relation( RS1.heading, [(1, 2, 3)] ) ) self.assertEqual( RS1, { k: relation( RS1.heading, [(1, 2, 3)] ) } )
def test_047_relation_dict(self): RS2 = relation_dict() # currently: defaultdict(relation( ('a', 'b', 'c'), [] ), {}) with self.assertRaises(RelationTupleLengthException): RS2[(1,2)] = relation( ('vol_id' , 'folder_id' , 'file_name' , 'file_id' , 'file_mod_date') , [ (1,2,7,2,7), (1,2,8,2,8) ] ) # can't really do this either, because the default factoty is created at init time, this will pass (fool) the RS2.heading = ('vol_id' , 'folder_id' , 'file_name' , 'file_id' , 'file_mod_date') # setitem test, but isn't really the thing RS2[(1,2)] = relation( ('vol_id' , 'folder_id' , 'file_name' , 'file_id' , 'file_mod_date') , [ (1,2,7,2,7), (1,2,8,2,8) ] ) print RS2.super_repr() == "defaultdict(relation( ('a', 'b', 'c'), [] ), {(1, 2): relation( ('vol_id', 'folder_id', 'file_name', 'file_id', 'file_mod_date'), [(1, 2, 7, 2, 7), (1, 2, 8, 2, 8)] )})" print repr(RS2)
def marshal_objects_relations(file, inkml_obj): with open(file, 'r') as file_obj: lines = list(file_obj) objs = [] rels = [] for line in lines: if line.startswith('O'): objs.append(line.strip().split(',')) elif line.startswith('R'): rels.append(line.strip().split(',')) for obj in objs: identity = obj[1].strip() label = obj[2] trs = [i.strip() for i in obj[4:]] new_obj = object(identity, label, trs) inkml_obj.objects.append(new_obj) for rel in rels: objids = [i.strip() for i in rel[1:3]] lab = rel[3].strip() weight = float(rel[4].strip()) iden = objids[0] + '' + objids[1] new_rel = relation(idd=iden, objids=objids, lab=lab, w=weight) inkml_obj.relations.append(new_rel) inkml_obj.compute_all_obj_bb()
def launch(self): if (self.source != None and self.target != None and type(self.source) != list and type(self.target) != list): seedget = self.seed.get() percentget = self.percent.get() try: fseed = int(seedget) except Exception: fseed = 100 try: fpercent = int(percentget) if fpercent <= 0 or fpercent > 100: fpercent = 100 except Exception: fpercent = 100 source, target = lecture(self.source, self.target, fseed, fpercent) result = relation(source, target) savetext = self.resum([[fseed], [fpercent]] + result) self.savef = Button(self.fen1, text="Save", command=lambda: self.savefile(savetext)) self.savef.grid(row=7, column=3, sticky="W") else: self.txtfile3.configure(text="Missing files")
def test_048_relation_dict(self): """default header""" RS1 = relation_dict() k = (5, 123423) self.assertEqual(RS1[k] , relation( RS1.heading, [] )) self.assertEqual(RS1.super_repr() , "defaultdict(relation( ('a', 'b', 'c'), [] ), {(5, 123423): relation( ('a', 'b', 'c'), [] )})" )
def test_075_relation_dict(self): """stack replacement""" RS1 = relation_dict() k11 = (3,234584) RS1[k11] = relation( RS1.heading, [(1, 2, 3)] ) # self.assertEqual( RS1._lengths_of_contents_at_depth() , [(3, 1)] ) self.assertEqual( RS1[ k11 ] , relation( RS1.heading, [(1, 2, 3)] ) ) k12 = (3,37482) RS1[k12] = relation( RS1.heading, [(4, 5, 6)] ) # self.assertEqual( RS1._lengths_of_contents_at_depth() , [(3, 1)] ) # stack is still an entry of length 1 at depth 3 self.assertEqual( RS1[ k12 ] , relation( RS1.heading, [(4, 5, 6)] ) ) self.assertEqual( RS1, { k11: relation( RS1.heading, [(1, 2, 3)] ) , k12: relation( RS1.heading, [(4, 5, 6)] ) } ) k13 = (3, 98765 ) RS1[k12] = relation( RS1.heading, [(9, 7, 5 ), (6, 2 , 8)] ) # self.assertEqual( RS1._lengths_of_contents_at_depth() , [(3, 2)] ) # now just one entry of length 2 at depth 3 self.assertEqual( str(RS1) , '(3, 37482):[(6, 2, 8), (9, 7, 5)], (3, 234584):[(1, 2, 3)]' )
def test_049_relation_dict(self): """default header""" RS2 = relation_dict(heading = ('vol_id' , 'folder_id' , 'file_name' , 'file_id' , 'file_mod_date')) k = (5, 123423) self.assertEqual(RS2[k] , relation( ('vol_id', 'folder_id', 'file_name', 'file_id', 'file_mod_date'), [] )) self.assertEqual(RS2.super_repr() , "defaultdict(relation( ('vol_id', 'folder_id', 'file_name', 'file_id', 'file_mod_date'), [] ), " "{(5, 123423): relation( ('vol_id', 'folder_id', 'file_name', 'file_id', 'file_mod_date'), [] )})" )
def test_071_relation_dict(self): """test internal stack-of-keys when using update()""" RS1 = relation_dict() k1 = (1,234584) # k2 = (2,444584) RS1[k1] += ( 1 ,2 , 3) self.assertEqual( RS1[ k1 ] , relation( RS1.heading, [(1, 2, 3)] ) ) self.assertEqual( RS1, { k1: relation( RS1.heading, [(1, 2, 3)] ) } ) r = relation( ('a', 'b', 'c'), [(4, 5, 6)] ) update_list = [ ( k1 , r ) ] RS1.update( update_list ) self.assertEqual( RS1[ k1 ] , relation( RS1.heading, [(4, 5, 6)] ) ) self.assertEqual( RS1, { k1: relation( RS1.heading, [(4, 5, 6)] ) } )
def test_070_relation_dict(self): """arithmetic behavior of relation_dict""" RS1 = relation_dict() k1 = (1,234584) k2 = (2,444584) RS1[k1] += ( 1 ,2 , 3) self.assertEqual( RS1[ k1 ] , relation( RS1.heading, [(1, 2, 3)] ) ) self.assertEqual( RS1, { k1: relation( RS1.heading, [(1, 2, 3)] ) } ) # self.assertEqual( RS1._lengths_of_contents_at_depth() , [(1, 1)] ) RS1[k1] += ( 4 ,5 , 6 ) self.assertEqual( RS1[ k1 ] , relation( RS1.heading, [(4, 5, 6), (1, 2, 3)] ) ) self.assertEqual( RS1, { k1: relation( RS1.heading, [(4, 5, 6), (1, 2, 3)] ) } ) # self.assertEqual( RS1._lengths_of_contents_at_depth() , [(1, 2)] ) RS1[k2] += ( 24 ,25 , 26 ) self.assertEqual( RS1[ k1 ] , relation( RS1.heading, [(4, 5, 6), (1, 2, 3)] ) ) self.assertEqual( RS1[ k2 ] , relation( RS1.heading, [(24, 25, 26)] ) ) self.assertEqual( RS1, { k1: relation( RS1.heading, [(4, 5, 6), (1, 2, 3)] ), k2: relation( RS1.heading, [(24, 25, 26)] ) } ) # self.assertEqual( RS1._lengths_of_contents_at_depth() , [(1, 2), (2, 1)] ) RS1[k1] -= ( 1 ,2 , 3) self.assertEqual( RS1[ k1 ] , relation( RS1.heading, [(4, 5, 6) ] ) ) # self.assertEqual( RS1._lengths_of_contents_at_depth() , [(1, 1), (2, 1)] ) # self.failUnlessRaises(tuple_set_exception, tuple_set, ('a', 'b', 'a') ) unittest.main() with self.assertRaises(KeyError): RS1[k1].subtract( (1, 2, 3) ) self.assertEqual( RS1[ k1 ] , relation( RS1.heading, [(4, 5, 6) ] ) )
def __init__(self,relations=()): if isinstance(relations,relation.relation): relations = (relations,) relations = tuple(relations) # If you only want one relation the following saves you having to # define it seperately before making the algebra. if len(relations) > 0 and isinstance(relations[0],tuple): assert len(relations[0]) == 2 relations = map(lambda x: relation.relation( *x),relations) for i in relations: assert isinstance(i,relation.relation) self.relations = relations
def __init__(self, update_list=(), heading=None): # **kwargs): self.heading = heading if heading else relation_dict._heading # update_list # [((1, 234584), relation( ('a', 'b', 'c'), [(1, 2, 3)] ))] # super(relation_dict, self).__init__( relation( relation_dict.default_heading ) ) super(relation_dict, self).__init__( relation( self.heading ), update_list ) # , **kwargs) #debugging if update_list: print "relation_dict-"+(hex(id(self))[7:-2])+".init( %r )" % update_list
def test_060_relation_dict(self): """init of relation_dict via list""" k = (1,234584) r = relation( ('a', 'b', 'c'), [(1, 2, 3)] ) update_list = [ ( k , r ) ] RS1 = relation_dict( update_list ) self.assertEqual (RS1[k] , r ) self.assertEqual (RS1.default_factory.heading , ('a', 'b', 'c') ) self.assertEqual( repr(RS1) , '(1, 234584):[(a=1, b=2, c=3)]' ) self.assertEqual( str(RS1) , '(1, 234584):[(1, 2, 3)]' ) self.assertEqual( RS1.pr_str() , '[1:(1)]' ) self.assertEqual( RS1.pr_str(short=True) , '1' )
def test_080_relation_dict(self): """relation_dict heading attribute""" RS2 = relation_dict(heading = ('vol_id' , 'folder_id' , 'file_name' , 'file_id' , 'file_mod_date')) self.assertEqual( RS2.heading , ('vol_id' , 'folder_id' , 'file_name' , 'file_id' , 'file_mod_date') ) RS2[(1,2)] += (1,2,7,2,7) # , (1,2,8,2,8) ] ) self.assertEqual( RS2[(1,2)] , relation( ('vol_id', 'folder_id', 'file_name', 'file_id', 'file_mod_date'), [(1, 2, 7, 2, 7)] )) with self.assertRaises(RelationTupleLengthException): RS2[(1,2)] += (1,2,7) # , (1,2,8,2,8) ] ) print RS2[(1,2)] print RS2 print "%r" % RS2
try: int(x) return x except ValueError: if(x in number_dic.keys()): return number_dic[x] else: return ""; def make_nice(text): text = [replace_int(x) for x in text.split('_')] return("".join(text)) hasExport = relation('hasExport' , 'GPE' , 'CDD' , make_nice , patterns_list=[ {'left': UNIVERSAL, 'middle': IS_EXPORT_MORE, 'comparator': 'more', 'inverted': True}, {'left': UNIVERSAL, 'middle': S_IS_EXPORT_MORE, 'comparator': 'more', 'inverted': True}, {'left': UNIVERSAL, 'middle': ACCOUNTED_FOR_EXPORT_MORE, 'comparator': 'more', 'inverted': True}, {'left': UNIVERSAL, 'middle': S_ACCOUNTED_FOR_EXPORT_MORE, 'comparator': 'more', 'inverted': True}, {'left': UNIVERSAL, 'middle': IS_EXPORT_LESS, 'comparator': 'less', 'inverted': True}, {'left': UNIVERSAL, 'middle': S_IS_EXPORT_LESS, 'comparator': 'less', 'inverted': True}, {'left': UNIVERSAL, 'middle': ACCOUNTED_FOR_EXPORT_LESS, 'comparator': 'less', 'inverted': True}, {'left': UNIVERSAL, 'middle': S_ACCOUNTED_FOR_EXPORT_LESS, 'comparator': 'less', 'inverted': True}, {'left': UNIVERSAL, 'middle': IS_EXPORT, 'comparator': 'egal', 'inverted': True}, {'left': UNIVERSAL, 'middle': S_IS_EXPORT, 'comparator': 'egal', 'inverted': True}, {'left': UNIVERSAL, 'middle': ACCOUNTED_FOR_EXPORT, 'comparator': 'egal', 'inverted': True}, {'left': UNIVERSAL, 'middle': S_ACCOUNTED_FOR_EXPORT, 'comparator': 'egal', 'inverted': True} ]) listeRelation.append(hasExport) dic.update(number_dic)
try: int(x) return x except ValueError: if(x in number_dic.keys()): return number_dic[x] else: return ""; def make_nice(text): text = [replace_int(x) for x in text.split('_') if replace_int(x) != ""] return(" ".join(text)) ppdensity_grammar = "PPDENSITY: {<CD>+<PPUNIT><PER><AREA>+}" hasPopulationDensity = relation('hasPopulationDensity' , 'GPE' , 'PPDENSITY' , make_nice , patterns_list=[ {'left': UNIVERSAL, 'middle': HAS_POPULATION_DENSITY_MORE, 'comparator': 'more', 'inverted':True}, {'left': UNIVERSAL, 'middle': HAS_POPULATION_DENSITY_LESS, 'comparator': 'less', 'inverted':True}, {'left': UNIVERSAL, 'middle': HAS_POPULATION_DENSITY, 'comparator': 'egal', 'inverted':True}, {'left': S_POPULATION_DENSITY, 'middle': IS_MORE, 'comparator': 'more', 'inverted':True}, {'left': POPULATION_DENSITY_OF, 'middle': IS_MORE, 'comparator': 'more', 'inverted':True}, {'left': S_POPULATION_DENSITY, 'middle': IS_LESS, 'comparator': 'less', 'inverted':True}, {'left': POPULATION_DENSITY_OF, 'middle': IS_LESS, 'comparator': 'less', 'inverted':True}, {'left': S_POPULATION_DENSITY, 'middle': IS, 'comparator': 'egal', 'inverted':True}, {'left': POPULATION_DENSITY_OF, 'middle': IS, 'comparator': 'egal', 'inverted':True}, ]) listeRelation.append(hasPopulationDensity) grammar.append(ppdensity_grammar) dic.update(ppdensity_dic)
'billion': '000000000', 'million': '000000', 'thousand': '000', 'hundread': '00' } def replace_int(x): try: int(x) return x except ValueError: if(x in number_dic.keys()): return number_dic[x] else: return ""; def make_nice(text): text = [replace_int(x) for x in text.split('_')] return("".join(text)) hasGDP = relation('hasGDP' , 'GPE' , 'CDD' , make_nice , patterns_list=[ {'left': UNIVERSAL, 'middle': S_GDP_MORE, 'comparator': 'more', 'inverted': True}, {'left': GDP_IN, 'middle': IS_MORE, 'comparator': 'more', 'inverted': True}, {'left': UNIVERSAL, 'middle': S_GDP_LESS, 'comparator': 'less', 'inverted': True}, {'left': GDP_IN, 'middle': IS_LESS, 'comparator': 'less', 'inverted': True}, {'left': UNIVERSAL, 'middle': S_GDP, 'comparator': 'egal', 'inverted': True}, {'left': GDP_IN, 'middle': IS, 'comparator': 'egal', 'inverted': True}, ]) listeRelation.append(hasGDP)
int(x) return x except ValueError: if x in number_dic.keys(): return number_dic[x] else: return "" def make_nice(text): text = [replace_int(x) for x in text.split("_")] return "".join(text) hasPopulation = relation( "hasPopulation", "PPCD", "GPE", make_nice, patterns_list=[ {"left": UNIVERSAL, "middle": LIVE_IN, "comparator": "egal"}, {"left": THERE, "middle": IN, "comparator": "egal"}, {"left": THERE_MORE, "middle": IN, "comparator": "more"}, {"left": THERE_LESS, "middle": IN, "comparator": "less"}, ], ) listeRelation.append(hasPopulation) dic.update(haspop_dic) grammar.append(haspop_grammar)
import re from relation import relation, relationData from supportedRelations import listeRelation UNIVERSAL = re.compile(r'.*') BORN_IN = re.compile(r'.*\bborn\b.*in.*') def make_nice(x): return x bornIn = relation('bornIn' , 'PERSON' , 'GPE' , make_nice , patterns_list=[ {'left': UNIVERSAL, 'middle': BORN_IN, 'comparator': 'egal'} ]) listeRelation.append(bornIn)
haspop_dic = { "people": "PPUNIT", "inhabitants": "PPUNIT" } def replace_int(x): try: int(x) return x except ValueError: if(x in number_dic.keys()): return number_dic[x] else: return ""; def make_nice(text): text = [replace_int(x) for x in text.split('_')] return("".join(text)) hasPopulation = relation('hasPopulation' , 'PPCD' , 'GPE' , make_nice , patterns_list=[ {'left': UNIVERSAL, 'middle': LIVE_IN, 'comparator': 'egal'}, {'left': THERE, 'middle': IN, 'comparator': 'egal'}, {'left': THERE_MORE, 'middle': IN, 'comparator': 'more'}, {'left': THERE_LESS, 'middle': IN, 'comparator': 'less'} ]) listeRelation.append(hasPopulation) dic.update(haspop_dic) grammar.append(haspop_grammar)