1
1
mirror of https://github.com/github/semantic.git synced 2024-12-22 06:11:49 +03:00
semantic/test/Data/RandomWalkSimilarity/Spec.hs

54 lines
3.0 KiB
Haskell
Raw Normal View History

{-# LANGUAGE DataKinds #-}
module Data.RandomWalkSimilarity.Spec where
import Data.Functor.Both
import Data.RandomWalkSimilarity
import Data.Record
import qualified Data.Vector as Vector
2016-06-27 22:37:32 +03:00
import Diff
import Info
2016-06-27 22:37:32 +03:00
import Patch
import Prologue
2016-06-27 22:37:32 +03:00
import Syntax
import Term
2016-10-13 02:19:36 +03:00
import Diffing
import Term.Arbitrary
import Test.Hspec
import Test.Hspec.QuickCheck
2016-06-27 18:46:22 +03:00
import Test.QuickCheck
spec :: Spec
spec = parallel $ do
2016-08-10 18:32:42 +03:00
let positively = succ . abs
2016-08-09 23:23:38 +03:00
describe "pqGramDecorator" $ do
prop "produces grams with stems of the specified length" $
2016-09-16 01:12:32 +03:00
\ (term, p, q) -> pqGramDecorator (rhead . headF) (positively p) (positively q) (toTerm term :: Term (Syntax Text) (Record '[Text])) `shouldSatisfy` all ((== positively p) . length . stem . rhead)
prop "produces grams with bases of the specified width" $
2016-09-16 01:12:32 +03:00
\ (term, p, q) -> pqGramDecorator (rhead . headF) (positively p) (positively q) (toTerm term :: Term (Syntax Text) (Record '[Text])) `shouldSatisfy` all ((== positively q) . length . base . rhead)
2016-08-09 23:34:10 +03:00
describe "featureVectorDecorator" $ do
prop "produces a vector of the specified dimension" $
2016-09-16 01:12:32 +03:00
\ (term, p, q, d) -> featureVectorDecorator (rhead . headF) (positively p) (positively q) (positively d) (toTerm term :: Term (Syntax Text) (Record '[Text])) `shouldSatisfy` all ((== positively d) . length . rhead)
2016-06-27 22:37:32 +03:00
describe "rws" $ do
let toTerm' = decorate . toTerm
prop "produces correct diffs" . forAll (scale (`div` 4) arbitrary) $
\ (as, bs) -> let tas = toTerm' <$> (as :: [ArbitraryTerm Text (Record '[Category])])
tbs = toTerm' <$> (bs :: [ArbitraryTerm Text (Record '[Category])])
root = cofree . ((Program .: RNil) :<) . Indexed
2016-10-13 02:19:36 +03:00
diff = wrap (pure (Program .: RNil) :< Indexed (stripDiff <$> rws compare getLabel tas tbs)) in
(beforeTerm diff, afterTerm diff) `shouldBe` (Just (root (stripTerm <$> tas)), Just (root (stripTerm <$> tbs)))
2016-08-11 18:53:51 +03:00
it "produces unbiased insertions within branches" $
2016-09-16 01:34:54 +03:00
let (a, b) = (decorate (cofree ((StringLiteral .: RNil) :< Indexed [ cofree ((StringLiteral .: RNil) :< Leaf ("a" :: Text)) ])), decorate (cofree ((StringLiteral .: RNil) :< Indexed [ cofree ((StringLiteral .: RNil) :< Leaf "b") ]))) in
2016-10-13 02:19:36 +03:00
fmap stripDiff (rws compare getLabel [ b ] [ a, b ]) `shouldBe` fmap stripDiff [ inserting a, copying b ]
2016-08-18 19:51:07 +03:00
where compare :: (HasField fields Category, Functor f, Eq (Cofree f Category)) => Term f (Record fields) -> Term f (Record fields) -> Maybe (Diff f (Record fields))
2016-08-18 18:40:33 +03:00
compare a b | (category <$> a) == (category <$> b) = Just (copying b)
| otherwise = if ((==) `on` category . extract) a b then Just (replacing a b) else Nothing
2016-08-18 18:40:33 +03:00
copying :: Functor f => Cofree f (Record fields) -> Free (CofreeF f (Both (Record fields))) (Patch (Cofree f (Record fields)))
copying = cata wrap . fmap pure
decorate :: SyntaxTerm leaf '[Category] -> SyntaxTerm leaf '[Vector.Vector Double, Category]
decorate = defaultFeatureVectorDecorator (category . headF)