Spaces:
Sleeping
Sleeping
Add Grapheme attribution
Browse files66 unmodified lines67686970717273747576777879808182838411 unmodified lines9697989910010110210310410510610710810911011111211311411566 unmodified lines_, _, acc, path = accelerated_dtw(ref_features, input_features, dist=distance_metric)return dist_matrix, path@staticmethoddef plot_waveform_with_overlay(wav, sr, dist_matrix, path, wav_type='ref', threshold=0.3, labels_data=None, input_number=None):feature_stride = 320scaling_factor = feature_stride / srtime_ref = np.linspace(0, len(wav) / sr, len(wav))fig, ax = plt.subplots(3, 1, figsize=(15, 10), gridspec_kw={'height_ratios': [5, 1, 1]})ax[0].plot(time_ref, wav, label="Waveform", color="blue", alpha=0.7)red_segments, green_segments, _ = get_red_green_segments(dist_matrix, path, wav_type=wav_type, threshold=threshold)for index in green_segments:start_time = index * scaling_factorend_time = (index + 1) * scaling_factor11 unmodified linesax[1].set_xlim(ax[0].get_xlim())ax[2].set_xlim(ax[0].get_xlim())if labels_data:for start, end, grapheme, *boolean_labels in labels_data:ax[0].axvline(start, color='gray', linestyle='--', alpha=0.7)ax[0].axvline(end, color='gray', linestyle='--', alpha=0.7)ax[1].axvline(start, color='gray', linestyle='--', alpha=0.7)ax[1].axvline(end, color='gray', linestyle='--', alpha=0.7)ax[2].axvline(start, color='gray', linestyle='--', alpha=0.7)ax[2].axvline(end, color='gray', linestyle='--', alpha=0.7)ax[1].text((start + end) / 2, max(wav) * 0.8, grapheme, ha='center', va='center', fontsize=10, color='black')ax[2].barh(input_number, end - start, left=start, color='green' if boolean_labels[input_number] else 'red', alpha=0.7)ax[2].set_xlabel("Time (s)")ax[2].set_yticks(range(1))ax[2].set_yticklabels([f"Input {input_number}"])66 unmodified lines67686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710811 unmodified lines12012112212312412512612712812913013113213313413513613713813914014114266 unmodified lines_, _, acc, path = accelerated_dtw(ref_features, input_features, dist=distance_metric)return dist_matrix, path@staticmethoddef calculate_red_percentage(red_segments, labels_data, scaling_factor):red_percentages = []def intersection_length(start1, end1, start2, end2):overlap_start = max(start1, start2)overlap_end = min(end1, end2)return max(0, overlap_end - overlap_start)for start, end, grapheme, *boolean_labels in labels_data:red_intersection = 0.0for index in red_segments:red_start_time = index * scaling_factorred_end_time = (index + 1) * scaling_factorred_intersection += intersection_length(start, end, red_start_time, red_end_time)total_grapheme_duration = end - startred_percentage = (red_intersection / total_grapheme_duration)red_percentages.append(min(red_percentage, 1.))return red_percentages@staticmethoddef plot_waveform_with_overlay(wav, sr, dist_matrix, path, wav_type='ref', threshold=0.3, labels_data=None, input_number=None):feature_stride = 320scaling_factor = feature_stride / srtime_ref = np.linspace(0, len(wav) / sr, len(wav))fig, ax = plt.subplots(4, 1, figsize=(15, 12), gridspec_kw={'height_ratios': [5, 1, 1, 1]})ax[0].plot(time_ref, wav, label="Waveform", color="blue", alpha=0.7)red_segments, green_segments, _ = get_red_green_segments(dist_matrix, path, wav_type=wav_type, threshold=threshold)for index in green_segments:start_time = index * scaling_factorend_time = (index + 1) * scaling_factor11 unmodified linesax[1].set_xlim(ax[0].get_xlim())ax[2].set_xlim(ax[0].get_xlim())ax[3].set_xlim(ax[0].get_xlim())if labels_data:red_percentages = PronunciationChecker.calculate_red_percentage(red_segments, labels_data, scaling_factor)for index, (start, end, grapheme, *boolean_labels) in enumerate(labels_data):for subplot in ax:subplot.axvline(start, color='gray', linestyle='--', alpha=0.7)subplot.axvline(end, color='gray', linestyle='--', alpha=0.7)ax[1].text((start + end) / 2, max(wav) * 0.8, grapheme, ha='center', va='center', fontsize=10, color='black')ax[2].barh(input_number, end - start, left=start, color='green' if boolean_labels[input_number] else 'red', alpha=0.7)red_percentage = red_percentages[index]ax[3].barh(input_number, end - start, left=start, color='red', alpha=red_percentage)ax[2].set_xlabel("Time (s)")ax[2].set_yticks(range(1))ax[2].set_yticklabels([f"Input {input_number}"])
src/pronunciation_checker.py+36-9