Skip to content

Commit bcfc43e

Browse files
Merge pull request #2103 from CentreForDigitalHumanities/feature/corpus-settings
Feature/corpus settings
2 parents aac38b2 + 0769c87 commit bcfc43e

61 files changed

Lines changed: 383 additions & 264 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

backend/addcorpus/python_corpora/corpus.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from datetime import datetime, date
88
from os.path import isdir
99
import os
10-
1110
from django.conf import settings
1211

1312
from ianalyzer_readers.readers.core import Reader, Field

backend/addcorpus/python_corpora/load_corpus.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,42 @@ def corpus_dir(corpus_name: str) -> str:
1717
Arguments:
1818
corpus_name {str} -- Key of the corpus in CORPORA object in settings
1919
"""
20-
corpus = load_corpus_definition(corpus_name)
21-
return dirname(getabsfile(corpus.__class__))
20+
corpus = import_corpus_class(corpus_name)
21+
return dirname(getabsfile(corpus))
2222

2323

24-
def load_corpus_definition(corpus_name) -> Type[CorpusDefinition]:
24+
def import_corpus_class(corpus_name: str) -> Type[CorpusDefinition]:
25+
'''Imports a corpus definition class'''
2526
import_path = settings.CORPORA.get(corpus_name)
26-
return import_string(import_path)()
27+
return import_string(import_path)
28+
29+
30+
def corpus_settings(corpus_name: str) -> Dict:
31+
'''Attribute overrides for a corpus as configured in settings'''
32+
corpora_settings = getattr(settings, 'CORPUS_SETTINGS', {})
33+
return corpora_settings.get(corpus_name, dict())
34+
35+
36+
def apply_corpus_settings(corpus_class: Type[CorpusDefinition], settings: Dict):
37+
'''
38+
Creates a subclass of a corpus that overrides parent attributes according to settings.
39+
40+
Overrides are applied on a class instead of an instance, as this allows you to
41+
override `@property` attributes with static values.
42+
'''
43+
class ConfiguredCorpus(corpus_class):
44+
pass
45+
46+
for attr, value in settings.items():
47+
setattr(ConfiguredCorpus, attr, value)
48+
return ConfiguredCorpus
49+
50+
51+
def load_corpus_definition(corpus_name: str) -> CorpusDefinition:
52+
'''Imports, configures and instantiates a corpus definition'''
53+
corpus_class = import_corpus_class(corpus_name)
54+
configured_class = apply_corpus_settings(corpus_class, corpus_settings(corpus_name))
55+
return configured_class()
2756

2857

2958
def _try_loading_corpus_definition(corpus_name, stderr=sys.stderr) -> Optional[CorpusDefinition]:

backend/addcorpus/python_corpora/tests/test_corpusimport.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,43 @@ def test_corpus_dir(db, settings, basic_mock_corpus):
3636
assert os.path.isabs(path)
3737
assert 'corpus.py' in os.listdir(path)
3838
assert 'source_data' in os.listdir(path)
39+
40+
41+
def test_corpus_settings(db, settings):
42+
'''Test configuration using CORPUS_SETTINGS'''
43+
settings.CORPORA = {
44+
'example': 'corpora_test.basic.corpus.ExampleCorpus',
45+
'example-2': 'corpora_test.basic.corpus.ExampleCorpus',
46+
}
47+
settings.CORPUS_SETTINGS = {
48+
'example-2': {
49+
'title': 'Different title'
50+
}
51+
}
52+
corpora = load_corpus.load_all_corpus_definitions()
53+
assert len(corpora) == 2
54+
assert corpora['example'].title == 'Example'
55+
assert corpora['example-2'].title == 'Different title'
56+
57+
58+
def test_corpus_property_override(db, settings):
59+
'''
60+
Test that CORPUS_SETTINGS also works when the original value is a
61+
property without a setter (e.g. in CorpusDefinition itself)
62+
'''
63+
settings.CORPORA = {
64+
'test': 'addcorpus.python_corpora.corpus.CorpusDefinition',
65+
}
66+
settings.CORPUS_SETTINGS = {
67+
'test': {
68+
'title': 'Example',
69+
'description': 'Description',
70+
'min_date': 1900,
71+
'max_date': 2000,
72+
'category': 'other',
73+
'fields': [],
74+
}
75+
}
76+
corpora = load_corpus.load_all_corpus_definitions()
77+
assert corpora['test'].title == 'Example'
78+

backend/addcorpus/python_corpora/tests/test_times_source.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ def times_test_settings(settings):
1111
settings.CORPORA = {
1212
'times': 'corpora.times.times.Times',
1313
}
14-
settings.TIMES_DATA = join(settings.BASE_DIR, 'addcorpus/python_corpora/tests')
15-
settings.TIMES_ES_INDEX = 'test-times'
16-
14+
settings.CORPUS_SETTINGS["times"] = {
15+
"data_directory": join(settings.BASE_DIR, "addcorpus/python_corpora/tests"),
16+
"es_index": "test-times",
17+
}
1718

1819

1920
def test_times_source(times_test_settings):

backend/corpora/dbnl/dbnl.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from tqdm import tqdm
55
from ianalyzer_readers.xml_tag import Tag, CurrentTag, TransformTag
66

7-
from django.conf import settings
87
from addcorpus.python_corpora.corpus import XMLCorpusDefinition, FieldDefinition
98
from ianalyzer_readers.extract import Metadata, XML, Pass, Order, Backup, Combined
109
import corpora.dbnl.utils as utils
@@ -15,10 +14,11 @@
1514
class DBNL(XMLCorpusDefinition):
1615
title = 'DBNL'
1716
description = 'Dutch literature and publications on literary or linguistic studies, from the Middle Ages to the 19th century'
18-
data_directory = settings.DBNL_DATA
17+
es_index = 'dbnl' # default setting for development
18+
1919
min_date = datetime(year=1200, month=1, day=1)
2020
max_date = datetime(year=1890, month=12, day=31)
21-
es_index = getattr(settings, 'DBNL_ES_INDEX', 'dbnl')
21+
2222
image = 'dbnl.png'
2323
description_page = 'dbnl.md'
2424
citation_page = 'citation.md'
@@ -39,7 +39,7 @@ class DBNL(XMLCorpusDefinition):
3939
language_field = 'language_code'
4040

4141
def sources(self, start = None, end = None):
42-
metadata_corpus = DBNLMetadata()
42+
metadata_corpus = DBNLMetadata(data_directory=self.data_directory)
4343
all_metadata = utils.index_by_id(metadata_corpus.documents())
4444

4545
print('Extracting XML files...')
@@ -394,7 +394,7 @@ def _xml_files(self):
394394
has_content = FieldDefinition(
395395
name='has_content',
396396
display_name='Content available',
397-
description=f'Whether the contents of this book are available on {settings.SITE_NAME}',
397+
description=f'Whether the contents of this book are available on Textcavator',
398398
extractor=Metadata('has_xml'),
399399
es_mapping=bool_mapping(),
400400
search_filter=BooleanFilter(

backend/corpora/dbnl/dbnl_metadata.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
from django.conf import settings
32
from addcorpus.python_corpora.corpus import CSVCorpusDefinition, FieldDefinition
43
from ianalyzer_readers.extract import CSV, Combined, Pass
54
import corpora.dbnl.utils as utils
@@ -10,7 +9,9 @@ class DBNLMetadata(CSVCorpusDefinition):
109
Used by the DBNL corpus for CSV extraction utilities -
1110
not intended as a standalone corpus.'''
1211

13-
data_directory = settings.DBNL_DATA
12+
def __init__(self, data_directory=None):
13+
if data_directory:
14+
self.data_directory = data_directory
1415

1516
field_entry = 'ti_id'
1617
delimiter = '|'

backend/corpora/dbnl/tests/test_dbnl_extraction.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,20 @@
1010

1111
@pytest.fixture
1212
def dbnl_corpus(settings):
13-
settings.DBNL_DATA = os.path.join(here, 'data')
14-
# for testing purposes, also add the metadata helper corpus
13+
# for testing purposes, also add the metadata helper corpus - this would normally
14+
# be instantiated from the DBNL class
1515
settings.CORPORA = {
1616
'dbnl': 'corpora.dbnl.dbnl.DBNL',
1717
'dbnl_metadata': 'corpora.dbnl.dbnl_metadata.DBNLMetadata',
1818
}
19+
settings.CORPUS_SETTINGS = {
20+
'dbnl': {
21+
'data_directory': os.path.join(here, 'data'),
22+
},
23+
'dbnl_metadata': {
24+
'data_directory': os.path.join(here, 'data'),
25+
}
26+
}
1927
return 'dbnl'
2028

2129
language_name_testcases = [

backend/corpora/dbnl/tests/test_dbnl_validation.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
here = os.path.abspath(os.path.dirname(__file__))
55

66
def test_dbnl_validation(settings, load_test_corpus):
7-
settings.DBNL_DATA = os.path.join(here, 'data')
87
settings.CORPORA = {
98
'dbnl': 'corpora.dbnl.dbnl.DBNL',
109
}
10+
settings.CORPUS_SETTINGS = {
11+
'dbnl': {
12+
'data_directory': os.path.join(here, 'data'),
13+
}
14+
}
1115

1216
load_test_corpus('dbnl')
1317

backend/corpora/dutchannualreports/dutchannualreports.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@ class DutchAnnualReports(XMLCorpusDefinition):
2525
description = "Annual reports of Dutch financial and non-financial institutes"
2626
min_date = datetime(year=1957, month=1, day=1)
2727
max_date = datetime(year=2008, month=12, day=31)
28-
data_directory = settings.DUTCHANNUALREPORTS_DATA
29-
es_index = getattr(settings, 'DUTCHANNUALREPORTS_ES_INDEX', 'dutchannualreports')
28+
es_index = 'dutchannualreports'
3029
image = 'dutchannualreports.jpg'
31-
scan_image_type = getattr(settings, 'DUTCHANNUALREPORTS_SCAN_IMAGE_TYPE', 'application/pdf')
30+
scan_image_type = 'application/pdf'
3231
description_page = 'dutchannualreports.md'
33-
allow_image_download = getattr(settings, 'DUTCHANNUALREPORTS_ALLOW_IMAGE_DOWNLOAD', True)
34-
word_model_path = getattr(settings, 'DUTCHANNUALREPORTS_WM', None)
32+
allow_image_download = True
3533

3634
languages = ['nl']
3735
category = 'finance'

backend/corpora/dutchnewspapers/dutchnewspapers_all.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ class DutchNewsPapersAll(DutchNewspapersPublic):
1414

1515
title = "Dutch Newspapers (full)"
1616
description = "Full collection of Dutch newspapers digitised by the Koninklijke Bibliotheek."
17-
data_directory = settings.DUTCHNEWSPAPERS_ALL_DATA
18-
es_index = getattr(settings, 'DUTCHNEWSPAPERS_ALL_ES_INDEX', 'dutchnewspapers-all')
17+
es_index = 'dutchnewspapers-all'
1918
max_date = datetime(year=1995, month=12, day=31)
20-
word_model_path = getattr(settings, "DUTCHNEWSPAPERS_WM", None)
2119

2220
def update_body(self, doc=None):
2321
if not doc:

0 commit comments

Comments
 (0)