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

  1. 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.
  2. To read csv file we have to first import pandas as pd
  3. 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)”

 

 

'anus',
 'ash0le',
 'ash0les',
 'asholes',
 'ass',
 'Ass Monkey',
 'Assface',

How to Detect offensive words in Python

  1. 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.
  2. 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.
  3. create a new list(store_list) to store offensive words from our sentences.
  4. we will execute a for loop over our sentence and use s1.split() to convert our string into elements of the list.
  5. 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.
  6. 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

Your email address will not be published. Required fields are marked *