Python Script to turn Text/Message abbreviations into actual Phrases

Idea Behind this script

Have you ever encountered a situation where you just didn’t get what the other person is trying to say you over text. Like Hey BBS”.

This is actually very confusing, and this usually happens with our elders who are not into messaging with using slang. So i have written a python script to replace those slang’s with proper phrases.

Of course this script is not getting integrated with you messaging apps, Its just for learning purpose. Or we can say like learning something and making use of that knowledge to create something useful out of it.


Getting Into Technical Part

This tutorial is going to be easy and short :)

Requirements

  • Python 3 installed on your machine. If not already installed get it here.
  • Any text editor would work.

That’s It..

Start with importing some libraries that we will use

import csv
import re
  • import csv → Because we will read the dictionary[which contains abbreviations and phrases ] as Comma Separated files. This will return 2 rows, Row[1] contains abbreviations and row[2] Actual Phrase.
  • import re → We are using a regular expression in our script, This is done to remove any special characters that might be added in abbreviation by user at the time of input. Example instead of BRB we might encounter BRB! or BRB.
_str = re.sub('[^a-zA-Z0-9-_.]', '', _str)
  • So the above code will remove any special character in that word.

Main Code

Defining a procedure which will do all the work for us. This takes string entered by user as input.

def translator(user_string):

Splitting the string entered by user on space[“ “], So that we are left list[] of words. Example :

Hi this is python tutorial → [‘Hi’, ’this’, ’is’, ’python’, ‘tutorial’]

user_string = user_string.split(" ")

Procedure Translator

Iterating over words in string passed to procedure.

j = 0
for _str in user_string:
# File path which consists of Abbreviations.
fileName = "C:\\xxxx\\Desktop\\slang.txt"
# File Access mode [Read Mode]
accessMode = "r"

Opening file as CSV , Removing special characters from selected word in loop and comparing abbreviations present in CSV with it.

with open(fileName, accessMode) as myCSVfile:
# Reading file as CSV with delimiter as "=", so that abbreviation are stored in row[0] and phrases in row[1]
dataFromFile = csv.reader(myCSVfile, delimiter="=")
# Removing Special Characters.
_str = re.sub('[^a-zA-Z0-9-_.]', '', _str)
for row in dataFromFile:
# Check if selected word matches short forms[LHS] in text file.
if _str.upper() == row[0]:
# If match found replace it with its appropriate phrase in text file.
user_string[j] = row[1]
myCSVfile.close()
j = j + 1

Result

Result is also a List[], So we will replace all comma’s [,] with spaces [“ “]

' '.join(user_string)

Driver loop for this procedure

Get user input and call procedure until string entered by user is EXIT or exit.

user = input()
if user.upper() == 'EXIT':
print("Exiting Script")
break
translator(user)

Complete Script

import csv
import re
print("Welcome to SMS abbreviation Translator")
print("Remember not to use any void character in continuation")
print('===================================================')
def translator(user_string):
user_string = user_string.split(" ")
j = 0
for _str in user_string:
# File path which consists of Abbreviations.
fileName = "C:\\xxxx\\Desktop\\slang.txt"
# File Access mode [Read Mode]
accessMode = "r"
with open(fileName, accessMode) as myCSVfile:
# Reading file as CSV with delimiter as "=", so that abbreviation are stored in row[0] and phrases in row[1]
dataFromFile = csv.reader(myCSVfile, delimiter="=")
# Removing Special Characters.
_str = re.sub('[^a-zA-Z0-9-_.]', '', _str)
for row in dataFromFile:
# Check if selected word matches short forms[LHS] in text file.
if _str.upper() == row[0]:
# If match found replace it with its appropriate phrase in text file.
user_string[j] = row[1]
myCSVfile.close()
j = j + 1
# Replacing commas with spaces for final output.
print(' '.join(user_string))
print('===================================================')
print('')
while True:
print("Provide Input below or print exit or EXIT to end script")
# Getting User String.
# Sample : user input = "Hi Rishabh meet me asap!"
user = input()
# Keep Calling procedure until EXIT or exit keyword is encountered.
if user.upper() == 'EXIT':
print("Exiting Script")
break
translator(user)

How to run this script

  • Python needs to be installed.
  • Open Command Prompt in Windows.
  • Go to location where this script is present.

Enter following :

python script.py

And you’re done.

Screenshot of result :

Output Screenshot
Code

Source on Github

Regards,

Rishabh