-
Notifications
You must be signed in to change notification settings - Fork 0
/
plag.py
89 lines (64 loc) · 2.16 KB
/
plag.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
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
from flask import Flask, request, render_template
import re
import math
app = Flask("__name__")
q = ""
@app.route("/")
def loadPage():
return render_template('index.html', query="")
@app.route("/", methods=['POST'])
def cosineSimilarity():
try:
universalSetOfUniqueWords = []
matchPercentage = 0
inputQuery = request.form['query']
lowercaseQuery = inputQuery.lower()
queryWordList = re.sub("[^\w]", " ",lowercaseQuery).split()
for word in queryWordList:
if word not in universalSetOfUniqueWords:
universalSetOfUniqueWords.append(word)
fd = open("database1.txt", "r")
database1 = fd.read().lower()
databaseWordList = re.sub("[^\w]", " ",database1).split()
for word in databaseWordList:
if word not in universalSetOfUniqueWords:
universalSetOfUniqueWords.append(word)
queryTF = []
databaseTF = []
for word in universalSetOfUniqueWords:
queryTfCounter = 0
databaseTfCounter = 0
for word2 in queryWordList:
if word == word2:
queryTfCounter += 1
queryTF.append(queryTfCounter)
for word2 in databaseWordList:
if word == word2:
databaseTfCounter += 1
databaseTF.append(databaseTfCounter)
dotProduct = 0
for i in range (len(queryTF)):
dotProduct += queryTF[i]*databaseTF[i]
queryVectorMagnitude = 0
for i in range (len(queryTF)):
queryVectorMagnitude += queryTF[i]**2
queryVectorMagnitude = math.sqrt(queryVectorMagnitude)
databaseVectorMagnitude = 0
for i in range (len(databaseTF)):
databaseVectorMagnitude += databaseTF[i]**2
databaseVectorMagnitude = math.sqrt(databaseVectorMagnitude)
matchPercentage = (float)(dotProduct / (queryVectorMagnitude * databaseVectorMagnitude))*100
'''
print queryWordList
print
print databaseWordList
print queryTF
print
print databaseTF
'''
output = "Input query text matches %0.02f%% with database."%matchPercentage
return render_template('index.html', query=inputQuery, output=output)
except Exception as e:
output = "Please Enter Valid Data"
return render_template('index.html', query=inputQuery, output=output)
app.run()