-
Notifications
You must be signed in to change notification settings - Fork 0
/
movie
97 lines (88 loc) · 1.73 KB
/
movie
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
import io.Source
import math._
def cosine(u:Int,i:Int){
var num=0.0
var den=0.0
var d1=0.0
var d2=0.0
for(j <- 1 to n-1){
if(um(u)(j) !=0 && um(i)(j) !=0){
num=num+(um(u)(j)*um(i)(j))
}
if(um(u)(j)!=0){
d1=d1+pow(um(u)(j),2)
}
if(um(i)(j)!=0){
d2=d2+pow(um(i)(j),2)
}
}
d1=sqrt(d1)
d2=sqrt(d2)
den=d1*d2
if(den!=0){
cosinedis(i)=num/den
}
}
var m=945
var n=1683
var cm=Array.ofDim[Int](m,n)
var um=Array.ofDim[Double](m,n)
var s = Source.fromFile("a.data")
val mov = Source.fromFile("u.item").getLines.toList
val tokens = s.mkString.split("\\s+")
val number = for (w <- tokens) yield w.toInt
for(i <- 1 to (number.size/4)-1){
var k=i*4
cm(number(k))(number(k+1))=number(k+2)
}
var count =0.0
var sum =0.0
var sub =0.0
for(i <- 1 to m-1){
count = 0
sum =0
for (j <- 1 to n-1){
if(cm(i)(j) != 0){
sum = sum + cm(i)(j)
count=count+1
}
}
sub = sum/count
for (j <- 1 to n-1){
if(cm(i)(j) != 0){
um(i)(j) = (cm(i)(j) - sub).toDouble
}
}
}
print("Enter user id\n")
val user = readInt
var cosinedis = new Array[Double](m)
for(i <- 1 to m-1){
cosinedis(i)= -99999.9
if(i != user){
cosine(user,i)
}
}
val simuser1=cosinedis.indexOf(cosinedis.max)
cosinedis(simuser1)= -99999.9
val simuser2=cosinedis.indexOf(cosinedis.max)
var max1=(0,0.0)
var max2=(0,0.0)
var max3=(0,0.0)
for(j <- 1 to n-1){
if(cm(user)(j) ==0){
if(cm(simuser1)(j)>max1._2){
max1=(j,cm(simuser1)(j))
}
else if(cm(simuser1)(j)>max2._2){
max2=(j,cm(simuser1)(j))
}
if(cm(simuser2)(j)>max3._2){
max3=(j,cm(simuser2)(j))
}
}
}
println("************Top Films recommended for user_id "+user+"*************")
println(mov(max1._1)+"\t"+max1._2)
println(mov(max2._1)+"\t"+max2._2)
println(mov(max3._1)+"\t"+max3._2)