Skip to content

Commit

Permalink
Merge pull request #21 from AdolfMacro/master
Browse files Browse the repository at this point in the history
Added function for Remove punctuation characters from text .
  • Loading branch information
amirshnll authored May 29, 2022
2 parents 96f0009 + 2269b57 commit a341b62
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
23 changes: 18 additions & 5 deletions PersianSwear.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@
created date : 7 October, 2021
updated date : 11 October, 2021
"""
import json
from string import punctuation
class PersianSwear(object):
def __init__(self):
self.data = json.load(open('data.json'))

#Rmove punctuation characters from text
# return string
def filter_words(self, text, symbol="*"):
def ignoreSY(self, text):
for i in punctuation:
if i in text:
text=text.replace(i,'')
return text
# return string
def filter_words(self, text , symbol="*" , ignoreOT=False):
if(self.is_empty()):
return text

text = text.split()
for i in range(len(text)):
if text[i] in self.data['word']:
if text[i] in self.data['word'] or (ignoreOT and self.ignoreSY(text[i]) in self.data['word']):
text[i] = symbol

return " ".join(text)
Expand All @@ -35,12 +44,16 @@ def remove_word(self, text):
self.data['word'].remove(text)

# return boolean
def is_bad(self, text):
def is_bad(self, text , ignoreOT=False):
if ignoreOT:
text=self.ignoreSY(text)
text=text.replace("\u200c","")
return text in self.data['word']

# return boolean
def has_swear(self, text):
def has_swear(self, text , ignoreOT=False):
if ignoreOT:
text=self.ignoreSY(text)
text=text.replace("\u200c","")
if(self.is_empty()):
return text
Expand All @@ -54,4 +67,4 @@ def has_swear(self, text):

# return string
def tostring(self):
return ' - '.join(self.data['word'])
return ' - '.join(self.data['word'])
33 changes: 24 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,39 @@ echo $persianswear->tostring(); // show all swear words
```
persianswear = PersianSwear()
print(persianswear.is_bad('خر')) # True
print(persianswear.is_bad(,'خر',ignoreOT=False )) # True
print(persianswear.is_bad('امروز')) # False
print(persianswear.is_bad('امروز',ignoreOT=False )) # False
print(persianswear.is_bad('چرت و پرت')) # False
print(persianswear.is_bad('چرت و پرت',ignoreOT=False )) # False
persianswear.add_word('چرت و پرت')
print(persianswear.is_bad('چرت و پرت')) # True
print(persianswear.is_bad('چرت و پرت' , ignoreOT=False )) # True
print(persianswear.has_swear('تو دوست من هستی')) # False
print(persianswear.has_swear('تو دوست من هستی' , ignoreOT=False )) # False
print(persianswear.has_swear('تو هیز هستی')) # True
print(persianswear.has_swear('تو هیز هستی' , ignoreOT=False )) # True
print(persianswear.filter_words('تو دوست من هستی')) # تو دوست من هستی
print(persianswear.filter_words('تو دوست من هستی' , ignoreOT=False )) # تو دوست من هستی
print(persianswear.filter_words('تو هیز هستی')) # تو * هستی
print(persianswear.filter_words('تو هیز هستی' , ignoreOT=False )) # تو * هستی
print(persianswear.filter_words('تو هیز هستی', '&')) # تو & هستی
print(persianswear.filter_words('تو هیز هستی', '&' , ignoreOT=False )) # تو & هستی
print(persianswear.is_bad('خ.ر' , ignoreOT=True )) # True
print(persianswear.is_bad( 'ام.روز' , ignoreOT=True )) # False
print(persianswear.has_swear('تو دو.ست من هستی' , ignoreOT=True )) # False
print(persianswear.has_swear('تو اسک.ل هستی' , ignoreOT=True )) # True
print(persianswear.filter_words('تو دو.ست من هستی',ignoreOT=True )) # تو دو.ست من هستی
print(persianswear.filter_words('تو هی.ز هستی',ignoreOT=True )) # تو * هستی
print(persianswear.filter_words('تو هی.ز هس.تی' , ignoreOT=True )) # تو * هس.تی
print(persianswear.tostring()) # show all swear words
```
Expand Down

0 comments on commit a341b62

Please sign in to comment.