2016-08-05 02:19:03 +03:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
2016-06-23 00:59:28 +03:00
|
|
|
module Data.RandomWalkSimilarity.Spec where
|
|
|
|
|
2017-01-08 07:27:01 +03:00
|
|
|
import Category
|
2017-06-15 16:46:51 +03:00
|
|
|
import Data.Array.IArray
|
2017-02-24 00:13:42 +03:00
|
|
|
import Data.Bifunctor
|
2017-01-08 07:27:01 +03:00
|
|
|
import Data.Functor.Listable
|
2017-04-24 22:53:11 +03:00
|
|
|
import RWS
|
2016-08-05 02:19:03 +03:00
|
|
|
import Data.Record
|
2017-01-08 07:27:01 +03:00
|
|
|
import Data.String
|
2017-02-23 22:17:52 +03:00
|
|
|
import Data.These
|
2016-06-27 22:37:32 +03:00
|
|
|
import Diff
|
2016-08-10 16:38:41 +03:00
|
|
|
import Info
|
2016-06-27 22:37:32 +03:00
|
|
|
import Patch
|
2016-06-23 00:59:28 +03:00
|
|
|
import Prologue
|
2016-06-27 22:37:32 +03:00
|
|
|
import Syntax
|
2016-06-23 00:59:28 +03:00
|
|
|
import Term
|
|
|
|
import Test.Hspec
|
2017-01-08 07:27:01 +03:00
|
|
|
import Test.Hspec.LeanCheck
|
2016-06-23 00:59:28 +03:00
|
|
|
|
|
|
|
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
|
2016-08-10 16:05:12 +03:00
|
|
|
prop "produces grams with stems of the specified length" $
|
2017-01-08 07:27:01 +03:00
|
|
|
\ (term, p, q) -> pqGramDecorator (rhead . headF) (positively p) (positively q) (unListableF term :: SyntaxTerm String '[Category]) `shouldSatisfy` all ((== positively p) . length . stem . rhead)
|
2016-06-23 00:59:28 +03:00
|
|
|
|
2016-08-10 16:05:12 +03:00
|
|
|
prop "produces grams with bases of the specified width" $
|
2017-01-08 07:27:01 +03:00
|
|
|
\ (term, p, q) -> pqGramDecorator (rhead . headF) (positively p) (positively q) (unListableF term :: SyntaxTerm String '[Category]) `shouldSatisfy` all ((== positively q) . length . base . rhead)
|
2016-06-23 00:59:28 +03:00
|
|
|
|
2016-08-09 23:34:10 +03:00
|
|
|
describe "featureVectorDecorator" $ do
|
|
|
|
prop "produces a vector of the specified dimension" $
|
2017-06-15 17:06:15 +03:00
|
|
|
\ (term, p, q, d) -> featureVectorDecorator (rhead . headF) (positively p) (positively q) (positively d) (unListableF term :: SyntaxTerm String '[Category]) `shouldSatisfy` all ((== (0, abs d)) . bounds . rhead)
|
2016-06-27 22:37:32 +03:00
|
|
|
|
|
|
|
describe "rws" $ do
|
2017-01-08 07:27:01 +03:00
|
|
|
prop "produces correct diffs" $
|
|
|
|
\ (as, bs) -> let tas = decorate <$> (unListableF <$> as :: [SyntaxTerm String '[Category]])
|
|
|
|
tbs = decorate <$> (unListableF <$> bs :: [SyntaxTerm String '[Category]])
|
2017-01-19 23:46:28 +03:00
|
|
|
root = cofree . ((Program :. Nil) :<) . Indexed
|
2017-02-24 00:13:42 +03:00
|
|
|
diff = wrap (pure (Program :. Nil) :< Indexed (stripDiff . diffThese <$> rws editDistance canCompare tas tbs)) in
|
2016-08-11 19:54:14 +03:00
|
|
|
(beforeTerm diff, afterTerm diff) `shouldBe` (Just (root (stripTerm <$> tas)), Just (root (stripTerm <$> tbs)))
|
2016-08-11 18:53:51 +03:00
|
|
|
|
2016-08-12 18:59:09 +03:00
|
|
|
it "produces unbiased insertions within branches" $
|
2017-02-23 22:13:13 +03:00
|
|
|
let (a, b) = (decorate (cofree ((StringLiteral :. Nil) :< Indexed [ cofree ((StringLiteral :. Nil) :< Leaf ("a" :: String)) ])), decorate (cofree ((StringLiteral :. Nil) :< Indexed [ cofree ((StringLiteral :. Nil) :< Leaf "b") ]))) in
|
2017-02-24 00:13:42 +03:00
|
|
|
fmap (bimap stripTerm stripTerm) (rws editDistance canCompare [ b ] [ a, b ]) `shouldBe` fmap (bimap stripTerm stripTerm) [ That a, These b b ]
|
|
|
|
|
2017-05-31 22:55:01 +03:00
|
|
|
where canCompare a b = headF a == headF b
|
2017-02-24 00:13:42 +03:00
|
|
|
|
2017-06-15 16:44:46 +03:00
|
|
|
decorate :: SyntaxTerm leaf '[Category] -> SyntaxTerm leaf '[FeatureVector, Category]
|
2016-09-16 01:32:57 +03:00
|
|
|
decorate = defaultFeatureVectorDecorator (category . headF)
|
2017-02-24 00:13:42 +03:00
|
|
|
|
|
|
|
diffThese = these deleting inserting replacing
|
|
|
|
|
|
|
|
editDistance = these (const 1) (const 1) (const (const 0))
|