class TestClassifyApi(unittest.TestCase): def setUp(self): self.client = FullClient(apiKey=conf.API_KEY, apiServer=conf.BASE_PATH, retinaName=conf.RETINA_NAME) def testCreateCategoryFilter(self): filterName = "filter1" filter1 = self.client.createCategoryFilter(filterName, positiveExamples, negativeExamples) self.assertGreater(len(filter1.positions), 50) self.assertEqual(filter1.categoryName, filterName) def testErrors(self): expectedException = False try: self.client.createCategoryFilter(None, positiveExamples, negativeExamples) except CorticalioException: expectedException = True self.assertTrue(expectedException) expectedException = False try: self.client.createCategoryFilter("filterName", [], negativeExamples) except CorticalioException: expectedException = True self.assertTrue(expectedException)
class LiteClient(object): """Minimalistic client for accessing core features of Cortical.io's Retina API in a simple way.""" def __init__(self, apiKey): self._fullClient = FullClient(apiKey, apiServer="http://api.cortical.io/rest", retinaName="en_associative") def _createDictionary(self, textOrFingerprint): if type(textOrFingerprint) == str: return {"text": textOrFingerprint} elif type(textOrFingerprint) == list: return {"positions": textOrFingerprint} else: raise CorticalioException("Invalid argument, cannot create input from: '%s'" % (str(textOrFingerprint))) def getSimilarTerms(self, textOrFingerprint): """Get the similar terms for a given text or fingerprint Args: textOrFingerprint, str OR list of integers Returns: list of str: the 20 most similar terms Raises: CorticalioException: if the request was not successful """ expression = self._createDictionary(textOrFingerprint) terms = self._fullClient.getSimilarTermsForExpression(json.dumps(expression), maxResults=20) return [t.term for t in terms] def getKeywords(self, text): """Get a list of keywords from the text Args: text, str: The input document Returns: list of str Raises: CorticalioException: if the request was not successful """ terms = self._fullClient.getKeywordsForText(text) return terms def getFingerprint(self, text): """Get the semantic fingerprint of the input text. Args: text, str: The text to be evaluated Returns: list of str: the positions of the semantic fingerprint Raises: CorticalioException: if the request was not successful """ fp = self._fullClient.getFingerprintForText(text) return fp.positions def compare(self, textOrFingerprint1, textOrFingerprint2): """Returns the semantic similarity of texts or fingerprints. Each argument can be eiter a text or a fingerprint. Args: textOrFingerprint1, str OR list of integers textOrFingerprint2, str OR list of integers Returns: float: the semantic similarity in the range [0;1] Raises: CorticalioException: if the request was not successful """ compareList = [self._createDictionary(textOrFingerprint1), self._createDictionary(textOrFingerprint2)] metric = self._fullClient.compare(json.dumps(compareList)) return metric.cosineSimilarity def createCategoryFilter(self, positiveExamples): """Creates a filter fingerprint. Args: positiveExamples, list(str): The list of positive example texts. Returns: list of int: the positions representing the filter representing the texts Raises: CorticalioException: if the request was not successful """ categoryFilter = self._fullClient.createCategoryFilter("CategoryFilter", positiveExamples) return categoryFilter.positions
class LiteClient(object): """Minimalistic client for accessing core features of Cortical.io's Retina API in a simple way.""" def __init__(self, apiKey): self._fullClient = FullClient(apiKey, apiServer="http://api.cortical.io/rest", retinaName="en_associative") def _createDictionary(self, textOrFingerprint): if type(textOrFingerprint) == str: return {"text": textOrFingerprint} elif type(textOrFingerprint) == list: return {"positions": textOrFingerprint} else: raise CorticalioException( "Invalid argument, cannot create input from: '%s'" % (str(textOrFingerprint))) def getSimilarTerms(self, textOrFingerprint): """Get the similar terms for a given text or fingerprint Args: textOrFingerprint, str OR list of integers Returns: list of str: the 20 most similar terms Raises: CorticalioException: if the request was not successful """ expression = self._createDictionary(textOrFingerprint) terms = self._fullClient.getSimilarTermsForExpression( json.dumps(expression), maxResults=20) return [t.term for t in terms] def getKeywords(self, text): """Get a list of keywords from the text Args: text, str: The input document Returns: list of str Raises: CorticalioException: if the request was not successful """ terms = self._fullClient.getKeywordsForText(text) return terms def getFingerprint(self, text): """Get the semantic fingerprint of the input text. Args: text, str: The text to be evaluated Returns: list of str: the positions of the semantic fingerprint Raises: CorticalioException: if the request was not successful """ fp = self._fullClient.getFingerprintForText(text) return fp.positions def compare(self, textOrFingerprint1, textOrFingerprint2): """Returns the semantic similarity of texts or fingerprints. Each argument can be eiter a text or a fingerprint. Args: textOrFingerprint1, str OR list of integers textOrFingerprint2, str OR list of integers Returns: float: the semantic similarity in the range [0;1] Raises: CorticalioException: if the request was not successful """ compareList = [ self._createDictionary(textOrFingerprint1), self._createDictionary(textOrFingerprint2) ] metric = self._fullClient.compare(json.dumps(compareList)) return metric.cosineSimilarity def createCategoryFilter(self, positiveExamples): """Creates a filter fingerprint. Args: positiveExamples, list(str): The list of positive example texts. Returns: list of int: the positions representing the filter representing the texts Raises: CorticalioException: if the request was not successful """ categoryFilter = self._fullClient.createCategoryFilter( "CategoryFilter", positiveExamples) return categoryFilter.positions