from dataclasses import asdict from typing import Optional import Levenshtein import pandas as pd from .types import CharacterIndices, Triplet, WordSpans def find_closest_text( *, original: pd.Series, replacement: pd.Series, ) -> pd.Series: # Returns a series of the replacement values aligned to the original values no_space_replacements = {text.replace(" ", ""): text for text in replacement} original_text = original.str.replace(" ", "") result = original_text.map(no_space_replacements) non_perfect_matches = result.isna().sum() assert non_perfect_matches / len(original) <= 0.20, ( "Poor alignment with replacement text. " f"{non_perfect_matches:,} of {len(original),} rows did not match well" ) def closest(text: str) -> str: distances = replacement.apply( lambda comparison: Levenshtein.distance(text, comparison) ) return replacement.iloc[distances.argmin()] result.loc[result.isna()] = original_text[result.isna()].apply(closest) result = result.str.strip() return result def to_character_indices_series(row: pd.Series) -> pd.Series: result = to_character_indices(triplet=row.triples, text=row.text) return pd.Series(asdict(result)) def to_character_indices( *, triplet: Triplet, text: str, ) -> CharacterIndices: aspect_span, opinion_span, _ = triplet assert _is_sequential(aspect_span), f"aspect span not sequential: {aspect_span}" assert _is_sequential(opinion_span), f"opinion span not sequential: {opinion_span}" spans = WordSpans.make(text) aspect_start_index, aspect_end_index = spans.to_indices(aspect_span) aspect_term = text[aspect_start_index : aspect_end_index + 1] opinion_start_index, opinion_end_index = spans.to_indices(opinion_span) opinion_term = text[opinion_start_index : opinion_end_index + 1] return CharacterIndices( aspect_start_index=aspect_start_index, aspect_end_index=aspect_end_index, aspect_term=aspect_term, opinion_start_index=opinion_start_index, opinion_end_index=opinion_end_index, opinion_term=opinion_term, ) def to_aligned_character_indices_series(row: pd.Series) -> pd.Series: indices = to_character_indices(triplet=row.triples, text=row.original) result = to_aligned_character_indices( original=row.original, replacement=row.text, original_indices=indices, ) return pd.Series(asdict(result)) def to_aligned_character_indices( *, original: str, replacement: str, original_indices: CharacterIndices, ) -> CharacterIndices: indices = _aligned_character_indices(original=original, replacement=replacement) aspect_start_index = _aligned_start_index( text=replacement, original_index=original_indices.aspect_start_index, indices=indices, ) aspect_end_index = _aligned_end_index( text=replacement, original_index=original_indices.aspect_end_index, indices=indices, ) aspect_term = replacement[aspect_start_index : aspect_end_index + 1] opinion_start_index = _aligned_start_index( text=replacement, original_index=original_indices.opinion_start_index, indices=indices, ) opinion_end_index = _aligned_end_index( text=replacement, original_index=original_indices.opinion_end_index, indices=indices, ) opinion_term = replacement[opinion_start_index : opinion_end_index + 1] return CharacterIndices( aspect_start_index=aspect_start_index, aspect_end_index=aspect_end_index, aspect_term=aspect_term, opinion_start_index=opinion_start_index, opinion_end_index=opinion_end_index, opinion_term=opinion_term, ) def _is_sequential(span: tuple[int, ...]) -> bool: return all(span[index + 1] - span[index] == 1 for index in range(len(span) - 1)) def _aligned_character_indices(original: str, replacement: str) -> list[Optional[int]]: original = original.casefold() replacement = replacement.casefold() indices: list[Optional[int]] = list(range(len(original))) for operation, _source_position, destination_position in Levenshtein.editops( original, replacement ): if operation == "replace": indices[destination_position] = None elif operation == "insert": indices.insert(destination_position, None) elif operation == "delete": del indices[destination_position] return indices def _aligned_start_index( text: str, original_index: int, indices: list[Optional[int]] ) -> int: closest_after = min( index for index in indices if index is not None and index >= original_index ) index = indices.index(closest_after) # Not every character in the original text is aligned to a character in the # replacement text. The replacement text may have deleted it, or replaced # it. Can step back through each letter until the word boundary is found or # an aligned character is found. while index > 0: if indices[index - 1] is not None: break if text[index - 1] == " ": break index -= 1 return index def _aligned_end_index( text: str, original_index: int, indices: list[Optional[int]] ) -> int: closest_before = max( index for index in indices if index is not None and index <= original_index ) index = indices.index(closest_before) # Not every character in the original text is aligned to a character in the # replacement text. The replacement text may have deleted it, or replaced # it. Can step back through each letter until the word boundary is found or # an aligned character is found. while index < len(indices) - 1: if indices[index + 1] is not None: break if text[index + 1] == " ": break index += 1 return index