Exemplo n.º 1
0
def test_assertion():
    # fresh start
    Dataset.drop_collection()
    Assertion.drop_collection()

    #create test dataset
    dataset = Dataset.create(language='en', name='/data/test')

    #make a test assertion
    a1 = Assertion.make('/data/test', "/rel/IsA",
                        ["/concept/test/assertion", "/concept/test/test"])

    #verify that it exists in the database
    assert a1.id is not None

    #make sure attributes are readable
    a1.dataset
    a1.relation
    a1.arguments
    a1.argstr
    a1.complete
    a1.context
    a1.polarity
    a1.confidence

    #make an identical test assertion
    a2 = Assertion.make('/data/test', "/rel/IsA",
                        ["/concept/test/assertion", "/concept/test/test"])

    #verify that it exists in the database
    assert a2.id is not None
    print a2.id
    print a1.id
    print a1.argstr

    #verify that attributes are readable
    a2.dataset
    a2.relation
    a2.arguments
    a2.argstr
    a2.complete
    a2.context
    a2.polarity
    a2.confidence

    #check that all checked attributes are the same
    assert a1.dataset == a2.dataset
    assert a1.relation == a2.relation
    assert a1.arguments == a2.arguments
    assert a1.polarity == a2.polarity
    assert a1.context == a2.context

    #verify that the ID's are the same
    assert (a1.id == a2.id)

    #clean up
    Dataset.drop_collection()
    Assertion.drop_collection()
Exemplo n.º 2
0
def test_assertion():
    # fresh start
    Dataset.drop_collection()
    Assertion.drop_collection() 
    
    #create test dataset
    dataset = Dataset.create(language = 'en', name = '/data/test')
    
    #make a test assertion
    a1 = Assertion.make('/data/test',"/rel/IsA",["/concept/test/assertion", "/concept/test/test"])

    #verify that it exists in the database
    assert a1.id is not None

    #make sure attributes are readable
    a1.dataset
    a1.relation
    a1.arguments
    a1.argstr
    a1.complete
    a1.context
    a1.polarity
    a1.confidence

    #make an identical test assertion
    a2 = Assertion.make('/data/test',"/rel/IsA",["/concept/test/assertion", "/concept/test/test"])

    #verify that it exists in the database
    assert a2.id is not None
    print a2.id
    print a1.id
    print a1.argstr
    
    #verify that attributes are readable
    a2.dataset
    a2.relation
    a2.arguments
    a2.argstr
    a2.complete
    a2.context
    a2.polarity
    a2.confidence
    
    #check that all checked attributes are the same
    assert a1.dataset == a2.dataset
    assert a1.relation == a2.relation
    assert a1.arguments == a2.arguments
    assert a1.polarity == a2.polarity
    assert a1.context == a2.context

    #verify that the ID's are the same
    assert (a1.id == a2.id)

    #clean up
    Dataset.drop_collection()
    Assertion.drop_collection() 
Exemplo n.º 3
0
    def fb_entity_property_from_id(self, dset, user, polarity=1, context=None):
        '''
        Called when the result field is not '*',
        Only returns (property,value) pairs for properties 
        listed in the results_args and query_args,
        Makes assertions for all of these pairs
        '''
        # create or get dataset
        try:
            dataset = Dataset.objects.get(name=dset, language='en')
        except DoesNotExist:
            dataset = Dataset.create(name=dset, language='en')

        query = [self.query_args]

        assertionscreated = []

        # start importing from freebase
        mss = HTTPMetawebSession("http://api.freebase.com")

        for searchterm in self.result_args:
            query[0][searchterm] = {}
            try:
                results = mss.mqlread(query)
                a = Assertion.make(
                    dataset.name,
                    '/rel/freebase/has%s' % searchterm.capitalize(),
                    [self.query_args['id'], results[0][searchterm]['id']])
                a.add_support(dataset.name + '/contributor/' + user)
                assertionscreated.append(a)

            except MetawebError as me1:
                if str(me1.args).rfind(
                        '/api/status/error/mql/result') is not -1:
                    query[0][searchterm] = [{}]
                    results = mss.mqlread(query)
                    for result in results[0][searchterm]:
                        a = Assertion.make(
                            dataset.name,
                            '/rel/freebase/has%s' % searchterm.capitalize(),
                            [self.query_args['id'], result['id']])
                        a.add_support(dataset.name + '/contributor/' + user)
                        assertionscreated.append(a)

                elif str(me1.args).rfind(
                        '/api/status/error/mql/type') is not -1:
                    print 'The property %s is not recognized.' % searchterm
                    return

                else:
                    print str(me1.args)
                    return

            del query[0][searchterm]
        return assertionscreated
