def test_get_props(): q = MQLQuery.make({ 'id': '/en/the_beatles', 'type': '/music/artist' }, ['*']) print MQLQuery.view_props(q.query_args)
def freebaseImport(self, request, obj_url): #TODO: import a freebase entity with MQL query, given id """ Imports only one layer deep (i.e. all of the types associated with id, or all of the properties of an id with a given type This method takes a json object or dictionary of query arguments, which may include id, type, guid, mid, timestamp, name, or any keyword found in the freebase schema. Using another json object to represent what to look for in the results (specific keywords representing certain field, or '*' representing all fields), does an MQL query, makes all results into assertions, and returns the result set. query_args: id:someid,type:sometype... result_args: name:somename,*:{} Accessed by going to the URL /api/assertionmake?dataset={dataset}&query_args={arg1:val1,arg2:val2,...}&result_args= {arg1,arg2,arg3,...}&polarity={polarity}&context={context}&user={user}&password={password} TODO: maybe include details about keywords, details about result_args field tested: curl --data "dataset=/data/test&args=id:/en/the_beatles&results=*&polarity=1&context=None&user=nholm&password=something" "http://127.0.0.1:8000/api/freebaseimport" """ dataset = request.POST['dataset'] query_args_str = request.POST['args'] query_args={} for arg in query_args_str.split(','): query_args[arg.split(':')[0]]=arg.split(':')[1] result_args_str = request.POST['results'] result_args = result_args_str.split(',') polarity = int(request.POST.get('polarity','1')) context = request.POST.get('context','None') user = request.POST['user'] password = request.POST['password'] if context == "None": context = None if User.objects.get(username=user).check_password(password): #the user's password is correct. Get their reason and add try: user_reason = ReasonConjunction.objects.get(target=dataset + '/contributor/' + user) except DoesNotExist: return rc.FORBIDDEN else: #incorrect password return rc.FORBIDDEN mqlquery = MQLQuery.make(query_args, result_args) assertions_from_freebase = mqlquery.get_results(dataset, user, polarity, context, False) # return '{imported assertions: '+str(assertions_from_freebase)+'}' return '{Added/voted for %s assertions from freebase for %s}'%(str(len(assertions_from_freebase)),str(query_args))
def test_get_entities(): property = 'type' q = MQLQuery.make({ 'id': '/en/the_beatles', 'type': '/music/artist' }, ['*']) print MQLQuery.view_entities(q.query_args, property)
def test_import_traversing(): Assertion.drop_collection() Dataset.drop_collection() q = MQLQuery.make({'mid':'/m/0p_47'},['*']) # 'mid':'/m/0p_47' q.get_results('/data/test', 'nholm', 1, None, True, 'mid') #print 'DONE WITH GET RESULTS' for a in Assertion.objects: print a.relation print a.arguments Assertion.drop_collection() Dataset.drop_collection()
def test_freebase_allresults(): Assertion.drop_collection() query_args = {'id': '/en/the_beatles', 'type': '/music/artist'} result_args = ['*'] q = MQLQuery.make(query_args, result_args) q.get_results('/data/test') for a in Assertion.objects: print str(a.arguments) print str(a.relation) Assertion.drop_collection()
def test_freebase_allresults(): Assertion.drop_collection() query_args = {'id':'/en/the_beatles', 'type':'/music/artist'} result_args = ['*'] q = MQLQuery.make(query_args, result_args) q.get_results('/data/test') for a in Assertion.objects: print str(a.arguments) print str(a.relation) Assertion.drop_collection()
def test_import_traversing(): Assertion.drop_collection() Dataset.drop_collection() q = MQLQuery.make({'mid': '/m/0p_47'}, ['*']) # 'mid':'/m/0p_47' q.get_results('/data/test', 'nholm', 1, None, True, 'mid') #print 'DONE WITH GET RESULTS' for a in Assertion.objects: print a.relation print a.arguments Assertion.drop_collection() Dataset.drop_collection()
def freebaseFullImport(self, request, obj_url): ''' Import ALL URIs associated with a given freebase URI freebase URI field : 'id' Basic operation: if only id given, does a separate import for all of the properties of each possible type. if type given, only do an import for that layer. if other property given, do that same query with every possible type ''' dataset = request.POST['dataset'] query_args_str = request.POST['args'] query_args = {} for arg in query_args_str.split(','): query_args[arg.split(':')[0]] = arg.split(':')[1] result_args_str = request.POST['results'] result_args = result_args_str.split(',') polarity = int(request.POST.get('polarity', '1')) context = request.POST.get('context', 'None') user = request.POST['user'] password = request.POST['password'] if context == "None": context = None if User.objects.get(username=user).check_password(password): #the user's password is correct. Get their reason and add try: user_reason = ReasonConjunction.objects.get( target=dataset + '/contributor/' + user) except DoesNotExist: return rc.FORBIDDEN else: #incorrect password return rc.FORBIDDEN mqlquery = MQLQuery.make(query_args, result_args) assertions_from_freebase = mqlquery.get_results( dataset, user, polarity, context, True) return '{Added/voted for %s assertions from freebase for %s}' % (str( len(assertions_from_freebase)), str(query_args))
def test_create_or_vote(): q = MQLQuery.make({'id': '/en/the_beatles'}, ['*']) Assertion.drop_collection() assertions = q.get_results('/data/test', 'nholm', 1, None, False) print str(len(assertions)) assertions2 = q.get_results('/data/test', 'nholm', 1, None, False) print str(len(assertions2)) count = 0 for a in Assertion.objects: count += 1 print a.arguments print count Assertion.drop_collection()
def freebaseFullImport(self, request, obj_url): ''' Import ALL URIs associated with a given freebase URI freebase URI field : 'id' Basic operation: if only id given, does a separate import for all of the properties of each possible type. if type given, only do an import for that layer. if other property given, do that same query with every possible type ''' dataset = request.POST['dataset'] query_args_str = request.POST['args'] query_args={} for arg in query_args_str.split(','): query_args[arg.split(':')[0]]=arg.split(':')[1] result_args_str = request.POST['results'] result_args = result_args_str.split(',') polarity = int(request.POST.get('polarity','1')) context = request.POST.get('context','None') user = request.POST['user'] password = request.POST['password'] if context == "None": context = None if User.objects.get(username=user).check_password(password): #the user's password is correct. Get their reason and add try: user_reason = ReasonConjunction.objects.get(target=dataset + '/contributor/' + user) except DoesNotExist: return rc.FORBIDDEN else: #incorrect password return rc.FORBIDDEN mqlquery = MQLQuery.make(query_args, result_args) assertions_from_freebase = mqlquery.get_results(dataset, user, polarity, context, True) return '{Added/voted for %s assertions from freebase for %s}'%(str(len(assertions_from_freebase)),str(query_args))
def test_create_or_vote(): q = MQLQuery.make({'id':'/en/the_beatles'}, ['*']) Assertion.drop_collection() assertions = q.get_results('/data/test','nholm', 1,None,False) print str(len(assertions)) assertions2 = q.get_results('/data/test','nholm', 1,None,False) print str(len(assertions2)) count = 0 for a in Assertion.objects: count += 1 print a.arguments print count Assertion.drop_collection()
def fb_datadumpread(filename): dump = open(filename, "r") count = 0 for line in dump: # ADDED as of 3/8/2011: lines 0-200 #if count <200: # print count # count += 1 # continue #else: print line.split()[0] q = MQLQuery.make({'mid': line.split()[0]}, ['*']) q.get_results('/data/freebase', 'nholm', 1, None, True, 'mid') count += 1 if count > 200: break dump.close()
def fb_datadumpread(filename): dump = open(filename, "r") count = 0 for line in dump: # ADDED as of 3/8/2011: lines 0-200 #if count <200: # print count # count += 1 # continue #else: print line.split()[0] q = MQLQuery.make({'mid':line.split()[0]},['*']) q.get_results('/data/freebase', 'nholm', 1, None, True, 'mid') count += 1 if count > 200: break dump.close()
def test_datadumpread(filename): dump = open(filename, "r") count = 0 for line in dump: #print line # ADDED: lines 0-200 if count < 100: print count count += 1 continue else: print line.split()[0] q = MQLQuery.make({'mid': line.split()[0]}, ['*']) q.get_results('/data/freebase', 'nholm', 1, None, True, 'mid') count += 1 if count > 200: break dump.close()
def test_datadumpread(filename): dump = open(filename, "r") count = 0 for line in dump: #print line # ADDED: lines 0-200 if count <100: print count count += 1 continue else: print line.split()[0] q = MQLQuery.make({'mid':line.split()[0]},['*']) q.get_results('/data/freebase', 'nholm', 1, None, True, 'mid') count += 1 if count > 200: break dump.close()
def test_import_all(): Assertion.drop_collection() q = MQLQuery.make({'id':'/en/the_beatles'}, ['*']) assertions = q.get_results('/data/test',1,None,'nholm',True) for a in Assertion.objects: print a.relation # # mss = freebase.HTTPMetawebSession('http://api.freebase.com') # # query = [{"*":{},"id":"/en/the_beatles","type":"/music/artist"}] # # results = mss.mqlread(query) # # print results Assertion.drop_collection()
def test_import_all(): Assertion.drop_collection() q = MQLQuery.make({'id': '/en/the_beatles'}, ['*']) assertions = q.get_results('/data/test', 1, None, 'nholm', True) for a in Assertion.objects: print a.relation # # mss = freebase.HTTPMetawebSession('http://api.freebase.com') # # query = [{"*":{},"id":"/en/the_beatles","type":"/music/artist"}] # # results = mss.mqlread(query) # # print results Assertion.drop_collection()
def freebaseImport(self, request, obj_url): #TODO: import a freebase entity with MQL query, given id """ Imports only one layer deep (i.e. all of the types associated with id, or all of the properties of an id with a given type This method takes a json object or dictionary of query arguments, which may include id, type, guid, mid, timestamp, name, or any keyword found in the freebase schema. Using another json object to represent what to look for in the results (specific keywords representing certain field, or '*' representing all fields), does an MQL query, makes all results into assertions, and returns the result set. query_args: id:someid,type:sometype... result_args: name:somename,*:{} Accessed by going to the URL /api/assertionmake?dataset={dataset}&query_args={arg1:val1,arg2:val2,...}&result_args= {arg1,arg2,arg3,...}&polarity={polarity}&context={context}&user={user}&password={password} TODO: maybe include details about keywords, details about result_args field tested: curl --data "dataset=/data/test&args=id:/en/the_beatles&results=*&polarity=1&context=None&user=nholm&password=something" "http://127.0.0.1:8000/api/freebaseimport" """ dataset = request.POST['dataset'] query_args_str = request.POST['args'] query_args = {} for arg in query_args_str.split(','): query_args[arg.split(':')[0]] = arg.split(':')[1] result_args_str = request.POST['results'] result_args = result_args_str.split(',') polarity = int(request.POST.get('polarity', '1')) context = request.POST.get('context', 'None') user = request.POST['user'] password = request.POST['password'] if context == "None": context = None if User.objects.get(username=user).check_password(password): #the user's password is correct. Get their reason and add try: user_reason = ReasonConjunction.objects.get( target=dataset + '/contributor/' + user) except DoesNotExist: return rc.FORBIDDEN else: #incorrect password return rc.FORBIDDEN mqlquery = MQLQuery.make(query_args, result_args) assertions_from_freebase = mqlquery.get_results( dataset, user, polarity, context, False) # return '{imported assertions: '+str(assertions_from_freebase)+'}' return '{Added/voted for %s assertions from freebase for %s}' % (str( len(assertions_from_freebase)), str(query_args))
def test_get_entities(): property = 'type' q = MQLQuery.make({'id':'/en/the_beatles','type':'/music/artist'}, ['*']) print MQLQuery.view_entities(q.query_args, property)
def test_get_props(): q = MQLQuery.make({'id':'/en/the_beatles','type':'/music/artist'}, ['*']) print MQLQuery.view_props(q.query_args)