Skip to content

Commit 266ad1a

Browse files
committed
progress on special challenges
1 parent 314fa15 commit 266ad1a

File tree

4 files changed

+100
-23
lines changed

4 files changed

+100
-23
lines changed

djAerolith/lib/domain.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ def append(self, question):
7070
def extend(self, questions):
7171
self.questions.extend(questions.questions)
7272

73+
def truncate(self, n):
74+
self.questions = self.questions[:n]
75+
7376
def size(self):
7477
return len(self.questions)
7578

djAerolith/wordwalls/challenges.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@
1919

2020
logger = logging.getLogger(__name__)
2121

22-
# try:
23-
# COMMON_SHORT_NAMED_LIST = NamedList.objects.get(
24-
# name=FRIENDLY_COMMON_SHORT)
25-
# COMMON_LONG_NAMED_LIST = NamedList.objects.get(name=FRIENDLY_COMMON_LONG)
26-
# except NamedList.DoesNotExist:
27-
# COMMON_SHORT_NAMED_LIST = None
28-
# COMMON_LONG_NAMED_LIST = None
29-
3022

3123
def generate_dc_questions(challenge_name, lex, challenge_date):
3224
"""

djAerolith/wordwalls/special_challenges.py

Lines changed: 75 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import logging
12
import random
23

3-
from lib.word_db_helper import WordDB
44
from lib.domain import Questions
5+
from lib.word_db_helper import WordDB, word_search
6+
from lib.word_searches import SearchDescription
57

8+
logger = logging.getLogger(__name__)
69
HIGH_PROB_CUTOFF = 5000
710
MED_PROB_CUTOFF = 10000
811

@@ -31,16 +34,34 @@ def get_alphagrams_for_challenge(lex, challenge_name):
3134
given the challenge name.
3235
3336
"""
37+
# try:
38+
# COMMON_SHORT_NAMED_LIST = NamedList.objects.get(
39+
# name=FRIENDLY_COMMON_SHORT)
40+
# COMMON_LONG_NAMED_LIST = NamedList.objects.get(name=FRIENDLY_COMMON_LONG)
41+
# except NamedList.DoesNotExist:
42+
# COMMON_SHORT_NAMED_LIST = None
43+
# COMMON_LONG_NAMED_LIST = None
44+
3445
db = WordDB(lex.lexiconName)
35-
if challenge_name == SpecialChallengeTypes.HIGH_PROB_BINGOS:
36-
return get_by_prob(db, [7, 8], [1, HIGH_PROB_CUTOFF])
37-
elif challenge_name == SpecialChallengeTypes.HIGH_PROB_7s:
38-
return get_by_prob(db, [7], [1, HIGH_PROB_CUTOFF])
39-
elif challenge_name == SpecialChallengeTypes.HIGH_PROB_8s:
40-
return get_by_prob(db, [8], [1, HIGH_PROB_CUTOFF])
41-
elif challenge_name == SpecialChallengeTypes.MED_PROB_BINGOS:
42-
return get_by_prob(db, [7, 8], [HIGH_PROB_CUTOFF + 1,
43-
MED_PROB_CUTOFF])
46+
func_table = {
47+
SpecialChallengeTypes.HIGH_PROB_BINGOS: lambda: get_by_prob(
48+
db, [7, 8], [1, HIGH_PROB_CUTOFF]),
49+
SpecialChallengeTypes.HIGH_PROB_7s: lambda: get_by_prob(
50+
db, [7], [1, HIGH_PROB_CUTOFF]),
51+
SpecialChallengeTypes.HIGH_PROB_8s: lambda: get_by_prob(
52+
db, [8], [1, HIGH_PROB_CUTOFF]),
53+
SpecialChallengeTypes.MED_PROB_BINGOS: lambda: get_by_prob(
54+
db, [7, 8], [HIGH_PROB_CUTOFF + 1, MED_PROB_CUTOFF]),
55+
# SpecialChallengeTypes.COMMON_WORDS_SHORT: lambda:
56+
# SpecialChallengeTypes.COMMON_WORDS_LONG: lambda:
57+
SpecialChallengeTypes.HIGH_VOWEL_BINGOS: lambda: get_by_vowels(
58+
lex, [7, 8], [4, 5]),
59+
SpecialChallengeTypes.JQXZ_SHORT_WORDS: lambda: get_by_jqxz(
60+
lex, [5, 6]),
61+
}
62+
63+
fn = func_table[challenge_name]
64+
return fn()
4465

