Building an Anagram Detector in Python

Building an Anagram Detector in Python

Beginner’s Project Idea 💡

·

3 min read

What is an Anagram?

An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Examples

act = cat
elbow = below
night = thing
a gentleman = elegant man
eleven plus two = twelve plus one
listen = silent
the detectives = detect thieves

AB017D2C-CDBD-4C74-985A-5D385701DFDF.png

History of Anagrams

Some historians have suggested, that anagrams originated in the 4th century B.C., but weren’t commonly used until the 13th century A.D. and even then, they were sometimes thought of as mystical.

Background Idea

Anagrams are fun linguistic phenomena to observe, but yet, one wonders if computers can be made to detect them too...
This thought brings to light the concept that, while computers are indeed fast, they still do not possess any human-like quality of intelligence, as we are all too familiar with, and as such would definitely need to be instructed to see patterns that exist in human language.

Due to the peculiarities of this task, a program would be written to give specific procedural instructions, on computational tasks to carry out.

To begin, one needs to understand that two (2) popular paradigms of coding exist:

...and, I have chosen to solve this task, using procedural code (AI helps via Natural Language Processing models but that’s another story)

Process

  • Step 1 - Clean the text

19A578E1-FD06-46AD-A5F0-C56ACE0646E9.jpeg

The snippet above represents the steps involved in cleaning the string representation of the words in question. One should always expect that text contain spaces and punctuation, hence the steps above outline the process involved in removing them, thereby allowing the algorithm to deal with the useful characters only.

  • Step 2 - Logic Algorithm

1) Using the "Counter" class from the built-in collections module

B3FAA35B-30F2-4033-B6CC-206A91BF97AC.jpeg

Python Anagram Detector - 1

2) Using the built-in "sorted" function

B62B37E3-A839-4363-BC38-AA8F1BEFE807.jpeg

Python Anagram Detector - 2


Conclusion

Now that you understand the backend of an anagram detecting algorithm, you can then go on to build on it. That is, by adding more sophisticated features which would help detect anagrams from a whole text file (.txt) or better still deploy such solutions to the Web where users can solve their Anagram problems.

A more advanced solution to detecting anagrams would be, using Artificial Intelligence to detect them in text, this would certainly be an interesting concept, and useful applications would be found in quizzes, and word games (scrabble, etc.).

Check out linked tweet

1/3 🧵 Beginner’s Coding Project

Building an algorithmic Python Anagram Detector (P.A.D.)

What is an Anagram?
An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.#Python #AI #NLP pic.twitter.com/fFSjuqNwxW

— Regressor (@Wilder_Maxim) February 12, 2022

Let's Connect

Project Portfolio GitHub Twitter Kaggle Linkedin Tableau
Â