Exemplo n.º 4
0
def test_sentence():

    dataset = Dataset.create(language='en', name='/data/test')
    
    #create test sentence with dataset
    sentence1 = Sentence.make('/data/test', "This is a test sentence.")

    #check it was saved to the database
    assert sentence1.id is not None

    #make sure its attributes are readable
    sentence1.text
    sentence1.words
    sentence1.dataset
    sentence1.derived_assertions
    sentence1.confidence

    #make the same sentence, this time using dataset object instead of string
    sentence2 = Sentence.make(dataset, "This is a test sentence.")

    #check that it was saved to the database
    assert sentence2.id is not None

    #check that sentence1 and sentence2 have the same ID
    assert (sentence1.id == sentence2.id)
    
    #check that its attributes are readable
    sentence2.text
    sentence2.words
    sentence2.dataset
    sentence2.derived_assertions
    sentence2.confidence

    #make a different sentence
    sentence3 = Sentence.make('/data/test', "This is a different test sentence.");
    
    #make sure it exists in the database and is different
    assert sentence3.id is not None
    assert sentence3.id is not sentence1.id
    assert sentence3.id is not sentence2.id

    #make sure its attributes are readable
    sentence3.text
    sentence3.words
    sentence3.dataset
    sentence3.derived_assertions
    sentence3.confidence

    #clean up by dropping collections
    Dataset.drop_collection()
    Sentence.drop_collection()
Exemplo n.º 5
0
def test_assertion():
    # fresh start
    Dataset.drop_collection()
    Assertion.drop_collection()
    
    #create test dataset
    dataset = Dataset.create(language = 'en', name = '/data/test')

    #make a test assertion
    a1 = Assertion.make('/data/test',"/rel/IsA",["/concept/test/assertion", "/concept/test/test"])
    e = Expression.make(a1, "{0} is a {1}", ['this assertion', 'test'], 'en')
    print e.assertion
    e.add_support([dataset.get_root_reason()])
    a1.add_support([dataset.get_root_reason()])
    a1.save()
    a1.make_generalizations('/data/test/root')
    a2 = Assertion.objects.get(
        dataset='/data/test',
        relation='/rel/IsA',
        argstr="/concept/test/assertion,/concept/test/test"
    )
    print a1.get_expressions()
    assert a2.get_expressions()[0].text == 'this assertion is a test'

    a3 = Assertion.objects.get(
        dataset='/data/test',
        relation='/rel/IsA',
        argstr="/concept/test/assertion,*"
    )
    assert a3.get_expressions()[0].text == 'this assertion is a {1}'
    
    a4 = Assertion.objects.get(
        dataset='/data/test',
        relation='/rel/IsA',
        argstr="*,/concept/test/test"
    )
    assert a4.get_expressions()[0].text == '{0} is a test'
    
    a5 = Assertion.objects.get(
        dataset='/data/test',
        relation='/rel/IsA',
        argstr="*,*"
    )
    assert a5.get_expressions()[0].text == '{0} is a {1}'
    
    #clean up
    Dataset.drop_collection()
    Assertion.drop_collection()
Exemplo n.º 6
0
 def fb_entity_property_from_id(self, dset, user, polarity=1, context=None):
     '''
     Called when the result field is not '*',
     Only returns (property,value) pairs for properties 
     listed in the results_args and query_args,
     Makes assertions for all of these pairs
     '''
     # create or get dataset
     try:
         dataset = Dataset.objects.get(name=dset, language='en')
     except DoesNotExist:
         dataset = Dataset.create(name=dset, language='en')
     
     query = [self.query_args]
     
     assertionscreated = []
     
     # start importing from freebase
     mss = HTTPMetawebSession("http://api.freebase.com")
     
     for searchterm in self.result_args:
         query[0][searchterm]={}
         try:    
             results = mss.mqlread(query)
             a = Assertion.make(dataset.name, '/rel/freebase/has%s'%searchterm.capitalize(), [self.query_args['id'],results[0][searchterm]['id']])
             a.add_support(dataset.name + '/contributor/' + user)           
             assertionscreated.append(a)
     
         except MetawebError as me1:
             if str(me1.args).rfind('/api/status/error/mql/result') is not -1:
                 query[0][searchterm]=[{}]
                 results = mss.mqlread(query)
                 for result in results[0][searchterm]:
                     a = Assertion.make(dataset.name, '/rel/freebase/has%s'%searchterm.capitalize(), [self.query_args['id'],result['id']])
                     a.add_support(dataset.name + '/contributor/' + user)
                     assertionscreated.append(a)
             
             elif str(me1.args).rfind('/api/status/error/mql/type') is not -1:
                 print 'The property %s is not recognized.' % searchterm
                 return
         
             else:
                 print str(me1.args)
                 return
     
         del query[0][searchterm]
     return assertionscreated
