1+ import logging
12import random
23
3- from lib .word_db_helper import WordDB
44from 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__ )
69HIGH_PROB_CUTOFF = 5000
710MED_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
4667def 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
0 commit comments