def generateKanaQuestion(word): candidates = [] for w in Word.objects.all(): length = len(getLcs(word.kana, w.kana)) if min(len(word.kana) - 1, 1) <= length < len(word.kana): candidates.append([w, length]) candidateFilter = list(filter(lambda e: e[1] / len(e[0].kana) > 0.4, candidates)) if len(candidateFilter) > 0: candidates = candidateFilter options = [word] + [e[0] for e in weightedSample(candidates, lambda e: e[1], 5)] random.shuffle(options) for i in range(len(options)): if options[i] == word: return { 'options': options, 'answer': i }
def makeChoices(word, kana, numberOfChoices = 5): word = katakanaToHiragana(word) lcs = getLcs(word, kana) choices = [] while len(choices) < numberOfChoices: choice = '' lcsCopy = lcs # TO-DO: 首先確定選項中變化的假名個數以及比例 for i in range(len(kana)): if kana[i] in 'ゃゅょっんを': choice += kana[i] continue if len(lcsCopy) > 0 and kana[i] == lcsCopy[0]: lcsCopy = lcsCopy[1:] choice += kana[i] continue # TO-DO: 針對促音及拗音部分檢查合法性 choice += pickSimilarKana(kana[i]) if choice != kana and choice not in choices: choices.append(choice) choices.append(kana) random.shuffle(choices) return choices