Changeset 5374 for ndgCommon


Ignore:
Timestamp:
10/06/09 15:10:30 (10 years ago)
Author:
cbyrom
Message:

Update vocab server 'getRelated' calls to include the required data
to allow proper invocation of services + adjust calls so that the
service is ran for both vocab terms + free text inputs and the results
are combined + extend tests.

Location:
ndgCommon/trunk/ndg/common
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ndgCommon/trunk/ndg/common/src/clients/http/vocabserverclient.py

    r5135 r5374  
    44 @author: B Lawrence?, C Byrom, Tessella, Feb 09 
    55''' 
    6 import logging, urlparse 
     6import logging, urlparse, urllib 
    77from xml.etree import ElementTree as ET 
    88from generichttpclient import GenericHTTPClient 
     
    2020    WHAT_LISTS_QUERY = '%swhatLists' 
    2121    SEARCH_VOCAB_QUERY = '%ssearchVocab?listKey=%s&searchTerm=%s' 
    22      
     22    GET_RELATED_QUERY = '%sgetRelatedRecordByCriteria?subjectText=%s&predicate=255&inferences=True&objectList=%s&caseSensitivity=%s' 
     23      
    2324    def __init__(self,  
    2425                 path = "http://vocab.ndg.nerc.ac.uk/axis2/services/vocab/", 
     
    3233        parsedURL = urlparse.urlparse(url) 
    3334        self.ns = "%s://%s/" %(parsedURL.scheme, parsedURL.hostname) 
    34  
     35        self.objectList = '%slist/P211/current' %self.ns 
    3536             
    3637        super(VocabServerClient, self).__init__(proxyServer = proxyServer) 
     
    3839 
    3940 
    40     def getRelated(self,subject): 
     41    def getRelated(self, subject ,caseSensitive = "false", withTarget = True): 
    4142        '''  
    4243        Get a related record 
    4344        @param subject: subject text to search for in vocab server 
     45        @keyword caseSensitive - currently the search only works if this is set to "false" 
     46        @keyword withTarget: if True, limits search to the specified object list 
     47        - this makes things run much faster 
    4448        @return 2D array of results:  
    4549        [0] - broad result matches 
     
    5155        parent entry term to this 
    5256        ''' 
     57        # avoid any spaces/funny non-html characters 
     58        subject = urllib.quote(subject) 
    5359        logging.debug("Getting vocab data on subject term, '%s'" %subject) 
    54         url='%sgetRelatedRecordByCriteria?subjectText=%s&predicate=255&inferences=True&objectList=%slist/P211/current'%(self.path,subject,self.ns) 
     60        url = self.GET_RELATED_QUERY  %(self.path, subject, self.objectList, caseSensitive) 
     61 
     62        if withTarget: 
     63            url+='&subjectList=%s'%self.objectList 
     64             
    5565        doc = self.readURL(url) 
    5666        x=ET.fromstring(doc) 
     
    6373        logging.debug("- returning info on subject term") 
    6474        return [self.broader,self.narrower,self.synonyms] 
    65  
     75     
    6676     
    6777    def getList(self, listKey): 
  • ndgCommon/trunk/ndg/common/unittests/clients/http/testvocabserverclient.py

    r5147 r5374  
    3232    def testGetRelated1(self): 
    3333        res = self.client.getRelated('rain') 
    34         self.assertEquals(res[1][0], 'rain') 
     34        self.assertEquals(res[0][0], 'precipitation') 
    3535 
    3636     
    3737    def testGetRelated2(self): 
    3838        res = self.client.getRelated('snow') 
    39         self.assertEquals(res[1][0], 'snow') 
     39        self.assertEquals(res[0][0], 'precipitation') 
     40 
     41     
     42    def testGetRelated3(self): 
     43        res = self.client.getRelated('sno*') 
     44        self.assertEquals(res[0][0], 'precipitation') 
     45        self.assertEquals(res[2][0], 'anser caerulescens') 
     46 
     47     
     48    def testGetRelated4(self): 
     49        res = self.client.getRelated('SNOW') 
     50        self.assertEquals(res[0][0], 'precipitation') 
     51     
     52    def testGetRelated5(self): 
     53        res = self.client.getRelated('precipitation') 
     54        self.assertEquals(res[0][0], 'meteorology') 
     55        self.assertEquals(res[1][1], 'snow') 
    4056 
    4157         
     
    4460        self.assertEquals([], res[0]) 
    4561        self.assertEquals([], res[1]) 
     62        self.assertEquals([], res[2]) 
    4663        self.assertEquals(3, len(res)) 
    4764         
Note: See TracChangeset for help on using the changeset viewer.