Beispiel #1
0
    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))
Beispiel #2
0
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()
Beispiel #3
0
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()
Beispiel #4
0
 def freebaseLookupProps(self, request, obj_url):
     '''
     Given a fb query, looks up all of the properties associated with that object. 
     These properties can become result_args fields.
     
     Format: /api/freebaselookupprops?args={arg1:val1,arg2:val1,...}
     
     tested:
     curl "http://127.0.0.1:8000/api/freebaselookupprops?args=id:/en/the_beatles"
     curl "http://127.0.0.1:8000/api/freebaselookupprops?args=id:/en/the_beatles,type:/music/artist"
     
     '''
     
     query_args={}
     query_args_str = request.GET['args']
     for a in query_args_str.split(','):
         query_args[a.split(':')[0]]=a.split(':')[1]
     
     return '{ The result set has the following properties: '+str(MQLQuery.view_props(query_args))+'}'
Beispiel #5
0
    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))
Beispiel #6
0
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()
Beispiel #7
0
    def freebaseLookupProps(self, request, obj_url):
        '''
        Given a fb query, looks up all of the properties associated with that object. 
        These properties can become result_args fields.
        
        Format: /api/freebaselookupprops?args={arg1:val1,arg2:val1,...}
        
        tested:
        curl "http://127.0.0.1:8000/api/freebaselookupprops?args=id:/en/the_beatles"
        curl "http://127.0.0.1:8000/api/freebaselookupprops?args=id:/en/the_beatles,type:/music/artist"
        
        '''

        query_args = {}
        query_args_str = request.GET['args']
        for a in query_args_str.split(','):
            query_args[a.split(':')[0]] = a.split(':')[1]

        return '{ The result set has the following properties: ' + str(
            MQLQuery.view_props(query_args)) + '}'
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()
Beispiel #9
0
    def freebaseLookupEntities(self, request, obj_url):
        '''
        Given a fb query, and a property, looks up all of the possible values that can 
        be inserted for that property, i.e. type='/common/topic','/music/artist', ...
        
        Format: /api/freebaselookupentities?args={arg1:val1,arg2:val1,...}&property={prop}
        
        tested:
        curl "http://127.0.0.1:8000/api/freebaselookupentities?args=id:/en/the_beatles&property=type"
        '''

        query_args = {}
        query_args_str = request.GET['args']
        for a in query_args_str.split(','):
            query_args[a.split(':')[0]] = a.split(':')[1]

        property = request.GET['property']

        return '{ The property %s can be assigned the following entities: %s}' % (
            property, str(MQLQuery.view_entities(query_args, property)))
Beispiel #10
0
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()
Beispiel #11
0
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()
Beispiel #12
0
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()
Beispiel #13
0
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()
Beispiel #14
0
    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))
Beispiel #15
0
def test_get_entities():
    property = 'type'
    
    q = MQLQuery.make({'id':'/en/the_beatles','type':'/music/artist'}, ['*'])
    
    print MQLQuery.view_entities(q.query_args, property)
Beispiel #16
0
def test_get_props():
    q = MQLQuery.make({'id':'/en/the_beatles','type':'/music/artist'}, ['*'])
    
    print MQLQuery.view_props(q.query_args)
Beispiel #17
0
 def freebaseLookupEntities(self, request, obj_url):
     '''
     Given a fb query, and a property, looks up all of the possible values that can 
     be inserted for that property, i.e. type='/common/topic','/music/artist', ...
     
     Format: /api/freebaselookupentities?args={arg1:val1,arg2:val1,...}&property={prop}
     
     tested:
     curl "http://127.0.0.1:8000/api/freebaselookupentities?args=id:/en/the_beatles&property=type"
     '''
     
     query_args={}
     query_args_str = request.GET['args']
     for a in query_args_str.split(','):
         query_args[a.split(':')[0]]=a.split(':')[1]
     
     property = request.GET['property']
     
     return '{ The property %s can be assigned the following entities: %s}'%(property,str(MQLQuery.view_entities(query_args, property)))