Detect slang words from a string in Python
Hello friends, you must have seen many slang words in the text you read. Either a blog post, comment section, or a 50-page long assignment, etc. Sometimes it’s not possible to read each comment or the entire document. However, we can screen text and filter such words using a module in Python, wordfilter. You can also use the words-slang list to detect slang words from a string in Python.
Contents of the tutorial :
- wordfilter module
- word-slang list
Detect slag words from a string using wordfilter module in Python
To use the module, first I installed the package on my local system using the command prompt on Windows (Terminal for macOS/Linux users).
pip install wordfilter
Once installed, import the package to your code.
from wordfilter import Wordfilter
I have created an object for the class and given it a temporary name, filter.
Code :
filter = Wordfilter() string = 'Hey crazy' print(filter.blacklisted(string))
Now, I have taken a string as input. I have used the function blacklisted() to detect slang words. This function takes a string as an input and returns a boolean value. As ‘crazy’ is a slang word, I get True as my output.
Output :
True
Detect slag words from a string using word-slang list in Python
You can download the word-slang list from this link. Once downloaded, I have a text file that I can read in Python. You can check out my tutorial on text file. I have used the split() function to get each word of my string and stored it in a variable, string_list. I have run a nested for loop. The first loop iterates over each word in my string_list and the other iterates over each word present in the list which I have gained by reading the text file. Now I have checked if the words are the same or not, if yes, I have incremented my counter, c value.
Code :
string = "hey crazy"
string_list = string.split()
file_path = r"/C:/Desktop/words-slang.txt"
file = open(file_path, 'r')
s = file.readlines()
c = 0
for word in string_list:
for w in s:
if word == w.strip('\n'):
c += 1
if c > 0:
print("True")
else:
print("False")After the loop, I check if the counter’s value is greater than 0 or not. If yes, then it indicates that the string contains slang words else it will return return me False. In this example the string value is ‘hey crazy’ where ‘crazy’ is a slang word. Thus I get an output as True.
Output :
True
Now you can detect slang words from strings in Python.
Leave a Reply