Skip to content

Commit

Permalink
Merge pull request #7 from dhmit/anagram
Browse files Browse the repository at this point in the history
Anagram
  • Loading branch information
ryaanahmed authored Feb 2, 2021
2 parents 400e802 + c030859 commit feaf9f0
Show file tree
Hide file tree
Showing 29 changed files with 238,193 additions and 143 deletions.
12 changes: 12 additions & 0 deletions .idea/lang_learn.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added assets/img/shuffle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 5 additions & 3 deletions backend/app/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
This file controls the administrative interface for lang_learn app
"""

# from django.contrib import admin
# from .models import ADD ME!
from django.contrib import admin
from .models import (
Text
)

# admin.site.register( ADD ME! )
admin.site.register(Text)
51 changes: 51 additions & 0 deletions backend/app/analysis/anagrams.py
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
4 changes: 4 additions & 0 deletions backend/app/analysis/nltk_data/words/README
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)
Loading

0 comments on commit feaf9f0

Please sign in to comment.