Tuesday 25 August 2015

new function: hash

Just a simple one today. Some new python that maps a superposition to a superposition of the hash's of the kets.

Here is the python:
# ket-hash[size] |some ket>
#
# one is a ket
def ket_hash(one,size):
  logger.debug("ket-hash one: " + str(one))
  logger.debug("ket-hash size: " + size)
  try:
    size = int(size)
  except:
    return ket("",0)
  our_hash = hashlib.md5(one.label.encode('utf-8')).hexdigest()[-size:]
  return ket(our_hash,one.value)
And some simple examples:
sa: hash[6] split |a b c d e f>
|772661> + |31578f> + |8b5f33> + |e091ad> + |41ec32> + |29cce7>

sa: hash[10] split |u v w x y z>
|4f4f21d34c> + |4664205d2a> + |e77c0c5d68> + |4e155c67a6> + |22904f345d> + |b808451dd7>

-- slightly more interesting example:
sa: load fred-sam-friends.sw
sa: dump
----------------------------------------
|context> => |context: friends>

friends |Fred> => |Jack> + |Harry> + |Ed> + |Mary> + |Rob> + |Patrick> + |Emma> + |Charlie>

friends |Sam> => |Charlie> + |George> + |Emma> + |Jack> + |Rober> + |Frank> + |Julie>
----------------------------------------

sa: hash-friends |Fred> => hash[4] friends |_self>
sa: hash-friends |Sam> => hash[4] friends |_self>

sa: dump
----------------------------------------
|context> => |context: friends>

friends |Fred> => |Jack> + |Harry> + |Ed> + |Mary> + |Rob> + |Patrick> + |Emma> + |Charlie>
hash-friends |Fred> => |4f62> + |72ec> + |f3e0> + |315a> + |19b1> + |06ec> + |4a79> + |5cd8>

friends |Sam> => |Charlie> + |George> + |Emma> + |Jack> + |Rober> + |Frank> + |Julie>
hash-friends |Sam> => |5cd8> + |93a3> + |4a79> + |4f62> + |75f6> + |3e4b> + |47dd>
----------------------------------------

sa: common[friends] split |Fred Sam>
|Jack> + |Emma> + |Charlie>

sa: common[hash-friends] split |Fred Sam>
|4f62> + |4a79> + |5cd8>
I guess the point is sometimes the exact ket label doesn't matter. It is the network structure that matters. I guess we could also use it as a compression scheme of sorts. Say your data has kets with very long text labels, we could, in theory, compress that down using hashes. Providing the structure is the only thing of interest.

No comments:

Post a Comment