math-words/fix-word-matching
#2
by ikarasz - opened
373 unmodified lines3743753763773783793803813823833843853863873883893903913923931 unmodified line395396397398399400401402403404405406407408409410411412413414415373 unmodified linesreturn outputdef load_math_terms():math_terms = []math_terms_dict = {}for term in MATH_WORDS:if term in MATH_PREFIXES:math_terms_dict[f"(^|[^a-zA-Z]){term}(s|es|d|ed)?([^a-zA-Z]|$)"] = termmath_terms.append(f"(^|[^a-zA-Z]){term}(s|es|d|ed)?([^a-zA-Z]|$)")else:math_terms.append(term)math_terms_dict[term] = termreturn math_terms, math_terms_dictdef run_math_density(transcript):math_terms, math_terms_dict = load_math_terms()sorted_terms = sorted(math_terms, key=len, reverse=True)teacher_math_word_cloud = {}student_math_word_cloud = {}for i, utt in enumerate(transcript.utterances):1 unmodified linenum_matches = 0matched_positions = set()match_list = []for term in sorted_terms:matches = list(re.finditer(term, text, re.IGNORECASE))# Filter out matches that share positions with longer termsmatches = [match for match in matches if not any(match.start() in range(existing[0], existing[1]) for existing in matched_positions)]# matched_text = [match.group(0) for match in matches]if len(matches) > 0:if utt.role == "teacher":if math_terms_dict[term] not in teacher_math_word_cloud:teacher_math_word_cloud[math_terms_dict[term]] = 0teacher_math_word_cloud[math_terms_dict[term]] += len(matches)else:if math_terms_dict[term] not in student_math_word_cloud:student_math_word_cloud[math_terms_dict[term]] = 0student_math_word_cloud[math_terms_dict[term]] += len(matches)match_list.append(math_terms_dict[term])# Update matched positionsmatched_positions.update((match.start(), match.end()) for match in matches)num_matches += len(matches)373 unmodified lines3743753763773783793803813823833843853863873883893903913923931 unmodified line395396397398399400401402403404405406407408409410411412413414415373 unmodified linesreturn outputdef load_math_terms():math_regexes = []math_terms_dict = {}for term in MATH_WORDS:if term in MATH_PREFIXES:math_terms_dict[rf"\b{term}(s|es|d|ed)?\b"] = termmath_regexes.append(rf"\b{term}(s|es|d|ed)?\b")else:math_regexes.append(rf"\b{term}\b")math_terms_dict[rf"\b{term}\b"] = termreturn math_regexes, math_terms_dictdef run_math_density(transcript):math_regexes, math_terms_dict = load_math_terms()sorted_regexes = sorted(math_regexes, key=len, reverse=True)teacher_math_word_cloud = {}student_math_word_cloud = {}for i, utt in enumerate(transcript.utterances):1 unmodified linenum_matches = 0matched_positions = set()match_list = []for regex in sorted_regexes:matches = list(re.finditer(regex, text, re.IGNORECASE))# Filter out matches that share positions with longer termsmatches = [match for match in matches if not any(match.start() in range(existing[0], existing[1]) for existing in matched_positions)]# matched_text = [match.group(0) for match in matches]if len(matches) > 0:if utt.role == "teacher":if math_terms_dict[regex] not in teacher_math_word_cloud:teacher_math_word_cloud[math_terms_dict[regex]] = 0teacher_math_word_cloud[math_terms_dict[regex]] += len(matches)else:if math_terms_dict[regex] not in student_math_word_cloud:student_math_word_cloud[math_terms_dict[regex]] = 0student_math_word_cloud[math_terms_dict[regex]] += len(matches)match_list.append(math_terms_dict[regex])# Update matched positionsmatched_positions.update((match.start(), match.end()) for match in matches)num_matches += len(matches)
handler.py+17-17