Skip to content

Commit 8267685

Browse files
Anagram Finder Script Added
1 parent c0b68a7 commit 8267685

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

Anagram Finder/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Anagram Finder
2+
3+
## Description
4+
5+
Anagram Finder is a Python script that helps you find anagrams of a given word from a list of words. Anagrams are words formed by rearranging the letters of another word. This script uses a dictionary of words and groups them based on their sorted letter representations, making it efficient to find anagrams.
6+
7+
## Requirements
8+
9+
- Python
10+
- nltk (Natural Language Toolkit) library
11+
12+
## Installation
13+
14+
1. Install Python from the official website: https://www.python.org/downloads/
15+
2. Install the required nltk library by running the following command:
16+
17+
```bash
18+
pip install nltk
19+
```
20+
21+
## Usage
22+
23+
1. Clone this repository or download the anagram_finder.py file.
24+
25+
2. Run the script using Python:
26+
27+
```bash
28+
python anagram_finder.py
29+
```
30+
31+
The script will display anagrams for some sample search words, as well as additional words retrieved from the nltk library.
32+
33+
## Customization
34+
35+
You can customize the search words and the word list in the anagram_finder.py script. To search for different anagrams, modify the search_words list in the __main__ section of the script. Similarly, you can expand the word_list by adding more words to include in the anagram search.
36+
37+
# Sample search words
38+
search_words = ["silent", "debit card", "funeral", "astronomer"]
39+
40+
# Combine the NLTK word list with the existing word list
41+
word_list = [
42+
"listen", "silent", "enlist", "tinsel", "hello", "world", "python",
43+
"astronomer", "moonstarer", "debit card", "bad credit", "punishments", "nine thumps",
44+
"dormitory", "dirty room", "the eyes", "they see", "a gentleman", "elegant man",
45+
"funeral", "real fun", "slot machines", "cash lost in me", "eleven plus two", "twelve plus one"
46+
] + nltk_word_list
47+
48+
## Contributing
49+
If you have any ideas, improvements, or bug fixes, feel free to open an issue or submit a pull request. We appreciate your contributions!

Anagram Finder/anagram_finder.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import nltk
2+
nltk.download('words')
3+
from nltk.corpus import words
4+
5+
def find_anagrams(search_words, word_list):
6+
# Function to sort letters in a word and return it as a string
7+
def sort_word(word):
8+
return ''.join(sorted(word))
9+
10+
# Create a dictionary to store anagrams
11+
anagrams = {}
12+
13+
# Group words based on their sorted letter representations
14+
for w in word_list:
15+
sorted_word = sort_word(w)
16+
if sorted_word in anagrams:
17+
anagrams[sorted_word].append(w)
18+
else:
19+
anagrams[sorted_word] = [w]
20+
21+
results = {}
22+
23+
# Find anagrams for each search word
24+
for search_word in search_words:
25+
sorted_word = sort_word(search_word)
26+
if sorted_word in anagrams:
27+
results[search_word] = anagrams[sorted_word]
28+
else:
29+
results[search_word] = []
30+
31+
return results
32+
33+
if __name__ == "__main__":
34+
# Retrieve the NLTK word list
35+
nltk_word_list = words.words()
36+
37+
# Sample search words
38+
search_words = ["silent", "debit card", "funeral", "astronomer"]
39+
40+
# Combine the NLTK word list with the existing word list
41+
word_list = [
42+
"listen", "silent", "enlist", "tinsel", "hello", "world", "python",
43+
"astronomer", "moonstarer", "debit card", "bad credit", "punishments", "nine thumps",
44+
"dormitory", "dirty room", "the eyes", "they see", "a gentleman", "elegant man",
45+
"funeral", "real fun", "slot machines", "cash lost in me", "eleven plus two", "twelve plus one"
46+
] + nltk_word_list
47+
48+
results = find_anagrams(search_words, word_list)
49+
50+
for search_word, anagrams in results.items():
51+
if anagrams:
52+
print(f"Anagrams of '{search_word}': {', '.join(anagrams)}")
53+
else:
54+
print(f"No anagrams found for '{search_word}'.")

Anagram Finder/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nltk

0 commit comments

Comments
 (0)