-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsimilar.py
52 lines (44 loc) · 1.46 KB
/
jsimilar.py
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
from itertools import combinations
from pyspark.ml.feature import RegexTokenizer
from pyspark.ml.feature import NGram
filesList = []
shringleList = []
output = []
def similarity(s1,s2):
#convert to rdd
x1 = sc.parallelize(getList(shringleList[s1]))
x2 = sc.parallelize(getList(shringleList[s2]))
# union and intersection
u = float(x1.union(x2).count())
i = float(x1.intersection(x2).count())
# get jaqards similarity
output.append(filesList[s1]+"#"+filesList[s2]+"\t"+str(float(i/u)))
def getTwoFileCombo(l):
pairs = combinations(range(len(l)),2)
for p in pairs:
similarity(p[0],p[1])
def read(fileName):
# read file
return spark.read.text(fileName)
def shringles(x,fileName):
# tokenize and ngrams
tokenizer = RegexTokenizer(inputCol="value",outputCol="words",pattern="\\W")
ngrams = NGram(n=x,inputCol="words",outputCol="kshringles")
shringleList.append(ngrams.transform(tokenizer.transform(read(fileName))))
def getList(df):
# get list from ngrams
tmp = []
for a in df.select("kshringles").collect():
for b in a:
for c in b:
tmp.append(c)
return tmp
outputLoc = '/home/ubuntu/A2/output'
filesList.append("/home/ubuntu/A2/book1.txt")
filesList.append("/home/ubuntu/A2/book2.txt")
filesList.append("/home/ubuntu/A2/book3.txt")
kshringle = 2
for i in filesList:
shringles(kshringle,i)
getTwoFileCombo(shringleList)
sc.parallelize(output).saveAsTextFile(outputLoc)