Ejemplo n.º 1
0
class TestClientCompareApi(unittest.TestCase):


    def setUp(self):
        self.client = FullClient(apiKey=conf.API_KEY, apiServer=conf.BASE_PATH, retinaName=conf.RETINA_NAME)

    def testCompare(self):
        resultMetric = self.client.compare(inputJSONarray)
        self.assertGreater(resultMetric.cosineSimilarity, 0.1)
        self.assertGreater(resultMetric.euclideanDistance, 0.1)
        self.assertGreater(resultMetric.jaccardDistance, 0.1)
        self.assertGreater(resultMetric.weightedScoring, 0.1)
        self.assertGreater(resultMetric.sizeRight, 10)
        self.assertGreater(resultMetric.sizeLeft, 10)
        self.assertGreater(resultMetric.overlappingLeftRight, 0.1)
        self.assertGreater(resultMetric.overlappingAll, 10)
        self.assertGreater(resultMetric.overlappingRightLeft, 0.1)

    def testCompareBulk(self):
        resultMetricList = self.client.compareBulk(bulkJSONarray)
        self.assertEqual(len(resultMetricList), 2)
        for resultMetric in resultMetricList:
            self.assertGreater(resultMetric.cosineSimilarity, 0.1)
            self.assertGreater(resultMetric.euclideanDistance, 0.1)
            self.assertGreater(resultMetric.jaccardDistance, 0.1)
            self.assertGreater(resultMetric.weightedScoring, 0.1)
            self.assertGreater(resultMetric.sizeRight, 10)
            self.assertGreater(resultMetric.sizeLeft, 10)
            self.assertGreater(resultMetric.overlappingLeftRight, 0.1)
            self.assertGreater(resultMetric.overlappingAll, 10)
            self.assertGreater(resultMetric.overlappingRightLeft, 0.1)
        

    def testException(self):
        # testing using only one input element in the array
        expectedException = False
        try:
            self.client.compare(oneTermInputJSONarray)
        except CorticalioException:
            expectedException = True
        self.assertTrue(expectedException)
        
        # testing JSON parse exception in the input
        expectedException = False
        try:
            self.client.compare(syntaxErrorJSONarray)
        except CorticalioException:
            expectedException = True
        self.assertTrue(expectedException)
Ejemplo n.º 2
0
class TestClientCompareApi(unittest.TestCase):
    def setUp(self):
        self.client = FullClient(apiKey=conf.API_KEY,
                                 apiServer=conf.BASE_PATH,
                                 retinaName=conf.RETINA_NAME)

    def testCompare(self):
        resultMetric = self.client.compare(inputJSONarray)
        self.assertGreater(resultMetric.cosineSimilarity, 0.1)
        self.assertGreater(resultMetric.euclideanDistance, 0.1)
        self.assertGreater(resultMetric.jaccardDistance, 0.1)
        self.assertGreater(resultMetric.weightedScoring, 0.1)
        self.assertGreater(resultMetric.sizeRight, 10)
        self.assertGreater(resultMetric.sizeLeft, 10)
        self.assertGreater(resultMetric.overlappingLeftRight, 0.1)
        self.assertGreater(resultMetric.overlappingAll, 10)
        self.assertGreater(resultMetric.overlappingRightLeft, 0.1)

    def testCompareBulk(self):
        resultMetricList = self.client.compareBulk(bulkJSONarray)
        self.assertEqual(len(resultMetricList), 2)
        for resultMetric in resultMetricList:
            self.assertGreater(resultMetric.cosineSimilarity, 0.1)
            self.assertGreater(resultMetric.euclideanDistance, 0.1)
            self.assertGreater(resultMetric.jaccardDistance, 0.1)
            self.assertGreater(resultMetric.weightedScoring, 0.1)
            self.assertGreater(resultMetric.sizeRight, 10)
            self.assertGreater(resultMetric.sizeLeft, 10)
            self.assertGreater(resultMetric.overlappingLeftRight, 0.1)
            self.assertGreater(resultMetric.overlappingAll, 10)
            self.assertGreater(resultMetric.overlappingRightLeft, 0.1)

    def testException(self):
        # testing using only one input element in the array
        expectedException = False
        try:
            self.client.compare(oneTermInputJSONarray)
        except CorticalioException:
            expectedException = True
        self.assertTrue(expectedException)

        # testing JSON parse exception in the input
        expectedException = False
        try:
            self.client.compare(syntaxErrorJSONarray)
        except CorticalioException:
            expectedException = True
        self.assertTrue(expectedException)
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
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