|
| 1 | +""" |
| 2 | +A classifier module that classifies books and subjects into various categories. This module is called when importing |
| 3 | +collections to a library or updating classifications. It's called by the core/model/classification.py. |
| 4 | +""" |
| 5 | + |
1 | 6 | # If the genre classification does not match the fiction classification, throw |
2 | 7 | # away the genre classifications. |
3 | 8 | # |
@@ -37,13 +42,15 @@ class ClassifierConstants: |
37 | 42 | BISAC = "BISAC" |
38 | 43 | BIC = "BIC" |
39 | 44 | TAG = "tag" # Folksonomic tags. |
| 45 | + DEMARQUE = "De Marque" |
40 | 46 |
|
41 | 47 | # Appeal controlled vocabulary developed by NYPL |
42 | 48 | NYPL_APPEAL = "NYPL Appeal" |
43 | 49 |
|
44 | 50 | GRADE_LEVEL = "Grade level" # "1-2", "Grade 4", "Kindergarten", etc. |
45 | 51 | AGE_RANGE = "schema:typicalAgeRange" # "0-2", etc. |
46 | 52 | AXIS_360_AUDIENCE = "Axis 360 Audience" |
| 53 | + DEMARQUE_AUDIENCE = "schema:Audience" |
47 | 54 |
|
48 | 55 | # We know this says something about the audience but we're not sure what. |
49 | 56 | # Could be any of the values from GRADE_LEVEL or AGE_RANGE, plus |
@@ -1223,6 +1230,32 @@ def add(self, classification): |
1223 | 1230 | # "Juvenile Fiction". |
1224 | 1231 | self.overdrive_juvenile_generic = classification |
1225 | 1232 |
|
| 1233 | + # E-kirjasto: Since De Marque classifications have target ages for children's and YA books, we want to weigh |
| 1234 | + # them more heavily by setting their weights to 1.0. This ensures that those books are classified accordingly. |
| 1235 | + if subject.type == "De Marque" and ( |
| 1236 | + subject.audience == Classifier.AUDIENCE_CHILDREN |
| 1237 | + or subject.audience == Classifier.AUDIENCE_YOUNG_ADULT |
| 1238 | + ): |
| 1239 | + if subject.target_age: |
| 1240 | + # Set the weight to 1.0 for any target age. |
| 1241 | + self.audience_weights = Counter() |
| 1242 | + self.audience_weights[subject.audience] += weight * 1.0 |
| 1243 | + scaled_weight = classification.weight_as_indicator_of_target_age |
| 1244 | + target_min = subject.target_age.lower |
| 1245 | + target_max = subject.target_age.upper |
| 1246 | + if target_min is not None: |
| 1247 | + self.target_age_lower_weights[target_min] = 1.0 |
| 1248 | + if target_max is not None: |
| 1249 | + self.target_age_upper_weights[target_max] = 1.0 |
| 1250 | + # E-kirjasto: Some De Marque adult books were incorrectly classified as children's books. Let's set the |
| 1251 | + # weight to 1.0 for any adult audience books. |
| 1252 | + if ( |
| 1253 | + subject.type == "De Marque" |
| 1254 | + and subject.audience == Classifier.AUDIENCE_ADULT |
| 1255 | + ): |
| 1256 | + self.audience_weights = Counter() |
| 1257 | + self.audience_weights[subject.audience] += weight * 1.0 |
| 1258 | + |
1226 | 1259 | def weigh_metadata(self): |
1227 | 1260 | """Modify the weights according to the given Work's metadata. |
1228 | 1261 |
|
@@ -1497,12 +1530,10 @@ def target_age(self, audience): |
1497 | 1530 | if target_age_min is None: |
1498 | 1531 | target_age_min = target_age_max |
1499 | 1532 |
|
1500 | | - if target_age_max is None: |
| 1533 | + # Err on the side of setting the minimum age too high but first ensure we have values to compare. |
| 1534 | + if target_age_min and target_age_max and target_age_min > target_age_max: |
1501 | 1535 | target_age_max = target_age_min |
1502 | 1536 |
|
1503 | | - # Err on the side of setting the minimum age too high. |
1504 | | - if target_age_min > target_age_max: |
1505 | | - target_age_max = target_age_min |
1506 | 1537 | return Classifier.range_tuple(target_age_min, target_age_max) |
1507 | 1538 |
|
1508 | 1539 | def genres(self, fiction, cutoff=0.15): |
@@ -1624,6 +1655,7 @@ def consolidate_genre_weights(cls, weights, subgenre_swallows_parent_at=0.03): |
1624 | 1655 | from core.classifier.bic import BICClassifier |
1625 | 1656 | from core.classifier.bisac import BISACClassifier |
1626 | 1657 | from core.classifier.ddc import DeweyDecimalClassifier |
| 1658 | +from core.classifier.demarque import DeMarqueClassifier |
1627 | 1659 | from core.classifier.gutenberg import GutenbergBookshelfClassifier |
1628 | 1660 | from core.classifier.keyword import ( |
1629 | 1661 | Eg, |
|
0 commit comments