Esempio n. 1
0
    def testGetClosestStrings(self):
        """ Type check """
        cept = Cept()
        response = cept.getBitmap("snake")

        result = cept.getClosestStrings(response['positions'])
        self.assertTrue(type(result), 'list')
Esempio n. 2
0
  def testGetClosestStrings(self):
    """ Type check """
    cept = Cept()
    response = cept.getBitmap("snake")

    result = cept.getClosestStrings(response['positions'])
    self.assertTrue(type(result), 'list')
Esempio n. 3
0
class Term():
    def __init__(self):
        self.bitmap = None
        self.cept = Cept()

    def createFromString(self, string):
        self.bitmap = self.cept.getBitmap(string)['positions']
        return self

    def createFromBitmap(self, bitmap):
        self.bitmap = bitmap
        return self

    def toArray(self):
        array = [0] * 128 * 128

        for i in self.bitmap:
            array[i] = 1

        return array

    def closestString(self):
        if not len(self.bitmap):
            return ""

        closestStrings = self.cept.getClosestStrings(self.bitmap)
        if not len(closestStrings):
            return ""

        return closestStrings[0]['term']
Esempio n. 4
0
  def testSamePositions(self):
     """ Test that the SDR we get from the server hasn't changed,
          using default settings
     """

     cept = Cept()
     httpResponse = cept.getBitmap("fox")

     self.assertEqual(set(httpResponse['positions']),
                      set(foxHardCodedResponse['positions']))
Esempio n. 5
0
    def testSamePositions(self):
        """ Test that the SDR we get from the server hasn't changed,
          using default settings
     """

        cept = Cept()
        httpResponse = cept.getBitmap("fox")

        self.assertEqual(set(httpResponse['positions']),
                         set(foxHardCodedResponse['positions']))
Esempio n. 6
0
  def testGetBitmap(self):
    """ Type check what we get back from the cept object """
    cept = Cept()
    response = cept.getBitmap("fox")

    self.assertLessEqual(set(("width", "positions", "sparsity", "height")),
                         set(response))

    self.assertIsInstance(response['positions'], list, "Positions field is not a list")
    self.assertIsInstance(response['sparsity'], float, "Sparsity field is not a list")
    self.assertIsInstance(response['width'], int, "Width field is not an int")
    self.assertIsInstance(response['height'], int, "Height field is not an int")
Esempio n. 7
0
    def testGetBitmap(self):
        """ Type check what we get back from the cept object """
        cept = Cept()
        response = cept.getBitmap("fox")

        self.assertLessEqual(set(("width", "positions", "sparsity", "height")),
                             set(response))

        self.assertIsInstance(response['positions'], list,
                              "Positions field is not a list")
        self.assertIsInstance(response['sparsity'], float,
                              "Sparsity field is not a list")
        self.assertIsInstance(response['width'], int,
                              "Width field is not an int")
        self.assertIsInstance(response['height'], int,
                              "Height field is not an int")
Esempio n. 8
0
class Term():


  def __init__(self):
    self.bitmap = None
    self.cept = Cept()


  def createFromString(self, string):
    self.bitmap = self.cept.getBitmap(string)['positions']
    return self


  def createFromBitmap(self, bitmap):
    self.bitmap = bitmap
    return self


  def toArray(self):
    array = [0] * 128 * 128

    for i in self.bitmap:
      array[i] = 1

    return array


  def closestString(self):
    print "closestString:  " + repr(self.bitmap)
    if not len(self.bitmap):
      return ""

    closestStrings = self.cept.getClosestStrings(self.bitmap)
    if not len(closestStrings):
      return ""

    return closestStrings[0]['term']
Esempio n. 9
0
 def __init__(self):
   self.bitmap = None
   self.cept = Cept()
Esempio n. 10
0
 def testExceptionIfAPIKeyNotPresent(self, mockOS):
   with self.assertRaises(Exception) as e:
     cept = Cept()
   self.assertIn("Missing API key.", e.exception)
