-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecommender-System.hs
More file actions
146 lines (105 loc) · 5.45 KB
/
Recommender-System.hs
File metadata and controls
146 lines (105 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
module FinalHaskell where
import DataSet
import System.Random
import System.IO.Unsafe
randomZeroToX :: Int -> Int
randomZeroToX x= unsafePerformIO (getStdRandom (randomR (0, x)))
createEmptyFreqList :: [a] -> [(a,[b])]
createEmptyFreqList [] = []
createEmptyFreqList (x:xs)= (x,[]):createEmptyFreqList xs
occur :: Eq a=> a ->[[a]]->[[a]]
occur a [] = []
occur a (x:xs) |elem a x = x:(occur a xs)
|otherwise = occur a xs
count1D :: Eq a => a->[a]->Int
count1D a [] = 0
count1D a (x:xs)|a==x = 1+count1D a xs
|otherwise = count1D a xs
count2D :: Eq a => a->[[a]]->Int
count2D a [] = 0
count2D a (x:xs) = count1D a x + count2D a xs
countAll [] l = []
countAll (x:xs) l|count2D x l ==0 = countAll xs l
|otherwise = (x,(count2D x l)):countAll xs l
remove a [] = []
remove a (x:xs) | a/=x = x:remove a xs
|otherwise = xs
remove2 (a,c) [] = []
remove2 (a,c) ((x,b):xs)| a/=x = (x,b):(remove2 (a,c) xs)
|otherwise = xs
getAllUserStatsHelper i ph [] = []
getAllUserStatsHelper i ph (y:ys) = (y,countAll r1 r2): (getAllUserStatsHelper i ph ys) where (r1 , r2) = ((remove y i),(occur y ph))
getAllUserStats [] = []
getAllUserStats ((a,l):xs) = (a,(getAllUserStatsHelper items l items)):getAllUserStats xs
findCorrectList a ((x,l):xs) | a==x = l
| otherwise = findCorrectList a xs
countFrequency1D a (x,[]) = 0
countFrequency1D a (b,((x,y):xs))|a==x = y+countFrequency1D a (b,xs)
|otherwise = countFrequency1D a (b,xs)
countFrequency2D a [] = 0
countFrequency2D a (x:xs) = countFrequency1D a x + countFrequency2D a xs
freqListItemsHelper l [] = []
freqListItemsHelper l (x:xs) |countFrequency2D x l ==0 = freqListItemsHelper l xs
|otherwise = (x,(countFrequency2D x l)):freqListItemsHelper l xs
freqListItems a = freqListItemsHelper l items where l = findCorrectList a (getAllUserStats purchasesHistory)
getNeededItemsLists1 ((x,l):xs) a |x == a = (x,l)
|otherwise = getNeededItemsLists1 xs a
getNeededItemsLists2 l [] = []
getNeededItemsLists2 l (x:xs) = (getNeededItemsLists1 l x):(getNeededItemsLists2 l xs)
freqListCart a l = freqListItemsHelper(getNeededItemsLists2 r l) items where r = findCorrectList a (getAllUserStats purchasesHistory)
freqListCartAndItemsHelper1 (a,c) ((x,b):xs)|a==x = (a,b+c)
|otherwise = freqListCartAndItemsHelper1 (a,c) xs
exists (a,b) [] = False
exists (a,b) ((x,c):xs)|a==x = True
|otherwise = exists (a,b) xs
freqListCartAndItemsHelper2 a l [] l2 = l2
freqListCartAndItemsHelper2 a l (x:xs) l2 |exists x l2 = (freqListCartAndItemsHelper1 x l2):(freqListCartAndItemsHelper2 a l xs (remove2 x l2))
|otherwise = x:(freqListCartAndItemsHelper2 a l xs l2)
freqListCartAndItems a l = freqListCartAndItemsHelper2 a l l1 l2 where (l1,l2) = (freqListItems a , freqListCart a l)
getRequiredItems [] [] = []
getRequiredItems ((a,l):xs) ((b,l2):ys) |((l == [])||(l2==[])) = getRequiredItems xs ys
|otherwise = a: getRequiredItems xs ys
helper [] l = []
helper (x:xs) l = (findCorrectList x l ):(helper xs l)
helper2 [] l2 = l2
helper2 (x:xs) l2 |exists x l2 = (freqListCartAndItemsHelper1 x l2):(helper2 xs (remove2 x l2))
|otherwise = x:(helper2 xs l2)
purchasesIntersectionHelper [] [] [] = []
purchasesIntersectionHelper (x:xs) (y:ys) (z:zs) = (z,(helper2 x y)):(purchasesIntersectionHelper xs ys zs)
remove3 l [] = []
remove3 l ((a,x):xs) | l==a = xs
| otherwise = (a,x):remove3 l xs
purchasesIntersection l [] = []
purchasesIntersection l ((x,l2):xs) = (purchasesIntersectionHelper (helper r l) (helper r l2 ) r):(purchasesIntersection l xs) where r = getRequiredItems l l2
looponitems (x:xs) l1 l2 = (x,helper2 a b) : looponitems xs l1 l2 where (a,b) = ( (helper x l1) ,(helper x l2))
loopOnRow a []=0
loopOnRow a ((b,c):xs)| b==a = c
|otherwise =loopOnRow a xs
freqListUsersHelper1D a [] =0
freqListUsersHelper1D a ((_,b):xs)= (loopOnRow a b) + (freqListUsersHelper1D a xs)
freqListUsersHelper2D a [] =0
freqListUsersHelper2D a (x:xs)=freqListUsersHelper1D a x + freqListUsersHelper2D a xs
freqListUsersHelper [] l =[]
freqListUsersHelper l []=[]
freqListUsersHelper (x:xs) l|freqListUsersHelper2D x l ==0 =freqListUsersHelper xs l
|otherwise =(x,freqListUsersHelper2D x l):freqListUsersHelper xs l
freqListUsers a = freqListUsersHelper items d where (c,d)=(getAllUserStats purchasesHistory,purchasesIntersection (findCorrectList a c) (remove3 a c))
-------------------------RECOMMENDATIONS------------------------
repeating x 0 = []
repeating x n = x:repeating x (n-1)
probabilityList [] =[]
probabilityList ((a,b):xs) = (repeating a b )++(probabilityList xs)
recommendBasedOnUsers a|c == [] =""
|otherwise = c !! randomZeroToX ((length c)-1) where (b,c)=(freqListUsers a,probabilityList b)
recommendEmptyCart a |length c == 0 =""
|otherwise = c !! randomZeroToX ((length c)-1) where (b,c)=(freqListItems a,probabilityList b)
test a l =b where (b,c)=(freqListCart a l,probabilityList b)
test a l =freqListCart a l
recommendBasedOnItemsInCart a l |freqListItems a ==[]=""
|l==[] =recommendEmptyCart a
|otherwise = c !! randomZeroToX ((length c)-1) where (b,c,d)=(freqListCart a l,probabilityList d,freqListCartAndItemsHelper2 1 1 b (freqListItems a) )
recommend a l |use == "" && cart == "" =items !! randomZeroToX (5)
|cart=="" =use
|use == "" =cart
|randomZeroToX (1) ==0 =use
|otherwise = cart where (cart , use) =(recommendBasedOnItemsInCart a l,recommendBasedOnUsers a)