Detect offensive words in Python
In this tutorial, we will learn how to Detect offensive words in Python.
What are offensive words?
- Offensive words are irritating, angering, or annoying words.
Examples: Arse, Bloody, Bugger
So this tutorial will consist of:
- How to read CSV files in python using the Python library.
- Learn how to Detect offensive words in Python.
Let’s start coding
How to read .csv files
- We have an offensive_word.csv file from that we will take words for detecting offensive words in a given sentence. So for that, we have to add items to a list from our CSV file.
- To read csv file we have to first import pandas as pd
- pd.read_csv(“file path”) return a DataFrame df
Remember: In this case, the CSV file(offensive_word.csv) and my python program file, i.e codespeedy.py both are in the same directory. If your CSV file is located in some other directory or folder then you must need to mention the location or path of your CSV file here:
df = pd.read_csv(‘path_of_your_file’)
4. Then we will convert that DataFrame into a Series by using “.squeeze()”
5. At last, we will store all the items in Series in the list by using “list(series)”
- filename: offensive_word.csv https://github.com/Sumitchhirush01/Offensive-words-detection-using-python.git
- offensive_list has more than 1000+ offensive words, some of them are shown below
'anus', 'ash0le', 'ash0les', 'asholes', 'ass', 'Ass Monkey', 'Assface',
How to Detect offensive words in Python
- Now we will have a list of offensive words(offensive_items) and with the help of this list, we will detect offensive words in a given sentence.
- we will take input a string from the user after that we will convert that string into lowercase because in our list all the elements are in lowercase and we know python is case sensitive so that’s why we have to convert our string in lowercase.
- create a new list(store_list) to store offensive words from our sentences.
- we will execute a for loop over our sentence and use s1.split() to convert our string into elements of the list.
- Then we use the if statement to check whether every word of our string was in the offensive list(offensive_items) or not and if it has that word, we will store that word in our list(store_list) using the append method.
- At last, we will check our store_list, If its length is greater than 0 that means it stores some offensive words then we will print that offensive word, and if its length not greater than 0 that means our store_list is empty and our sentence has no offensive words.
#Read a csv file in Python import pandas as pd df=pd.read_csv("Offensive_word.csv") series=df.squeeze() offensive_items=list(series) s1=str(input("plz enter your sentence: ")) s1=s1.lower() store_list=[] for i in s1.split(): if i in offensive_items: store_list.append(i) if len(store_list)>0: print("yes,this sentence contain offensives words which is/are :",store_list) else: print("sentence have no offensive words")
output:
plz enter sentence: he lied to me, the bastard yes,this sentence contain offensive words which is/are : ['bastard']
Thus, we have learned how to Detect offensive words in Python.
Leave a Reply