def test_load_data(self): authors = Authors() author = authors.get_author_by_mapping("Abbott", "I,1") compare("William Abbott", author[0].name) compare(None, author[0].death) author = authors.get_author_by_mapping("Abel", "I,1") compare("Herman Abel", author[0].name) compare(1998, author[0].death) author = authors.get_author_by_mapping("Abel", "XVI,1") compare("Abel", author[0].name) compare(1987, author[0].death) author = authors.get_author_by_mapping("redirect.", "XVI,1") compare("Abert", author[0].name) compare(1927, author[0].death) author = authors.get_author_by_mapping("redirect_list", "XVI,1") compare("Abert", author[0].name) compare("Herman Abel", author[1].name) compare(1927, author[0].death) compare([], authors.get_author_by_mapping("Tada", "XVI,1")) author = authors.get_author("Abert|") compare("Abert", author.name)
class CleanAuthors: def __init__(self): self.authors = Authors() self.registers = Registers() def _get_deletable_authors(self): raw_author_set = set(self.authors.authors_dict.keys()) author_set = copy.copy(raw_author_set) for author in raw_author_set: author_obj = self.authors.authors_dict[author] if author_obj.redirect: author_set.remove(author_obj.redirect) return author_set def _get_all_authors_from_mapping(self) -> Set[str]: mapping_set = set() for mapping_key in self.authors.authors_mapping: mapping_value = self.authors.authors_mapping[mapping_key] if isinstance(mapping_value, str): mapping_set.add(mapping_value.replace("|", "")) elif isinstance(mapping_value, list): for item in mapping_value: mapping_set.add(item.replace("|", "")) elif isinstance(mapping_value, dict): for item in mapping_value.values(): mapping_set.add(item.replace("|", "")) return mapping_set def delete_authors_without_mapping(self): for _ in range(2): author_set = self._get_deletable_authors() for item in sorted( author_set.difference( self._get_all_authors_from_mapping())): del self.authors.authors_dict[item] self.authors.persist() def delete_mappings_without_use(self): register_authors = set() for register in self.registers.volumes.values(): for lemma in register: for chapter in lemma.chapters: if chapter.author: register_authors.add(chapter.author) for key in set(self.authors.authors_mapping.keys()).difference( register_authors): del self.authors.authors_mapping[key] self.authors.persist() def remap(self): for mapping in self.authors.authors_mapping: # key is the same linke value if mapping == self.authors.authors_mapping[mapping]: old_author_list = self.authors.get_author_by_mapping( mapping, "") if len(old_author_list) == 1: old_author = old_author_list[0] if old_author.death: # now analyse if there is a better author self._filter_and_replace(old_author) self.authors.persist() def _filter_and_replace(self, old_author: Author): new_candidates = self._create_candidates(old_author) if len(new_candidates) > 1: new_candidates = self._filter_candidates(new_candidates, old_author) if len(new_candidates) == 1: self.authors.set_mappings( {old_author.name: new_candidates[0].name}) def _create_candidates(self, old_author: Author) -> List[Author]: new_candidates = [] for author in self.authors.authors_dict: if author.find(old_author.name) > -1: new_candidates.append(self.authors.get_author(author)) return new_candidates @staticmethod def _filter_candidates(new_candidates: List[Author], old_author: Author) -> List[Author]: # eleminate old author new_candidates = list( filter(lambda x: x is not old_author, new_candidates)) # eleminate dublicates new_candidates = list(set(new_candidates)) # eleminate wrong death years if old_author.death: if (old_author.death % 1111) == 0: new_candidates = list( filter(lambda x: x.death is None, new_candidates)) else: new_candidates = list( filter(lambda x: x.death == old_author.death, new_candidates)) return new_candidates return []
class TestAuthorRegister(BaseTestRegister): def setUp(self): copy_tst_data("I_1_alpha", "I_1") copy_tst_data("III_1_alpha", "III_1") self.authors = Authors() self.volumes = Volumes() self.registers = OrderedDict() self.registers["I,1"] = VolumeRegister(self.volumes["I,1"], self.authors) self.registers["III,1"] = VolumeRegister(self.volumes["III,1"], self.authors) def test_init(self): abel_register = AuthorRegister(self.authors.get_author("Herman Abel"), self.authors, self.registers) compare(4, len(abel_register)) def test_make_table(self): abel_register = AuthorRegister(self.authors.get_author("Herman Abel"), self.authors, self.registers) expected_table = """{{RERegister |AUTHOR=Herman Abel |SUM=4 |UNK=1 |KOR=1 |FER=2 }} {|class="wikitable sortable" !Artikel !Kurztext !Wikilinks !Band !Seite !Autor !Stat |- |data-sort-value="aba 001"|[[RE:Aba 1|'''{{Anker2|Aba 1}}''']] ||This is Aba 1 || ||I,1 |[https://elexikon.ch/meyers/RE/I,1_5.png 4] |Herman Abel |style="background:#AA0000"|UNK |- |data-sort-value="aba 002"|[[RE:Aba 2|'''{{Anker2|Aba 2}}''']] || || ||I,1 |[https://elexikon.ch/meyers/RE/I,1_5.png 4] |Herman Abel |style="background:#556B2F"|KOR |- |rowspan=2 data-sort-value="beta"|[[RE:Beta|'''{{Anker2|Beta}}''']] |rowspan=2|This is Beta |rowspan=2| |rowspan=2|I,1 |[https://elexikon.ch/meyers/RE/I,1_5.png 4] |Abert |rowspan=2 style="background:#669966"|FER |- |[https://elexikon.ch/meyers/RE/I,1_5.png 4]-5 |Herman Abel |- |data-sort-value="charlie"|[[RE:Charlie|'''{{Anker2|Charlie}}''']] || || ||III,1 |[https://elexikon.ch/meyers/RE/III,1_5.png 4] |Herman Abel |style="background:#669966"|FER |} [[Kategorie:RE:Register|Abel, Herman]]""" compare(expected_table, abel_register.get_register_str()) def test_overview_line(self): abel_register = AuthorRegister(self.authors.get_author("Herman Abel"), self.authors, self.registers) compare( "|-\n" "|data-sort-value=\"Abel, Herman\"" "|[[Paulys Realencyclopädie der classischen Altertumswissenschaft/Register/Herman Abel|Herman Abel]]\n" "|data-sort-value=\"0004\"|4\n" "|data-sort-value=\"075.0\"|75.0%\n" "|<span style=\"color:#669966\">██████████</span>" "<span style=\"color:#556B2F\">█████</span>" "<span style=\"color:#AA0000\">█████</span>", abel_register.overview_line) def test_proofread_parts_of_20(self): compare((10, 5, 5), AuthorRegister.proofread_parts_of_20(4, 2, 1)) compare((0, 1, 19), AuthorRegister.proofread_parts_of_20(40, 1, 1)) compare((1, 0, 19), AuthorRegister.proofread_parts_of_20(45, 2, 1)) compare((0, 1, 19), AuthorRegister.proofread_parts_of_20(45, 1, 2)) compare((0, 1, 19), AuthorRegister.proofread_parts_of_20(79, 1, 1)) compare((0, 0, 20), AuthorRegister.proofread_parts_of_20(80, 1, 1)) def test_bug_to_much_percent(self): copy_tst_data("I_1_author_bug_percent", "I_1") copy_tst_data("III_1_author_bug_percent", "III_1") registers = OrderedDict() registers["I,1"] = VolumeRegister(self.volumes["I,1"], self.authors) registers["III,1"] = VolumeRegister(self.volumes["III,1"], self.authors) abel_register = AuthorRegister(self.authors.get_author("Herman Abel"), self.authors, registers) compare( "|-\n" "|data-sort-value=\"Abel, Herman\"" "|[[Paulys Realencyclopädie der classischen Altertumswissenschaft/Register/Herman Abel|Herman Abel]]\n" "|data-sort-value=\"0005\"|5\n" "|data-sort-value=\"100.0\"|100.0%\n" "|<span style=\"color:#669966\">████████████████████</span>" "<span style=\"color:#556B2F\"></span>" "<span style=\"color:#AA0000\"></span>", abel_register.overview_line)