Coders Packet

Anagram generator in python

By Tasneem Fatima

Here is a python code for generating anagrams for any given string. An anagram is any new meaningful word formed by rearranging the words in the given string.

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.

As the title suggests, the code produces anagrams for any given word. This was achieved by using the NLTK library to check if any permutation of the string characters forms a meaningful word.

To find the various permutations of the words we use two techniques, 

Recursion: To find anagrams of length smaller than the length of user input.

Itertool: To find anagrams of length equal to the length of user input.

Recursion is a technique, where a function being defined is applied within its own definition. This defines an infinite number of instances (function values), which are used to find the various re-arrangements of the characters in the string and then analyzed to confirm whether it exists in the English dictionary.

Itertools is a module in Python that provides us with a collection of functions to handle iterators. Itertools in Python is a fast, memory-efficient module that utilizes the system's computational resources efficiently. The itertools.permutations function accepts a single iterable and creates every possible permutation of input values. These permutations are then used to create anagrams by comparing them with words in the English dictionary.

 

Some examples of anagrams are:

bored = robed cat = act cider = cried
dusty = study elbow = below inch = chin
night = thing peach = cheap players = parsley
sadder = dreads save = vase state = taste

 

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by Tasneem Fatima (tasneem)

Download packets of source code on Coders Packet