from nltk import edit_distance from utilities.utils import answer_letter from utilities_language_general.similarity_measures import make_decision from utilities_language_general.esp_constants import nlp, FIX_LEMMA, COMBINE_POS def prepare_target_words(target_words): target_words = target_words.replace(' ,', ',').replace(',', ', ').replace(' ', ' ').split(', ') return list(set(target_words)) def compute_frequency_dict(text: str) -> dict: """ Compute frequency dictionary of given text and return it sorted in descending order. :param text: given text as string variable :return: frequency dictionary {word: frequency} sorted in descending order """ freq_dict = {} doc = nlp(text) lemma_list_spacy = [token.lemma_ for token in doc] for lemma in lemma_list_spacy: if lemma.isalpha(): if lemma not in freq_dict.keys(): freq_dict[lemma] = 1 else: freq_dict[lemma] += 1 return freq_dict def get_tags(token: str): return nlp(token)[0].morph.to_dict() def fix_irregular_lemma(lemma, fixed_lemmas=FIX_LEMMA): for key, value in fixed_lemmas.items(): if lemma in value: return key return lemma def check_token(token, lemma_pos, model, current_minimum: set = None, check_allowed_pos: set = None, check_allowed_dep: set = None) -> bool: not_allowed_pos = {'PROPN', 'PUNCT', 'NUM'} not_allowed_dep = {'cop', } # 'ROOT' if lemma_pos == 'auto': lemma_pos = f'{token.lemma_}_{token.pos_}' if not token.text.isalpha(): return False if current_minimum is not None and token.lemma_ not in current_minimum: return False if not model.has_index_for(lemma_pos): return False if (not token.is_oov and not token.is_stop): if check_allowed_pos is None and check_allowed_dep is None: if token.pos_ not in not_allowed_pos and token.dep_ not in not_allowed_dep: return True return False elif check_allowed_pos is not None and check_allowed_dep is None: if token.pos_ in check_allowed_pos and token.dep_ not in not_allowed_dep: return True return False elif check_allowed_pos is None and check_allowed_dep is not None: if token.pos_ not in not_allowed_pos and token.dep_ in check_allowed_dep: return True return False else: if token.pos_ in check_allowed_pos and token.dep_ in check_allowed_dep: return True return False else: return False def check_token_bert(token, current_minimum: set = None, check_allowed_pos: set = None, check_allowed_dep: set = None) -> bool: not_allowed_pos = {'PROPN', 'PUNCT', 'NUM'} not_allowed_synt_dep = {'cop', } # 'ROOT' if not token.text.isalpha(): return False if current_minimum is not None and token.lemma_ not in current_minimum: return False if get_tags(token.text) is not None: tags = get_tags(token.text) else: tags = None if not token.is_stop and tags is not None: if check_allowed_pos is None and check_allowed_dep is None: if token.pos_ not in not_allowed_pos and token.dep_ not in not_allowed_synt_dep: return True return False elif check_allowed_pos is not None and check_allowed_dep is None: if token.pos_ in check_allowed_pos and token.dep_ not in not_allowed_synt_dep: return True return False elif check_allowed_pos is None and check_allowed_dep is not None: if token.pos_ not in not_allowed_pos and token.dep_ in check_allowed_dep: return True return False else: if token.pos_ in check_allowed_pos and token.dep_ in check_allowed_dep: return True return False else: return False def get_distractors_from_model(doc, model, scaler, classifier, pos_dict:dict, target_text:str, lemma: str, pos: str, gender: str, lemma_index:int, global_distractors: set, distractor_minimum: set, level_name: str, max_num_distractors: int, max_length_ratio=5, min_edit_distance_ratio=0.5): distractors = [] query = lemma if '_' in lemma else f'{lemma}_{pos}' lemma = '_'.join(lemma.split('_')[::2]) if model.has_index_for(query): candidates = model.most_similar(query, topn=max_num_distractors + 100) else: if query.count('_') == 1: return None query_raw_list = query.split('_') query_parts = ['_'.join(query_raw_list[i:i + 2]) for i in range(len(query_raw_list))][::2] query_vector = model.get_mean_vector(query_parts) candidates = model.similar_by_vector(query_vector, topn=max_num_distractors + 100) for candidate in candidates: if candidate[0].count('_') == 1 and pos != 'phrase': distractor_lemma, distractor_pos = candidate[0].split('_') decision = make_decision(doc, model_type='w2v', model=model, scaler=scaler, classifier=classifier, pos_dict=pos_dict, level=level_name, target_lemma=query, target_text=target_text, target_pos=pos, target_position=lemma_index, substitute_lemma=distractor_lemma, substitute_pos=distractor_pos) distractor_similarity = candidate[1] candidate_gender = get_tags(distractor_lemma).get('Gender') length_ratio = abs(len(lemma) - len(distractor_lemma)) condition = ((distractor_pos == pos or (COMBINE_POS['simple'][level_name].get(pos) is not None and COMBINE_POS['simple'][level_name].get(distractor_pos) is not None and distractor_pos in COMBINE_POS['simple'][level_name][pos] and pos in COMBINE_POS['simple'][level_name][distractor_pos])) and decision and distractor_lemma != lemma and distractor_lemma not in lemma and lemma not in distractor_lemma and (candidate_gender == gender and level_name in ('B1', 'B2', 'C1', 'C2') or level_name in ('A1', 'A2')) and length_ratio <= max_length_ratio and distractor_lemma not in global_distractors and edit_distance(lemma, distractor_lemma) / ((len(lemma) + len(distractor_lemma)) / 2) > min_edit_distance_ratio) if condition: if distractor_minimum is not None: if distractor_lemma in distractor_minimum: distractors.append((distractor_lemma, distractor_similarity)) global_distractors.add(distractor_lemma) else: distractors.append((distractor_lemma, distractor_similarity)) global_distractors.add(distractor_lemma) else: if (candidate[0].count('_') == 1 # REMOVE HOTFIX or candidate[0].count('_') > 3 or pos in ('NOUN', 'ADJ', 'NUM')): continue d1_lemma, d1_pos, d2_lemma, d2_pos = candidate[0].split('_') d_pos = f'{d1_pos}_{d2_pos}' distractor_lemma = f'{d1_lemma}_{d2_lemma}' distractor_similarity = candidate[1] decision = make_decision(doc, model_type='w2v', model=model, scaler=scaler, classifier=classifier, pos_dict=pos_dict, level=level_name, target_lemma=query, target_text=target_text, target_pos=pos, target_position=lemma_index, substitute_lemma=candidate[0], substitute_pos=d_pos) condition = (((d1_pos == pos or d2_pos == pos) or (COMBINE_POS['phrase'][level_name].get(d_pos) is not None and COMBINE_POS['phrase'][level_name].get(pos) is not None and d_pos in COMBINE_POS['phrase'][level_name].get(d_pos) and pos in COMBINE_POS['phrase'][level_name].get(pos) ) or (d1_pos in ('VERB', 'AUX', 'SCONJ', 'ADP') and pos in ('phrase', 'VERB', 'AUX', 'SCONJ', 'ADP')) or (d2_pos in ('VERB', 'AUX', 'SCONJ', 'ADP') and pos in ('phrase', 'VERB', 'AUX', 'SCONJ', 'ADP'))) and decision and candidate[0] != lemma and distractor_lemma not in lemma and lemma not in distractor_lemma and distractor_lemma != lemma and distractor_lemma not in global_distractors) if condition: if distractor_minimum is not None: if (distractor_lemma in distractor_minimum or (d1_lemma in distractor_minimum and d2_lemma in distractor_minimum)): distractors.append((candidate[0], distractor_similarity)) global_distractors.add(distractor_lemma) else: distractors.append((candidate[0], distractor_similarity)) global_distractors.add(distractor_lemma) max_num_distractors = min(4, max_num_distractors) if max_num_distractors >= 4 else max_num_distractors if len(distractors) < max_num_distractors: return None else: return distractors def get_distractors_from_model_bert(model, scaler, classifier, pos_dict:dict, level_name: str, lemma: str, pos: str, gender: str, text_with_masked_task: str, global_distractors: set, distractor_minimum: set, max_num_distractors: int, max_length_ratio=5, min_edit_distance_ratio=0.5): _distractors = [] try: bert_candidates = [token for token in model(text_with_masked_task, top_k=max_num_distractors + 100)] candidates = [] for candidate in bert_candidates: if isinstance(candidate, list): bert_candidates = candidate continue if candidate['token_str'].isalpha(): candidate_morph = nlp(candidate['token_str'])[0] candidates.append((f"{candidate_morph.lemma_}_{candidate_morph.pos_}", candidate['score'])) except KeyError: return None for candidate_distractor in candidates: if '_' in candidate_distractor[0]: distractor_lemma, distractor_pos = candidate_distractor[0].split('_') else: candidate_morph = nlp(candidate_distractor[0])[0] distractor_lemma, distractor_pos = candidate_morph.lemma_, candidate_morph.pos_ distractor_similarity = candidate_distractor[1] candidate_gender = get_tags(distractor_lemma).get('Gender') length_ratio = abs(len(lemma) - len(distractor_lemma)) decision = make_decision(doc=None, model_type='bert', scaler=scaler, classifier=classifier, pos_dict=pos_dict, level=level_name, target_lemma=lemma, target_text=None, target_pos=pos, target_position=None, substitute_lemma=distractor_lemma, substitute_pos=distractor_pos, bert_score=distractor_similarity) if ((distractor_pos == pos or (COMBINE_POS['simple'][level_name].get(pos) is not None and COMBINE_POS['simple'][level_name].get(distractor_pos) is not None and distractor_pos in COMBINE_POS['simple'][level_name][pos] and pos in COMBINE_POS['simple'][level_name][distractor_pos])) and decision and distractor_lemma != lemma and (len(_distractors) < max_num_distractors+100) and (candidate_gender == gender and level_name in ('B1', 'B2', 'C1', 'C2') or level_name in ('A1', 'A2')) and (length_ratio <= max_length_ratio) # May be changed if case of phrases and (distractor_lemma not in global_distractors) and (edit_distance(lemma, distractor_lemma) # May be changed if case of phrases / ((len(lemma) + len(distractor_lemma)) / 2) > min_edit_distance_ratio)): if distractor_minimum is not None: if distractor_lemma in distractor_minimum: _distractors.append((distractor_lemma, candidate_distractor[1])) global_distractors.add(distractor_lemma) else: _distractors.append((distractor_lemma, candidate_distractor[1])) num_distractors = min(4, max_num_distractors) if max_num_distractors >= 4 else max_num_distractors if len(_distractors) < num_distractors: return None return _distractors def prepare_tasks(input_variants): TASKS_STUDENT = '' TASKS_TEACHER = '' KEYS_ONLY = '' RAW_TASKS = [] RAW_KEYS_ONLY = [] RESULT_TASKS_STUDENT = [] TASKS_WITH_ANSWERS_L = [] KEYS = [] for num, item in enumerate(input_variants): item = item[0] answer = item[0] variants = '\t'.join([i.lower() for i in item[1]]) current_answer_letter = answer_letter(answer=answer, variants=[i.lower() for i in item[1]]) RAW_TASKS.append((num + 1, variants)) RAW_KEYS_ONLY.append((num + 1, current_answer_letter.split(' ')[0])) RESULT_TASKS_STUDENT.append(f"{num + 1}.\t{variants}") TASKS_WITH_ANSWERS_L.append(f"{num + 1}.\t" f"Ответ: {current_answer_letter}\n\t" f"Варианты: {variants}") KEYS.append(f"{num + 1}.\tОтвет: {current_answer_letter}") for task in RESULT_TASKS_STUDENT: TASKS_STUDENT += f'{task}\n' for task in TASKS_WITH_ANSWERS_L: TASKS_TEACHER += f'{task}\n' for task in KEYS: KEYS_ONLY += f'{task}\n' return {'TASKS_STUDENT': TASKS_STUDENT, 'TASKS_TEACHER': TASKS_TEACHER, 'KEYS_ONLY': KEYS_ONLY, 'RAW_TASKS': RAW_TASKS, 'RAW_KEYS_ONLY': RAW_KEYS_ONLY}