Esempio n. 11
0
 def testAPIKeyPresent(self):
   with patch.dict('os.environ', {'CEPT_API_KEY': 'apikey123'}):
       cept = Cept()
Esempio n. 12
0
 def __init__(self):
     self.bitmap = None
     self.cept = Cept()
Esempio n. 13
0
 def __init__(self):
     self.bitmap = None
     self.sparsity = None
     self.width = None
     self.height = None
     self.cept = Cept()
Esempio n. 14
0
class Term():
    def __init__(self):
        self.bitmap = None
        self.sparsity = None
        self.width = None
        self.height = None
        self.cept = Cept()

    def __repr__(self):
        return termJSONEncoder(self)

    def createFromString(self, string, enablePlaceholder=True):
        response = self.cept.getBitmap(string)
        self.bitmap = response['positions']
        self.sparsity = response['sparsity']
        self.width = response['width']
        self.height = response['height']

        if enablePlaceholder and self.sparsity == 0:
            state = random.getstate()
            random.seed(string)
            num = self.width * self.height
            bitmap = random.sample(range(num),
                                   int(TARGET_SPARSITY * num / 100))
            self.createFromBitmap(bitmap, self.width, self.height)
            random.setstate(state)

        return self

    def createFromBitmap(self, bitmap, width, height):
        self.bitmap = bitmap
        self.width = width
        self.height = height
        self.sparsity = (100.0 * len(bitmap)) / (width * height)
        return self

    def compare(self, term):
        """
    Compare self with the provided term. Calls CEPT compare and returns the
    corresponding dict.
    """
        return self.cept.client.compare(self.bitmap, term.bitmap)

    def toArray(self):
        array = [0] * self.width * self.height

        for i in self.bitmap:
            array[i] = 1

        return array

    def closestStrings(self):
        if not len(self.bitmap):
            return []

        return [
            result['term']
            for result in self.cept.getClosestStrings(self.bitmap)
        ]

    def closestString(self):
        closestStrings = self.closestStrings()

        if not len(closestStrings):
            return ""

        return closestStrings[0]
Esempio n. 15
0
 def __init__(self):
   self.bitmap   = None
   self.sparsity = None
   self.width    = None
   self.height   = None
   self.cept     = Cept()
Esempio n. 16
0
class Term():


  def __init__(self):
    self.bitmap   = None
    self.sparsity = None
    self.width    = None
    self.height   = None
    self.cept     = Cept()


  def __repr__(self):
    return termJSONEncoder(self)

  
  def createFromString(self, string, enablePlaceholder=True):
    response = self.cept.getBitmap(string)
    self.bitmap   = response['positions']
    self.sparsity = response['sparsity']
    self.width    = response['width']
    self.height   = response['height']

    if enablePlaceholder and self.sparsity == 0:
      state = random.getstate()
      random.seed(string)
      num = self.width * self.height
      bitmap = random.sample(range(num), int(TARGET_SPARSITY * num / 100))
      self.createFromBitmap(bitmap, self.width, self.height)
      random.setstate(state)

    return self


  def createFromBitmap(self, bitmap, width, height):
    self.bitmap = bitmap
    self.width = width
    self.height = height
    self.sparsity = (100.0 * len(bitmap)) / (width*height)
    return self


  def compare(self, term):
    """
    Compare self with the provided term. Calls CEPT compare and returns the
    corresponding dict.
    """
    return self.cept.client.compare(self.bitmap, term.bitmap)
  

  def toArray(self):
    array = [0] * self.width * self.height

    for i in self.bitmap:
      array[i] = 1

    return array


  def closestStrings(self):
    if not len(self.bitmap):
      return []

    return [result['term'] for result in
            self.cept.getClosestStrings(self.bitmap)]


  def closestString(self):
    closestStrings = self.closestStrings()

    if not len(closestStrings):
      return ""

    return closestStrings[0]