2016-09-02 15:45:10 +00:00
|
|
|
class chatFilters:
|
2016-08-10 10:00:33 +00:00
|
|
|
def __init__(self, fileName="filters.txt"):
|
2016-11-17 18:13:06 +00:00
|
|
|
"""
|
|
|
|
Initialize chat filters
|
|
|
|
|
|
|
|
:param fileName: name of the file containing filters. Default: filters.txt
|
|
|
|
"""
|
2016-08-10 10:00:33 +00:00
|
|
|
self.filters = {}
|
|
|
|
self.loadFilters(fileName)
|
|
|
|
|
|
|
|
def loadFilters(self, fileName="filters.txt"):
|
2016-11-17 18:13:06 +00:00
|
|
|
"""
|
|
|
|
Load filters from a file
|
|
|
|
|
|
|
|
:param fileName: name of the file containing filters. Default: filters.txt
|
|
|
|
:return:
|
|
|
|
"""
|
2016-08-10 10:00:33 +00:00
|
|
|
# Reset chat filters
|
|
|
|
self.filters = {}
|
|
|
|
|
2016-08-10 10:24:41 +00:00
|
|
|
# Open filters file
|
|
|
|
with open(fileName, "r") as f:
|
2016-08-10 10:00:33 +00:00
|
|
|
# Read all lines
|
|
|
|
data = f.readlines()
|
|
|
|
|
|
|
|
# Process each line
|
|
|
|
for line in data:
|
|
|
|
# Get old/new word and save it in dictionary
|
|
|
|
lineSplit = line.split("=")
|
2016-09-13 09:46:01 +00:00
|
|
|
self.filters[lineSplit[0].lower()] = lineSplit[1].replace("\n", "")
|
|
|
|
|
2016-08-10 10:00:33 +00:00
|
|
|
def filterMessage(self, message):
|
2016-11-17 18:13:06 +00:00
|
|
|
"""
|
|
|
|
Replace forbidden words with filtered ones
|
|
|
|
|
|
|
|
:param message: normal message
|
|
|
|
:return: filtered message
|
|
|
|
"""
|
2016-08-10 10:00:33 +00:00
|
|
|
# Split words by spaces
|
|
|
|
messageTemp = message.split(" ")
|
|
|
|
|
|
|
|
# Check each word
|
|
|
|
for word in messageTemp:
|
2016-09-13 09:46:01 +00:00
|
|
|
lowerWord = word.lower()
|
|
|
|
|
2016-08-10 10:00:33 +00:00
|
|
|
# If the word is filtered, replace it
|
2016-09-13 09:46:01 +00:00
|
|
|
if lowerWord in self.filters:
|
|
|
|
message = message.replace(word, self.filters[lowerWord])
|
2016-08-10 10:00:33 +00:00
|
|
|
|
|
|
|
# Return filtered message
|
|
|
|
return message
|