Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- > makeInverseIndex ["hello world", "world test", "hello test"]
- fromList [("hello",fromList [0,2]),("test",fromList [1,2]),("world",fromList [0,1])]
- > orSearch ["hello", "world"] $ makeInverseIndex ["hello world", "world test", "hello test"]
- fromList [0,1,2]
- > andSearch ["hello", "world"] $ makeInverseIndex ["hello world", "world test", "hello test"]
- fromList [0]
- import Data.List as L
- import Data.Map.Strict as M
- import Data.Set as S
- makeInverseIndex :: [String] -> Map String (Set Int)
- makeInverseIndex = L.foldr (unionWith S.union . uncurry group) M.empty . zipWithIndex . L.map words
- where
- zipWithIndex :: [a] -> [(Int, a)]
- zipWithIndex xs = zip [0..length xs] xs
- group :: Ord k => v -> [k] -> Map k (Set v)
- group n = M.fromList . L.map (w -> (w, S.singleton n))
- orSearch :: [String] -> Map String (Set Int) -> Set Int
- orSearch words =
- M.foldr S.union S.empty . pick words
- andSearch :: [String] -> Map String (Set Int) -> Set Int
- andSearch words =
- M.foldr S.intersection (S.fromList [0..length words]) . pick words
- pick :: Ord k => [k] -> Map k v -> Map k v
- pick keys m =
- restrictKeys m $ S.fromList keys
Add Comment
Please, Sign In to add comment