def produce_scansion(self, stresses: list, syllables_wspaces: list, offset_map: dict) -> str: """Create a scansion string that has stressed and unstressed syllable positions in locations that correspond with the original texts syllable vowels. :param stresses list of syllable positions :param syllables_wspaces list of syllables with spaces escaped for punctuation or elision :param offset_map dictionary of syllable positions, and an offset amount which is the number of spaces to skip in the original line before inserting the accent. """ scansion = list(" " * len(StringUtils.flatten(syllables_wspaces))) unstresses = StringUtils.get_unstresses(stresses, len(syllables_wspaces)) try: for idx in unstresses: location = offset_map[idx] if location is not None: scansion[location] = self.constants.UNSTRESSED for idx in stresses: location = offset_map[idx] if location is not None: scansion[location] = self.constants.STRESSED except Exception as e: print("problem with syllables; check syllabification %s %s" % (syllables_wspaces, e)) pass return "".join(scansion)
def calc_offset(self, syllables_spaces: list) -> dict: """Calculate a dictionary of accent positions from a list of syllables with spaces.""" line = StringUtils.flatten(syllables_spaces) mydict = defaultdict(lambda: None) for idx, syl in enumerate(syllables_spaces): target_syllable = syllables_spaces[idx] skip_qu = StringUtils.starts_with_qu(target_syllable) matches = list(self.syllable_matcher.finditer(target_syllable)) for position, possible in enumerate(matches): if skip_qu: skip_qu = False continue (start, end) = possible.span() if target_syllable[start:end] in \ self.constants.VOWELS + self.constants.ACCENTED_VOWELS: part = line[:len("".join(syllables_spaces[:idx]))] offset = len(part) + start if line[offset] not in self.constants.VOWELS + self.constants.ACCENTED_VOWELS: LOG.error("Problem at line {} offset {}".format(line, offset)) mydict[idx] = offset return mydict
def calc_offset(self, syllables_spaces: list) -> dict: """Calculate a dictionary of accent positions from a list of syllables with spaces.""" line = StringUtils.flatten(syllables_spaces) mydict = defaultdict(lambda: None) for idx, syl in enumerate(syllables_spaces): target_syllable = syllables_spaces[idx] skip_qu = StringUtils.starts_with_qu(target_syllable) matches = list(self.syllable_matcher.finditer(target_syllable)) for position, possible in enumerate(matches): if skip_qu: skip_qu = False continue (start, end) = possible.span() if target_syllable[start:end] in \ self.constants.VOWELS + self.constants.ACCENTED_VOWELS: part = line[:len("".join(syllables_spaces[:idx]))] offset = len(part) + start if line[offset] not in self.constants.VOWELS + self.constants.ACCENTED_VOWELS: print("Problem at line %s offset %s" % (line, offset)) mydict[idx] = offset return mydict
def produce_scansion(self, stresses: list, syllables_wspaces: list, offset_map: dict) -> str: """Create a scansion string that has stressed and unstressed syllable positions in locations that correspond with the original texts syllable vowels. :param stresses list of syllable positions :param syllables_wspaces list of syllables with spaces escaped for punctuation or elision :param offset_map dictionary of syllable positions, and an offset amount which is the number of spaces to skip in the original line before inserting the accent. """ scansion = list(" " * len(StringUtils.flatten(syllables_wspaces))) unstresses = StringUtils.get_unstresses(stresses, len(syllables_wspaces)) try: for idx in unstresses: location = offset_map[idx] if location is not None: scansion[location] = self.constants.UNSTRESSED for idx in stresses: location = offset_map[idx] if location is not None: scansion[location] = self.constants.STRESSED except Exception as e: LOG.error("problem with syllables; check syllabification {}, {}".format( syllables_wspaces, e)) return "".join(scansion)