forked from prathimacode-hub/Awesome_Python_Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcensor.py
121 lines (94 loc) · 4.1 KB
/
censor.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
import tkinter as tk
from tkinter import *
from PIL import Image
from PIL import ImageTk
from datetime import date
import shortuuid
import multiprocessing
import time
### This Function is used to detect censor words ###
def censor(listOfWords):
listOfWords=listOfWords.split(",") ## This list contains dataset of curse words
#print(" ugly" in listOfWords)
li=[] # In this list we are putting the detected words so that there won't be any repetitions
filename = "Utils/CurseWordsDetected/detectedWords"+str(shortuuid.uuid())+".txt" #For unique name
while True:
f = open("Utils/tempFile.txt","r")
userInput=f.read()
userInput=userInput.replace(".", " ")
userInput=userInput.replace(",", " ")
userInput=userInput.replace("/", " ")
userInputList=userInput.split( )
#Removing fullstops and commas which can be problematic later
#userInput=userInput.split(" ")
#print(userInput)
# Traversing the dataset and checking if any match is found or not
for ele in listOfWords:
#for eleUserinp in userInput:
#print(eleUserinp,"INPUT") if str(ele).strip() in str(eleUserinp).strip():
if str(ele).strip() in str(userInput):
## Now match is found but there is possibility of false alert eg. in homework there is word ho, so it will be false positive so removing that
if len(str(ele).strip().split(" "))<2 and str(ele).strip() not in userInputList:
continue
#print("Less than 2",len(str(ele).strip().split(" ")))
#print("False Alert")
#else:
#print(len(str(ele).strip().split(" ")))
#pass
# Print on terminal detected word
print(ele,"detected")
# Save in text file
f = open(filename,"a+")
if ele not in li:
li.append(ele)
ele=ele+"\n"
f.write(ele)
time.sleep(1)
else:
#print(ele,userInput)
continue
## This function creates GUI for detector
def guiForCensor():
# This function is for saving written input by the user to the file after clicking save button
def saveToFile():
nameOfFile = 'Utils/InputFile/'+str(date.today())+str(shortuuid.uuid())+'.txt'
inputTextMatter=inputtxt.get(1.0,"end-1c")
f = open(nameOfFile, "w+")
f.write(str(inputTextMatter))
print(">> saveFunction")
# This is for backend purpose, we are saving file in temp so we can get it later
def printInput():
inputTextMatter=inputtxt.get(1.0,"end-1c")
f = open("Utils/tempFile.txt", "w+")
f.write(str(inputTextMatter))
frame.after(2000, printInput)
# Creating Frame
frame = tk.Tk()
frame.title("Censor Words")
frame.geometry('640x480')
# Setting Background and necessary components
bg="Images/bg.jpg"
image=Image.open(bg)
image = image.resize((640,480), Image.ANTIALIAS)
bg = ImageTk.PhotoImage(image)
bglabel=Label(frame,image=bg)
bglabel.place(x=0,y=0)
inputtxt = tk.Text(frame, height = 15,width = 50)
inputtxt.place(x=120,y=100)
heading = tk.Label(frame, text="Censor Words", font=("Gentium Basic", 25), bg='#255543', fg='#fff')
heading.place(x=220,y=50)
saveButton = tk.Button(frame,text="Save",command=saveToFile)
saveButton.place(x=315,y=400)
frame.after(2000, printInput)
frame.mainloop()
# opening dataset
f = open("Utils/censor_word_list.txt", "r")
listOfWords = f.read()
# Use of multiprocessing to run both functions at same time
p1 = multiprocessing.Process(target=censor,args=(listOfWords,))
p2 = multiprocessing.Process(target=guiForCensor)
if __name__ == "__main__":
p1.start()
p2.start()
#censor(listOfWords)
#guiForCensor()