-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from dhmit/anagram
Anagram
- Loading branch information
Showing
29 changed files
with
238,193 additions
and
143 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import os | ||
from django.conf import settings | ||
|
||
|
||
def get_word_set(): | ||
""" | ||
:return: A set of english words | ||
""" | ||
word_file = os.path.join(settings.NLTK_WORD_DIR, 'en') | ||
with open(word_file) as file: | ||
word_set = set(line.strip() for line in file.readlines()) | ||
return word_set | ||
|
||
|
||
def get_letter_freq(letters): | ||
""" | ||
Given a word, find the frequency of letters in this word | ||
:param letters: the word to find the frequency of | ||
:return: a dictionary that maps letters to their frequency in the word | ||
""" | ||
freq = {} | ||
for letter in letters: | ||
letter = letter.lower() | ||
cur_freq = freq.setdefault(letter, 0) | ||
freq[letter] = cur_freq + 1 | ||
return freq | ||
|
||
|
||
def is_anagram(test_freq, word_freq): | ||
""" | ||
:param test_freq: The frequency dictionary of the word you are testing | ||
:param word_freq: The frequency dictionary that you want the anagram of | ||
:return: true if the test word is an anagram | ||
""" | ||
for letter in test_freq: | ||
if letter not in word_freq or test_freq[letter] > word_freq[letter]: | ||
return False | ||
return True | ||
|
||
|
||
def get_anagrams(anagram_freq): | ||
""" | ||
:param anagram_freq: the frequency dictionary of the scrambled letters | ||
:return: a set of all the anagrams | ||
""" | ||
anagrams = set() | ||
word_set = get_word_set() | ||
for word in word_set: | ||
if is_anagram(get_letter_freq(word), anagram_freq): | ||
anagrams.add(word.lower()) | ||
return anagrams |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Wordlists | ||
|
||
en: English, http://en.wikipedia.org/wiki/Words_(Unix) | ||
en-basic: 850 English words: C.K. Ogden in The ABC of Basic English (1932) |
Oops, something went wrong.