Exemplo n.º 7
0
def test_assertion():
    # fresh start
    Dataset.drop_collection()
    Assertion.drop_collection()

    #create test dataset
    dataset = Dataset.create(language='en', name='/data/test')

    #make a test assertion
    a1 = Assertion.make('/data/test', "/rel/IsA",
                        ["/concept/test/assertion", "/concept/test/test"])
    e = Expression.make(a1, "{0} is a {1}", ['this assertion', 'test'], 'en')
    print e.assertion
    e.add_support([dataset.get_root_reason()])
    a1.add_support([dataset.get_root_reason()])
    a1.save()
    a1.make_generalizations('/data/test/root')
    a2 = Assertion.objects.get(
        dataset='/data/test',
        relation='/rel/IsA',
        argstr="/concept/test/assertion,/concept/test/test")
    print a1.get_expressions()
    assert a2.get_expressions()[0].text == 'this assertion is a test'

    a3 = Assertion.objects.get(dataset='/data/test',
                               relation='/rel/IsA',
                               argstr="/concept/test/assertion,*")
    assert a3.get_expressions()[0].text == 'this assertion is a {1}'

    a4 = Assertion.objects.get(dataset='/data/test',
                               relation='/rel/IsA',
                               argstr="*,/concept/test/test")
    assert a4.get_expressions()[0].text == '{0} is a test'

    a5 = Assertion.objects.get(dataset='/data/test',
                               relation='/rel/IsA',
                               argstr="*,*")
    assert a5.get_expressions()[0].text == '{0} is a {1}'

    #clean up
    Dataset.drop_collection()
    Assertion.drop_collection()
Exemplo n.º 8
0
 def fb_entity_from_id(self, dset, user, polarity=1, context=None):
     ''' 
     Called when the result field is '*'
     Returns all of the results (property-value) pairs for the given id object,
     Makes assertions of these results
     '''
     try:
         dataset = Dataset.objects.get(name=dset, language='en')
     except DoesNotExist:
         dataset = Dataset.create(name=dset, language='en')
 
     query = [dict(self.query_args,**{'*':{}})]
     
     assertionscreated = []
     
     # start importing from freebase
     mss = freebase.HTTPMetawebSession("http://api.freebase.com")
     results = mss.mqlread(query)
     #print 'RESULTS ARE'
     #print results
     if type(results)==list:
         results = results[0]
     
     for key in self.query_args.keys():
         initial_assertion = Assertion.make(dataset.name, '/rel/freebase/has%s'%key.capitalize(), [self.query_args['id'],self.query_args[key]])
         initial_assertion.add_support(dataset.name + '/contributor/' + user)
         assertionscreated.append(initial_assertion)
     #print 'MADE INITIAL ASSERTION'
     # Go through all properties, excluding properties in the skip_props list and properties whose results are not of type list  
     for property in [r for r in results if r not in self.skip_props and r not in self.query_args.keys()]:
         # Use properties to index concepts, and only use concepts with an explicit 'mmid' field
         if type(results[property])==list:
             for value in results[property]:
                 try:
                     #print self.query_args['id']
                     #print value['id']
                     a = Assertion.make(dataset.name, '/rel/freebase/has%s'%property.capitalize(), [self.query_args['id'],value['id']])
                 except:
                     #print self.query_args['id']
                     #print value['id']
                     a = Assertion.make(dataset.name, '/rel/freebase/has%s'%property.capitalize(), [self.query_args['id'],value['value']])
             
                 a.add_support(dataset.name + '/contributor/' + user)
                 assertionscreated.append(a)
         else:
             #print results[property]
             try:
                 a = Assertion.make(dataset.name, '/rel/freebase/has%s'%property.capitalize(), [self.query_args['id'],results[property]['value']])
                 a.add_support(dataset.name + '/contributor/' + 'nholm')
                 assertionscreated.append(a)
             except:
                 #print 'second exception'
                 #a = Assertion.make(dataset.name, '/rel/freebase/has%s'%property.capitalize(), [self.query_args['mmid'],results[property]['mmid']])
                 try:
                     a = Assertion.make(dataset.name, '/rel/freebase/has%s'%property.capitalize(), [self.query_args['id'],results[property]['id']])
                     a.add_support(dataset.name + '/contributor/' + 'nholm')
                     assertionscreated.append(a)
                 except:
                     pass
         
     return assertionscreated