4566

4667
def get_by_prob(db, lengths, probs):
@@ -51,10 +72,51 @@ def get_by_prob(db, lengths, probs):
5172
"""
5273
questions = Questions()
5374
for lg in lengths:
54-
probs = list(range(probs[0], probs[1] + 1))
55-
random.shuffle(probs)
75+
prob_list = list(range(probs[0], probs[1] + 1))
76+
random.shuffle(prob_list)
77+
num_qs = int(50 / len(lengths))
78+
qs = db.get_questions_for_probability_list(prob_list[:num_qs], lg)
79+
questions.extend(qs)
80+
81+
return questions
82+
83+
84+
def get_by_vowels(lex, lengths, vowels):
85+
"""
86+
get_by_vowels returns 50 alphagrams.
87+
88+
Half of them have length lengths[0] and vowels vowels[0]+
89+
Half of them have length lengths[1] and vowels vowels[1]+
90+
91+
"""
92+
questions = Questions()
93+
for idx, lg in enumerate(lengths):
94+
qs = word_search([
95+
SearchDescription.lexicon(lex),
96+
SearchDescription.length(lg, lg),
97+
SearchDescription.number_vowels(vowels[idx], lg),
98+
])
99+
100+
qs.shuffle()
101+
num_qs = int(50 / len(lengths))
102+
qs.truncate(num_qs)
103+
questions.extend(qs)
104+
105+
return questions
106+
107+
108+
def get_by_jqxz(lex, lengths):
109+
questions = Questions()
110+
for idx, lg in enumerate(lengths):
111+
qs = word_search([
112+
SearchDescription.lexicon(lex),
113+
SearchDescription.length(lg, lg),
114+
SearchDescription.matching_anagram('[JQXZ]+' + '?' * (lg - 1)),
115+
])
116+
117+
qs.shuffle()
56118
num_qs = int(50 / len(lengths))
57-
qs = db.get_questions_for_probability_list(probs[:num_qs], lg)
119+
qs.truncate(num_qs)
58120
questions.extend(qs)
59121

60122
return questions

djAerolith/wordwalls/tests/test_special_challenges.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
import re
2+
13
from django.test import TestCase
24

5+
from base.models import Lexicon
36
from lib.word_db_helper import WordDB
4-
from wordwalls.special_challenges import get_by_prob
7+
from wordwalls.special_challenges import get_by_prob, get_by_vowels
58

69

710
class SpecialChallengeTest(TestCase):
11+
fixtures = ['test/lexica.json']
12+
813
def setUp(self):
14+
self.lex = Lexicon.objects.get(lexiconName='America')
915
self.db = WordDB(lexicon_name='America')
1016

1117
def test_get_by_prob(self):
@@ -18,4 +24,18 @@ def test_get_by_prob(self):
1824
assert (len(q.alphagram.alphagram) >= 7 and
1925
len(q.alphagram.alphagram) <= 8)
2026
alphas.add(q.alphagram.alphagram)
21-
assert(len(alphas) == 50)
27+
assert(len(alphas) == 50)
28+
29+
def test_get_by_vowels(self):
30+
qs = get_by_vowels(self.lex, [7, 8], [4, 5])
31+
assert len(qs) == 50
32+
alphas = set()
33+
for q in qs.questions_array():
34+
assert (len(q.alphagram.alphagram) >= 7 and
35+
len(q.alphagram.alphagram) <= 8)
36+
if len(q.alphagram.alphagram) == 7:
37+
assert len(re.findall('[AEIOU]{1}', q.alphagram.alphagram)) >= 4 # noqa
38+
if len(q.alphagram.alphagram) == 8:
39+
assert len(re.findall('[AEIOU]{1}', q.alphagram.alphagram)) >= 5 # noqa
40+
alphas.add(q.alphagram.alphagram)
41+
assert(len(alphas) == 50)

0 commit comments

Comments
 (0)