Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions backend/app/analysis/anagrams.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Helper functions for generating and checking anagrams
"""
import os
import random
from django.conf import settings


Expand All @@ -15,6 +16,16 @@ def get_word_set():
return word_set


def scramble_word(word):
"""
:param word: the word to scramble for single word anagrams
:return: a string of the word with the letters scrambled
"""
word_as_list = list(word)
random.shuffle(word_as_list)
return ''.join(word_as_list)


def get_letter_freq(letters):
"""
Given a word, find the frequency of letters in this word
Expand Down
27 changes: 18 additions & 9 deletions backend/app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
TextSerializer
)
from .analysis.parts_of_speech import (
filter_pos,
get_part_of_speech_words,
get_valid_words,
)
from .analysis.anagrams import (
get_anagrams,
get_letter_freq,
get_word_set,
scramble_word,
)
from .analysis.textdata import (
get_text_data,
Expand Down Expand Up @@ -100,10 +101,6 @@ def get_anagram(request, text_id, part_of_speech):
elif anagram_freq[letter] < cur_freq[letter]:
anagram_freq[letter] = cur_freq[letter]

extra_words = get_anagrams(anagram_freq)
extra_words -= set(words) # Remove words from text from extra words
extra_words = filter_pos(extra_words, part_of_speech)

scrambled_letters = []
for letter in anagram_freq:
for i in range(anagram_freq[letter]):
Expand All @@ -113,13 +110,25 @@ def get_anagram(request, text_id, part_of_speech):
'letters': scrambled_letters,
'word_data': [{'word': word,
'definition': definitions[word].get(part_of_speech, []),
'example': examples[word].get(part_of_speech, [])}
for word in words],
'extra_words': extra_words
'example': examples[word].get(part_of_speech, []),
'scrambled': scramble_word(word)}
for word in words]
}
return Response(res)


word_set = get_word_set()


@api_view(['GET'])
def check_word(request, word, pos):
"""
API endpoint for checking whether an extra word is part of the extra word set
"""
words = get_part_of_speech_words(word, pos) # Used to check for part of speech in extra words
return Response(len(word) >= 3 and len(words) > 0 and word in word_set)


@api_view(['GET'])
def get_picturebook_prompt(request, text_id, part_of_speech):
"""
Expand Down
2 changes: 2 additions & 0 deletions backend/config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from app.views import (
all_text,
get_anagram,
check_word,
update_text,
delete_text,
add_text,
Expand Down Expand Up @@ -51,6 +52,7 @@ def react_view_path(route, component_name):
path('api/all_text', all_text),
path('api/all_text/', all_text),
path('api/get_anagram/<int:text_id>/<str:part_of_speech>', get_anagram),
path('api/check_word/<str:word>/<str:pos>', check_word),
path('api/update_text', update_text),
path('api/delete_text', delete_text),
path('api/add_text', add_text),
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/UILibrary/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,13 @@ body {
text-align: center;
height: 86%;
}

.loading-spinner {
margin: 0 auto auto;
}

.loading-text {
margin: auto auto 30px;
font-size: 30px;
font-weight: bold;
}
Loading