-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_author_catalog.py
More file actions
164 lines (149 loc) · 7.15 KB
/
test_author_catalog.py
File metadata and controls
164 lines (149 loc) · 7.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import csv
import tempfile
import unittest
from pathlib import Path
import author_catalog
FIELDNAMES = [
"source",
"author_raw",
"author_first_last",
"author_last_first",
"title_example",
"language",
"source_author_id",
"source_work_id",
"source_url",
"confidence",
"notes",
]
class AuthorCatalogTests(unittest.TestCase):
def test_load_author_catalog_resolves_aliases_and_splits(self) -> None:
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "author_patterns.csv"
with path.open("w", encoding="utf-8-sig", newline="") as handle:
writer = csv.DictWriter(handle, fieldnames=FIELDNAMES)
writer.writeheader()
writer.writerow(
{
"source": "lubimyczytac | openlibrary",
"author_raw": "Aguirre Ann",
"author_first_last": "Ann Aguirre",
"author_last_first": "Aguirre Ann",
"title_example": "Enklawa",
"language": "pl | en",
"source_author_id": "lubimyczytac:1 | openlibrary:OL1A",
"source_work_id": "",
"source_url": "https://lubimyczytac.pl/autor/1/ann-aguirre | https://openlibrary.org/authors/OL1A",
"confidence": "high",
"notes": "test",
}
)
catalog = author_catalog.load_author_catalog(path)
self.assertEqual(catalog.resolve("Ann Aguirre"), "Ann Aguirre")
self.assertEqual(catalog.resolve("Aguirre Ann"), "Ann Aguirre")
self.assertTrue(catalog.is_known("Aguirre Ann"))
self.assertEqual(catalog.split_prefix("Aguirre Ann Enklawa"), ("Ann Aguirre", "Enklawa"))
self.assertEqual(catalog.split_suffix("Enklawa Ann Aguirre"), ("Ann Aguirre", "Enklawa"))
def test_load_author_catalog_prefers_query_spelling_when_canonical_splits_surname(self) -> None:
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "author_patterns.csv"
with path.open("w", encoding="utf-8-sig", newline="") as handle:
writer = csv.DictWriter(handle, fieldnames=FIELDNAMES)
writer.writeheader()
writer.writerow(
{
"source": "lubimyczytac | openlibrary",
"author_raw": "",
"author_first_last": "Alfred Hitch Cock",
"author_last_first": "Cock Alfred Hitch",
"title_example": "Jak w komiksie",
"language": "pl | en",
"source_author_id": "lubimyczytac:1 | openlibrary:OL1A",
"source_work_id": "",
"source_url": "https://lubimyczytac.pl/autor/1/alfred-hitchcock | https://openlibrary.org/authors/OL1A",
"confidence": "high",
"notes": "test",
}
)
catalog = author_catalog.load_author_catalog(path)
self.assertEqual(catalog.resolve("Alfred Hitchcock"), "Alfred Hitchcock")
self.assertEqual(catalog.resolve("Hitchcock Alfred"), "Alfred Hitchcock")
self.assertEqual(catalog.resolve_authors("Alfred Hitchcock"), ["Alfred Hitchcock"])
def test_load_author_catalog_writes_and_reuses_cache(self) -> None:
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "author_patterns.csv"
with path.open("w", encoding="utf-8-sig", newline="") as handle:
writer = csv.DictWriter(handle, fieldnames=FIELDNAMES)
writer.writeheader()
writer.writerow(
{
"source": "lc",
"author_raw": "",
"author_first_last": "Ann Aguirre",
"author_last_first": "Aguirre Ann",
"title_example": "",
"language": "",
"source_author_id": "",
"source_work_id": "",
"source_url": "",
"confidence": "high",
"notes": "",
}
)
first_catalog = author_catalog.load_author_catalog(path)
cache_path = path.with_name(f"{path.name}.catalog-cache.pkl")
self.assertTrue(cache_path.exists())
second_catalog = author_catalog.load_author_catalog(path)
self.assertEqual(first_catalog.resolve("Aguirre Ann"), "Ann Aguirre")
self.assertEqual(second_catalog.resolve("Aguirre Ann"), "Ann Aguirre")
def test_load_author_catalog_resolves_multi_author_segments_with_noise(self) -> None:
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "author_patterns.csv"
with path.open("w", encoding="utf-8-sig", newline="") as handle:
writer = csv.DictWriter(handle, fieldnames=FIELDNAMES)
writer.writeheader()
writer.writerow(
{
"source": "lubimyczytac",
"author_raw": "Ludlum Robert",
"author_first_last": "Robert Ludlum",
"author_last_first": "Ludlum Robert",
"title_example": "",
"language": "pl",
"source_author_id": "lubimyczytac:1",
"source_work_id": "",
"source_url": "https://lubimyczytac.pl/autor/1/robert-ludlum",
"confidence": "medium",
"notes": "test",
}
)
writer.writerow(
{
"source": "lubimyczytac | openlibrary",
"author_raw": "Lustbader Eric van",
"author_first_last": "Eric van Lustbader",
"author_last_first": "Lustbader Eric van",
"title_example": "",
"language": "pl | en",
"source_author_id": "lubimyczytac:2 | openlibrary:OL2A",
"source_work_id": "",
"source_url": "https://lubimyczytac.pl/autor/2/eric-van-lustbader | https://openlibrary.org/authors/OL2A",
"confidence": "high",
"notes": "test",
}
)
catalog = author_catalog.load_author_catalog(path)
self.assertEqual(
catalog.resolve_authors("Robert Ludlum & Eric van Lustbader"),
["Robert Ludlum", "Eric van Lustbader"],
)
self.assertEqual(
catalog.resolve_authors("Ludlum Robert Van Lustbader Eric"),
["Robert Ludlum", "Eric van Lustbader"],
)
self.assertEqual(
catalog.resolve_authors("Bourne'a Ludlum Robert Van Lustbader Eric"),
["Robert Ludlum", "Eric van Lustbader"],
)
if __name__ == "__main__":
unittest.main()