![]() |
VOOZH | about |
The days when one would get data in tabulated spreadsheets are truly behind us. A moment of silence for the data residing in the spreadsheet pockets. Today, more than 80% of the data is unstructured β it is either present in data silos or scattered around the digital archives. Data is being produced as we speak β from every conversation we make in the social media to every content generated from news sources. In order to produce any meaningful actionable insight from data, it is important to know how to work with it in its unstructured form. As a Data Scientist at one of the fastest growing Decision Sciences firm, my bread and butter comes from deriving meaningful insights from unstructured text information.
One of the first steps in working with text data is to pre-process it. It is an essential step before the data is ready for analysis. Majority of available text data is highly unstructured and noisy in nature β to achieve better insights or to build better algorithms, it is necessary to play with clean data. For example, social media data is highly unstructured β it is an informal communication β typos, bad grammar, usage of slang, presence of unwanted content like URLs, Stopwords, Expressions etc. are the usual suspects.
In this blog, therefore I discuss about these possible noise elements and how you could clean them step by step. I am providing ways to clean data using Python.
As a typical business problem, assume you are interested in finding: which are the features of an iPhone which are more popular among the fans. You have extracted consumer opinions related to iPhone and here is a tweet you extracted:
[stextbox id = βgreyβ]βI luv my <3 iphone & youβre awsm apple. DisplayIsAwesome, sooo happppppy π http://www.apple.comβ
[/stextbox]Here is what you do:
[stextbox id = βgreyβ]
Snippet:
tweet = original_tweet.decode("utf8").encode(βasciiβ,βignoreβ)
Output:
>> βI luv my <3 iphone & youβre awsm apple. DisplayIsAwesome, sooo happppppy π http://www.apple.comβ
[/stextbox]
For example βitβs is a contraction for it is or it hasβ.
All the apostrophes should be converted into standard lexicons. One can use a lookup table of all possible keys to get rid of disambiguates.
[stextbox id = βgreyβ]
Snippet:
APPOSTOPHES = {β's" : " is", "'re" : " are", ...} ## Need a huge dictionary
words = tweet.split()
reformed = [APPOSTOPHES[word] if word in APPOSTOPHES else word for word in words]
reformed = " ".join(reformed)
Outcome:
>> βI luv my <3 iphone & you are awsm apple. DisplayIsAwesome, sooo happppppy π http://www.apple.comβ
[/stextbox]
[stextbox id = βgreyβ]
Snippet:
cleaned = β β.join(re.findall(β[A-Z][^A-Z]*β, original_tweet))
Outcome:
>> βI luv my <3 iphone & you are awsm apple. Display Is Awesome, sooo happppppy π http://www.apple.comβ
[/stextbox]
[stextbox id = βgreyβ]
Snippet:
tweet = _slang_loopup(tweet)
Outcome:
>> βI love my <3 iphone & you are awesome apple. Display Is Awesome, sooo happppppy π http://www.apple.comβ
[/stextbox]
[stextbox id = βgreyβ]
Snippet:
tweet = ''.join(''.join(s)[:2] for _, s in itertools.groupby(tweet))
Outcome:
>> βI love my <3 iphone & you are awesome apple. Display Is Awesome, so happy π http://www.apple.comβ
[/stextbox]
Final cleaned tweet:
>> βI love my iphone & you are awesome apple. Display Is Awesome, so happy!β , <3 , π
[/stextbox]Hope you found this article helpful. These were some tips and tricks, I have learnt while working with a lot of text data. If you follow the above steps to clean the data, you can drastically improve the accuracy of your results and draw better insights. Do share your views/doubts in the comments section and I would be happy to participate.
Go Hack π
Shivam Bansal is a data scientist with exhaustive experience in Natural Language Processing and Machine Learning in several domains. He is passionate about learning and always looks forward to solving challenging analytical problems.
GPT-4 vs. Llama 3.1 β Which Model is Better?
Llama-3.1-Storm-8B: The 8B LLM Powerhouse Surpa...
A Comprehensive Guide to Building Agentic RAG S...
Top 10 Machine Learning Algorithms in 2026
45 Questions to Test a Data Scientist on Basics...
90+ Python Interview Questions and Answers (202...
8 Easy Ways to Access ChatGPT for Free
Prompt Engineering: Definition, Examples, Tips ...
What is LangChain?
What is Retrieval-Augmented Generation (RAG)?
Can All this be done in R?
Yes !! Packages like "tm" (text mining) provides support for many of these functions. Rest of them can be explicitly written in R.
Can you explain words standardization in more detail ,what are the improper formats ?
Hey Shivam, I want to tokenize 1000 tweets stored in a text file using a single loop in PYTHON , please can you help me with that? please reply asap. Thanks
Edit
Resend OTP
Resend OTP in 45s