Exemplo n.º 9
0
 def fb_entity_from_mid(self, dset, user, polarity=1, context=None):
     ''' 
     Called when the result field is '*'
     Returns all of the results (property-value) pairs for the given mid object,
     Makes assertions of these results
     '''
     try:
         dataset = Dataset.objects.get(name=dset, language='en')
     except DoesNotExist:
         dataset = Dataset.create(name=dset, language='en')
 
     query = [dict(self.query_args,**{'*':{}})]
     
     assertionscreated = []
     
     # start importing from freebase
     mss = freebase.HTTPMetawebSession("http://api.freebase.com")
     try:
         results = mss.mqlread(query)
     except MetawebError:
         print 'MetawebError occurred at %s'%str(query)
         return []
     
     
     #print results[0]
     
     if type(results)==list:
         results = results[0]
     
     #for key in self.query_args.keys():
     #    initial_assertion = Assertion.make(dataset.name, '/rel/freebase/has%s'%key.capitalize(), [self.query_args['mid'],self.query_args[key]])
     #    initial_assertion.add_support(dataset.name + '/contributor/' + user)
     #    assertionscreated.append(initial_assertion)
     
     for property in [r for r in results if r not in self.skip_props and r not in self.query_args.keys()]:
         #print property
         
         if type(results[property])==list:
             for value in results[property]:
                 try:
                     #print self.query_args['id']
                     #print value['id']
                     a = Assertion.make(dataset.name, '/rel/freebase/has%s'%property.capitalize(), [self.query_args['mid'],value['id']])
                 except:
                     #print self.query_args['id']
                     #print value['id']
                     a = Assertion.make(dataset.name, '/rel/freebase/has%s'%property.capitalize(), [self.query_args['mid'],value['value']])
             
                 a.add_support(dataset.name + '/contributor/' + user)
                 assertionscreated.append(a)
         else:
             #print results[property]
             try:
                 a = Assertion.make(dataset.name, '/rel/freebase/has%s'%property.capitalize(), [self.query_args['mid'],results[property]['value']])
                 a.add_support(dataset.name + '/contributor/' + 'nholm')
                 assertionscreated.append(a)
             except:
                 #print 'second exception'
                 #a = Assertion.make(dataset.name, '/rel/freebase/has%s'%property.capitalize(), [self.query_args['mmid'],results[property]['mmid']])
                 try:
                     a = Assertion.make(dataset.name, '/rel/freebase/has%s'%property.capitalize(), [self.query_args['mid'],results[property]['id']])
                     a.add_support(dataset.name + '/contributor/' + 'nholm')
                     assertionscreated.append(a)
                 except:
                     pass
     return assertionscreated
