1
1
mirror of https://github.com/github/semantic.git synced 2024-12-22 22:31:36 +03:00

Extract the computation of the unit vector to the where clause.

This commit is contained in:
Rob Rix 2016-08-09 11:03:02 -04:00
parent ccd734c117
commit dc4b0be491

View File

@ -96,9 +96,9 @@ decorateTermWithPQGram q = cata algebra
-- | Replaces bags of p,q-grams in a terms annotations with corresponding feature vectors. -- | Replaces bags of p,q-grams in a terms annotations with corresponding feature vectors.
decorateTermWithFeatureVector :: (Hashable label, Prologue.Foldable f, Functor f) => Int -> Cofree f (Record (Gram label ': fields)) -> Cofree f (Record (Vector.Vector Double ': fields)) decorateTermWithFeatureVector :: (Hashable label, Prologue.Foldable f, Functor f) => Int -> Cofree f (Record (Gram label ': fields)) -> Cofree f (Record (Vector.Vector Double ': fields))
decorateTermWithFeatureVector d = cata $ \ (RCons gram rest :< functor) -> decorateTermWithFeatureVector d = cata $ \ (RCons gram rest :< functor) ->
let unitVector = normalize ((`evalRand` mkQCGen (hash gram)) (sequenceA (Vector.replicate d getRandom))) in cofree (RCons (foldr (Vector.zipWith (+) . getField . extract) (unitVector (hash gram)) functor) rest :< functor)
cofree (RCons (foldr (Vector.zipWith (+) . getField . extract) unitVector functor) rest :< functor)
where normalize vec = fmap (/ vmagnitude vec) vec where normalize vec = fmap (/ vmagnitude vec) vec
unitVector hash = normalize ((`evalRand` mkQCGen hash) (sequenceA (Vector.replicate d getRandom)))
decorateTermWithUnitVector :: (Hashable label, Functor f) => Int -> Cofree f (Record (Gram label ': fields)) -> Cofree f (Record (Vector.Vector Double ': fields)) decorateTermWithUnitVector :: (Hashable label, Functor f) => Int -> Cofree f (Record (Gram label ': fields)) -> Cofree f (Record (Vector.Vector Double ': fields))
decorateTermWithUnitVector d = fmap $ \ (RCons gram rest) -> normalize ((`evalRand` mkQCGen (hash gram)) (sequenceA (Vector.replicate d getRandom))) .: rest decorateTermWithUnitVector d = fmap $ \ (RCons gram rest) -> normalize ((`evalRand` mkQCGen (hash gram)) (sequenceA (Vector.replicate d getRandom))) .: rest