-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlagiarismChecker.py
165 lines (150 loc) · 7.92 KB
/
PlagiarismChecker.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from PyQt5 import QtCore, QtGui, QtWidgets
import re
def iconFromBase64(base64):
pixmap = QtGui.QPixmap()
pixmap.loadFromData(QtCore.QByteArray.fromBase64(base64))
icon = QtGui.QIcon(pixmap)
return icon
class Tokenizer:
def tokenize(self, text):
regex = re.compile(r'([^a-zA-Z0-9 \n\t\r])')
text = regex.sub(r' \1 ', text)
return text.split()
class Detector:
tokenizer = Tokenizer()
listSinonim = {}
ngram_match = 0
def __init__(self,sinonim_path,suspect_text,source_text, n=3):
self.sinonim_path = sinonim_path
self.susText = suspect_text
self.souText = source_text
self.n = n
def init_sinonim(self):
try:
with open(self.sinonim_path, 'r', encoding='utf-8-sig') as fp:
for row in fp.readlines():
rowSinonim = tuple(row.rstrip().lower().split("=="))
valSinonim = rowSinonim[0]
for sinonim in rowSinonim:
self.listSinonim[sinonim] = valSinonim
except IOError:
pass
def ngramize(self,subset):
return tuple([self.listSinonim.get(i, i) for i in subset])
def tokenize(self, text):
token = self.tokenizer.tokenize(text.lower())
temp = []
for i in range(len(token) - self.n +1):
temp.append(self.ngramize(token[i:i+self.n]))
return temp
def detect(self):
if(self.sinonim_path):
self.init_sinonim()
source_ngram=suspect_ngram={}
self.n+=1
while(len(source_ngram) is 0 or len(suspect_ngram) is 0):
self.n -=1
source_ngram = self.tokenize(self.souText)
suspect_ngram = self.tokenize(self.susText)
for data in suspect_ngram:
if data in source_ngram:
self.ngram_match += 1
return self.ngram_match / len(suspect_ngram) * 100
class Ui_main(object):
def setupUi(self, main):
main.setObjectName("main")
main.resize(666, 550)
main.setWindowIcon(iconFromBase64(ico))
self.labelSuspect = QtWidgets.QLabel(main)
self.labelSuspect.setGeometry(QtCore.QRect(15, 10, 111, 23))
self.labelSuspect.setObjectName("labelSuspect")
self.labelSource = QtWidgets.QLabel(main)
self.labelSource.setGeometry(QtCore.QRect(345, 10, 111, 23))
self.labelSource.setObjectName("labelSource")
self.accuracyBar = QtWidgets.QProgressBar(main)
self.accuracyBar.setGeometry(QtCore.QRect(10, 420, 641, 23))
self.accuracyBar.setProperty("value", 0)
self.accuracyBar.setObjectName("accuracyBar")
self.startButton = QtWidgets.QPushButton(main)
self.startButton.setGeometry(QtCore.QRect(540, 500, 100, 30))
self.startButton.setObjectName("startButton")
self.suspectedText = QtWidgets.QPlainTextEdit(main)
self.suspectedText.setGeometry(QtCore.QRect(10, 40, 320, 371))
self.suspectedText.setObjectName("Suspected")
self.sourceText = QtWidgets.QPlainTextEdit(main)
self.sourceText.setGeometry(QtCore.QRect(340, 40, 320, 371))
self.sourceText.setObjectName("Source")
self.suspectPath = QtWidgets.QLineEdit(main)
self.suspectPath.setGeometry(QtCore.QRect(10,460,400,30))
self.suspectPath.setObjectName("suspectPath")
self.suspectPath.setReadOnly(True)
self.suspectButton = QtWidgets.QPushButton(main)
self.suspectButton.setGeometry(QtCore.QRect(420,460,100,30))
self.suspectButton.setObjectName("suspectButton")
self.sourcePath = QtWidgets.QLineEdit(main)
self.sourcePath.setGeometry(QtCore.QRect(10,500,400,30))
self.sourcePath.setObjectName("sourcePath")
self.sourcePath.setReadOnly(True)
self.sourceButton = QtWidgets.QPushButton(main)
self.sourceButton.setGeometry(QtCore.QRect(420,500,100,30))
self.sourceButton.setObjectName("sourceButton")
self.n = QtWidgets.QComboBox(main)
self.n.setGeometry(QtCore.QRect(540, 460, 100, 30))
self.n.addItems(['Auto','1','2','3','4','5'])
self.retranslateUi(main)
QtCore.QMetaObject.connectSlotsByName(main)
self.startButton.clicked.connect(self.startngram)
self.suspectButton.clicked.connect(lambda:self.selectFile(1))
self.sourceButton.clicked.connect(lambda:self.selectFile(2))
def retranslateUi(self, main):
_translate = QtCore.QCoreApplication.translate
main.setWindowTitle(_translate("main", "Plagiarism Checker"))
self.labelSuspect.setText(_translate("main", "Suspected Document"))
self.labelSource.setText(_translate("main", "Source Document"))
self.startButton.setText(_translate("main", "Start"))
self.suspectButton.setText(_translate("main", "Suspect"))
self.sourceButton.setText(_translate("main", "Source"))
def startngram(self):
if(self.suspectedText.toPlainText() is not '' and self.sourceText.toPlainText() is not ''):
finalScore =0
self.accuracyBar.reset()
if(self.n.currentIndex()==0):
for i in range(1,6):
program = Detector("synbank.txt", self.suspectedText.toPlainText(), self.sourceText.toPlainText(), i)
finalScore+=program.detect()
finalScore/=5
else:
program = Detector("synbank.txt", self.suspectedText.toPlainText(), self.sourceText.toPlainText(), self.n.currentIndex())
finalScore = program.detect()
self.accuracyBar.setValue(finalScore)
else:
self.accuracyBar.setValue(0)
def selectFile(self, flag):
if(flag==1):
name,type = QtWidgets.QFileDialog.getOpenFileName(filter = "Text files (*.txt)")
if(name is not ''):
self.suspectedText.clear()
with open(name, 'r', encoding='utf-8-sig') as fp:
text=fp.read()
self.suspectPath.setText(name)
self.suspectedText.insertPlainText(text)
elif(flag==2):
names,type = QtWidgets.QFileDialog.getOpenFileNames(filter = "Text files (*.txt)")
text =''
for name in names:
if(name is not ''):
with open(name, 'r', encoding='utf-8-sig') as fp:
text+="\n" + fp.read()
if(text != ''):
self.sourceText.clear()
self.sourcePath.setText(name)
self.sourceText.insertPlainText(text)
ico=b"iVBORw0KGgoAAAANSUhEUgAAAKAAAACgCAYAAACLz2ctAAAAAXNSR0IArs4c6QAAAAR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JCVAUrccIXvtHStXt8rgmDgBSQmQlABJCZDU0iOE63MCkhIgKQGSEiChl5d/AG2mJMp4S80KAAAAAElFTkSuQmCC"
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
main = QtWidgets.QDialog()
ui = Ui_main()
ui.setupUi(main)
main.show()
sys.exit(app.exec_())