Skip to content

Commit

Permalink
Handle documents w/o textTranscripts
Browse files Browse the repository at this point in the history
Fixes #47
  • Loading branch information
thvitt committed Feb 7, 2023
1 parent a93f819 commit f93e546
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions src/macrogen/witnesses.py
Expand Up @@ -82,29 +82,33 @@ def __init__(self, source: Path):

def verses(self, text_transcript: Optional[etree._ElementTree] = None) -> Dict[str, List[int]]:
if not self._verses:
if text_transcript is None:
text_transcript = etree.parse(fspath(self.text_transcript))

lines = text_transcript.xpath('//tei:l[@n]', namespaces=config.namespaces) + \
text_transcript.xpath('//tei:milestone[@unit="reflines"]', namespaces=config.namespaces)
insc_lines = defaultdict(list)
for line in lines:
precs = line.xpath('preceding::tei:milestone[@unit="stage"]', namespaces=config.namespaces)
linenos = [int(n) for n in _ids(line.get('n')) if n.isdigit()]
if precs:
prec = precs[0]
for insc in _ids(prec.get('change')):
insc_lines[insc].extend(linenos)
else:
insc_lines[''].extend(linenos)

# contained = line.xpath('descendant-or-self::*/@change')
# if contained is not None:
# for change in contained:
# for insc in _ids(change):
# insc_lines[insc].extend(linenos)

self._verses = insc_lines
try:
if text_transcript is None:
text_transcript = etree.parse(fspath(self.text_transcript))

lines = text_transcript.xpath('//tei:l[@n]', namespaces=config.namespaces) + \
text_transcript.xpath('//tei:milestone[@unit="reflines"]', namespaces=config.namespaces)
insc_lines = defaultdict(list)
for line in lines:
precs = line.xpath('preceding::tei:milestone[@unit="stage"]', namespaces=config.namespaces)
linenos = [int(n) for n in _ids(line.get('n')) if n.isdigit()]
if precs:
prec = precs[0]
for insc in _ids(prec.get('change')):
insc_lines[insc].extend(linenos)
else:
insc_lines[''].extend(linenos)

# contained = line.xpath('descendant-or-self::*/@change')
# if contained is not None:
# for change in contained:
# for insc in _ids(change):
# insc_lines[insc].extend(linenos)

self._verses = insc_lines
except Exception as e:
logger.exception('Failed to read %s: %s', self, e)
self._verses = defaultdict(list)
return self._verses

def paralipomena(self):
Expand Down

0 comments on commit f93e546

Please sign in to comment.