Exemplo n.º 10
0
    def fb_entity_from_id(self, dset, user, polarity=1, context=None):
        ''' 
        Called when the result field is '*'
        Returns all of the results (property-value) pairs for the given id object,
        Makes assertions of these results
        '''
        try:
            dataset = Dataset.objects.get(name=dset, language='en')
        except DoesNotExist:
            dataset = Dataset.create(name=dset, language='en')

        query = [dict(self.query_args, **{'*': {}})]

        assertionscreated = []

        # start importing from freebase
        mss = freebase.HTTPMetawebSession("http://api.freebase.com")
        results = mss.mqlread(query)
        #print 'RESULTS ARE'
        #print results
        if type(results) == list:
            results = results[0]

        for key in self.query_args.keys():
            initial_assertion = Assertion.make(
                dataset.name, '/rel/freebase/has%s' % key.capitalize(),
                [self.query_args['id'], self.query_args[key]])
            initial_assertion.add_support(dataset.name + '/contributor/' +
                                          user)
            assertionscreated.append(initial_assertion)
        #print 'MADE INITIAL ASSERTION'
        # Go through all properties, excluding properties in the skip_props list and properties whose results are not of type list
        for property in [
                r for r in results if r not in self.skip_props
                and r not in self.query_args.keys()
        ]:
            # Use properties to index concepts, and only use concepts with an explicit 'mmid' field
            if type(results[property]) == list:
                for value in results[property]:
                    try:
                        #print self.query_args['id']
                        #print value['id']
                        a = Assertion.make(
                            dataset.name,
                            '/rel/freebase/has%s' % property.capitalize(),
                            [self.query_args['id'], value['id']])
                    except:
                        #print self.query_args['id']
                        #print value['id']
                        a = Assertion.make(
                            dataset.name,
                            '/rel/freebase/has%s' % property.capitalize(),
                            [self.query_args['id'], value['value']])

                    a.add_support(dataset.name + '/contributor/' + user)
                    assertionscreated.append(a)
            else:
                #print results[property]
                try:
                    a = Assertion.make(
                        dataset.name,
                        '/rel/freebase/has%s' % property.capitalize(),
                        [self.query_args['id'], results[property]['value']])
                    a.add_support(dataset.name + '/contributor/' + 'nholm')
                    assertionscreated.append(a)
                except:
                    #print 'second exception'
                    #a = Assertion.make(dataset.name, '/rel/freebase/has%s'%property.capitalize(), [self.query_args['mmid'],results[property]['mmid']])
                    try:
                        a = Assertion.make(
                            dataset.name,
                            '/rel/freebase/has%s' % property.capitalize(),
                            [self.query_args['id'], results[property]['id']])
                        a.add_support(dataset.name + '/contributor/' + 'nholm')
                        assertionscreated.append(a)
                    except:
                        pass

        return assertionscreated
Exemplo n.º 11
0
    def fb_entity_from_mid(self, dset, user, polarity=1, context=None):
        ''' 
        Called when the result field is '*'
        Returns all of the results (property-value) pairs for the given mid object,
        Makes assertions of these results
        '''
        try:
            dataset = Dataset.objects.get(name=dset, language='en')
        except DoesNotExist:
            dataset = Dataset.create(name=dset, language='en')

        query = [dict(self.query_args, **{'*': {}})]

        assertionscreated = []

        # start importing from freebase
        mss = freebase.HTTPMetawebSession("http://api.freebase.com")
        try:
            results = mss.mqlread(query)
        except MetawebError:
            print 'MetawebError occurred at %s' % str(query)
            return []

        #print results[0]

        if type(results) == list:
            results = results[0]

        #for key in self.query_args.keys():
        #    initial_assertion = Assertion.make(dataset.name, '/rel/freebase/has%s'%key.capitalize(), [self.query_args['mid'],self.query_args[key]])
        #    initial_assertion.add_support(dataset.name + '/contributor/' + user)
        #    assertionscreated.append(initial_assertion)

        for property in [
                r for r in results if r not in self.skip_props
                and r not in self.query_args.keys()
        ]:
            #print property

            if type(results[property]) == list:
                for value in results[property]:
                    try:
                        #print self.query_args['id']
                        #print value['id']
                        a = Assertion.make(
                            dataset.name,
                            '/rel/freebase/has%s' % property.capitalize(),
                            [self.query_args['mid'], value['id']])
                    except:
                        #print self.query_args['id']
                        #print value['id']
                        a = Assertion.make(
                            dataset.name,
                            '/rel/freebase/has%s' % property.capitalize(),
                            [self.query_args['mid'], value['value']])

                    a.add_support(dataset.name + '/contributor/' + user)
                    assertionscreated.append(a)
            else:
                #print results[property]
                try:
                    a = Assertion.make(
                        dataset.name,
                        '/rel/freebase/has%s' % property.capitalize(),
                        [self.query_args['mid'], results[property]['value']])
                    a.add_support(dataset.name + '/contributor/' + 'nholm')
                    assertionscreated.append(a)
                except:
                    #print 'second exception'
                    #a = Assertion.make(dataset.name, '/rel/freebase/has%s'%property.capitalize(), [self.query_args['mmid'],results[property]['mmid']])
                    try:
                        a = Assertion.make(
                            dataset.name,
                            '/rel/freebase/has%s' % property.capitalize(),
                            [self.query_args['mid'], results[property]['id']])
                        a.add_support(dataset.name + '/contributor/' + 'nholm')
                        assertionscreated.append(a)
                    except:
                        pass
        return assertionscreated