Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
537 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ handlers: | |
root: | ||
handlers: | ||
- console | ||
level: INFO | ||
level: WARNING | ||
|
||
loggers: | ||
graph: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
from .graph import macrogenesis_graphs, BiblSource | ||
from .graph import macrogenesis_graphs | ||
from macrogen.bibliography import BiblSource | ||
from .uris import Reference, Witness, AmbiguousRef, Inscription | ||
from .visualize import simplify_graph, write_dot, render_file, render_all |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import csv | ||
from collections import namedtuple, defaultdict | ||
from typing import Dict, Union, IO | ||
|
||
import requests | ||
from lxml import etree | ||
|
||
from .config import config | ||
from . import faust | ||
|
||
BibEntry = namedtuple('BibEntry', ['uri', 'citation', 'reference', 'weight']) | ||
|
||
|
||
def _parse_bibliography(bibxml: Union[str, IO]) -> Dict[str, BibEntry]: | ||
"""Parses the bibliography file at url. Returns a dictionary mapping an URI to a corresponding bibliography entry.""" | ||
db: Dict[str, BibEntry] = {} | ||
scores = config.bibscores | ||
et = etree.parse(bibxml) | ||
for bib in et.xpath('//f:bib', namespaces=faust.namespaces): | ||
uri = bib.get('uri') | ||
citation = bib.find('f:citation', namespaces=faust.namespaces).text | ||
reference = bib.find('f:reference', namespaces=faust.namespaces).text | ||
db[uri] = BibEntry(uri, citation, reference, scores[uri]) | ||
return db | ||
|
||
|
||
_bib_labels = { | ||
'faust://self': 'Faustedition', | ||
'faust://model/inscription': 'Inskription von', | ||
'faust://orphan/adoption': 'Datierungsinhalt für', | ||
'faust://heuristic': 'künstliche Datierung' | ||
} | ||
|
||
|
||
class BiblSource: | ||
""" | ||
A bibliographic source in a macrogenesis XML file. | ||
""" | ||
|
||
def __init__(self, uri, detail=''): | ||
""" | ||
Creates a bibliographic source. | ||
Args: | ||
uri: should be a faust://bibliography/ URI or one of the special values | ||
detail: detail string like pages | ||
""" | ||
self.uri = uri | ||
if detail is None: | ||
detail = '' | ||
self.detail = detail | ||
self.weight = config.bibliography[uri].weight if uri in config.bibliography else 1 | ||
|
||
def __eq__(self, other): | ||
if isinstance(other, BiblSource): | ||
return self.uri == other.uri and self.detail == other.detail | ||
else: | ||
return super().__eq__(other) | ||
|
||
def __hash__(self): | ||
return hash(self.uri) ^ hash(self.detail) | ||
|
||
def __str__(self): | ||
result = self.citation | ||
if self.detail is not None: | ||
result += '\n' + self.detail | ||
return result | ||
|
||
@property | ||
def filename(self): | ||
""" | ||
A string representation of the source (w/o detail) that is usable as part of a filename. | ||
""" | ||
if self.uri.startswith('faust://bibliography'): | ||
return self.uri.replace('faust://bibliography/', '') | ||
else: | ||
return self.uri.replace('faust://', '').replace('/', '-') | ||
|
||
@property | ||
def citation(self): | ||
""" | ||
String representation of only the citation, w/o detail. | ||
Example: | ||
Bohnenkamp 19994 | ||
""" | ||
if self.uri in config.bibliography: | ||
return config.bibliography[self.uri].citation | ||
elif self.uri in _bib_labels: | ||
return _bib_labels[self.uri] | ||
else: | ||
return self.filename | ||
|
||
@property | ||
def long_citation(self): | ||
if self.uri in config.bibliography: | ||
return config.bibliography[self.uri].reference | ||
else: | ||
return self.citation | ||
|
||
|
||
def read_scores(scorefile): | ||
""" | ||
Parses the bibliography score file. | ||
Returns: | ||
Map uri -> score | ||
""" | ||
scores = defaultdict(lambda: 1) | ||
logger = config.getLogger(__name__) | ||
r = csv.reader(scorefile, delimiter='\t') | ||
for row in r: | ||
try: | ||
scores[row[0]] = int(row[1]) | ||
except ValueError as e: | ||
logger.warning('Skipping scorefile row %s: %s', row, e) | ||
return scores |
Oops, something went wrong.