1
1
mirror of https://github.com/github/semantic.git synced 2025-01-04 13:34:31 +03:00

Add a defaultFeatureVectorDecorator function.

This commit is contained in:
Rob Rix 2016-08-18 14:02:48 -04:00
parent 900a0b7a1f
commit 1473c10659

View File

@ -2,6 +2,7 @@
module Data.RandomWalkSimilarity module Data.RandomWalkSimilarity
( rws ( rws
, pqGramDecorator , pqGramDecorator
, defaultFeatureVectorDecorator
, featureVectorDecorator , featureVectorDecorator
, editDistanceUpTo , editDistanceUpTo
, defaultD , defaultD
@ -125,13 +126,17 @@ unitVector d hash = normalize ((`evalRand` mkQCGen hash) (sequenceA (Vector.repl
where normalize vec = fmap (/ vmagnitude vec) vec where normalize vec = fmap (/ vmagnitude vec) vec
vmagnitude = sqrtDouble . Vector.sum . fmap (** 2) vmagnitude = sqrtDouble . Vector.sum . fmap (** 2)
-- | Annotates a term with a feature vector at each node. -- | Annotates a term with a feature vector at each node, parameterized by stem length, base width, and feature vector dimensions.
featureVectorDecorator :: (Hashable label, Traversable f) => (forall b. CofreeF f (Record fields) b -> label) -> Int -> Int -> Int -> Cofree f (Record fields) -> Cofree f (Record (Vector.Vector Double ': fields)) featureVectorDecorator :: (Hashable label, Traversable f) => (forall b. CofreeF f (Record fields) b -> label) -> Int -> Int -> Int -> Cofree f (Record fields) -> Cofree f (Record (Vector.Vector Double ': fields))
featureVectorDecorator getLabel p q d featureVectorDecorator getLabel p q d
= cata (\ (RCons gram rest :< functor) -> = cata (\ (RCons gram rest :< functor) ->
cofree ((foldr (Vector.zipWith (+) . getField . extract) (unitVector d (hash gram)) functor .: rest) :< functor)) cofree ((foldr (Vector.zipWith (+) . getField . extract) (unitVector d (hash gram)) functor .: rest) :< functor))
. pqGramDecorator getLabel p q . pqGramDecorator getLabel p q
-- | Annotates a term with a feature vector at each node, using the default values for the p, q, and d parameters.
defaultFeatureVectorDecorator :: (Hashable label, Traversable f) => (forall b. CofreeF f (Record fields) b -> label) -> Cofree f (Record fields) -> Cofree f (Record (Vector.Vector Double ': fields))
defaultFeatureVectorDecorator getLabel = featureVectorDecorator getLabel defaultP defaultQ defaultD
-- | Strips the head annotation off a term annotated with non-empty records. -- | Strips the head annotation off a term annotated with non-empty records.
stripTerm :: Functor f => Cofree f (Record (h ': t)) -> Cofree f (Record t) stripTerm :: Functor f => Cofree f (Record (h ': t)) -> Cofree f (Record t)
stripTerm = fmap rtail stripTerm = fmap rtail