1+ module Movies where
2+ -- import Data.Map
3+ import Gibbon.Prelude
4+ import Gibbon.PList
5+ import Gibbon.Vector
6+ import Gibbon.Maybe
7+ import Basics
8+ --
9+ type Text = Vector Char
10+ --
11+ type IsMovie = Bool
12+ type MovieTitle = Text
13+ type ReleaseDate = Text
14+ type Director = Text
15+ type Writers = PList Text
16+ type CastInfo = PList Text
17+ type MovieTags = PList Text
18+ type Rating = Int
19+
20+ -- --if IsMovie == True, the maybe values will exist
21+ data Movie = Empty
22+ | Movie MovieTitle
23+ ReleaseDate Director Writers CastInfo
24+ MovieTags Rating
25+
26+ data MovieTrie = Root
27+ | MovieTrie (Maybe Char ) (Maybe IsMovie ) (Maybe Movie ) (Maybe (PList MovieTrie )) deriving (Show )
28+
29+ -- intToVec :: Int -> (Vector Int)
30+ -- intToVec i = let
31+ -- remainder = mod i 10
32+ -- quotient = div i 10
33+ -- in if(quotient == 0) then singleton remainder
34+ -- else append (intToVec quotient) (singleton remainder)
35+ -- intToChar :: Int -> Char
36+ -- intToChar i = case i of
37+ -- 0 -> '0'
38+ -- 1 -> '1'
39+ -- 2 -> '2'
40+ -- 3 -> '3'
41+ -- 4 -> '4'
42+ -- 5 -> '5'
43+ -- 6 -> '6'
44+ -- 7 -> '7'
45+ -- 8 -> '8'
46+ -- 9 -> '9'
47+ -- intToText :: (Vector Int) -> Text
48+ -- intToText i = if (length i == 1) then
49+ -- movieToText :: Movie -> Text
50+ -- movieToText m = case m of
51+ -- Empty -> "empty Movie"
52+ -- Movie c title date director wrriters castinfo movietags rating ->
53+ mkMovieContent :: Text -> Movie
54+ mkMovieContent t =
55+ let
56+ movietitle = t
57+ rdate = (getRandomString 5 )
58+ director = (getRandomString 6 )
59+ writer = Cons (getRandomString 5 ) Nil
60+ cast = Cons (getRandomString 5 ) Nil
61+ movietag = Cons (getRandomString 5 ) Nil
62+ rating = 5
63+ in Movie movietitle rdate director writer cast movietag rating
64+
65+ -- insert a movie into movieTrie
66+ insertMovie :: Text -> (Maybe Char ) -> Movie -> MovieTrie -> MovieTrie
67+ insertMovie t c m mt =
68+ if (length t == 0 )
69+ then case mt of
70+ Root -> MovieTrie c (Just True ) (Just m) Nothing
71+ MovieTrie c ismovie movie cmovieTrie -> MovieTrie c (Just True ) (Just m) cmovieTrie
72+ else
73+ case mt of
74+ Root -> MovieTrie c (Just False ) Nothing (Just (Cons (insertMovie (tail t) (Just (head t)) m Root ) Nil ))
75+ MovieTrie c ismovie movie cmovieTrie -> MovieTrie c ismovie movie (Just (insert_Mhelper t m (fromMaybe Nil cmovieTrie)))
76+
77+ movieGetChar :: MovieTrie -> Maybe Char
78+ movieGetChar mt = case mt of
79+ Root -> Nothing
80+ -- let _ = printsym (quote "nothing")
81+ -- in Nothing
82+ MovieTrie c ismovie movie lmt -> c
83+ -- let _ = printchar (fromMaybe ' ' c)
84+ -- in c
85+
86+ insert_Mhelper :: Text -> Movie -> (PList MovieTrie ) -> (PList MovieTrie )
87+ insert_Mhelper t m lmt =
88+ case lmt of
89+ Nil -> Cons (insertMovie (tail t) (Just (head t)) m Root ) Nil
90+ Cons x xs -> if (fromMaybe ' ' (movieGetChar x)) *==* head t then Cons (insertMovie (tail t) (Just (head t)) m x) xs
91+ else Cons x (insert_Mhelper t m xs)
92+
93+ deleteMovie :: Text -> MovieTrie -> MovieTrie
94+ deleteMovie t mt =
95+ if (length t == 0 )
96+ then case mt of
97+ Root -> mt
98+ MovieTrie c ismovie movie cmovieTrie -> if (fromMaybe False ismovie) then MovieTrie c (Just False ) (Nothing ) cmovieTrie
99+ else MovieTrie c ismovie movie cmovieTrie
100+ else
101+ case mt of
102+ Root -> Root
103+ MovieTrie c ismovie movie cmovieTrie -> MovieTrie c ismovie movie (Just (delete_Mhelper t (fromMaybe Nil cmovieTrie)))
104+
105+
106+ delete_Mhelper :: Text -> (PList MovieTrie ) -> (PList MovieTrie )
107+ delete_Mhelper t lmt =
108+ case lmt of
109+ Nil -> Nil
110+ Cons x xs -> if (fromMaybe ' ' (movieGetChar x)) *==* head t then Cons (deleteMovie (tail t) x) xs
111+ else Cons x (delete_Mhelper t xs)
112+ -- given movietitle, find movie return empty if not found
113+ searchMovieTitle :: Text -> MovieTrie -> Movie
114+ searchMovieTitle t mt =
115+ if (length t == 0 ) then case mt of
116+ Root -> Empty
117+ MovieTrie c ismovie movie cmovieTrie -> if (fromMaybe False ismovie) then (fromMaybe Empty movie) else Empty
118+ else
119+ case mt of
120+ Root -> Empty
121+ MovieTrie c ismovie movie cmovieTrie -> if (isNothing cmovieTrie) then Empty
122+ else
123+ let
124+ a = fromMaybe Nil cmovieTrie
125+ in
126+ case a of
127+ Nil -> Empty
128+ Cons x xs -> search_Mhelper t (Cons x xs)
129+
130+ search_Mhelper :: Text -> (PList MovieTrie ) -> Movie
131+ search_Mhelper t lmt =
132+ case lmt of
133+ Nil -> Empty
134+ Cons x xs -> if (fromMaybe ' ' (movieGetChar x)) *==* (head t)
135+ then searchMovieTitle (tail t) x
136+ else search_Mhelper t xs
137+
138+ -- searchMovieRating :: Int -> MovieTrie ->(PList Movie) -> (PList Movie)
139+ -- searchMovieTitle i mt lm =
140+ -- case mt of
141+ -- Root -> Nil
142+ -- MovieTrie c ismovie movie cmovieTrie ->
143+ -- let
144+ -- rating = getMovieRating (fromMaybe Empty movie)
145+ -- in if (rating > i) then Cons (fromMaybe Empty movie) (Search_MRhelper i cmovieTrie )
146+ -- else Search_MRhelper i cmovieTrie
147+
148+ -- search_MRhelper :: Int -> (PList MovieTrie) -> (PList Movie) -> (PList Movie)
149+
150+ -- getMovieRating :: Movie -> Int
151+ -- getMovieRating m = case m of
152+ -- Empty -> 0
153+ -- Movie movietitle releasedate director writers casinfo movitags rating -> rating
154+
155+ -- generateNode :: Char -> MovieTrie -> MovieTrie
156+ -- generateNode a mt =
157+ -- insertMovie :: Text -> MovieTrie -> MovieTrie
158+ -- insertMovie title mt =
159+ -- if isEmpty title then mt
160+ -- else if mt == End then
161+ -- let
162+ -- a = head title
163+ -- b = tail title
164+ -- curNode = MovieTrie Nothing (singleton a) insertMovie b End
165+ -- in curNode
166+
167+ isEmptyM :: Movie -> Bool
168+ isEmptyM m = case m of
169+ Empty -> True
170+ Movie _ _ _ _ _ _ _ -> False
171+
172+ genMovieList :: Int -> (PList Text )
173+ genMovieList i =
174+ if (i == 0 )
175+ then Nil
176+ else
177+ let
178+ a = mod rand 10
179+ t = getRandomString a
180+ nt = genMovieList (i - 1 )
181+ in Cons t nt
182+
183+ consMovieTrie :: (PList Text ) -> MovieTrie -> MovieTrie
184+ consMovieTrie lt mt =
185+ case lt of
186+ Nil -> mt
187+ Cons x xs ->
188+ let
189+ movieContent = mkMovieContent x
190+ in consMovieTrie xs (insertMovie x Nothing movieContent mt)
0 commit comments