sdb-reader 0.9.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sdb_reader/__init__.py +157 -0
- sdb_reader/ask.py +437 -0
- sdb_reader/ask_record.schema.json +388 -0
- sdb_reader/cli.py +1079 -0
- sdb_reader/clp_statements_de.json +997 -0
- sdb_reader/compliance.py +546 -0
- sdb_reader/current.py +134 -0
- sdb_reader/diff.py +520 -0
- sdb_reader/embedded.py +250 -0
- sdb_reader/extraction.py +506 -0
- sdb_reader/layout.py +894 -0
- sdb_reader/line_breaks.py +40 -0
- sdb_reader/messages.py +394 -0
- sdb_reader/messages_data/de.toml +146 -0
- sdb_reader/messages_data/en.toml +141 -0
- sdb_reader/model.py +321 -0
- sdb_reader/parser.py +4601 -0
- sdb_reader/pictograms.py +374 -0
- sdb_reader/presets.py +110 -0
- sdb_reader/presets.toml +23 -0
- sdb_reader/py.typed +0 -0
- sdb_reader/read_record.schema.json +792 -0
- sdb_reader/reading.py +94 -0
- sdb_reader/scan.py +265 -0
- sdb_reader/schema.py +96 -0
- sdb_reader/serialise.py +463 -0
- sdb_reader/show.py +370 -0
- sdb_reader/statements.py +1011 -0
- sdb_reader/verification.py +322 -0
- sdb_reader/versions.py +31 -0
- sdb_reader-0.9.0.dist-info/METADATA +117 -0
- sdb_reader-0.9.0.dist-info/RECORD +37 -0
- sdb_reader-0.9.0.dist-info/WHEEL +5 -0
- sdb_reader-0.9.0.dist-info/entry_points.txt +2 -0
- sdb_reader-0.9.0.dist-info/licenses/LICENSE +661 -0
- sdb_reader-0.9.0.dist-info/licenses/NOTICE +40 -0
- sdb_reader-0.9.0.dist-info/top_level.txt +1 -0
sdb_reader/parser.py
ADDED
|
@@ -0,0 +1,4601 @@
|
|
|
1
|
+
"""Parse normalized SDS text into structured data."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
from collections import Counter
|
|
7
|
+
from collections.abc import Iterable, Sequence
|
|
8
|
+
from dataclasses import dataclass, field, replace
|
|
9
|
+
from datetime import date
|
|
10
|
+
from itertools import pairwise
|
|
11
|
+
|
|
12
|
+
import structlog
|
|
13
|
+
|
|
14
|
+
from .extraction import ExtractionResult
|
|
15
|
+
from .line_breaks import join_broken_words
|
|
16
|
+
from .messages import Message
|
|
17
|
+
from .pictograms import get_signal_word, pictograms_for, suppress_pictograms
|
|
18
|
+
from .schema import (
|
|
19
|
+
ContactInfo,
|
|
20
|
+
HazardStatements,
|
|
21
|
+
SafetyDataSheet,
|
|
22
|
+
Section,
|
|
23
|
+
StatedClassification,
|
|
24
|
+
)
|
|
25
|
+
from .statements import (
|
|
26
|
+
OLD_REQUEST_SENTENCE,
|
|
27
|
+
codes_from_wording,
|
|
28
|
+
euh_statements_by_wording,
|
|
29
|
+
holds_a_wording_of,
|
|
30
|
+
open_end,
|
|
31
|
+
wording_bounds,
|
|
32
|
+
wordings_apart,
|
|
33
|
+
wordings_in_print_order,
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
logger = structlog.stdlib.get_logger(__name__)
|
|
37
|
+
|
|
38
|
+
# Three of the parser's messages are what compliance.py judges by: a label
|
|
39
|
+
# stating that nobody determined the classification, an EUH wording standing
|
|
40
|
+
# outside the label (SDB-28), and a section 2 that classifies by the rules
|
|
41
|
+
# before CLP (SDB-73).
|
|
42
|
+
LABEL_UNDETERMINED = "label-undetermined"
|
|
43
|
+
EUH_WORDING_OUTSIDE_LABEL = "euh-wording-outside-label"
|
|
44
|
+
CLASSIFIED_BEFORE_CLP = "classified-before-clp"
|
|
45
|
+
|
|
46
|
+
SECTION_BY_NUMBER: dict[int, Section] = {
|
|
47
|
+
1: Section.IDENTIFICATION,
|
|
48
|
+
2: Section.HAZARDS,
|
|
49
|
+
3: Section.COMPOSITION,
|
|
50
|
+
4: Section.FIRST_AID,
|
|
51
|
+
5: Section.FIRE_FIGHTING,
|
|
52
|
+
6: Section.ACCIDENTAL_RELEASE,
|
|
53
|
+
7: Section.HANDLING_STORAGE,
|
|
54
|
+
8: Section.EXPOSURE_PPE,
|
|
55
|
+
9: Section.PHYSICAL_CHEMICAL,
|
|
56
|
+
10: Section.STABILITY_REACTIVITY,
|
|
57
|
+
11: Section.TOXICOLOGY,
|
|
58
|
+
12: Section.ECOLOGY,
|
|
59
|
+
13: Section.DISPOSAL,
|
|
60
|
+
14: Section.TRANSPORT,
|
|
61
|
+
15: Section.REGULATORY,
|
|
62
|
+
16: Section.OTHER,
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
# Pattern handles OCR spacing issues within "ABSCHNITT" (e.g., "ABSC HNITT 1")
|
|
66
|
+
#
|
|
67
|
+
# "SECTION" is accepted beside it because sheets are not reliably monolingual.
|
|
68
|
+
# Four sheets in the corpus head every section "ABSCHNITT n" and section 2
|
|
69
|
+
# alone "SECTION 2: Hazards identification" -- a translation memory that lost
|
|
70
|
+
# one string. LABEL_SECTION_END_PATTERN already reads "SECTION 3" as a boundary,
|
|
71
|
+
# so the two spellings were half-equivalent here already.
|
|
72
|
+
#
|
|
73
|
+
# The leading zero is the same kind of variation: "02 MÖGLICHE GEFAHREN".
|
|
74
|
+
#
|
|
75
|
+
# Anchored at the start of the line, which is what separates a heading from a
|
|
76
|
+
# cross-reference. "Der Wortlaut der angeführten Gefahrenhinweise ist dem
|
|
77
|
+
# Abschnitt 16 zu entnehmen." sits in the middle of section 2 on 152 sheets and
|
|
78
|
+
# used to open section 16 right there, taking the rest of section 2 with it --
|
|
79
|
+
# including 2.2 Kennzeichnungselemente, which is where the label is.
|
|
80
|
+
#
|
|
81
|
+
# What may precede the word is any short run of non-word characters. Counted
|
|
82
|
+
# over the corpus, a heading is preceded by nothing (13629), "!" (161), "("
|
|
83
|
+
# (39) or "* " (20); a cross-reference is preceded by a word -- "Siehe " (660),
|
|
84
|
+
# "siehe " (42), "in ", "unter ". So the rule is punctuation yes, letters no.
|
|
85
|
+
# Enumerating the punctuation instead was tried and cost 96 sheets their
|
|
86
|
+
# section 1, all of them headed "!ABSCHNITT 1:" -- invisible in a label
|
|
87
|
+
# measurement, because find_label_section reads the raw text.
|
|
88
|
+
#
|
|
89
|
+
# A sheet that marks its changed sections prints the mark and the number in
|
|
90
|
+
# front of the word: "* 1 Abschnitt 1: Bezeichnung des Stoffs …".
|
|
91
|
+
SECTION_HEADER_PATTERN = re.compile(
|
|
92
|
+
r"^\W{0,6}(?:\d{1,2}\W{1,3})?"
|
|
93
|
+
r"(?:a\s*b\s*s\s*c\s*h\s*n\s*i\s*t\s*t|s\s*e\s*c\s*t\s*i\s*o\s*n)"
|
|
94
|
+
r"\s+0?(\d{1,2})\b",
|
|
95
|
+
re.IGNORECASE,
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
# The sixteen section titles are fixed by REACH Annex II, but the way suppliers
|
|
99
|
+
# write the heading is not. Most use "ABSCHNITT 2: Mögliche Gefahren", which
|
|
100
|
+
# SECTION_HEADER_PATTERN reads. A sizeable minority number the sections plainly
|
|
101
|
+
# instead -- "2. Mögliche Gefahren", or "2 Mögliche Gefahren" without the dot --
|
|
102
|
+
# and those sheets have no heading this parser could see at all.
|
|
103
|
+
#
|
|
104
|
+
# The consequence was not merely a missing section. With no heading recognised,
|
|
105
|
+
# the first thing that did match was whatever cross-reference in the body prose
|
|
106
|
+
# mentioned an "Abschnitt" ("Persönliche Schutzausrüstung siehe unter Abschnitt
|
|
107
|
+
# 8."), so the rest of the document was filed under that number. Section 2 stayed
|
|
108
|
+
# empty on 81 sheets in the corpus.
|
|
109
|
+
#
|
|
110
|
+
# The number alone cannot identify a heading: numbered lists inside sections 1, 8
|
|
111
|
+
# and 16 are written exactly the same way, and keying on the number alone moved
|
|
112
|
+
# content out of 43 sections that were previously correct. The title has to agree
|
|
113
|
+
# with the number. One distinctive word per section is enough -- suppliers vary
|
|
114
|
+
# the wording around it ("Mögliche Gefahren", "MÖGLICHE GEFAHREN", "Gefahren und
|
|
115
|
+
# Einstufung") but not the word itself.
|
|
116
|
+
#
|
|
117
|
+
# Case is deliberately not a criterion. All-caps headings are as valid as any
|
|
118
|
+
# other, here and in every other heading pattern in this module.
|
|
119
|
+
SECTION_TITLE_PATTERNS: dict[int, re.Pattern[str]] = {
|
|
120
|
+
number: re.compile(keyword, re.IGNORECASE)
|
|
121
|
+
for number, keyword in {
|
|
122
|
+
1: r"bezeichnung|stoff|gemisch",
|
|
123
|
+
2: r"gefahren",
|
|
124
|
+
3: r"zusammensetzung|bestandteil",
|
|
125
|
+
4: r"erste[-\s]?hilfe",
|
|
126
|
+
5: r"brandbek",
|
|
127
|
+
6: r"unbeabsichtigt|freisetzung",
|
|
128
|
+
7: r"handhabung|lagerung",
|
|
129
|
+
8: r"begrenzung|exposition|schutzaus",
|
|
130
|
+
9: r"physikalisch|chemische",
|
|
131
|
+
10: r"stabilit|reaktivit",
|
|
132
|
+
11: r"toxikolog",
|
|
133
|
+
12: r"umweltbezogen|ökolog",
|
|
134
|
+
13: r"entsorgung",
|
|
135
|
+
14: r"transport",
|
|
136
|
+
15: r"rechtsvorschrift|vorschrift",
|
|
137
|
+
16: r"sonstige|weitere",
|
|
138
|
+
}.items()
|
|
139
|
+
}
|
|
140
|
+
# A heading whose number and title landed on separate lines. Two sheets here
|
|
141
|
+
# read "2\nMögliche Gefahren", which no single-line pattern can see. Merging the
|
|
142
|
+
# pair is safe only because the title still has to agree with the number
|
|
143
|
+
# afterwards -- a bare "2" is otherwise one of the commonest lines in a data
|
|
144
|
+
# sheet, sitting in every table.
|
|
145
|
+
BARE_SECTION_NUMBER_PATTERN = re.compile(r"^0?(1[0-6]|[1-9])\s*\.?\s*$")
|
|
146
|
+
# A sheet whose section 2 heading is missing while its subsections are all
|
|
147
|
+
# there: "2.1 Einstufung", "2.2 Kennzeichnungselemente", "2.3 Sonstige
|
|
148
|
+
# Gefahren". Without this the label sits in whatever section preceded it.
|
|
149
|
+
# Only consulted while section 2 has not been seen, so a later cross-reference
|
|
150
|
+
# cannot pull the rest of a document into it.
|
|
151
|
+
#
|
|
152
|
+
# The same holds for sections 1 and 3, whose headings a sheet read by OCR
|
|
153
|
+
# often loses (one supplier prints them white on a dark bar): "1.1
|
|
154
|
+
# Produktidentifikator" opens section 1 and "3.2 Gemische" section 3, which
|
|
155
|
+
# is where section 2 ends.
|
|
156
|
+
SUBSECTION_PATTERN = re.compile(
|
|
157
|
+
r"^[-·•\s]*([123])\s*\.\s*([1-4])\b\.?\s*(.{3,79}?)\s*$"
|
|
158
|
+
)
|
|
159
|
+
SUBSECTION_TITLES: dict[int, re.Pattern[str]] = {
|
|
160
|
+
1: re.compile(
|
|
161
|
+
r"produktidentifikator|identifizierte\s+verwendung|einzelheiten\s+zum\s+lieferanten",
|
|
162
|
+
re.IGNORECASE,
|
|
163
|
+
),
|
|
164
|
+
2: re.compile(r"einstufung|kennzeichnung|sonstige\s+gefahren", re.IGNORECASE),
|
|
165
|
+
3: re.compile(r"^(?:stoffe|gemische)\b", re.IGNORECASE),
|
|
166
|
+
}
|
|
167
|
+
# The dot after the number is optional: "3 Zusammensetzung" is as common as
|
|
168
|
+
# "3. Zusammensetzung", and both are headings. Section numbers stop at 16.
|
|
169
|
+
# A mark for a changed section may stand in front (SDB-152): "* 1 Bezeichnung
|
|
170
|
+
# des …", "! 2. Mögliche Gefahren".
|
|
171
|
+
NUMBERED_SECTION_PATTERN = re.compile(
|
|
172
|
+
r"^(?:[*!]\s*)?0?(1[0-6]|[1-9])\s*\.?\s+(\S.{3,79}?)\s*$"
|
|
173
|
+
)
|
|
174
|
+
# Suppliers state the document's own version several ways: "Version 8.2",
|
|
175
|
+
# "Version: 1.1", "Versionsnummer 1", "Nummer der Fassung: 1.0" and
|
|
176
|
+
# "Ausgabenr. : 15.01" all appear in the corpus, so the label is matched
|
|
177
|
+
# loosely and the separator may be a colon. Digits must follow immediately:
|
|
178
|
+
# that is what keeps "Fassung: GHS 1.0" (the GHS revision, not this document's
|
|
179
|
+
# version) and bare prose like "Fassung." out.
|
|
180
|
+
# A leading "V" and dash separators are accepted too, for "Versionsnummer:
|
|
181
|
+
# V01-02" -- the same supplier writes "2.01" elsewhere, so V01-02 is 01.02.
|
|
182
|
+
VERSION_PATTERN = re.compile(
|
|
183
|
+
r"\b(?:Version(?:snummer)?|Nummer\s+der\s+Fassung|Fassung|Ausgabenr\.?)"
|
|
184
|
+
r"\s*:?\s*V?(\d+(?:[-.,]\d+)*)",
|
|
185
|
+
re.IGNORECASE | re.MULTILINE,
|
|
186
|
+
)
|
|
187
|
+
# Sheets name the version they supersede right next to their own, as in
|
|
188
|
+
# "Versionsnummer: 2.01 (ersetzt Version 1.03)". Both then repeat once per page,
|
|
189
|
+
# so counting occurrences alone cannot separate them.
|
|
190
|
+
# Word boundaries matter here: without the leading \b, the "alte" branch fires
|
|
191
|
+
# inside "enthalten" and discards a perfectly good version.
|
|
192
|
+
SUPERSEDED_MARKER = re.compile(
|
|
193
|
+
r"\b(?:ersetzt|ersetzen|vorherig\w*|vorgänger\w*|alte)\b\s*(?:\w+\s+)?$",
|
|
194
|
+
re.IGNORECASE,
|
|
195
|
+
)
|
|
196
|
+
_DATE_AS_VERSION = re.compile(r"\d{1,2}[-.,]\d{1,2}[-.,](?:19|20)\d{2}")
|
|
197
|
+
# "Überarbeitet am / Version: 31.05.2012 / 0007": one label for two values,
|
|
198
|
+
# the day and behind it the number. The line below it, "Ersetzt Fassung vom /
|
|
199
|
+
# Version: 19.01.2011 / 0006", is built alike and names the version before.
|
|
200
|
+
VERSION_BEHIND_DATE_PATTERN = re.compile(
|
|
201
|
+
r"^[^\S\n]*Überarbeitet[^\S\n]+am[^\S\n]*/[^\S\n]*Version[^\S\n]*:[^\S\n]*"
|
|
202
|
+
r"\d{1,2}\.\d{1,2}\.\d{4}[^\S\n]*/[^\S\n]*V?(\d+(?:[-.,]\d+)*)",
|
|
203
|
+
re.IGNORECASE | re.MULTILINE,
|
|
204
|
+
)
|
|
205
|
+
# Fallback used only when no version label is present: some sheets number their
|
|
206
|
+
# revisions instead, as "Revisionsnummer 1", "Revisions-Nr.: 1,2", "Rev. Nr. 4"
|
|
207
|
+
# or plain "REV. 1". The "Nr." branch is tried before the bare one so
|
|
208
|
+
# "Rev. Nr. 4" yields 4 rather than failing on the "Nr".
|
|
209
|
+
REVISION_NUMBER_PATTERN = re.compile(
|
|
210
|
+
r"\b(?:Revisions?nummer|Rev(?:isions?)?\s*[-.]?\s*Nr\.?|Revision(?=[^\S\n]*:)|Rev\.?)"
|
|
211
|
+
r"\s*:?\s*V?(\d+(?:[-.,]\d+)*)",
|
|
212
|
+
re.IGNORECASE | re.MULTILINE,
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
# "23-Nov-2023" is day-first like "23.11.2023", just written with dashes and a
|
|
216
|
+
# month name -- not a month-first US date, so both forms parse the same way.
|
|
217
|
+
# Keyed by the first three letters, so both the abbreviation and the spelled-out
|
|
218
|
+
# month resolve: "Nov"/"November", "März"/"Mrz", "Juli"/"Jul".
|
|
219
|
+
_MONTH_NAMES: dict[str, int] = {
|
|
220
|
+
"jan": 1,
|
|
221
|
+
"feb": 2,
|
|
222
|
+
"mar": 3,
|
|
223
|
+
"mär": 3,
|
|
224
|
+
"mae": 3,
|
|
225
|
+
"mrz": 3,
|
|
226
|
+
"apr": 4,
|
|
227
|
+
"mai": 5,
|
|
228
|
+
"may": 5,
|
|
229
|
+
"jun": 6,
|
|
230
|
+
"jul": 7,
|
|
231
|
+
"aug": 8,
|
|
232
|
+
"sep": 9,
|
|
233
|
+
"okt": 10,
|
|
234
|
+
"oct": 10,
|
|
235
|
+
"nov": 11,
|
|
236
|
+
"dez": 12,
|
|
237
|
+
"dec": 12,
|
|
238
|
+
}
|
|
239
|
+
# The separators may stand between spaces: "15 / 6 / 2010" (SDB-91).
|
|
240
|
+
_DATE_SEPARATOR = r"(?:[^\S\n]?[.\-/]\s*|\s)"
|
|
241
|
+
_DATE_FRAGMENT = (
|
|
242
|
+
rf"(\d{{1,2}}){_DATE_SEPARATOR}([A-Za-zÄÖÜäöüß]{{3,9}}|\d{{1,2}})"
|
|
243
|
+
rf"{_DATE_SEPARATOR}(\d{{4}})"
|
|
244
|
+
)
|
|
245
|
+
# "Überarbeitet am: 2024-08-26": the same date, year first. Only behind a label,
|
|
246
|
+
# and only with its dashes: a CAS number or an article number is built alike.
|
|
247
|
+
_DATED = rf"(?:{_DATE_FRAGMENT}|(?<!\d)(\d{{4}})-(\d{{2}})-(\d{{2}})(?!\d))"
|
|
248
|
+
# A page marker may sit between a date's label and its value. Suppliers put both
|
|
249
|
+
# in the page footer, and flattening the layout interleaves them:
|
|
250
|
+
#
|
|
251
|
+
# Ausgabedatum/
|
|
252
|
+
# Überarbeitungsdatum
|
|
253
|
+
# :
|
|
254
|
+
# Seite: 1/13
|
|
255
|
+
# 10-2-2021
|
|
256
|
+
#
|
|
257
|
+
# Only a page marker is tolerated, nothing else. Allowing any 45 characters of
|
|
258
|
+
# filler instead was measured on the corpus: it bound labels to unrelated
|
|
259
|
+
# dates and moved five sheets from 2013 to 2015.
|
|
260
|
+
_PAGE_MARKER = r"(?:\s*Seite\s*:?\s*\d{1,3}\s*(?:/|von)\s*\d{1,3})?"
|
|
261
|
+
|
|
262
|
+
# Revision date: "Überarbeitet am: 16.11.2020", "Revisionsdatum 02.03.2021",
|
|
263
|
+
# "Änderungsdatum: 11.11.2020", "Version vom: 04-Jan-2024", "REV. 1 vom
|
|
264
|
+
# 14.11.2019". A bare "vom" is too common to anchor on, so it only counts when
|
|
265
|
+
# a revision or version number precedes it.
|
|
266
|
+
#
|
|
267
|
+
# "Bearbeitungsdatum", "Fassung vom" and "Stand:" are the same statement in
|
|
268
|
+
# other suppliers' words, and together they carry 75 of the sheets this parser
|
|
269
|
+
# used to leave undated. "Stand" needs its colon: on its own the word is far too
|
|
270
|
+
# common in German prose to anchor a date on.
|
|
271
|
+
#
|
|
272
|
+
# "Druckdatum" is deliberately absent. It records when the PDF was rendered, not
|
|
273
|
+
# when the document was revised, and the two can be years apart -- one sheet here
|
|
274
|
+
# reads "überarbeitet am: 30.04.2012 ... Druckdatum: 13.07.2018".
|
|
275
|
+
# The optional second word absorbs a break inside the label itself: one sheet
|
|
276
|
+
# here reads "Überarbeitungsd\natum:\n01.10.2021", and the layout reader joins
|
|
277
|
+
# the two lines of the label to "Überarbeitungsd atum: 01.10.2021". It matches
|
|
278
|
+
# letters only -- an earlier draft wrote \w, which also matches digits, so it ate
|
|
279
|
+
# the first digit of the day and turned 25.08.2020 into 05.08.2020 on 37 sheets.
|
|
280
|
+
# Lower-case letters only, and only before the label's colon: a word that
|
|
281
|
+
# merely follows ("Überarbeitet Druckdatum: 13.07.2018", "Überarbeitungsdatum
|
|
282
|
+
# ersetzt 12.03.2019") is another label's, not the rest of this one.
|
|
283
|
+
#
|
|
284
|
+
# A version carries its date behind it, in brackets or behind a dash: "Nummer
|
|
285
|
+
# der Fassung: GHS 8.0 (10.06.2024)", "Version 2.2 (18/07/2017)", "Revision :
|
|
286
|
+
# Nr. 2 (22/11/2019)", "Rev. 14 – 19/12/2024". The version a sheet replaces
|
|
287
|
+
# does too ("Ersetzt Fassung: GHS 7 (29.01.2024)"), and that date is not this
|
|
288
|
+
# sheet's.
|
|
289
|
+
REVISION_DATE_PATTERN = re.compile(
|
|
290
|
+
r"(?:überarbeit[a-zäöüß]*(?:\s+(?-i:[a-zäöüß]{2,})(?=\s*:))?(?:\s+am)?"
|
|
291
|
+
r"|revisions?datum|änderungsdatum|bearbeitungsdatum|überprüfung"
|
|
292
|
+
r"|(?<!ersetzt )fassung\s+vom|stand\s*:|sicherheitsdatenblatt\s+vom"
|
|
293
|
+
r"|(?:version|rev\.?)\s*(?:nr\.?)?\s*:?\s*\d+(?:[.,]\d+)*\s*vom"
|
|
294
|
+
r"|(?<!ersetzt )(?:version|fassung|revision|rev\.?)\s*:?\s*(?:nr\.?\s*)?(?:GHS\s*)?"
|
|
295
|
+
r"\d+(?:[.,]\d+)*\.?\s*[(–—-]"
|
|
296
|
+
# "Durchsicht Nr. 3" / "vom 11/10/2023" (SDB-102).
|
|
297
|
+
r"|durchsicht\s+nr\.?\s*\d+\s+vom"
|
|
298
|
+
r"|version\s+vom)"
|
|
299
|
+
rf"\s*:?{_PAGE_MARKER}\s*{_DATED}",
|
|
300
|
+
re.IGNORECASE | re.MULTILINE,
|
|
301
|
+
)
|
|
302
|
+
# "20/09/2021 (Version: 9.4)": the date first, its version behind it. Also
|
|
303
|
+
# behind a comma, "20/11/2012, version 2." (SDB-98).
|
|
304
|
+
REVISION_DATE_BEFORE_VERSION_PATTERN = re.compile(
|
|
305
|
+
rf"{_DATED}(?:\s*\(Version\s*:|,[^\S\n]*Version[^\S\n]+\d)", re.IGNORECASE
|
|
306
|
+
)
|
|
307
|
+
# "23.04.2024 (Überarbeitungsdatum)": the date first, its label behind it in
|
|
308
|
+
# brackets, in the footer of every page (SDB-97).
|
|
309
|
+
REVISION_DATE_BEFORE_LABEL_PATTERN = re.compile(
|
|
310
|
+
rf"{_DATED}[^\S\n]*\((?:Überarbeitungsdatum|Revisionsdatum|überarbeitet\s+am)\)",
|
|
311
|
+
re.IGNORECASE,
|
|
312
|
+
)
|
|
313
|
+
# A version's date is this sheet's only where the version is: "ersetzt Version
|
|
314
|
+
# 003 vom 12.03.2019", "(ungültig wird Version 1.3 vom 04.03.2022)", "Geänderte
|
|
315
|
+
# Daten in der Version 11 gegenüber der Version 10 vom 06.03.2023", "Ersetzt die
|
|
316
|
+
# vorherige Version vom 27.01.2017" and "Vorversion vom 31.05.2019" name the
|
|
317
|
+
# version before it (SDB-57). Mostly that cost nothing, the sheet's own date
|
|
318
|
+
# being the newer one; it decided where the sheet's own date was not read (a
|
|
319
|
+
# two-digit year, a date of issue only) or was the older of the two.
|
|
320
|
+
_OTHER_VERSION_BEFORE = re.compile(
|
|
321
|
+
r"(?:\b(?:ersetz\w*|ersatz[^\S\n]+für|ungültig[^\S\n]+wird|gegenüber)"
|
|
322
|
+
r"\s*:?\s*(?:(?:die|der|das|den)\s+)?(?:\w+e[nrs]?\s+)?|\bvor)$",
|
|
323
|
+
re.IGNORECASE,
|
|
324
|
+
)
|
|
325
|
+
_VERSION_LABEL_START = re.compile(r"version|fassung|revision|rev\b", re.IGNORECASE)
|
|
326
|
+
# The day the version in hand was made is the day of its revision (SDB-128):
|
|
327
|
+
# "Aktuelle Version: 2.0.1, erstellt am: 20.03.2020", or the version on one
|
|
328
|
+
# line and "Erstellt am 30.03.2023" on the next. The line begins with the
|
|
329
|
+
# version, so "Ersetzte Version: 2.0.0, erstellt am: 04.06.2019" below it is
|
|
330
|
+
# not one.
|
|
331
|
+
VERSION_CREATED_PATTERN = re.compile(
|
|
332
|
+
r"^[^\S\n]*(?:Aktuelle[^\S\n]+)?Version[^\S\n]*:?[^\S\n]*V?\d[^\n]*?"
|
|
333
|
+
r"(?:,[^\S\n]*|[^\S\n]+|[^\S\n]*\|?[^\S\n]*\n[^\S\n]*)"
|
|
334
|
+
rf"erstellt[^\S\n]+am[^\S\n]*:?[^\S\n]*{_DATED}",
|
|
335
|
+
re.IGNORECASE | re.MULTILINE,
|
|
336
|
+
)
|
|
337
|
+
# The same with the day above its version: "Erstellt am 24.07.2013" /
|
|
338
|
+
# "Version 1.2".
|
|
339
|
+
VERSION_CREATED_ABOVE_PATTERN = re.compile(
|
|
340
|
+
rf"^[^\S\n]*erstellt[^\S\n]+am[^\S\n]*:?[^\S\n]*{_DATED}[^\S\n]*\n"
|
|
341
|
+
r"[^\S\n]*Version[^\S\n]*:?[^\S\n]*V?\d",
|
|
342
|
+
re.IGNORECASE | re.MULTILINE,
|
|
343
|
+
)
|
|
344
|
+
# "überarbeitet am: 30.04.2012 + 10.06.2016": revised twice, both days behind
|
|
345
|
+
# the one label (SDB-129).
|
|
346
|
+
_FURTHER_DATE = re.compile(rf"[^\S\n]*(?:\+|und)[^\S\n]*{_DATED}", re.IGNORECASE)
|
|
347
|
+
# SAP prints an older header as a grid of labels over a row of values, and the
|
|
348
|
+
# reader gives the labels on one line: "Überarbeitet am: Druckdatum Datum der
|
|
349
|
+
# letzten Ausgabe: 13.06.2017", then "3.4", "27.02.2018", "28.11.2018". The
|
|
350
|
+
# first date below is the revision's, the second the day it was printed.
|
|
351
|
+
REVISION_DATE_GRID_PATTERN = re.compile(
|
|
352
|
+
r"überarbeitet\s+am:[^\S\n]*Druckdatum[^\n]*\n"
|
|
353
|
+
rf"(?:\s*\d+(?:[.,]\d+)*[^\S\n]*\n)?\s*{_DATED}",
|
|
354
|
+
re.IGNORECASE,
|
|
355
|
+
)
|
|
356
|
+
# Issue date: "Ausstellungsdatum: 11.07.2013", "Ausgabedatum: 17.08.2021",
|
|
357
|
+
# "Datum der Erstellung: 29.05.2020".
|
|
358
|
+
# "Ausgabe:" counts only at the start of its line: "Datum der letzten Ausgabe:"
|
|
359
|
+
# names the sheet before this one.
|
|
360
|
+
ISSUE_DATE_PATTERN = re.compile(
|
|
361
|
+
r"(?:ausstellungsdatum|ausgabedatum|erstellungsdatum|erstelldatum"
|
|
362
|
+
r"|erstellt(?:\s+am|\s*:)"
|
|
363
|
+
# "Erste Fassung: 15.12.2025" (SDB-93).
|
|
364
|
+
r"|erste\s+fassung|erstfassung"
|
|
365
|
+
r"|datum\s+der\s+erstellung|datum\s+ausgestellt|(?<![a-zäöüß])erstellung"
|
|
366
|
+
# "Datum der Ausstellung: 5/05/2022" (SDB-96), "Ausgestellt am" (SDB-101).
|
|
367
|
+
r"|datum\s+der\s+ausstellung|ausgestellt\s+am"
|
|
368
|
+
r"|^[^\S\n]*ausgabe)"
|
|
369
|
+
rf"\s*:?{_PAGE_MARKER}\s*{_DATED}",
|
|
370
|
+
re.IGNORECASE | re.MULTILINE,
|
|
371
|
+
)
|
|
372
|
+
# "Tritt in Kraft ab: 23.05.2022", "gültig ab: 18.07.2022". Neither is a
|
|
373
|
+
# revision date -- a sheet is revised on one day and takes effect on another,
|
|
374
|
+
# and one document here says 30/09/2021 and 01/10/2021 in that order. Ranking
|
|
375
|
+
# revisions on the wrong one of those would pick the wrong sheet.
|
|
376
|
+
#
|
|
377
|
+
# So this is consulted last, only for a document that states neither when it was
|
|
378
|
+
# revised nor when it was issued. For those 35 sheets it is the difference
|
|
379
|
+
# between a known date and none at all, and "in force since" is a sound floor
|
|
380
|
+
# for how old the document is.
|
|
381
|
+
EFFECTIVE_DATE_PATTERN = re.compile(
|
|
382
|
+
r"(?:tritt\s+in\s+kraft(?:\s+ab)?|gültig\s+ab)"
|
|
383
|
+
rf"\s*:?{_PAGE_MARKER}\s*{_DATED}",
|
|
384
|
+
re.IGNORECASE | re.MULTILINE,
|
|
385
|
+
)
|
|
386
|
+
# "Datum: 16.05.2024" in a page's footer, "Stand 13.12.2014" under the title.
|
|
387
|
+
# Neither says what the date is of, so it stands last, behind every date that
|
|
388
|
+
# does, and only at the start of its line: "TRGS 900, Stand 29.01.2018" dates
|
|
389
|
+
# a list of limit values.
|
|
390
|
+
PLAIN_DATE_PATTERN = re.compile(
|
|
391
|
+
rf"^[^\S\n]*(?:datum[^\S\n]*:|stand)[^\S\n]*{_DATED}",
|
|
392
|
+
re.IGNORECASE | re.MULTILINE,
|
|
393
|
+
)
|
|
394
|
+
# "Überarbeitung 29.06.11". A two-digit year is ambiguous by construction, and
|
|
395
|
+
# widening _DATE_FRAGMENT to accept one everywhere would let version numbers and
|
|
396
|
+
# article numbers read as dates across the whole catalogue.
|
|
397
|
+
#
|
|
398
|
+
# So it gets its own pattern and the same standing as the effective date: last,
|
|
399
|
+
# and only for a document that has yielded nothing else. A guess that cannot
|
|
400
|
+
# displace a certainty costs nothing to try.
|
|
401
|
+
#
|
|
402
|
+
# (?!\d) is what keeps it off the front of a four-digit year -- without it,
|
|
403
|
+
# "29.06.2011" reads as the 29th of June 20.
|
|
404
|
+
#
|
|
405
|
+
# The century boundary is fixed at 90 rather than pegged to the current year, so
|
|
406
|
+
# the same document reads the same way next year: 90-99 is the 1990s, everything
|
|
407
|
+
# below is the 2000s. The oldest sheet in the corpus is from 1994.
|
|
408
|
+
_SHORT_YEAR_FRAGMENT = r"(\d{1,2})[.\-/]\s*(\d{1,2})[.\-/]\s*(\d{2})(?!\d)"
|
|
409
|
+
SHORT_YEAR_DATE_PATTERN = re.compile(
|
|
410
|
+
r"(?:überarbeit[a-zäöüß]*(?:\s+(?-i:[a-zäöüß]{2,})(?=\s*:))?(?:\s+am)?"
|
|
411
|
+
r"|revisions?datum|änderungsdatum|bearbeitungsdatum|fassung\s+vom|stand\s*:"
|
|
412
|
+
r"|ausstellungsdatum|ausgabedatum|erstellungsdatum|erstellt\s+am"
|
|
413
|
+
r"|ausgestellt\s+am)"
|
|
414
|
+
rf"\s*:?{_PAGE_MARKER}\s*{_SHORT_YEAR_FRAGMENT}",
|
|
415
|
+
re.IGNORECASE | re.MULTILINE,
|
|
416
|
+
)
|
|
417
|
+
# Section 2.2 of an SDS carries the label: exactly the statements that appear on
|
|
418
|
+
# the product. Section 2.1 above it carries the classification, which lists more
|
|
419
|
+
# -- typically H400 next to H410, where only H410 is labelled. Reading the whole
|
|
420
|
+
# hazards section conflates the two, so the block is located on its own.
|
|
421
|
+
LABEL_SECTION_START_PATTERN = re.compile(
|
|
422
|
+
r"2\s*\.\s*2\s*\.?\s*(?:Kennzeichnungselemente|Label\s+elements)",
|
|
423
|
+
re.IGNORECASE,
|
|
424
|
+
)
|
|
425
|
+
# The same heading without its number, for sheets written before the REACH
|
|
426
|
+
# restructuring: they number their sections 1 to 17 and put the label at
|
|
427
|
+
# "4. Kennzeichnungselemente".
|
|
428
|
+
#
|
|
429
|
+
# Dropping the number is safe on its own -- the heading is the first occurrence
|
|
430
|
+
# of the word, measured over 400 sheets, including the 49 that mention it twice
|
|
431
|
+
# (the second is a back-reference and comes later). It is only used where a real
|
|
432
|
+
# boundary follows; see find_label_section for why.
|
|
433
|
+
LABEL_HEADING_PATTERN = re.compile(
|
|
434
|
+
r"\b(?:Kennzeichnungselemente|Label\s+elements)\b",
|
|
435
|
+
re.IGNORECASE,
|
|
436
|
+
)
|
|
437
|
+
# Only a heading ends it. An unanchored "3.1" also matches inside a date such as
|
|
438
|
+
# "13.10.2024", which truncates the block before the codes it should contain.
|
|
439
|
+
#
|
|
440
|
+
# The last alternative ends the old layout, where the label is section 4 and
|
|
441
|
+
# section 5 follows. It wants a top-level number and a capitalised word, so
|
|
442
|
+
# "2.3." does not match it -- after "2." comes a digit, not a letter.
|
|
443
|
+
# The leading zero is not decoration. 248 sheets in the corpus number their
|
|
444
|
+
# sections "ABSCHNITT 01" through "ABSCHNITT 16", and without the optional zero
|
|
445
|
+
# the boundary is simply not found: the block then runs to LABEL_SECTION_MAX_CHARS
|
|
446
|
+
# and takes the ingredient table with it. Measured, that put thirteen sheets'
|
|
447
|
+
# raw-material classifications on the label -- one of them reporting H301, H310,
|
|
448
|
+
# H314, H330 and nine more from a sheet whose label says "Gefahrenpiktogramme
|
|
449
|
+
# entfällt, Signalwort entfällt".
|
|
450
|
+
#
|
|
451
|
+
# split_sections already reads "03" correctly, because it captures \d{1,2} and
|
|
452
|
+
# converts. Only this pattern spells the number out, and only here did it matter.
|
|
453
|
+
# Section 2.3 by its name, for sheets that number nothing. Some suppliers write
|
|
454
|
+
# the whole document as a bulleted list -- "· Kennzeichnungselemente",
|
|
455
|
+
# "· Sonstige Gefahren" -- and follow it with "3 Zusammensetzung" without a dot,
|
|
456
|
+
# so neither the numeric boundary nor the "ABSCHNITT 3" one matches. The label
|
|
457
|
+
# heading is then found and the block discarded for want of an end, which costs
|
|
458
|
+
# the whole label: three sheets here carry GHS05, GHS07, "Signalwort Gefahr" and
|
|
459
|
+
# H315, H318, H335 in a block nothing could close.
|
|
460
|
+
LABEL_SECTION_END_PATTERN = re.compile(
|
|
461
|
+
r"(?m)^[ \t]*(?:[·•∙][ \t]*)?(?:2\s*\.\s*3\s*\.?\s|ABSCHNITT\s*0?3\b|SECTION\s*0?3\b"
|
|
462
|
+
r"|[·•\-\s]*(?:Sonstige\s+Gefahren|Other\s+hazards)\b"
|
|
463
|
+
r"|\d{1,2}\s*\.\s*[A-ZÄÖÜ][a-zäöüß])",
|
|
464
|
+
re.IGNORECASE,
|
|
465
|
+
)
|
|
466
|
+
# The line a version number follows. Some sheets print "Version" in their
|
|
467
|
+
# running header with the number alone on the next line, and where that number
|
|
468
|
+
# is 2.3 it is the heading of section 2.3 to the pattern above. The header lands
|
|
469
|
+
# between two pages in the text, so a label running across a page break ended
|
|
470
|
+
# there -- before its pictograms, signal word and statements. Nine sheets in
|
|
471
|
+
# the corpus are cut there; see below for what they get back.
|
|
472
|
+
VERSION_KEY_PATTERN = re.compile(r"(?:Version|Versionsnummer)\s*:?", re.IGNORECASE)
|
|
473
|
+
# Cap for documents whose section 2.3 heading never matches, so a failed
|
|
474
|
+
# boundary yields the label block rather than the rest of the sheet.
|
|
475
|
+
LABEL_SECTION_MAX_CHARS = 3500
|
|
476
|
+
|
|
477
|
+
# A classification entry, not a label element: "Skin Corr. 1, H314",
|
|
478
|
+
# "Aquatic Chronic 1, H410", "Carc. 2 - H351". Class, category, code.
|
|
479
|
+
#
|
|
480
|
+
# These belong to section 2.1 and are not what the label says. They reached the
|
|
481
|
+
# label block in the file's order, which flattens a two-column layout into one
|
|
482
|
+
# stream: 69 sheets here. Read in print order (SDB-42) two still have one there,
|
|
483
|
+
# and OCR text keeps its own order (SDB-46). Article 26 is why it matters:
|
|
484
|
+
# a label carrying H410 may omit H400, and one carrying H314 may omit H318, so
|
|
485
|
+
# reading the classification puts statements on a product that its label
|
|
486
|
+
# deliberately leaves off.
|
|
487
|
+
CLASSIFICATION_ENTRY = re.compile(
|
|
488
|
+
r"\b[A-Z][A-Za-z]*\.?(?:\s+[A-Z][A-Za-z]*\.?)*\s+\d[A-F]?\s*[,\-–]\s*H\d{3}(?!\d)"
|
|
489
|
+
)
|
|
490
|
+
|
|
491
|
+
HAZARD_CODE_PATTERN = re.compile(r"\bH\d{3}(?!\d)")
|
|
492
|
+
PRECAUTION_CODE_PATTERN = re.compile(r"\bP\d{3}(?!\d)")
|
|
493
|
+
SUPPLEMENTAL_CODE_PATTERN = re.compile(r"\bEUH\d{3}\b", re.IGNORECASE)
|
|
494
|
+
PICTOGRAM_PATTERN = re.compile(r"\bGHS\d{1,2}\b", re.IGNORECASE)
|
|
495
|
+
# Two bugs kept this from ever matching German text: no IGNORECASE against a
|
|
496
|
+
# lowercase "signal", and a capture that took whatever word came next. Every
|
|
497
|
+
# signal word of the stored readings therefore came from the H-code derivation, not from
|
|
498
|
+
# the sheet -- including the two the derivation had wrong.
|
|
499
|
+
#
|
|
500
|
+
# The trailing \b is what stops "Gefahr" from matching inside
|
|
501
|
+
# "Gefahrenhinweise", which is the row label sitting directly above the value in
|
|
502
|
+
# the two-column layouts these sheets use.
|
|
503
|
+
# Case-insensitive because 140 sheets in the corpus set the value in caps --
|
|
504
|
+
# 97 "GEFAHR", 43 "ACHTUNG" -- and a case-sensitive read sent every one of them
|
|
505
|
+
# to the derivation instead. The \b anchors keep that safe: "Gefahrenhinweise"
|
|
506
|
+
# still fails the trailing boundary whatever its case.
|
|
507
|
+
# The lookahead is what keeps EUH211 and EUH212 out. Both phrases are fixed by
|
|
508
|
+
# the regulation and both open with the word itself -- "Achtung! Beim Spruehen
|
|
509
|
+
# koennen gefaehrliche lungengaengige Troepfchen entstehen." -- so a bare word
|
|
510
|
+
# search read 53 sheets' supplemental phrase as their signal word. CLP prints
|
|
511
|
+
# the signal word without punctuation, which makes the "!" diagnostic: it never
|
|
512
|
+
# belongs to the label element, only to the sentence that quotes it.
|
|
513
|
+
SIGNAL_WORD_PATTERN = re.compile(r"\b(Gefahr|Achtung)\b(?!\s*!)", re.IGNORECASE)
|
|
514
|
+
|
|
515
|
+
# How a sheet classifies under Directive 67/548/EEC or 1999/45/EC, the rules
|
|
516
|
+
# before CLP (SDB-73): an R phrase with its number, or a hazard symbol with its
|
|
517
|
+
# designation. A sheet that only names the directives, or prints "R-Sätze:
|
|
518
|
+
# keine", states no hazard and is not meant.
|
|
519
|
+
#
|
|
520
|
+
# A combination is unmistakable: "R20/21/22", "R 36/38".
|
|
521
|
+
_R_COMBINATION = re.compile(r"(?<![\w/])R[^\S\n]?\d{1,2}(?:/\d{1,2})+(?![\w/])")
|
|
522
|
+
# A single number is not -- "ABCDE R 1" is a product, "R134a" a refrigerant --
|
|
523
|
+
# so it counts only with two digits and where section 2 names the old rules.
|
|
524
|
+
_R_SINGLE = re.compile(r"(?<![\w/])R[^\S\n]?\d{2}(?![\w/.,])")
|
|
525
|
+
_OLD_RULES_NAMED = re.compile(
|
|
526
|
+
r"67/548|(?:19)?99/45|\bR[^\S\n]?-?[^\S\n]?S[äa]tze", re.IGNORECASE
|
|
527
|
+
)
|
|
528
|
+
# Or each on a line of its own, its sentence below: "R11" / "Leichtentzündlich" /
|
|
529
|
+
# "R19" / "Kann explosionsfähige Peroxide bilden". One such line may be part of
|
|
530
|
+
# a product's name, two are not.
|
|
531
|
+
_R_ALONE = re.compile(r"(?m)^[^\S\n]*R[^\S\n]?\d{2}[^\S\n]*$")
|
|
532
|
+
# Under the heading the numbers stand alone: "R-Sätze:" / "11" / "Leichtentzündlich."
|
|
533
|
+
_R_PHRASES_LISTED = re.compile(
|
|
534
|
+
r"\bR[^\S\n]?-?[^\S\n]?S[äa]tze[^\S\n]*:*\s*\d{1,2}(?:/\d{1,2})*(?![\w.,])",
|
|
535
|
+
re.IGNORECASE,
|
|
536
|
+
)
|
|
537
|
+
# "Xn; Gesundheitsschädlich", "Xi - Reizend", "F Leichtentzündlich".
|
|
538
|
+
_HAZARD_SYMBOL = re.compile(
|
|
539
|
+
r"(?<![\w/])(?:Xn|Xi|T\+?|F\+?|C|N|O|E)[^\S\n]*[;,:\u2013\u2014-]?[^\S\n]*"
|
|
540
|
+
r"(?:Gesundheitsschädlich|Reizend|Sensibilisierend|Sehr[^\S\n]+giftig|Giftig"
|
|
541
|
+
r"|Leichtentzündlich|Hochentzündlich|Ätzend|Umweltgefährlich|Brandfördernd"
|
|
542
|
+
r"|Explosionsgefährlich)\b"
|
|
543
|
+
)
|
|
544
|
+
# Or the designation alone, as a sentence of its own: "Hochentzündlich. Reizend.",
|
|
545
|
+
# "Für die Umwelt :" / "Umweltgefährlich" (SDB-120).
|
|
546
|
+
_HAZARD_DESIGNATION = re.compile(
|
|
547
|
+
r"(?m)(?:^|[.:;][^\S\n]+)(?:Hochentzündlich|Leichtentzündlich|Umweltgefährlich"
|
|
548
|
+
r"|Gesundheitsschädlich|Reizend|Ätzend|Brandfördernd|Sehr[^\S\n]+giftig|Giftig)"
|
|
549
|
+
r"[^\S\n]*(?:\.|$)"
|
|
550
|
+
)
|
|
551
|
+
# A sheet of the years between the two systems prints both; with a CLP code in
|
|
552
|
+
# section 2 it has a label to read: "R 10 Entzündlich." beside "H 226 …".
|
|
553
|
+
_CLP_CODE_PRINTED = re.compile(r"\b(?:[HP][^\S\n]?\d{3}|EUH[^\S\n]?\d{3})\b")
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+
def old_classification(hazards_section: str) -> str | None:
|
|
557
|
+
"""What section 2 classifies with under the rules before CLP, else None (SDB-73)."""
|
|
558
|
+
if _CLP_CODE_PRINTED.search(hazards_section):
|
|
559
|
+
return None
|
|
560
|
+
patterns = [_R_COMBINATION, _R_PHRASES_LISTED, _HAZARD_SYMBOL]
|
|
561
|
+
if _OLD_RULES_NAMED.search(hazards_section):
|
|
562
|
+
patterns.append(_R_SINGLE)
|
|
563
|
+
for pattern in patterns:
|
|
564
|
+
match = pattern.search(hazards_section)
|
|
565
|
+
if match:
|
|
566
|
+
return " ".join(match.group(0).split())
|
|
567
|
+
alone = _R_ALONE.findall(hazards_section)
|
|
568
|
+
if len({" ".join(code.split()) for code in alone}) >= 2:
|
|
569
|
+
return " ".join(alone[0].split())
|
|
570
|
+
designation = _HAZARD_DESIGNATION.search(hazards_section)
|
|
571
|
+
if designation:
|
|
572
|
+
return designation.group(0).strip(" .:;\n")
|
|
573
|
+
return None
|
|
574
|
+
|
|
575
|
+
|
|
576
|
+
# What section 2 says about the classification (SDB-75). REACH Article 31(1)
|
|
577
|
+
# asks whether the product meets the criteria for classification, and this is
|
|
578
|
+
# where a sheet answers it; the label only follows from it. The part read runs
|
|
579
|
+
# from the heading "Einstufung des Stoffs oder Gemischs" -- not from its number,
|
|
580
|
+
# which sheets leave out or write "2,1" -- to the label's heading.
|
|
581
|
+
_CLASSIFICATION_HEADING = re.compile(
|
|
582
|
+
r"Einstufung\s+des\s+Stoff(?:e?s)?\s+oder\s+(?:des\s+)?Gemische?s?"
|
|
583
|
+
r"|Classification\s+of\s+the\s+substance\s+or\s+mixture",
|
|
584
|
+
re.IGNORECASE,
|
|
585
|
+
)
|
|
586
|
+
# A hazard class is told by its H code or by the abbreviation CLP gives it with
|
|
587
|
+
# its category ("Skin Corr. 1B", "Aquatic Chronic 3"), which some sheets print
|
|
588
|
+
# without the code -- also in German ("Entz. Fl. 2", "Augenreiz. 2").
|
|
589
|
+
_HAZARD_CLASS_CODE = re.compile(r"\bH[2-4]\d{2}[A-Za-z]{0,2}\b")
|
|
590
|
+
_HAZARD_CLASS_NAME = re.compile(
|
|
591
|
+
r"\b(?:Expl\.|Flam\.\s?(?:Gas|Liq\.|Sol\.)|Aerosol|Ox\.\s?(?:Gas|Liq\.|Sol\.)"
|
|
592
|
+
r"|Press\.\s?Gas|Self-react\.|Pyr\.\s?(?:Liq\.|Sol\.)|Self-heat\.|Water-react\."
|
|
593
|
+
r"|Org\.\s?Perox\.|Met\.\s?Corr\.|Acute\s?Tox\.|Skin\s?Corr\.|Skin\s?Irrit\."
|
|
594
|
+
r"|Eye\s?Dam\.|Eye\s?Irrit\.|Resp\.\s?Sens\.|Skin\s?Sens\.|Muta\.|Carc\.|Repr\."
|
|
595
|
+
r"|STOT\s?[SR]E|Asp\.\s?Tox\.|Aquatic\s?(?:Acute|Chronic)"
|
|
596
|
+
r"|Entz\.\s?Fl\.|Augenreiz\.|Augenschäd\.|Hautreiz\.|Hautätz\.|Akut\s?Tox\.)"
|
|
597
|
+
# "Eye Irrit. 2", and "Eye Irrit. Gefahrenkategorie 2" (SDB-81).
|
|
598
|
+
r"\s?(?:(?:Gefahren)?[Kk]ategorie\s)?[1-4][A-C]?\b"
|
|
599
|
+
)
|
|
600
|
+
# Or the class is written out, as an entry of its own with its category (SDB-80):
|
|
601
|
+
# "Reizwirkung auf die Haut, Kategorie 2", "Korrosiv gegenüber Metallen
|
|
602
|
+
# (Kategorie 1)". The names differ from one supplier's software to the next, the
|
|
603
|
+
# shape does not. The bare word "Kategorie" is not enough: sheets use it of
|
|
604
|
+
# their ingredients too ("enthält Stoffe, die als Akut Toxisch
|
|
605
|
+
# (Gefahrenkategorie 4) eingestuft sind"), which the sentence around it tells.
|
|
606
|
+
_HAZARD_CLASS_IN_WORDS = re.compile(
|
|
607
|
+
r"(?m)(?:^|[:.]\s)[^\S\n]*[A-ZÄÖÜ][^.:\n]{3,70}?[^\S\n]*[,(][^\S\n]*"
|
|
608
|
+
r"(?:Gefahren)?[Kk]ategorie[^\S\n]+[1-4][A-C]?\b"
|
|
609
|
+
# A table sets the category in a cell of its own (SDB-81): "Aerosol" /
|
|
610
|
+
# "Kategorie 1", "Schwere Augenschädigung/-reizung," / "Kategorie 1", and
|
|
611
|
+
# "… auf die Haut, Kategorie" / "2".
|
|
612
|
+
r"|^[^\S\n]*(?:Gefahren)?Kategorie[^\S\n]+[1-4][A-C]?[^\S\n]*$"
|
|
613
|
+
r"|\b(?:Gefahren)?Kategorie[^\S\n]*\n[^\S\n]*[1-4][A-C]?[^\S\n]*$"
|
|
614
|
+
# Or the table drops the word (SDB-84): a line that is the name, a comma
|
|
615
|
+
# and the category, "Ätz-/Reizwirkung auf die Haut, 2".
|
|
616
|
+
r"|^[^\S\n]*[A-ZÄÖÜ][^.:\n]{3,70}?,[^\S\n]*[1-4][A-C]?[^\S\n]*$"
|
|
617
|
+
# A list under "Gefahrenkategorien:" names each class with an abbreviation
|
|
618
|
+
# of the supplier's software: "Entzündbare Feststoffe: Entz. Festst. 1".
|
|
619
|
+
r"|^[^\S\n]*Gefahrenkategorien:[^\S\n]*\n[^\n:]{3,80}:[^\S\n]*[^\n]{2,30}?"
|
|
620
|
+
r"[^\S\n][1-4][A-C]?[^\S\n]*$"
|
|
621
|
+
)
|
|
622
|
+
# Or the sheet says so in a sentence: "Das Gemisch ist als gefährlich eingestuft
|
|
623
|
+
# im Sinne der Verordnung (EG) Nr. 1272/2008" -- not "nicht als gefährlich".
|
|
624
|
+
_CLASSIFIED_AS_HAZARDOUS = re.compile(
|
|
625
|
+
r"(nicht\s+)?als\s+gefährlich\s+eingestuft", re.IGNORECASE
|
|
626
|
+
)
|
|
627
|
+
_ABOUT_INGREDIENTS = re.compile(r"enthält|Inhaltsstoff|Bestandteil", re.IGNORECASE)
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
def _names_a_hazard_class(part: str) -> bool:
|
|
631
|
+
"""Whether a classification part names a hazard class of the product."""
|
|
632
|
+
if _HAZARD_CLASS_CODE.search(part) or _HAZARD_CLASS_NAME.search(part):
|
|
633
|
+
return True
|
|
634
|
+
for entry in _HAZARD_CLASS_IN_WORDS.finditer(part):
|
|
635
|
+
start = part.rfind(".", 0, entry.start()) + 1
|
|
636
|
+
end = part.find(".", entry.end())
|
|
637
|
+
sentence = part[start : end if end != -1 else len(part)]
|
|
638
|
+
if not _ABOUT_INGREDIENTS.search(sentence):
|
|
639
|
+
return True
|
|
640
|
+
return False
|
|
641
|
+
|
|
642
|
+
|
|
643
|
+
def _says_classified(part: str) -> bool:
|
|
644
|
+
"""Whether a part says in a sentence that the product is classified as hazardous."""
|
|
645
|
+
return any(
|
|
646
|
+
match.group(1) is None for match in _CLASSIFIED_AS_HAZARDOUS.finditer(part)
|
|
647
|
+
)
|
|
648
|
+
|
|
649
|
+
|
|
650
|
+
# How sheets say that the product is not classified.
|
|
651
|
+
_NOT_CLASSIFIED = re.compile(
|
|
652
|
+
r"nicht\s+(?:als\s+gefährlich\s+)?eingestuft"
|
|
653
|
+
r"|nicht\s+als\s+(?:gesundheits-\s+oder\s+umwelt)?gefährlich"
|
|
654
|
+
r"|nicht\s+(?:den|die)\s+Kriterien|keinem\s+Kriterium\s+für\s+die\s+Einstufung"
|
|
655
|
+
r"|kriterien\s+(?:sind\s+)?nicht\s+erfüllt"
|
|
656
|
+
r"|keiner\s+Gefahren(?:kategorie|klasse)"
|
|
657
|
+
r"|nicht\s+einstufungspflichtig|keine\s+(?:Gefahren)?einstufung"
|
|
658
|
+
# "Keine Klassifizierung für physikalische Gefahren." (SDB-104).
|
|
659
|
+
r"|keine\s+Klassifizierung"
|
|
660
|
+
# "… als Fertigprodukt unterliegen nicht der Einstufung gemäß …" (SDB-85).
|
|
661
|
+
r"|unterlieg\w*\s+nicht\s+der\s+Einstufung"
|
|
662
|
+
r"|nicht\s+gefährlich|\bungefährlich|nicht\s+klassifiziert"
|
|
663
|
+
r"|nicht\s+kennzeichnungspflichtig|von\s+der\s+Etikettierung\s+befreit"
|
|
664
|
+
r"|keine?\s+gefährliche[nrs]?\s+"
|
|
665
|
+
r"(?:Substanz|Stoffe?|Mischung|Gemische?|Produkt|Zubereitung)"
|
|
666
|
+
r"|keine\s+relevanten\s+Anteile\s+an\s+gefährlichen"
|
|
667
|
+
r"|keine\s+gesundheitsschädlichen?\s+Wirkung"
|
|
668
|
+
r"|keine\s+besonder(?:en|s)\s+(?:zu\s+erwähnenden\s+)?Gefahren"
|
|
669
|
+
r"|Gefahrenbezeichnung:\s*keine"
|
|
670
|
+
# A cosmetic product is exempt from CLP (Article 1(5)(c)).
|
|
671
|
+
r"|kosmetisches\s+Mittel"
|
|
672
|
+
r"|keine\s+Gefährdung\s+für\s+die\s+Gesundheit|keine\s+weiteren\s+Risiken"
|
|
673
|
+
# An article is not classified at all: CLP covers substances and mixtures.
|
|
674
|
+
r"|\bErzeugnis\b"
|
|
675
|
+
r"|\bentfällt\b|nicht\s+(?:relevant|anwendbar|zutreffend)"
|
|
676
|
+
r"|(?m:(?:^|:)[^\w\n]*keine?\.?[^\w\n]*$)"
|
|
677
|
+
# "GHS Einstufung:" / "N/A" -- not applicable (SDB-86).
|
|
678
|
+
r"|(?m:(?:^|:)[^\w\n]*N\s?/\s?A\b[^\w\n]*$)"
|
|
679
|
+
# A dash where the classification would stand.
|
|
680
|
+
r"|(?m:^[^\S\n]*[-\u2013\u2014]+[^\S\n]*$)",
|
|
681
|
+
re.IGNORECASE,
|
|
682
|
+
)
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
# A classification that only says nothing is at hand (SDB-87): a classified
|
|
686
|
+
# product has to name its classification there, so this names none.
|
|
687
|
+
_NO_ANSWER = re.compile(
|
|
688
|
+
r"Es\s+liegen\s+keine\s+Informationen\s+vor"
|
|
689
|
+
r"|Keine\s+Daten\s+verfügbar"
|
|
690
|
+
r"|\bn\.\s?a\.",
|
|
691
|
+
re.IGNORECASE,
|
|
692
|
+
)
|
|
693
|
+
# An EUH statement is supplemental labelling, not a hazard class (SDB-88).
|
|
694
|
+
_EUH_CODE = re.compile(r"\bEUH\s?\d{3}[A-Z]?\b")
|
|
695
|
+
|
|
696
|
+
|
|
697
|
+
def _classification_parts(hazards_section: str) -> list[str]:
|
|
698
|
+
"""The text under each classification heading, up to the label's heading.
|
|
699
|
+
|
|
700
|
+
A document that holds several sheets -- the two components of an adhesive
|
|
701
|
+
-- has one part per sheet. Without the heading the section itself is the
|
|
702
|
+
part, as far as the label's heading.
|
|
703
|
+
"""
|
|
704
|
+
starts = [m.end() for m in _CLASSIFICATION_HEADING.finditer(hazards_section)]
|
|
705
|
+
ends = [*starts[1:], len(hazards_section)]
|
|
706
|
+
parts = [
|
|
707
|
+
hazards_section[start:end] for start, end in zip(starts, ends, strict=False)
|
|
708
|
+
] or [hazards_section]
|
|
709
|
+
return [
|
|
710
|
+
part[: label.start()] if (label := LABEL_HEADING_PATTERN.search(part)) else part
|
|
711
|
+
for part in parts
|
|
712
|
+
]
|
|
713
|
+
|
|
714
|
+
|
|
715
|
+
# What the label part says when the section has no classification heading
|
|
716
|
+
# (SDB-107): that the product is not classified, or that it needs no label.
|
|
717
|
+
_LABEL_SAYS_UNCLASSIFIED = re.compile(
|
|
718
|
+
r"nicht\s+(?:als\s+gefährlich\s+)?eingestuft|als\s+nicht\s+gefährlich\s+eingestuft"
|
|
719
|
+
r"|nicht\s+kennzeichnungspflichtig|keine\s+Kennzeichnung\s+erforderlich",
|
|
720
|
+
re.IGNORECASE,
|
|
721
|
+
)
|
|
722
|
+
_OTHER_HAZARDS_HEADING = re.compile(r"Sonstige\s+Gefahren", re.IGNORECASE)
|
|
723
|
+
|
|
724
|
+
|
|
725
|
+
def _label_says_unclassified(hazards_section: str) -> bool:
|
|
726
|
+
"""Whether the label part, up to the other hazards, says so and prints no
|
|
727
|
+
hazard code."""
|
|
728
|
+
heading = LABEL_HEADING_PATTERN.search(hazards_section)
|
|
729
|
+
if heading is None:
|
|
730
|
+
return False
|
|
731
|
+
label = hazards_section[heading.end() :]
|
|
732
|
+
other = _OTHER_HAZARDS_HEADING.search(label)
|
|
733
|
+
label = label[: other.start()] if other else label
|
|
734
|
+
return (
|
|
735
|
+
_LABEL_SAYS_UNCLASSIFIED.search(label) is not None
|
|
736
|
+
and _HAZARD_CLASS_CODE.search(label) is None
|
|
737
|
+
)
|
|
738
|
+
|
|
739
|
+
|
|
740
|
+
def _gives_no_answer(part: str) -> bool:
|
|
741
|
+
"""Nothing under the classification but "no information" (SDB-87)."""
|
|
742
|
+
rest, found = _NO_ANSWER.subn("", part)
|
|
743
|
+
return found > 0 and not re.search(r"[^\W\d]", rest)
|
|
744
|
+
|
|
745
|
+
|
|
746
|
+
def stated_classification(hazards_section: str) -> StatedClassification:
|
|
747
|
+
"""What section 2 says about the product's classification (SDB-75).
|
|
748
|
+
|
|
749
|
+
Of several sheets in one document the product is classified if one is.
|
|
750
|
+
"""
|
|
751
|
+
parts = _classification_parts(hazards_section)
|
|
752
|
+
if any(_names_a_hazard_class(part) for part in parts):
|
|
753
|
+
return StatedClassification.CLASSIFIED
|
|
754
|
+
if any(old_classification(part) is not None for part in parts):
|
|
755
|
+
return StatedClassification.OLD_SYSTEM
|
|
756
|
+
if any(_says_classified(part) for part in parts):
|
|
757
|
+
return StatedClassification.CLASSIFIED
|
|
758
|
+
if any(_NOT_CLASSIFIED.search(part) for part in parts):
|
|
759
|
+
return StatedClassification.NOT_CLASSIFIED
|
|
760
|
+
if any(_gives_no_answer(part) for part in parts):
|
|
761
|
+
return StatedClassification.NOT_CLASSIFIED
|
|
762
|
+
# Only under the classification's own heading: without it the part is the
|
|
763
|
+
# whole section, where an EUH statement may stand beside a hazard in words.
|
|
764
|
+
headed = _CLASSIFICATION_HEADING.search(hazards_section) is not None
|
|
765
|
+
# The regulation's wording of a hazard statement without its code
|
|
766
|
+
# (SDB-106): "Schädlich für Wasserorganismen, mit langfristiger Wirkung."
|
|
767
|
+
if headed and any(
|
|
768
|
+
_HAZARD_CLASS_CODE.fullmatch(code)
|
|
769
|
+
for part in parts
|
|
770
|
+
for code in codes_from_wording(part)
|
|
771
|
+
):
|
|
772
|
+
return StatedClassification.CLASSIFIED
|
|
773
|
+
if headed and any(_EUH_CODE.search(part) for part in parts):
|
|
774
|
+
return StatedClassification.NOT_CLASSIFIED
|
|
775
|
+
if not headed and _label_says_unclassified(hazards_section):
|
|
776
|
+
return StatedClassification.NOT_CLASSIFIED
|
|
777
|
+
return StatedClassification.NOT_STATED
|
|
778
|
+
|
|
779
|
+
|
|
780
|
+
# A sheet that says of itself that it is issued voluntarily or that none is
|
|
781
|
+
# prescribed for its product (SDB-142). The sentence speaks of the safety data
|
|
782
|
+
# sheet itself; within it a full stop only ends an abbreviation or stands
|
|
783
|
+
# before a number ("gem. Artikel 31", "Art. 31", "Nr. 1907/2006").
|
|
784
|
+
_SHEET_WORD = r"(?:Produkt)?[Ss]icherheitsdatenbl[aä]tt(?:es|s|er|ern)?|SDB|SDS"
|
|
785
|
+
_SHEET = rf"\b(?:{_SHEET_WORD})\b(?:\s*\((?:SDB|SDS)\))?"
|
|
786
|
+
_IN_SENTENCE = (
|
|
787
|
+
r"(?:\b(?:gem|Nr|Art|Abs|bzw|ggf|lt|vgl)\.|[^.!?;:]|\.(?=\s*[\da-zäöü(]))"
|
|
788
|
+
)
|
|
789
|
+
_NOT_DUE = (
|
|
790
|
+
r"(?:verpflichtend|vorgeschrieben|erforderlich|notwendig|nötig|verlangt"
|
|
791
|
+
r"|gefordert|zu\s+erstellen)"
|
|
792
|
+
)
|
|
793
|
+
_VOLUNTARILY = r"(?:freiwillig|auf\s+freiwilliger\s+(?:Basis|Grundlage))"
|
|
794
|
+
_SAYS_VOLUNTARY = re.compile(
|
|
795
|
+
"|".join(
|
|
796
|
+
(
|
|
797
|
+
# "… ist gemäß Artikel 31 … kein Sicherheitsdatenblatt erforderlich."
|
|
798
|
+
rf"\b[Kk]ein\s+{_SHEET}{_IN_SENTENCE}{{0,140}}?\b{_NOT_DUE}\b",
|
|
799
|
+
# "Das SDB ist für dieses Produkt rechtlich nicht vorgeschrieben."
|
|
800
|
+
rf"{_SHEET}\s+(?:ist|sind)\s{_IN_SENTENCE}{{0,140}}?"
|
|
801
|
+
rf"\b(?:nicht|keine?)\s+(?:\w+\s+){{0,2}}{_NOT_DUE}\b",
|
|
802
|
+
# "… eines Sicherheitsdatenblattes (SDS) gesetzlich nicht verpflichtend."
|
|
803
|
+
rf"{_SHEET}\s+(?:(?:gesetzlich|rechtlich)\s+)?nicht\s+{_NOT_DUE}\b",
|
|
804
|
+
# "… sind wir nicht verpflichtet ein Sicherheitsdatenblatt zu erstellen."
|
|
805
|
+
rf"\bnicht\s+verpflichtet,?\s+eine?\s+{_SHEET}",
|
|
806
|
+
# "Dieses Sicherheitsdatenblatt wurde freiwillig erstellt"
|
|
807
|
+
rf"{_SHEET}\s+(?:wurde|wird|ist|erfolgt)\s+(?:\w+\s+){{0,4}}?{_VOLUNTARILY}\b",
|
|
808
|
+
# "… dieses Sicherheitsdatenblatts auf freiwilliger Basis."
|
|
809
|
+
rf"{_SHEET}\s+auf\s+freiwilliger\s+(?:Basis|Grundlage)\b",
|
|
810
|
+
# "Dieses auf freiwilliger Basis erstellte SDS", "die von uns
|
|
811
|
+
# freiwillig zur Verfügung gestellten Sicherheitsdatenblätter"
|
|
812
|
+
rf"\b{_VOLUNTARILY}\s+(?:erstellte|bereitgestellte"
|
|
813
|
+
rf"|zur\s+Verfügung\s+gestellte)[nrs]?\s+{_SHEET}",
|
|
814
|
+
# "Freiwillige Produktinformation in Anlehnung an das
|
|
815
|
+
# Sicherheitsdatenblatt-Format"
|
|
816
|
+
r"\b[Ff]reiwillige[ns]?\s+(?:Produkt|Sicherheits)?[Ii]nformation(?:en)?"
|
|
817
|
+
r"\s+in\s+Anlehnung\s+an\s+(?:das|ein)\s+Sicherheitsdatenbl[aä]tt",
|
|
818
|
+
# "… ist eine freiwillige Information und kein Sicherheitsdatenblatt"
|
|
819
|
+
r"\bfreiwillige\s+Information\s+und\s+kein\s+Sicherheitsdatenbl[aä]tt",
|
|
820
|
+
)
|
|
821
|
+
)
|
|
822
|
+
)
|
|
823
|
+
|
|
824
|
+
|
|
825
|
+
# The sheet calls its product an article beside that sentence (SDB-142): the
|
|
826
|
+
# word within this many characters of it, and not in the regulation's row
|
|
827
|
+
# "Stoffe, Gemische und Erzeugnisse", which stands in every sheet.
|
|
828
|
+
_ARTICLE_REACH = 400
|
|
829
|
+
_ARTICLE = re.compile(r"\bErzeugnis(?:se[sn]?|s)?\b")
|
|
830
|
+
_ARTICLE_IN_A_ROW = re.compile(
|
|
831
|
+
r"\b(?:Stoffe|Gemische|Mischungen|Zubereitungen)\b[^.]{0,60}?\bund\s+Erzeugnisse\b"
|
|
832
|
+
)
|
|
833
|
+
|
|
834
|
+
|
|
835
|
+
# The sheet says its product is an article and therefore not subject to
|
|
836
|
+
# labelling (SDB-154): that it is one, and within this many characters behind
|
|
837
|
+
# it that no labelling is due. The bare word says nothing of the product
|
|
838
|
+
# ("Das Erzeugnis ist ... nicht kennzeichnungspflichtig"), and "kein
|
|
839
|
+
# Erzeugnis" denies it.
|
|
840
|
+
_ARTICLE_EXEMPT_REACH = 250
|
|
841
|
+
_IS_ARTICLE = re.compile(
|
|
842
|
+
r"\b(?:ist|sind)\s+ein\s+Erzeugnis\b"
|
|
843
|
+
r"|\bstell(?:t|en)\s[^.]{0,80}?\bein\s+Erzeugnis\s+dar\b"
|
|
844
|
+
r"|\bgilt\s[^.]{0,120}?\bals\s+Erzeugnis\b"
|
|
845
|
+
r"|\bhandelt\s+(?:es\s+)?sich\s+um\s+ein\s+Erzeugnis\b"
|
|
846
|
+
r"|\bunterliegt\s+als\s+Erzeugnis\s+nicht\b"
|
|
847
|
+
)
|
|
848
|
+
_NOT_LABELLED = re.compile(
|
|
849
|
+
r"\bnicht\s+kennzeichnungspflichtig\b"
|
|
850
|
+
r"|\bnicht\s+der\s+(?:\w+\s+){0,2}Kennzeichnungspflicht\b"
|
|
851
|
+
)
|
|
852
|
+
|
|
853
|
+
|
|
854
|
+
def says_article(text: str) -> bool:
|
|
855
|
+
"""Whether a sheet calls its product an article (SDB-142, SDB-154).
|
|
856
|
+
|
|
857
|
+
Beside the sentence that makes the sheet voluntary: "Dieses Produkt ist
|
|
858
|
+
ein Erzeugnis und daher ist die Erstellung eines Sicherheitsdatenblattes
|
|
859
|
+
nicht verpflichtend." Or with the sentence that an article needs no
|
|
860
|
+
labelling: "Dieses Produkt ist ein Erzeugnis, deshalb ist es nicht
|
|
861
|
+
kennzeichnungspflichtig."
|
|
862
|
+
|
|
863
|
+
>>> says_article(
|
|
864
|
+
... "Dieses Produkt ist ein Erzeugnis und daher ist die Erstellung eines "
|
|
865
|
+
... "Sicherheitsdatenblattes gesetzlich nicht verpflichtend."
|
|
866
|
+
... )
|
|
867
|
+
True
|
|
868
|
+
>>> says_article(
|
|
869
|
+
... "Dieses Produkt ist ein Erzeugnis, deshalb ist es nach "
|
|
870
|
+
... "EG-Richtlinien nicht kennzeichnungspflichtig."
|
|
871
|
+
... )
|
|
872
|
+
True
|
|
873
|
+
>>> says_article("Das Erzeugnis ist nicht kennzeichnungspflichtig.")
|
|
874
|
+
False
|
|
875
|
+
>>> says_article("Dieses Sicherheitsdatenblatt wurde freiwillig erstellt.")
|
|
876
|
+
False
|
|
877
|
+
"""
|
|
878
|
+
flat = _ARTICLE_IN_A_ROW.sub(" ", re.sub(r"\s+", " ", text))
|
|
879
|
+
if any(
|
|
880
|
+
_NOT_LABELLED.search(flat, said.end(), said.end() + _ARTICLE_EXEMPT_REACH)
|
|
881
|
+
for said in _IS_ARTICLE.finditer(flat)
|
|
882
|
+
):
|
|
883
|
+
return True
|
|
884
|
+
return any(
|
|
885
|
+
_ARTICLE.search(
|
|
886
|
+
flat, max(0, said.start() - _ARTICLE_REACH), said.end() + _ARTICLE_REACH
|
|
887
|
+
)
|
|
888
|
+
for said in _SAYS_VOLUNTARY.finditer(flat)
|
|
889
|
+
)
|
|
890
|
+
|
|
891
|
+
|
|
892
|
+
def says_voluntary(text: str) -> bool:
|
|
893
|
+
"""Whether a sheet says it is issued voluntarily or none is prescribed (SDB-142).
|
|
894
|
+
|
|
895
|
+
Anywhere in its text: sheets print the sentence under the title, in
|
|
896
|
+
section 1, 2, 3, 15 or 16, or in the page header.
|
|
897
|
+
|
|
898
|
+
>>> says_voluntary(
|
|
899
|
+
... "Für dieses Produkt ist gemäß Artikel 31 der REACH-Verordnung "
|
|
900
|
+
... "kein Sicherheitsdatenblatt erforderlich."
|
|
901
|
+
... )
|
|
902
|
+
True
|
|
903
|
+
>>> says_voluntary("Signalwort: nicht erforderlich. UFI: nicht notwendig.")
|
|
904
|
+
False
|
|
905
|
+
"""
|
|
906
|
+
return _SAYS_VOLUNTARY.search(re.sub(r"\s+", " ", text)) is not None
|
|
907
|
+
|
|
908
|
+
|
|
909
|
+
# Which countries a German-language sheet is issued for (SDB-69). Annex II
|
|
910
|
+
# asks for the limit values and regulations of the country of sale and for its
|
|
911
|
+
# advisory body, so suppliers issue the same sheet per country. A code printed
|
|
912
|
+
# beside the version says it outright ("Version: 3 / CH"); it comes first,
|
|
913
|
+
# because a template carries some national names into every version
|
|
914
|
+
# ("Lagerklasse nach TRGS 510" stands in one supplier's Austrian and Swiss sheets).
|
|
915
|
+
_COUNTRY_CODE = re.compile(
|
|
916
|
+
r"(?m)Version[^\S\n]*:?[^\n/]{0,12}/[^\S\n]*(DE|AT|CH)[^\S\n]*$"
|
|
917
|
+
)
|
|
918
|
+
_NATIONAL_RULES: dict[str, re.Pattern[str]] = {
|
|
919
|
+
"DE": re.compile(
|
|
920
|
+
r"\bTRGS\s?\d{3}|Wassergef[äa]hrdungsklasse|\bWGK\b|\bAwSV\b|\bVwVwS\b|\bAGW\b"
|
|
921
|
+
),
|
|
922
|
+
"AT": re.compile(r"\bGKV\b|Grenzwerteverordnung|\bVbF\b"),
|
|
923
|
+
"CH": re.compile(r"\bSUVA\b|\bChemRRV\b"),
|
|
924
|
+
}
|
|
925
|
+
# An advisory body by its name or number, looked for in section 1 only:
|
|
926
|
+
# "GIFTINFORMATIONSZENTRUM" alone is the wording of P statements.
|
|
927
|
+
_ADVISORY_BODIES: dict[str, re.Pattern[str]] = {
|
|
928
|
+
"DE": re.compile(
|
|
929
|
+
r"19\s?2\s?40\b|GIZ-Nord|Vergiftungs-Informations-Zentrale"
|
|
930
|
+
r"|Giftnotruf\s+(?:Berlin|München|Mainz|Bonn|Erfurt|Freiburg|Göttingen|Homburg"
|
|
931
|
+
r"|Nürnberg|der\s+Charité)"
|
|
932
|
+
r"|Giftinformationszentrum(?:-Nord|\s+der\s+Länder|\s+Erfurt|\s+Mainz|\s+Nord)"
|
|
933
|
+
r"|\+\s?49[^\n]{0,30}(?:Gift|Notruf)|(?:Gift|Notruf)[^\n]{0,40}\+\s?49"
|
|
934
|
+
),
|
|
935
|
+
"AT": re.compile(
|
|
936
|
+
r"406\s?43\s?43|1\s?406\s?43|14064343|Vergiftungsinformationszentrale"
|
|
937
|
+
),
|
|
938
|
+
"CH": re.compile(
|
|
939
|
+
r"Tox\s?Info\s?Suisse|Toxikologisches\s+Informationszentrum|\bSTIZ\b"
|
|
940
|
+
r"|(?:Tel\.?|Notruf\w*:?)\s*145\b"
|
|
941
|
+
),
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
|
|
945
|
+
def sheet_countries(text: str, identification: str) -> list[str]:
|
|
946
|
+
"""The countries a sheet is issued for, of DE, AT and CH (SDB-69)."""
|
|
947
|
+
printed = {match.group(1) for match in _COUNTRY_CODE.finditer(text)}
|
|
948
|
+
if printed:
|
|
949
|
+
return sorted(printed)
|
|
950
|
+
named = {country for country, rule in _NATIONAL_RULES.items() if rule.search(text)}
|
|
951
|
+
named |= {
|
|
952
|
+
country
|
|
953
|
+
for country, body in _ADVISORY_BODIES.items()
|
|
954
|
+
if body.search(identification)
|
|
955
|
+
}
|
|
956
|
+
return sorted(named)
|
|
957
|
+
|
|
958
|
+
|
|
959
|
+
# "Kein Signalwort" is an answer, not a missing one: the sheet states that this
|
|
960
|
+
# classification carries none. Distinguishing it from "not found" is what lets
|
|
961
|
+
# the caller decide whether to fall back to the derivation.
|
|
962
|
+
#
|
|
963
|
+
# On one line: "Gefahrenpiktogramme" / "keine" / "Signalwort" / "ACHTUNG" is a
|
|
964
|
+
# label without a pictogram, not one without a signal word.
|
|
965
|
+
# A sentence that says a label is not applied (SDB-78): "Eine Kennzeichnung des
|
|
966
|
+
# Silikons mit GHS 07 / Achtung / H317 erfolgt daher nicht." Read as a label it
|
|
967
|
+
# gave 19 sealants the very statement the sheet explains away.
|
|
968
|
+
# Or that one is not required: "Die Kennzeichnung einer Aspirationsgefahr (Asp.
|
|
969
|
+
# Tox. 1 H304) ist für Aerosolpackungen … nicht vorgeschrieben". The class's
|
|
970
|
+
# abbreviation carries full stops, so the sentence is not held to them.
|
|
971
|
+
_LABEL_DECLINED = re.compile(
|
|
972
|
+
r"Eine\s+Kennzeichnung\b[^.]{0,200}?\berfolgt\s+(?:daher\s+)?nicht\."
|
|
973
|
+
r"|(?:Eine|Die)\s+(?:Einstufung\s+und\s+)?Kennzeichnung\b[^\n]{0,80}?"
|
|
974
|
+
r"\bH\d{3}\b[\s\S]{0,160}?\bnicht\s+(?:vorgeschrieben|erforderlich|notwendig)\b"
|
|
975
|
+
)
|
|
976
|
+
NO_SIGNAL_WORD_PATTERN = re.compile(
|
|
977
|
+
r"\bkein(?:e[ns]?)?[^\S\n]+Signalwort\b"
|
|
978
|
+
# Suppliers write the absence four ways. All of them are anchored to the
|
|
979
|
+
# label, because a bare dash or "entfällt" says nothing on its own:
|
|
980
|
+
# Signalwort entfällt
|
|
981
|
+
# Signalwort (CLP) : -
|
|
982
|
+
# Signalwort nicht erforderlich
|
|
983
|
+
r"|\bSignalwort\b\s*(?:\([^)]{0,12}\))?\s*:?\s*"
|
|
984
|
+
r"(?:entfällt|nicht\s+erforderlich|nicht\s+anwendbar|[-\u2013\u2014](?!\w))"
|
|
985
|
+
# The value in a form, behind the label or on the line below it, and
|
|
986
|
+
# nothing after it on its line:
|
|
987
|
+
# Signalwort
|
|
988
|
+
# keine
|
|
989
|
+
r"|\bSignalwort\b[^\S\n]*(?:\([^)]{0,12}\))?[^\S\n]*:?[^\S\n]*\n?[^\S\n]*"
|
|
990
|
+
r"(?:kein(?:e[ns]?)?|ohne)[^\S\n]*(?=\n|$)",
|
|
991
|
+
re.IGNORECASE,
|
|
992
|
+
)
|
|
993
|
+
# A product that carries no label at all carries no signal word either. These
|
|
994
|
+
# sheets never print a Signalwort row, because there is no label to print it on
|
|
995
|
+
# -- they say so in section 2.2 instead:
|
|
996
|
+
#
|
|
997
|
+
# Keine Kennzeichnung erforderlich
|
|
998
|
+
# Der Stoff oder das Gemisch ist nicht gefährlich gemäß Verordnung (EG)
|
|
999
|
+
# Nr. 1272/2008 (CLP)
|
|
1000
|
+
# NC Nicht Eingestuft
|
|
1001
|
+
#
|
|
1002
|
+
# That is a determination, not silence, and it matters: 97 sheets in this
|
|
1003
|
+
# catalogue state it, and the model gave 19 of them "Gefahr" -- read off the
|
|
1004
|
+
# ingredient classifications in section 3, which belong to the raw materials
|
|
1005
|
+
# and not to the product.
|
|
1006
|
+
#
|
|
1007
|
+
# Searched only within section 2.2, which is what keeps it safe: "nicht
|
|
1008
|
+
# eingestuft" is commonplace in 2.1 beside a hazard class the product does not
|
|
1009
|
+
# meet, and would say nothing about the label there. Measured over the
|
|
1010
|
+
# catalogue, all 147 sheets this matches carry no hazard code and no printed
|
|
1011
|
+
# signal word in their label block.
|
|
1012
|
+
UNLABELLED_PATTERN = re.compile(
|
|
1013
|
+
r"kein[a-zäöü]*\s+Kennzeichnung\b[^.\n]{0,40}\berforderlich"
|
|
1014
|
+
r"|nicht\s+gefährlich\s+(?:gemäß|nach|im\s+Sinne)"
|
|
1015
|
+
r"|\bnicht\s+eingestuft\b",
|
|
1016
|
+
re.IGNORECASE,
|
|
1017
|
+
)
|
|
1018
|
+
|
|
1019
|
+
# "Nicht bestimmt" under the 2.2 heading is a third state, and conflating it
|
|
1020
|
+
# with either of the other two gets the product wrong.
|
|
1021
|
+
#
|
|
1022
|
+
# It is not UNLABELLED_PATTERN. That one reads a *determination* -- the supplier
|
|
1023
|
+
# has established that this product carries no label -- and settling the signal
|
|
1024
|
+
# word on it is sound. "Nicht bestimmt" establishes nothing: the supplier has
|
|
1025
|
+
# not worked the labelling out. Reading it as "no hazard" would put an absence
|
|
1026
|
+
# on the product that the sheet never states.
|
|
1027
|
+
#
|
|
1028
|
+
# What it does settle is that no classification was declared, and that is enough
|
|
1029
|
+
# to keep an undeclared one off the label. On the sheet this was measured from,
|
|
1030
|
+
# a 2012 motor oil, the model returned H304 -- read from "Asp. Tox. 1, H304" in
|
|
1031
|
+
# the section 3 ingredient table and from the section 16 glossary. The mixture
|
|
1032
|
+
# may well merit it; 10-20% of an Asp. Tox. 1 substance would. But that is our
|
|
1033
|
+
# inference from a component, not the supplier's declaration, and section 2.2 is
|
|
1034
|
+
# the authority on what the product is labelled with.
|
|
1035
|
+
#
|
|
1036
|
+
# Bounded to the top of the block on purpose. "Nicht bestimmt" is commonplace in
|
|
1037
|
+
# section 9 next to a physical property nobody measured, and a label block whose
|
|
1038
|
+
# boundary failed runs on for LABEL_SECTION_MAX_CHARS.
|
|
1039
|
+
_UNDETERMINED_WINDOW = 300
|
|
1040
|
+
UNDETERMINED_LABELLING_PATTERN = re.compile(
|
|
1041
|
+
r"\bnicht\s+bestimmt\b|\bnicht\s+festgelegt\b", re.IGNORECASE
|
|
1042
|
+
)
|
|
1043
|
+
|
|
1044
|
+
|
|
1045
|
+
def states_undetermined_labelling(label_text: str) -> bool:
|
|
1046
|
+
"""Whether the label block says the labelling was never determined."""
|
|
1047
|
+
return bool(
|
|
1048
|
+
UNDETERMINED_LABELLING_PATTERN.search(label_text[:_UNDETERMINED_WINDOW])
|
|
1049
|
+
)
|
|
1050
|
+
|
|
1051
|
+
|
|
1052
|
+
STATEMENT_CODE_PREFIX = re.compile(r"^(H|P|EUH)\d{2,3}\b", re.IGNORECASE)
|
|
1053
|
+
|
|
1054
|
+
# Where a statement of the parser's text ends, and what it reads past (SDB-27).
|
|
1055
|
+
# Measured over the 1997 current sheets, a statement that ran on to the next code
|
|
1056
|
+
# carried a page header or a foreign heading 4454 times; 118 are left.
|
|
1057
|
+
_ANY_STATEMENT_CODE = re.compile(r"\b(?:EUH\d{3}|[HP]\d{3})(?!\d)")
|
|
1058
|
+
# "2.3 Sonstige Gefahren", and "2.3" on its own line with its title below; not
|
|
1059
|
+
# "2.5 kg".
|
|
1060
|
+
_NUMBERED_SUBHEADING = re.compile(r"^\d{1,2}\.\d{1,2}\.?(?:\s+[A-ZÄÖÜ].*)?$")
|
|
1061
|
+
# The label's own headings and the rows of its 2.2 table, capitalised as headings
|
|
1062
|
+
# are: a line that continues a sentence in lower case is not one.
|
|
1063
|
+
_LABEL_HEADING = re.compile(
|
|
1064
|
+
r"^(?=(?-i:[A-ZÄÖÜ]))(?:Gefahrenhinweis\w*|Sicherheitshinweis\w*|[HP]-Sätze|EUH[- ]?Sätze"
|
|
1065
|
+
r"|Ergänzende|Zusätzliche|Besondere\s+(?:Vorschriften|Kennzeichnung|Gefahrenhinweise)"
|
|
1066
|
+
r"|Andere\s+Kennzeichnung|Kindersicherung|Ta[ks]t\w+\s+Warn(?:zeichen|hinweis)"
|
|
1067
|
+
r"|Ergebnisse\s+der\s+PBT-\s+und\s+vPvB-Beurteilung"
|
|
1068
|
+
r"|Endokrinschädliche\s+Eigenschaften"
|
|
1069
|
+
r"|Mögliche\s+Gefahren|Hinweise?\s+zur\s+Kennzeichnung|Signalwort"
|
|
1070
|
+
r"|Gefahrenpiktogramm\w*|Piktogramm\w*|Gefahr(?:en)?bestimmende"
|
|
1071
|
+
r"|Gefährliche\s+Inhaltsstoffe|[RS]-Sätze"
|
|
1072
|
+
r"|Kennzeichnung|Kennzeichnungselemente|Sonstige\s+Gefahren"
|
|
1073
|
+
r"|Wortlaut\s+der|Volltext\s+der|Mit\s+kindergesicherten"
|
|
1074
|
+
r"|Kindergesicherte[rn]?|Tastbare[rn]?|Spezielle\s+Verpackungs\w*"
|
|
1075
|
+
r"|Anhang\s+XVII|Nicht\s+anwendbar\.?$"
|
|
1076
|
+
r"|(?:Prävention|Reaktion|Lagerung|Entsorgung|Allgemein(?:es)?)\s*:)(?![\w-])",
|
|
1077
|
+
re.IGNORECASE,
|
|
1078
|
+
)
|
|
1079
|
+
# A hazard class with its category, the way 2.1 lists them: "Eye Irrit. 2",
|
|
1080
|
+
# "STOT SE 3", "Langfristig (chronisch) gewässergefährdend, Kategorie 3". The
|
|
1081
|
+
# classes are CLP's own abbreviations (Annex VI, table 1.1), so a page header's
|
|
1082
|
+
# "Version 3" is not one of them.
|
|
1083
|
+
_CLASSIFICATION_LINE = re.compile(
|
|
1084
|
+
r"^(?:Unst\.\s*Expl|Expl|Flam\.\s*(?:Gas|Liq|Sol)|Chem\.\s*Unst\.\s*Gas|Aerosol"
|
|
1085
|
+
r"|Ox\.\s*(?:Gas|Liq|Sol)|Press\.\s*Gas|Self-react|Pyr\.\s*(?:Liq|Sol)"
|
|
1086
|
+
r"|Self-heat|Water-react|Org\.\s*Perox|Met\.\s*Corr|Acute\s*Tox|Skin\s*Corr"
|
|
1087
|
+
r"|Skin\s*Irrit|Eye\s*Dam|Eye\s*Irrit|Resp\.\s*Sens|Skin\s*Sens|Muta|Carc"
|
|
1088
|
+
r"|Repr|Lact|STOT\s*(?:SE|RE)|Asp\.\s*Tox|Aquatic\s*(?:Acute|Chronic)"
|
|
1089
|
+
r"|Ozone|Desen\.\s*Expl|ED\s*(?:HH|ENV)|PBT|vPvB|PMT|vPvM)\b\.?\s*\d?[A-F]?$"
|
|
1090
|
+
r"|\bKategorie\s+\d"
|
|
1091
|
+
)
|
|
1092
|
+
_PAGE_NUMBER = re.compile(
|
|
1093
|
+
r"^(?:Seite\s*:?\s*)?\d{1,3}\s*(?:/|von)\s*\d{1,3}$"
|
|
1094
|
+
r"|^Seite\s*:?\s*\d{1,3}(?:\s*(?:/|von)\s*\d{1,3}|\s*\(\d{1,3}\))?$",
|
|
1095
|
+
re.IGNORECASE,
|
|
1096
|
+
)
|
|
1097
|
+
_CONTINUED = re.compile(r"\(?Fortsetzung\s+(?:auf|von)\s+Seite\s*\d+\)?", re.IGNORECASE)
|
|
1098
|
+
# A statement's own heading, repeated at the top of the next page: read as a
|
|
1099
|
+
# heading, it cut P210 of one sheet off before "fernhalten. Nicht rauchen."
|
|
1100
|
+
_OWN_HEADING = {
|
|
1101
|
+
HAZARD_CODE_PATTERN: re.compile(
|
|
1102
|
+
r"^Gefahrenhinweis\w*(?:\s*\(Fortsetzung\))?\s*:?$", re.IGNORECASE
|
|
1103
|
+
),
|
|
1104
|
+
PRECAUTION_CODE_PATTERN: re.compile(
|
|
1105
|
+
r"^Sicherheitshinweis\w*(?:\s*\(Fortsetzung\))?\s*:?$", re.IGNORECASE
|
|
1106
|
+
),
|
|
1107
|
+
SUPPLEMENTAL_CODE_PATTERN: re.compile(
|
|
1108
|
+
r"^(?:Ergänzende\s+Gefahrenhinweise|EUH[- ]?Sätze)(?:\s*\(Fortsetzung\))?\s*:?$",
|
|
1109
|
+
re.IGNORECASE,
|
|
1110
|
+
),
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
|
|
1114
|
+
def _page_furniture(text: str, pages: int) -> frozenset[str]:
|
|
1115
|
+
"""The lines that repeat like a page header: on half the pages, three times at least.
|
|
1116
|
+
|
|
1117
|
+
The pages themselves are not kept with the record, so this counts the
|
|
1118
|
+
joined text. A sentence a sheet prints in 2.2 and again in section 16 can
|
|
1119
|
+
repeat as often on a short sheet, which is why _extract_statement_text
|
|
1120
|
+
passes over these lines only once a statement has its wording.
|
|
1121
|
+
"""
|
|
1122
|
+
counts = Counter(line.strip() for line in text.split("\n") if line.strip())
|
|
1123
|
+
threshold = max(3, (pages + 1) // 2)
|
|
1124
|
+
return frozenset(line for line, n in counts.items() if n >= threshold)
|
|
1125
|
+
|
|
1126
|
+
|
|
1127
|
+
@dataclass(slots=True)
|
|
1128
|
+
class ParsedSheet:
|
|
1129
|
+
"""A parsed sheet, and what the parser noticed while reading it."""
|
|
1130
|
+
|
|
1131
|
+
sheet: SafetyDataSheet
|
|
1132
|
+
warnings: list[Message]
|
|
1133
|
+
# Each sheet of a file that holds several, read on its own, in the file's
|
|
1134
|
+
# order (SDB-118); empty for a file that is one sheet.
|
|
1135
|
+
parts: list[SheetPart] = field(default_factory=list)
|
|
1136
|
+
# Whether a cover page stands in front of the file's sheets, and the word
|
|
1137
|
+
# on it that names a display, as printed (SDB-138); it is read for nothing
|
|
1138
|
+
# else and is no sheet.
|
|
1139
|
+
cover_page: bool = False
|
|
1140
|
+
display_term: str | None = None
|
|
1141
|
+
|
|
1142
|
+
|
|
1143
|
+
@dataclass(slots=True)
|
|
1144
|
+
class SheetPart:
|
|
1145
|
+
"""One sheet of a file that holds several, read on its own (SDB-118)."""
|
|
1146
|
+
|
|
1147
|
+
sheet: SafetyDataSheet
|
|
1148
|
+
warnings: list[Message]
|
|
1149
|
+
# The sheet's text, as split_into_sheets cuts it from the file's.
|
|
1150
|
+
text: str
|
|
1151
|
+
|
|
1152
|
+
|
|
1153
|
+
class SDSParser:
|
|
1154
|
+
"""Convert extracted SDS text into structured fields."""
|
|
1155
|
+
|
|
1156
|
+
def __init__(self, *, language: str = "de") -> None:
|
|
1157
|
+
self.language = language
|
|
1158
|
+
|
|
1159
|
+
def parse(self, extraction: ExtractionResult) -> ParsedSheet:
|
|
1160
|
+
"""The file's reading, and each sheet's where it holds several.
|
|
1161
|
+
|
|
1162
|
+
The file's reading unites its sheets (SDB-3, SDB-9). Beside it each
|
|
1163
|
+
sheet is read from its own text, as a file holding only that sheet
|
|
1164
|
+
would be (SDB-118). A cover page in front of the sheets is left out
|
|
1165
|
+
of both (SDB-138).
|
|
1166
|
+
"""
|
|
1167
|
+
whole = max(len(extraction.text), 1)
|
|
1168
|
+
|
|
1169
|
+
def share(text: str) -> int:
|
|
1170
|
+
# The pages are not kept with the text: a part of it has its share
|
|
1171
|
+
# of them, which is what repeating page headers are counted by.
|
|
1172
|
+
return max(1, round(extraction.pages_sampled * len(text) / whole))
|
|
1173
|
+
|
|
1174
|
+
cover, body = split_cover_page(extraction.text)
|
|
1175
|
+
if cover:
|
|
1176
|
+
# The cover page is read for nothing (SDB-138): the file's reading
|
|
1177
|
+
# is that of the sheets behind it.
|
|
1178
|
+
extraction = replace(extraction, text=body, pages_sampled=share(body))
|
|
1179
|
+
result = self._parse_text(extraction)
|
|
1180
|
+
result.cover_page = bool(cover)
|
|
1181
|
+
result.display_term = _display_term(cover)
|
|
1182
|
+
texts = split_into_sheets(body)
|
|
1183
|
+
if len(texts) > 1:
|
|
1184
|
+
for text in texts:
|
|
1185
|
+
read = self._parse_text(
|
|
1186
|
+
replace(
|
|
1187
|
+
extraction,
|
|
1188
|
+
text=text,
|
|
1189
|
+
raw_text=text,
|
|
1190
|
+
pages_sampled=share(text),
|
|
1191
|
+
warnings=[],
|
|
1192
|
+
metadata={},
|
|
1193
|
+
)
|
|
1194
|
+
)
|
|
1195
|
+
result.parts.append(SheetPart(read.sheet, read.warnings, text))
|
|
1196
|
+
# A file calls itself voluntary where each of its sheets does
|
|
1197
|
+
# (SDB-142): one sheet's word does not speak for the others.
|
|
1198
|
+
result.sheet.stated_voluntary = all(
|
|
1199
|
+
part.sheet.stated_voluntary for part in result.parts
|
|
1200
|
+
)
|
|
1201
|
+
result.sheet.stated_article = all(
|
|
1202
|
+
part.sheet.stated_article for part in result.parts
|
|
1203
|
+
)
|
|
1204
|
+
return result
|
|
1205
|
+
|
|
1206
|
+
def _parse_text(self, extraction: ExtractionResult) -> ParsedSheet:
|
|
1207
|
+
sections = split_sections(extraction.text)
|
|
1208
|
+
|
|
1209
|
+
hazards, hazard_warnings = _parse_hazard_statements(
|
|
1210
|
+
sections, extraction.text, pages=extraction.pages_sampled
|
|
1211
|
+
)
|
|
1212
|
+
stated = stated_classification(sections.get(Section.HAZARDS, ""))
|
|
1213
|
+
if (
|
|
1214
|
+
stated is StatedClassification.NOT_CLASSIFIED
|
|
1215
|
+
and hazards.signal_word
|
|
1216
|
+
and not hazards.hazard_codes
|
|
1217
|
+
):
|
|
1218
|
+
# A signal word comes with a hazard statement. On a sheet that says
|
|
1219
|
+
# "not classified" and prints none, "Gefahr" is a word of its text
|
|
1220
|
+
# (SDB-77): "Es besteht keine Gefahr, wenn …", "Gefahr bestimmende
|
|
1221
|
+
# Komponente zur Etikettierung".
|
|
1222
|
+
hazard_warnings.append(
|
|
1223
|
+
Message("signal-word-not-taken", {"signal_word": hazards.signal_word})
|
|
1224
|
+
)
|
|
1225
|
+
hazards.signal_word = None
|
|
1226
|
+
contact_info, contact_warnings = _parse_contact_info(
|
|
1227
|
+
sections.get(Section.IDENTIFICATION, ""),
|
|
1228
|
+
_page_furniture(extraction.text, extraction.pages_sampled),
|
|
1229
|
+
)
|
|
1230
|
+
product_identifier = (
|
|
1231
|
+
_value_before_label(extraction.text)
|
|
1232
|
+
or _extract_product_identifier(sections.get(Section.IDENTIFICATION, ""))
|
|
1233
|
+
or _name_in_header(
|
|
1234
|
+
sections.get(Section.IDENTIFICATION, ""),
|
|
1235
|
+
_lines_above_section_1(extraction.text),
|
|
1236
|
+
)
|
|
1237
|
+
)
|
|
1238
|
+
|
|
1239
|
+
# Version and the dates are document-level facts carried in the page
|
|
1240
|
+
# header/footer, not Section 2 content -- reading only the hazard
|
|
1241
|
+
# section missed every sheet whose pagination put the footer elsewhere.
|
|
1242
|
+
version = _extract_version(extraction.text)
|
|
1243
|
+
revision_date = _extract_revision_date(extraction.text)
|
|
1244
|
+
issue_date = _extract_issue_date(extraction.text)
|
|
1245
|
+
if revision_date is None and issue_date is None:
|
|
1246
|
+
# An undated sheet cannot be ranked against its own revisions, and
|
|
1247
|
+
# cannot be checked against the statutory cut-offs that decide
|
|
1248
|
+
# whether it is still a valid document. See EFFECTIVE_DATE_PATTERN.
|
|
1249
|
+
revision_date = (
|
|
1250
|
+
_extract_date_from_columns(extraction.text)
|
|
1251
|
+
or _extract_date_below_label(extraction.text)
|
|
1252
|
+
or _extract_effective_date(extraction.text)
|
|
1253
|
+
or _extract_short_year_date(extraction.text)
|
|
1254
|
+
or _latest_dated_match(extraction.text, PLAIN_DATE_PATTERN)
|
|
1255
|
+
or _extract_month_year_date(extraction.text)
|
|
1256
|
+
)
|
|
1257
|
+
|
|
1258
|
+
warnings: list[Message] = [
|
|
1259
|
+
*extraction.warnings,
|
|
1260
|
+
*hazard_warnings,
|
|
1261
|
+
*contact_warnings,
|
|
1262
|
+
]
|
|
1263
|
+
|
|
1264
|
+
sheet = SafetyDataSheet(
|
|
1265
|
+
product_identifier=product_identifier,
|
|
1266
|
+
issue_date=issue_date,
|
|
1267
|
+
revision_date=revision_date,
|
|
1268
|
+
version=version,
|
|
1269
|
+
language=self.language,
|
|
1270
|
+
hazard_statements=hazards,
|
|
1271
|
+
contact_info=contact_info,
|
|
1272
|
+
section_text=sections,
|
|
1273
|
+
countries=sheet_countries(
|
|
1274
|
+
extraction.text, sections.get(Section.IDENTIFICATION, "")
|
|
1275
|
+
),
|
|
1276
|
+
stated_classification=stated,
|
|
1277
|
+
stated_voluntary=says_voluntary(extraction.text),
|
|
1278
|
+
stated_article=says_article(extraction.text),
|
|
1279
|
+
)
|
|
1280
|
+
|
|
1281
|
+
return ParsedSheet(sheet=sheet, warnings=warnings)
|
|
1282
|
+
|
|
1283
|
+
|
|
1284
|
+
def read_numbered_section_heading(line: str) -> int | None:
|
|
1285
|
+
"""Return the section number of a plainly numbered heading, else None.
|
|
1286
|
+
|
|
1287
|
+
Recognises "2. Mögliche Gefahren" and "2 Mögliche Gefahren", the two forms
|
|
1288
|
+
that carry no "Abschnitt" for SECTION_HEADER_PATTERN to find. The title must
|
|
1289
|
+
agree with the number: see SECTION_TITLE_PATTERNS for why the number alone
|
|
1290
|
+
is not enough.
|
|
1291
|
+
"""
|
|
1292
|
+
match = NUMBERED_SECTION_PATTERN.match(line)
|
|
1293
|
+
if match is None:
|
|
1294
|
+
return None
|
|
1295
|
+
number = int(match.group(1))
|
|
1296
|
+
title_pattern = SECTION_TITLE_PATTERNS.get(number)
|
|
1297
|
+
if title_pattern is not None and title_pattern.search(match.group(2)):
|
|
1298
|
+
return number
|
|
1299
|
+
# The order before REACH (SDB-89): the composition was section 2 and the
|
|
1300
|
+
# hazards section 3. The title says which section it is.
|
|
1301
|
+
swapped = _ORDER_BEFORE_REACH.get(number)
|
|
1302
|
+
if swapped is not None and SECTION_TITLE_PATTERNS[swapped].search(match.group(2)):
|
|
1303
|
+
return swapped
|
|
1304
|
+
return None
|
|
1305
|
+
|
|
1306
|
+
|
|
1307
|
+
_ORDER_BEFORE_REACH = {2: 3, 3: 2}
|
|
1308
|
+
|
|
1309
|
+
|
|
1310
|
+
def _merge_split_headings(lines: list[str]) -> list[str]:
|
|
1311
|
+
"""Join a heading whose number and title landed on separate lines.
|
|
1312
|
+
|
|
1313
|
+
Only when the join reads as a heading afterwards -- the title still has to
|
|
1314
|
+
agree with the number, exactly as it does on one line. A bare "2" followed
|
|
1315
|
+
by a table value stays two lines, which is what keeps this from eating the
|
|
1316
|
+
commonest line in a data sheet.
|
|
1317
|
+
"""
|
|
1318
|
+
merged: list[str] = []
|
|
1319
|
+
i = 0
|
|
1320
|
+
while i < len(lines):
|
|
1321
|
+
current = lines[i].strip()
|
|
1322
|
+
if BARE_SECTION_NUMBER_PATTERN.match(current):
|
|
1323
|
+
j = i + 1
|
|
1324
|
+
while j < len(lines) and not lines[j].strip():
|
|
1325
|
+
j += 1
|
|
1326
|
+
if j < len(lines):
|
|
1327
|
+
candidate = f"{current.rstrip('.').strip()} {lines[j].strip()}"
|
|
1328
|
+
if read_numbered_section_heading(candidate) is not None:
|
|
1329
|
+
merged.append(candidate)
|
|
1330
|
+
i = j + 1
|
|
1331
|
+
continue
|
|
1332
|
+
merged.append(lines[i])
|
|
1333
|
+
i += 1
|
|
1334
|
+
return merged
|
|
1335
|
+
|
|
1336
|
+
|
|
1337
|
+
def _read_section_heading(line: str) -> tuple[int, str | None, bool] | None:
|
|
1338
|
+
"""The section number a heading line opens, the text after it on the same
|
|
1339
|
+
line, and whether the line carries its section's title.
|
|
1340
|
+
|
|
1341
|
+
A plainly numbered heading always does: read_numbered_section_heading
|
|
1342
|
+
accepts it only with the title.
|
|
1343
|
+
"""
|
|
1344
|
+
number = read_numbered_section_heading(line)
|
|
1345
|
+
if number is not None:
|
|
1346
|
+
return number, None, True
|
|
1347
|
+
match = SECTION_HEADER_PATTERN.search(line)
|
|
1348
|
+
if match is None:
|
|
1349
|
+
return None
|
|
1350
|
+
number = int(match.group(1))
|
|
1351
|
+
remainder = SECTION_HEADER_PATTERN.sub("", line, count=1).strip(" :")
|
|
1352
|
+
title_pattern = SECTION_TITLE_PATTERNS.get(number)
|
|
1353
|
+
titled = bool(title_pattern and title_pattern.search(remainder))
|
|
1354
|
+
if not titled and remainder[:1].islower():
|
|
1355
|
+
# A sentence that wraps before the word: "… ist der volle Wortlaut in"
|
|
1356
|
+
# / "Abschnitt 16 zu finden." is a reference, whatever its number.
|
|
1357
|
+
return None
|
|
1358
|
+
return number, remainder, titled
|
|
1359
|
+
|
|
1360
|
+
|
|
1361
|
+
def split_sections(text: str) -> dict[Section, str]:
|
|
1362
|
+
"""Split normalised SDS text into section buckets.
|
|
1363
|
+
|
|
1364
|
+
A heading numbered below a section already begun in the same sheet, and
|
|
1365
|
+
without its own section's title, is a line of the section it stands in
|
|
1366
|
+
(SDB-21). One supplier's sheets close with a change list whose every line opens
|
|
1367
|
+
"ABSCHNITT 2 gelöscht: P405 …"; read as headings, they put the deleted
|
|
1368
|
+
statements back into section 2 -- into the parser's statement text and into
|
|
1369
|
+
what the model is shown.
|
|
1370
|
+
|
|
1371
|
+
The title is what keeps the rule from going further. Sections do not
|
|
1372
|
+
reliably come in order: a line opening "Abschnitt 8 zu geeigneten
|
|
1373
|
+
Materialien …" in section 6 is a reference, not a heading, and two-column
|
|
1374
|
+
layouts reach the text layer as "6, 8, 7, 9". Ignoring every heading below
|
|
1375
|
+
the highest so far emptied 519 sections of 603 sheets. Section 1
|
|
1376
|
+
begins the next sheet, as in _split_sheets, whose section 2 is a section 2
|
|
1377
|
+
again.
|
|
1378
|
+
"""
|
|
1379
|
+
|
|
1380
|
+
sections: dict[Section, list[str]] = {}
|
|
1381
|
+
current_section: Section | None = None
|
|
1382
|
+
highest = 0
|
|
1383
|
+
|
|
1384
|
+
lines = text.split("\n")
|
|
1385
|
+
merged_lines = _merge_split_headings(_merge_broken_pairs(lines))
|
|
1386
|
+
seen: set[int] = set()
|
|
1387
|
+
|
|
1388
|
+
for line in merged_lines:
|
|
1389
|
+
stripped = line.strip()
|
|
1390
|
+
if not stripped:
|
|
1391
|
+
if current_section is not None:
|
|
1392
|
+
sections.setdefault(current_section, []).append("")
|
|
1393
|
+
continue
|
|
1394
|
+
|
|
1395
|
+
heading = _read_section_heading(stripped)
|
|
1396
|
+
if heading is not None:
|
|
1397
|
+
section_number, remainder, titled = heading
|
|
1398
|
+
if section_number == 1 and highest >= 2:
|
|
1399
|
+
highest = 0
|
|
1400
|
+
if section_number >= highest or titled:
|
|
1401
|
+
highest = max(highest, section_number)
|
|
1402
|
+
current_section = SECTION_BY_NUMBER.get(section_number, Section.OTHER)
|
|
1403
|
+
seen.add(section_number)
|
|
1404
|
+
if remainder:
|
|
1405
|
+
sections.setdefault(current_section, []).append(remainder)
|
|
1406
|
+
continue
|
|
1407
|
+
|
|
1408
|
+
subsection = SUBSECTION_PATTERN.match(stripped)
|
|
1409
|
+
if subsection:
|
|
1410
|
+
section_number = int(subsection.group(1))
|
|
1411
|
+
if (
|
|
1412
|
+
section_number not in seen
|
|
1413
|
+
and highest <= section_number
|
|
1414
|
+
and SUBSECTION_TITLES[section_number].search(subsection.group(3))
|
|
1415
|
+
):
|
|
1416
|
+
current_section = SECTION_BY_NUMBER[section_number]
|
|
1417
|
+
seen.add(section_number)
|
|
1418
|
+
highest = section_number
|
|
1419
|
+
sections.setdefault(current_section, []).append(stripped)
|
|
1420
|
+
continue
|
|
1421
|
+
|
|
1422
|
+
if current_section is None:
|
|
1423
|
+
# skip preamble until the first recognised section appears
|
|
1424
|
+
continue
|
|
1425
|
+
|
|
1426
|
+
sections.setdefault(current_section, []).append(stripped)
|
|
1427
|
+
|
|
1428
|
+
return {
|
|
1429
|
+
section: "\n".join(filter(None, lines)).strip()
|
|
1430
|
+
for section, lines in sections.items()
|
|
1431
|
+
if lines
|
|
1432
|
+
}
|
|
1433
|
+
|
|
1434
|
+
|
|
1435
|
+
def find_label_section(text: str) -> str | None:
|
|
1436
|
+
"""Return the text of section 2.2 (Kennzeichnungselemente), if present.
|
|
1437
|
+
|
|
1438
|
+
Returns None when the heading is absent, which leaves the caller on its
|
|
1439
|
+
previous source rather than guessing at a block.
|
|
1440
|
+
"""
|
|
1441
|
+
|
|
1442
|
+
start = LABEL_SECTION_START_PATTERN.search(text)
|
|
1443
|
+
if start:
|
|
1444
|
+
rest = text[start.end() :]
|
|
1445
|
+
end = _label_section_end(rest)
|
|
1446
|
+
block = rest[: end.start()] if end else rest[:LABEL_SECTION_MAX_CHARS]
|
|
1447
|
+
return block or None
|
|
1448
|
+
|
|
1449
|
+
# No "2.2". The heading may still be there under another number, which is
|
|
1450
|
+
# how sheets from before the REACH restructuring are written. Those are
|
|
1451
|
+
# taken only when a section boundary actually follows.
|
|
1452
|
+
#
|
|
1453
|
+
# The cap is not good enough here. Sheets in that shape often have no
|
|
1454
|
+
# recognisable heading after the label at all, and the block then runs 3500
|
|
1455
|
+
# characters into the ingredient table -- where H-statements belong to the
|
|
1456
|
+
# raw materials, not to the product. Measured, four sheets would have
|
|
1457
|
+
# reported H304 as their label while their label says "Kennzeichnung
|
|
1458
|
+
# entfällt". A block with no boundary is worth less than no block.
|
|
1459
|
+
heading = LABEL_HEADING_PATTERN.search(text)
|
|
1460
|
+
if not heading:
|
|
1461
|
+
return None
|
|
1462
|
+
rest = text[heading.end() :]
|
|
1463
|
+
end = _label_section_end(rest)
|
|
1464
|
+
if not end:
|
|
1465
|
+
return None
|
|
1466
|
+
return rest[: end.start()] or None
|
|
1467
|
+
|
|
1468
|
+
|
|
1469
|
+
def _label_section_end(rest: str) -> re.Match[str] | None:
|
|
1470
|
+
"""The first boundary in *rest* that is a heading, not a version number.
|
|
1471
|
+
|
|
1472
|
+
See VERSION_KEY_PATTERN. A boundary always begins a line, so the line before
|
|
1473
|
+
it is whatever *rest* holds up to it, read back to the last line break.
|
|
1474
|
+
"""
|
|
1475
|
+
for end in LABEL_SECTION_END_PATTERN.finditer(rest):
|
|
1476
|
+
preceding = rest[: end.start()].rstrip().rpartition("\n")[2]
|
|
1477
|
+
if not VERSION_KEY_PATTERN.fullmatch(preceding.strip()):
|
|
1478
|
+
return end
|
|
1479
|
+
return None
|
|
1480
|
+
|
|
1481
|
+
|
|
1482
|
+
def _parts_with_labels(text: str) -> list[tuple[str, str | None]]:
|
|
1483
|
+
"""Every part _split_sheets cuts *text* into, with its label if it has one.
|
|
1484
|
+
|
|
1485
|
+
The one place that decides how many sheets a file holds (SDB-2): the
|
|
1486
|
+
parts that carry a label of their own and are a sheet (_is_no_sheet); a
|
|
1487
|
+
part that is none comes back without its label.
|
|
1488
|
+
|
|
1489
|
+
One PDF can hold several complete sheets, one after another. Suppliers ship them
|
|
1490
|
+
for products that contain more than one chemical: a combination pack of two
|
|
1491
|
+
plant protection products, a two-component adhesive, a mortar with its
|
|
1492
|
+
hardener. 18 files in the corpus are built that way, for 13 products. Reading
|
|
1493
|
+
only the first label put half a product's hazards on it: a combination
|
|
1494
|
+
pack showed the insecticide's H411 and lost the fungicide's H410.
|
|
1495
|
+
|
|
1496
|
+
Every sheet is searched on its own, so a sheet's label ends where that
|
|
1497
|
+
sheet's section 2.3 does. A document holding one sheet gives exactly the
|
|
1498
|
+
block find_label_section returns for it -- measured over all 3232
|
|
1499
|
+
documents, the first block is unchanged on every one of them.
|
|
1500
|
+
"""
|
|
1501
|
+
pairs = [(part, find_label_section(part)) for part in _split_sheets(text)]
|
|
1502
|
+
sheets = [
|
|
1503
|
+
label is not None and not _is_no_sheet(part, label) for part, label in pairs
|
|
1504
|
+
]
|
|
1505
|
+
if not any(sheets):
|
|
1506
|
+
# Nothing here is a sheet by these signs: the labels stand as found.
|
|
1507
|
+
return pairs
|
|
1508
|
+
return [
|
|
1509
|
+
(part, label if sheet else None)
|
|
1510
|
+
for (part, label), sheet in zip(pairs, sheets, strict=True)
|
|
1511
|
+
]
|
|
1512
|
+
|
|
1513
|
+
|
|
1514
|
+
def _section_number(line: str) -> int | None:
|
|
1515
|
+
"""The number of the section *line* heads, in either way of writing it."""
|
|
1516
|
+
stripped = line.strip()
|
|
1517
|
+
number = read_numbered_section_heading(stripped)
|
|
1518
|
+
if number is not None:
|
|
1519
|
+
return number
|
|
1520
|
+
match = SECTION_HEADER_PATTERN.search(stripped)
|
|
1521
|
+
return int(match.group(1)) if match else None
|
|
1522
|
+
|
|
1523
|
+
|
|
1524
|
+
# A heading of section 1 in another language: "SECTION 1: Identification of
|
|
1525
|
+
# the substance/mixture …". "Section" with the German title is a German
|
|
1526
|
+
# heading.
|
|
1527
|
+
_FOREIGN_SECTION_1 = re.compile(
|
|
1528
|
+
r"^\W{0,6}(?:\d{1,2}\W{1,3})?s\s*e\s*c\s*t\s*i\s*o\s*n\s+0?1\b(.*)$",
|
|
1529
|
+
re.IGNORECASE,
|
|
1530
|
+
)
|
|
1531
|
+
# How many headings in a row make a list of sections.
|
|
1532
|
+
SECTION_LIST_LINES = 3
|
|
1533
|
+
|
|
1534
|
+
|
|
1535
|
+
def _is_no_sheet(part: str, label: str) -> bool:
|
|
1536
|
+
"""Whether a part with a label's heading is still no sheet (SDB-2).
|
|
1537
|
+
|
|
1538
|
+
Two kinds in the corpus. A list of changes at the end of section 16
|
|
1539
|
+
names the sections it changed one below the other -- "Abschnitt 1: UFI /
|
|
1540
|
+
Abschnitt 2: Einstufung/Kennzeichnungselemente / Abschnitt 3: …": that
|
|
1541
|
+
names the label's heading and holds no label. And the same sheet once
|
|
1542
|
+
more in another language, opened by "SECTION 1: Identification …": the
|
|
1543
|
+
reader reads German sheets.
|
|
1544
|
+
"""
|
|
1545
|
+
lines = [line.strip() for line in part.split("\n") if line.strip()]
|
|
1546
|
+
for line in lines:
|
|
1547
|
+
if _section_number(line) != 1:
|
|
1548
|
+
continue
|
|
1549
|
+
foreign = _FOREIGN_SECTION_1.match(line)
|
|
1550
|
+
if foreign and not SECTION_TITLE_PATTERNS[1].search(foreign.group(1)):
|
|
1551
|
+
return True
|
|
1552
|
+
break
|
|
1553
|
+
if label.strip():
|
|
1554
|
+
return False
|
|
1555
|
+
run = 0
|
|
1556
|
+
for line in lines:
|
|
1557
|
+
run = run + 1 if _section_number(line) is not None else 0
|
|
1558
|
+
if run >= SECTION_LIST_LINES:
|
|
1559
|
+
return True
|
|
1560
|
+
return False
|
|
1561
|
+
|
|
1562
|
+
|
|
1563
|
+
# What a cover page in front of a file's sheets is titled, at a line's start
|
|
1564
|
+
# (SDB-138): "Deckblatt", "Deckblatt zum Sicherheitsdatenblatt", "Kit
|
|
1565
|
+
# Sicherheitsinformationsblatt", "Sicherheitsdatenblätter für Kombi-Produkte",
|
|
1566
|
+
# "Set aus Sicherheitsdatenblättern bestehend aus:", "Setbestandteile".
|
|
1567
|
+
_COVER_TITLE = re.compile(
|
|
1568
|
+
r"^\W*(?:Deckblatt\b|Kit[- ]?Sicherheits(?:informations|daten)blatt\b"
|
|
1569
|
+
r"|Sicherheitsdatenbl(?:ä|ae)tter\b"
|
|
1570
|
+
r"|Set\s+(?:aus|von)\s+Sicherheitsdatenbl(?:ä|ae)ttern\b|Setbestandteile\b)",
|
|
1571
|
+
re.IGNORECASE,
|
|
1572
|
+
)
|
|
1573
|
+
# A word that names a display, the unit a supplier ships to be set up in a
|
|
1574
|
+
# shop: "Mix-Display", "Mixdisplay".
|
|
1575
|
+
_DISPLAY_TERM = re.compile(r"[\w-]*display[\w-]*", re.IGNORECASE)
|
|
1576
|
+
|
|
1577
|
+
|
|
1578
|
+
def split_cover_page(text: str) -> tuple[str, str]:
|
|
1579
|
+
"""The cover page in front of *text*'s sheets, and the text behind it (SDB-138).
|
|
1580
|
+
|
|
1581
|
+
A cover page is titled as one (_COVER_TITLE) among the first
|
|
1582
|
+
SHEET_TOP_LINES lines with text, and ends where the first sheet begins:
|
|
1583
|
+
at the top (_sheet_top) of the first heading of section 1 that is not
|
|
1584
|
+
the cover page's own. A cover page may head a section 1 itself, "Kit
|
|
1585
|
+
Identifizierung" or a country's additions: that heading is its own where
|
|
1586
|
+
the cover's title is the nearest title above it. Without such a title,
|
|
1587
|
+
or without a sheet behind it, there is no cover page and *text* comes
|
|
1588
|
+
back whole. The two joined by a line break are *text* again.
|
|
1589
|
+
"""
|
|
1590
|
+
lines = text.split("\n")
|
|
1591
|
+
titled = None
|
|
1592
|
+
seen = 0
|
|
1593
|
+
for index, line in enumerate(lines):
|
|
1594
|
+
if not line.strip():
|
|
1595
|
+
continue
|
|
1596
|
+
seen += 1
|
|
1597
|
+
if seen > SHEET_TOP_LINES:
|
|
1598
|
+
break
|
|
1599
|
+
if _COVER_TITLE.match(line):
|
|
1600
|
+
titled = index
|
|
1601
|
+
break
|
|
1602
|
+
if titled is None:
|
|
1603
|
+
return "", text
|
|
1604
|
+
for index in range(titled + 1, len(lines)):
|
|
1605
|
+
if _section_number(lines[index]) != 1:
|
|
1606
|
+
continue
|
|
1607
|
+
top = _sheet_top(lines, index, titled)
|
|
1608
|
+
if _nearest_title_is_the_cover_s(lines, index, top):
|
|
1609
|
+
continue
|
|
1610
|
+
return "\n".join(lines[:top]), "\n".join(lines[top:])
|
|
1611
|
+
return "", text
|
|
1612
|
+
|
|
1613
|
+
|
|
1614
|
+
def _nearest_title_is_the_cover_s(lines: Sequence[str], heading: int, top: int) -> bool:
|
|
1615
|
+
"""Whether the heading in line *heading* is a cover page's own.
|
|
1616
|
+
|
|
1617
|
+
*top* is where _sheet_top puts the heading's sheet: the heading itself
|
|
1618
|
+
where no sheet's title stands above it. Then the cover's title among
|
|
1619
|
+
the lines right above makes it the cover's.
|
|
1620
|
+
"""
|
|
1621
|
+
if top != heading:
|
|
1622
|
+
return False
|
|
1623
|
+
seen = 0
|
|
1624
|
+
for index in range(heading - 1, -1, -1):
|
|
1625
|
+
if not lines[index].strip():
|
|
1626
|
+
continue
|
|
1627
|
+
seen += 1
|
|
1628
|
+
if seen > SHEET_TOP_LINES:
|
|
1629
|
+
return False
|
|
1630
|
+
if _COVER_TITLE.match(lines[index]):
|
|
1631
|
+
return True
|
|
1632
|
+
return False
|
|
1633
|
+
|
|
1634
|
+
|
|
1635
|
+
def _display_term(cover: str) -> str | None:
|
|
1636
|
+
"""The first word on the cover page that names a display, as printed."""
|
|
1637
|
+
found = _DISPLAY_TERM.search(cover)
|
|
1638
|
+
return found.group(0).strip("-_") if found else None
|
|
1639
|
+
|
|
1640
|
+
|
|
1641
|
+
def split_into_sheets(text: str) -> list[str]:
|
|
1642
|
+
"""The text of every safety data sheet *text* holds, in its order (SDB-2).
|
|
1643
|
+
|
|
1644
|
+
A cover page in front of the sheets is none of them and is left out
|
|
1645
|
+
(SDB-138). As many as _parts_with_labels finds labels, and at least one:
|
|
1646
|
+
a file with fewer than two labelled parts is one sheet and comes back
|
|
1647
|
+
whole. A part without a label is no sheet of its own -- a list of
|
|
1648
|
+
revisions that names "Abschnitt 1" at the end of section 16 -- and stays
|
|
1649
|
+
with the sheet before it, or with the first sheet where it stands before
|
|
1650
|
+
that. The sheets joined by line breaks are *text* again, without its
|
|
1651
|
+
cover page.
|
|
1652
|
+
"""
|
|
1653
|
+
text = split_cover_page(text)[1]
|
|
1654
|
+
parts = _parts_with_labels(text)
|
|
1655
|
+
if sum(label is not None for _, label in parts) < 2:
|
|
1656
|
+
return [text]
|
|
1657
|
+
sheets: list[list[str]] = []
|
|
1658
|
+
labelled = False
|
|
1659
|
+
for part, label in parts:
|
|
1660
|
+
if sheets and not (labelled and label is not None):
|
|
1661
|
+
sheets[-1].append(part)
|
|
1662
|
+
labelled = labelled or label is not None
|
|
1663
|
+
else:
|
|
1664
|
+
sheets.append([part])
|
|
1665
|
+
labelled = label is not None
|
|
1666
|
+
return ["\n".join(sheet) for sheet in sheets]
|
|
1667
|
+
|
|
1668
|
+
|
|
1669
|
+
def _split_sheets(text: str) -> list[str]:
|
|
1670
|
+
"""Cut *text* where a further safety data sheet begins.
|
|
1671
|
+
|
|
1672
|
+
A sheet begins where section 1 is headed after a later section has been,
|
|
1673
|
+
at the top of that heading's page as far as the text shows it (_sheet_top).
|
|
1674
|
+
Section 1 coming round again is not enough on its own: four single sheets
|
|
1675
|
+
in the corpus list their revisions at the end of section 16 --
|
|
1676
|
+
"Änderungen mit Version 1.2: / Abschnitt 1: Handelsname" -- which is why the
|
|
1677
|
+
caller counts only the parts that carry a label of their own. The cut is
|
|
1678
|
+
made between lines, so the parts join back into *text* exactly.
|
|
1679
|
+
"""
|
|
1680
|
+
lines = text.split("\n")
|
|
1681
|
+
starts = [0]
|
|
1682
|
+
highest = 0
|
|
1683
|
+
for index, line in enumerate(lines):
|
|
1684
|
+
number = _section_number(line)
|
|
1685
|
+
if number is None:
|
|
1686
|
+
continue
|
|
1687
|
+
if number == 1 and highest >= 2:
|
|
1688
|
+
starts.append(_sheet_top(lines, index, starts[-1]))
|
|
1689
|
+
highest = 0
|
|
1690
|
+
highest = max(highest, number)
|
|
1691
|
+
bounds = [*starts, len(lines)]
|
|
1692
|
+
return ["\n".join(lines[start:end]) for start, end in pairwise(bounds)]
|
|
1693
|
+
|
|
1694
|
+
|
|
1695
|
+
# The title a sheet prints at the top of its first page, at a line's start.
|
|
1696
|
+
_SHEET_TITLE = re.compile(r"^\W*(?:E[GU]-)?Sicherheitsdatenblatt", re.IGNORECASE)
|
|
1697
|
+
# How many lines with text above a heading of section 1 can be its page's top.
|
|
1698
|
+
SHEET_TOP_LINES = 12
|
|
1699
|
+
|
|
1700
|
+
|
|
1701
|
+
def _sheet_top(lines: Sequence[str], heading: int, floor: int) -> int:
|
|
1702
|
+
"""Where the sheet begins whose section 1 is headed in line *heading* (SDB-2).
|
|
1703
|
+
|
|
1704
|
+
A sheet prints its title, its product and its dates above section 1, at
|
|
1705
|
+
the top of its first page. The text holds no page boundaries, so the top
|
|
1706
|
+
is the nearest line above the heading that begins with the title, among
|
|
1707
|
+
the SHEET_TOP_LINES lines with text right above it and not before
|
|
1708
|
+
*floor*; else the heading itself.
|
|
1709
|
+
"""
|
|
1710
|
+
seen = 0
|
|
1711
|
+
for index in range(heading - 1, floor, -1):
|
|
1712
|
+
if not lines[index].strip():
|
|
1713
|
+
continue
|
|
1714
|
+
seen += 1
|
|
1715
|
+
if seen > SHEET_TOP_LINES:
|
|
1716
|
+
break
|
|
1717
|
+
if _SHEET_TITLE.match(lines[index]):
|
|
1718
|
+
return index
|
|
1719
|
+
return heading
|
|
1720
|
+
|
|
1721
|
+
|
|
1722
|
+
def read_signal_word(label_text: str) -> tuple[str | None, bool]:
|
|
1723
|
+
"""Read the signal word out of a label section.
|
|
1724
|
+
|
|
1725
|
+
Returns the word and whether the label said anything about it at all. Three
|
|
1726
|
+
answers, and only the last of them is silence:
|
|
1727
|
+
|
|
1728
|
+
* ``("Gefahr", True)`` -- the label prints it
|
|
1729
|
+
* ``(None, True)`` -- the label states that it has none, either of the
|
|
1730
|
+
signal word specifically or of the whole label
|
|
1731
|
+
* ``(None, False)`` -- the label is silent, and the caller may fall back
|
|
1732
|
+
|
|
1733
|
+
The negations are tested first, because a sheet that states an absence can
|
|
1734
|
+
still carry the word elsewhere in the block: one sheet prints "Signalwort
|
|
1735
|
+
entfällt" and then "Achtung" 570 characters later in an unrelated sentence.
|
|
1736
|
+
The statement anchored to the label wins.
|
|
1737
|
+
|
|
1738
|
+
Shared with the service layer so that "the label has settled this" means one
|
|
1739
|
+
thing in both places rather than two regexes drifting apart.
|
|
1740
|
+
"""
|
|
1741
|
+
if NO_SIGNAL_WORD_PATTERN.search(label_text) or UNLABELLED_PATTERN.search(
|
|
1742
|
+
label_text
|
|
1743
|
+
):
|
|
1744
|
+
return None, True
|
|
1745
|
+
|
|
1746
|
+
match = SIGNAL_WORD_PATTERN.search(label_text)
|
|
1747
|
+
if match:
|
|
1748
|
+
# Normalise "GEFAHR" to "Gefahr": the value is displayed, and the
|
|
1749
|
+
# sheet's typography is not part of the classification.
|
|
1750
|
+
raw = _clean_value(match.group(1))
|
|
1751
|
+
return (raw.capitalize() if raw else raw), True
|
|
1752
|
+
|
|
1753
|
+
return None, False
|
|
1754
|
+
|
|
1755
|
+
|
|
1756
|
+
# A joined code whose cell is too narrow for it (SDB-121): the code breaks
|
|
1757
|
+
# inside its last number, the rest of the number alone on the next line.
|
|
1758
|
+
# P305+P351+P33 BEI KONTAKT MIT DEN AUGEN: … Eventuell vorhandene
|
|
1759
|
+
# 8
|
|
1760
|
+
# Kontaktlinsen nach Möglichkeit entfernen. Weiter spülen.
|
|
1761
|
+
_BROKEN_CODE = re.compile(
|
|
1762
|
+
r"(?m)^([^\S\n]*(?:[HP]\d{3}[^\S\n]*\+[^\S\n]*)+[HP])(\d{1,2})(?!\d)([^\n]*)\n"
|
|
1763
|
+
r"[^\S\n]*(\d{1,2})[^\S\n]*$"
|
|
1764
|
+
)
|
|
1765
|
+
|
|
1766
|
+
|
|
1767
|
+
# "EUH 208" for EUH208 (SDB-121). Not H and P: "P 750" can be a product's name.
|
|
1768
|
+
_SPACED_EUH_CODE = re.compile(r"\bEUH[^\S\n](?=\d{3}\b)")
|
|
1769
|
+
|
|
1770
|
+
|
|
1771
|
+
def _mend_codes(text: str) -> str:
|
|
1772
|
+
"""*text* with its statement codes as the regulation writes them (SDB-121):
|
|
1773
|
+
a code whole that broke inside its last number, EUH and its number joined."""
|
|
1774
|
+
|
|
1775
|
+
def whole(match: re.Match[str]) -> str:
|
|
1776
|
+
head, begun, rest, end = match.groups()
|
|
1777
|
+
if len(begun) + len(end) != 3:
|
|
1778
|
+
return match.group(0)
|
|
1779
|
+
return f"{head}{begun}{end}{rest}"
|
|
1780
|
+
|
|
1781
|
+
return _SPACED_EUH_CODE.sub("EUH", _BROKEN_CODE.sub(whole, text))
|
|
1782
|
+
|
|
1783
|
+
|
|
1784
|
+
def _without_the_old_directives_phrase(text: str) -> tuple[str, list[str]]:
|
|
1785
|
+
"""*text* without the Preparations Directive's sentence that a safety data
|
|
1786
|
+
sheet is to be had on request, and the sentences taken out (SDB-123): it
|
|
1787
|
+
is no statement of CLP and gets no code; the caller keeps it as printed.
|
|
1788
|
+
"""
|
|
1789
|
+
taken = [" ".join(found.split()) for found in OLD_REQUEST_SENTENCE.findall(text)]
|
|
1790
|
+
return OLD_REQUEST_SENTENCE.sub("", text), taken
|
|
1791
|
+
|
|
1792
|
+
|
|
1793
|
+
def _parse_hazard_statements(
|
|
1794
|
+
sections: dict[Section, str],
|
|
1795
|
+
full_text: str,
|
|
1796
|
+
*,
|
|
1797
|
+
pages: int,
|
|
1798
|
+
) -> tuple[HazardStatements, list[Message]]:
|
|
1799
|
+
warnings: list[Message] = []
|
|
1800
|
+
full_text, _ = _without_the_old_directives_phrase(_mend_codes(full_text))
|
|
1801
|
+
old_phrases: list[str] = []
|
|
1802
|
+
if Section.HAZARDS in sections:
|
|
1803
|
+
hazards_text, old_phrases = _without_the_old_directives_phrase(
|
|
1804
|
+
_mend_codes(sections[Section.HAZARDS])
|
|
1805
|
+
)
|
|
1806
|
+
sections = {**sections, Section.HAZARDS: hazards_text}
|
|
1807
|
+
|
|
1808
|
+
# Prefer the label block. Section splitting mis-assigns the hazards section
|
|
1809
|
+
# often enough to matter -- in one sheet it captured section 11 text, so no
|
|
1810
|
+
# code was found although every one of them sat in the document -- and even
|
|
1811
|
+
# when it lands correctly it spans 2.1 as well, mixing classification
|
|
1812
|
+
# statements into the label.
|
|
1813
|
+
labelled = [
|
|
1814
|
+
(part, label)
|
|
1815
|
+
for part, label in (_parts_with_labels(full_text) if full_text else [])
|
|
1816
|
+
if label is not None
|
|
1817
|
+
]
|
|
1818
|
+
labels = [label for _, label in labelled]
|
|
1819
|
+
if not labels and full_text:
|
|
1820
|
+
warnings.append(Message("no-label-section"))
|
|
1821
|
+
if len(labels) > 1:
|
|
1822
|
+
warnings.append(Message("several-sheets", {"count": len(labels)}))
|
|
1823
|
+
|
|
1824
|
+
# Each sheet's label is read as if it stood alone, then the product carries
|
|
1825
|
+
# all of them. A combination pack holds both products, so it carries both
|
|
1826
|
+
# hazards: such a pack is H410 and H411, not whichever sheet came
|
|
1827
|
+
# first. Combining after reading, not before, is what keeps one sheet's
|
|
1828
|
+
# "Kein Signalwort" from silencing the other sheet's "Achtung".
|
|
1829
|
+
readings = [
|
|
1830
|
+
_read_label(label, _classification_referred_to(part, label))
|
|
1831
|
+
for part, label in labelled
|
|
1832
|
+
] or [_read_label(sections.get(Section.HAZARDS, ""))]
|
|
1833
|
+
for reading in readings:
|
|
1834
|
+
warnings.extend(reading.warnings)
|
|
1835
|
+
|
|
1836
|
+
hazard_codes = sorted(set().union(*(r.hazard_codes for r in readings)))
|
|
1837
|
+
precautionary_codes = sorted(
|
|
1838
|
+
set().union(*(r.precautionary_codes for r in readings))
|
|
1839
|
+
)
|
|
1840
|
+
|
|
1841
|
+
if not hazard_codes:
|
|
1842
|
+
# A sheet from before CLP states its hazards as R phrases and hazard
|
|
1843
|
+
# symbols. Read as a CLP label it yields what it does not say: "R33:
|
|
1844
|
+
# Gefahr kumulativer Wirkungen." gave the signal word "Gefahr", and the
|
|
1845
|
+
# S phrase "Bei Berührung mit der Haut sofort abwaschen …" the code P302
|
|
1846
|
+
# by its wording (SDB-52). Such a sheet carries no CLP label at all.
|
|
1847
|
+
old = old_classification(
|
|
1848
|
+
"\n".join([sections.get(Section.HAZARDS, ""), *labels])
|
|
1849
|
+
)
|
|
1850
|
+
if old is not None:
|
|
1851
|
+
return HazardStatements(), [Message(CLASSIFIED_BEFORE_CLP, {"basis": old})]
|
|
1852
|
+
warnings.append(Message("no-hazard-codes"))
|
|
1853
|
+
# Two very different documents reach this line, and reporting has to be
|
|
1854
|
+
# able to tell them apart. One states that the product carries no label;
|
|
1855
|
+
# the other states that nobody worked out what its label should be. The
|
|
1856
|
+
# second is not a product without hazards, it is a sheet without an
|
|
1857
|
+
# answer -- and a product resting on one belongs on the list of sheets
|
|
1858
|
+
# the supplier owes a replacement for, not in the untroubled majority.
|
|
1859
|
+
if any(states_undetermined_labelling(label) for label in labels):
|
|
1860
|
+
warnings.append(Message(LABEL_UNDETERMINED))
|
|
1861
|
+
if not precautionary_codes:
|
|
1862
|
+
warnings.append(Message("no-precautionary-codes"))
|
|
1863
|
+
|
|
1864
|
+
# The label's own block of supplemental elements can stand outside the block
|
|
1865
|
+
# read as the label: set in two columns, "Ergänzende Kennzeichnungselemente:
|
|
1866
|
+
# Enthält … Kann allergische Reaktionen hervorrufen." reached the text after
|
|
1867
|
+
# 2.3 in the file's order on the sheets of four paint brands.
|
|
1868
|
+
# In print order (SDB-42) it stands in the label block, and these
|
|
1869
|
+
# passages change nothing else (215 sheets before, 0 after);
|
|
1870
|
+
# SDB-28 keeps them until the clause is withdrawn.
|
|
1871
|
+
supplemental = set().union(*(r.supplemental for r in readings))
|
|
1872
|
+
worded_supplemental = [w for r in readings for w in r.worded_supplemental]
|
|
1873
|
+
passages = _supplemental_passages(sections.get(Section.HAZARDS, ""))
|
|
1874
|
+
if labels and passages:
|
|
1875
|
+
beside = euh_statements_by_wording("\n".join(passages), skip=supplemental)
|
|
1876
|
+
worded_supplemental.extend(beside)
|
|
1877
|
+
supplemental |= {code for code, _ in beside}
|
|
1878
|
+
|
|
1879
|
+
hazard_statements = HazardStatements(
|
|
1880
|
+
hazard_codes=hazard_codes,
|
|
1881
|
+
precautionary_codes=precautionary_codes,
|
|
1882
|
+
supplemental_codes=sorted(supplemental),
|
|
1883
|
+
signal_word=_strongest_signal_word(r.signal_word for r in readings),
|
|
1884
|
+
pictograms=sorted(set().union(*(r.pictograms for r in readings))),
|
|
1885
|
+
)
|
|
1886
|
+
# The text has to say what the codes say. The codes are read off the label;
|
|
1887
|
+
# the text is cut from the whole hazards section, which also holds 2.1's
|
|
1888
|
+
# classification and, where a heading goes unread, the section 3 ingredient
|
|
1889
|
+
# table or the section 16 change log ("gelöscht: Asp. Tox. 1"). Measured over the
|
|
1890
|
+
# 1997 current sheets, 102 carried a statement their label does not. On 16
|
|
1891
|
+
# the label carries no code at all and the model found none either, so the
|
|
1892
|
+
# text went into the output as it was: a filler, "Nicht eingestuft"
|
|
1893
|
+
# in 2.1, displayed H330 "Lebensgefahr bei Einatmen" from a preservative
|
|
1894
|
+
# below 0.001%. On the other 86 the model's answer covered it.
|
|
1895
|
+
#
|
|
1896
|
+
# Dropping a hazard statement is the dangerous direction, so the label had
|
|
1897
|
+
# to prove itself first: on all 102 every block was found, and the model
|
|
1898
|
+
# confirmed a dropped code on two, both H318 beside H314 -- the pair Article
|
|
1899
|
+
# 27 lets the label print as H314 alone.
|
|
1900
|
+
furniture = _page_furniture(full_text, pages)
|
|
1901
|
+
hazard_statements.hazard_text = _statements_within(
|
|
1902
|
+
_extract_statements_from_sections(
|
|
1903
|
+
sections,
|
|
1904
|
+
allowed_sections={Section.HAZARDS},
|
|
1905
|
+
pattern=HAZARD_CODE_PATTERN,
|
|
1906
|
+
furniture=furniture,
|
|
1907
|
+
),
|
|
1908
|
+
set(hazard_codes),
|
|
1909
|
+
)
|
|
1910
|
+
# Precautionary text is deliberately not held to the label's codes: P101 and
|
|
1911
|
+
# P102 stood behind 2.3 on whole supplier templates, and they are wanted. In
|
|
1912
|
+
# print order (SDB-42) the label carries every code of the P text on all
|
|
1913
|
+
# 1,991 local sheets, 251 before; SDB-10 keeps the exception until it is
|
|
1914
|
+
# withdrawn.
|
|
1915
|
+
hazard_statements.precautionary_text = _extract_statements_from_sections(
|
|
1916
|
+
sections,
|
|
1917
|
+
allowed_sections={Section.HAZARDS},
|
|
1918
|
+
pattern=PRECAUTION_CODE_PATTERN,
|
|
1919
|
+
furniture=furniture,
|
|
1920
|
+
)
|
|
1921
|
+
# A label that prints its statements without their codes (SDB-6) leaves
|
|
1922
|
+
# no text to cut; the codes take the sentences they were read from.
|
|
1923
|
+
codeless = _passages(sections.get(Section.HAZARDS, "").split("\n"), furniture)
|
|
1924
|
+
if hazard_statements.hazard_text is None and hazard_codes:
|
|
1925
|
+
hazard_statements.hazard_text = _in_print_order(hazard_codes, codeless)
|
|
1926
|
+
if hazard_statements.precautionary_text is None and precautionary_codes:
|
|
1927
|
+
hazard_statements.precautionary_text = _in_print_order(
|
|
1928
|
+
precautionary_codes, codeless
|
|
1929
|
+
)
|
|
1930
|
+
supplemental_text = _extract_statements_from_sections(
|
|
1931
|
+
sections,
|
|
1932
|
+
allowed_sections={Section.HAZARDS},
|
|
1933
|
+
pattern=SUPPLEMENTAL_CODE_PATTERN,
|
|
1934
|
+
furniture=furniture,
|
|
1935
|
+
)
|
|
1936
|
+
# The text is cut from all of section 2, the codes from the label: a
|
|
1937
|
+
# statement the text holds under a code the label does not carry is the
|
|
1938
|
+
# label's if it is that code's wording, and else no statement (SDB-124).
|
|
1939
|
+
if supplemental_text:
|
|
1940
|
+
kept: list[str] = []
|
|
1941
|
+
for line in supplemental_text.split("\n"):
|
|
1942
|
+
code = SUPPLEMENTAL_CODE_PATTERN.match(line)
|
|
1943
|
+
if code is None or code.group(0).upper() in supplemental:
|
|
1944
|
+
kept.append(line)
|
|
1945
|
+
elif any(
|
|
1946
|
+
found == code.group(0).upper()
|
|
1947
|
+
for found, _ in euh_statements_by_wording(line)
|
|
1948
|
+
):
|
|
1949
|
+
kept.append(line)
|
|
1950
|
+
supplemental.add(code.group(0).upper())
|
|
1951
|
+
supplemental_text = "\n".join(kept) or None
|
|
1952
|
+
hazard_statements.supplemental_codes = sorted(supplemental)
|
|
1953
|
+
if supplemental_text:
|
|
1954
|
+
supplemental_text = _with_substances_before_the_code(
|
|
1955
|
+
supplemental_text, sections.get(Section.HAZARDS, "")
|
|
1956
|
+
)
|
|
1957
|
+
# On one line with the statement before, the worded one is part of it
|
|
1958
|
+
# (SDB-27 stops at a code, and SDB-51 keeps what a wording's own line
|
|
1959
|
+
# holds): "EUH210 … auf Anfrage erhältlich. Enthält Linalool. Kann
|
|
1960
|
+
# allergische …". The sentence leaves that statement and stands on its own
|
|
1961
|
+
# line, with its code.
|
|
1962
|
+
for _, sentence in worded_supplemental:
|
|
1963
|
+
if supplemental_text:
|
|
1964
|
+
supplemental_text = _without_sentence(supplemental_text, sentence)
|
|
1965
|
+
worded = [f"{code}: {sentence}" for code, sentence in worded_supplemental]
|
|
1966
|
+
# The old directive's request sentence: as printed, under no code (SDB-123).
|
|
1967
|
+
hazard_statements.supplemental_text = (
|
|
1968
|
+
"\n".join(
|
|
1969
|
+
dict.fromkeys([*filter(None, [supplemental_text]), *worded, *old_phrases])
|
|
1970
|
+
)
|
|
1971
|
+
or None
|
|
1972
|
+
)
|
|
1973
|
+
if labels:
|
|
1974
|
+
warnings.extend(
|
|
1975
|
+
_euh_wordings_outside(
|
|
1976
|
+
sections.get(Section.HAZARDS, ""),
|
|
1977
|
+
[*labels, *passages],
|
|
1978
|
+
set(hazard_statements.supplemental_codes),
|
|
1979
|
+
)
|
|
1980
|
+
)
|
|
1981
|
+
return hazard_statements, warnings
|
|
1982
|
+
|
|
1983
|
+
|
|
1984
|
+
# EUH208 printed with its code inside the sentence (SDB-125): "Enthält: A, B.
|
|
1985
|
+
# EUH208 Kann allergische Reaktionen hervorrufen.", or the code on a line of
|
|
1986
|
+
# its own between the two halves of the name.
|
|
1987
|
+
_SUBSTANCES_BEFORE_CODE = re.compile(
|
|
1988
|
+
r"(Enthält\b[^\n]*?)[^\S\n]*\n?[^\S\n]*EUH208\b", re.IGNORECASE
|
|
1989
|
+
)
|
|
1990
|
+
_EUH208_WITHOUT_SUBSTANCES = re.compile(
|
|
1991
|
+
r"^EUH208\b(?![^\n]*\bEnth[äa]lt)[^\n]*\S[^\n]*$"
|
|
1992
|
+
)
|
|
1993
|
+
|
|
1994
|
+
|
|
1995
|
+
def _with_substances_before_the_code(text: str, hazards_text: str) -> str:
|
|
1996
|
+
"""*text* with what EUH208 names where the sheet prints it before the code.
|
|
1997
|
+
|
|
1998
|
+
The statement is cut from its code on (SDB-27), which leaves "EUH208 Kann
|
|
1999
|
+
allergische Reaktionen hervorrufen." without the substances the sentence
|
|
2000
|
+
began with. They stand right before the code, on its line or the one above.
|
|
2001
|
+
"""
|
|
2002
|
+
before = _SUBSTANCES_BEFORE_CODE.search(hazards_text)
|
|
2003
|
+
if before is None or "hervorrufen" in before.group(1):
|
|
2004
|
+
return text
|
|
2005
|
+
substances = " ".join(before.group(1).split())
|
|
2006
|
+
lines = text.split("\n")
|
|
2007
|
+
for index, line in enumerate(lines):
|
|
2008
|
+
if _EUH208_WITHOUT_SUBSTANCES.match(line) and line.strip() != "EUH208":
|
|
2009
|
+
rest = re.sub(r"^EUH208[^\S\n]*[-:–]?[^\S\n]*", "", line)
|
|
2010
|
+
lines[index] = f"EUH208 {substances} {rest}"
|
|
2011
|
+
break
|
|
2012
|
+
return "\n".join(lines)
|
|
2013
|
+
|
|
2014
|
+
|
|
2015
|
+
# A heading of the label's supplemental elements, and the line that ends what
|
|
2016
|
+
# follows it: the next numbered heading ("2.3 Sonstige Gefahren").
|
|
2017
|
+
_SUPPLEMENTAL_HEADING = re.compile(
|
|
2018
|
+
r"Ergänzende\s+(?:Kennzeichnungselemente|Gefahrenmerkmale|Gefahrenhinweise|Angaben)"
|
|
2019
|
+
r"|Zusätzliche\s+Angaben",
|
|
2020
|
+
re.IGNORECASE,
|
|
2021
|
+
)
|
|
2022
|
+
_PASSAGE_END = re.compile(
|
|
2023
|
+
r"^\s*[·.]?\s*\d{1,2}\.\d{1,2}\.?(?:[^\S\n]+[A-ZÄÖÜ]|[^\S\n]*$)", re.MULTILINE
|
|
2024
|
+
)
|
|
2025
|
+
_PASSAGE_LIMIT = 800
|
|
2026
|
+
|
|
2027
|
+
|
|
2028
|
+
def _in_print_order(codes: list[str], passages: list[str]) -> str | None:
|
|
2029
|
+
statements = wordings_in_print_order(codes, passages)
|
|
2030
|
+
return "\n".join(statements) if statements else None
|
|
2031
|
+
|
|
2032
|
+
|
|
2033
|
+
def _without_sentence(text: str, sentence: str) -> str:
|
|
2034
|
+
words = re.escape(sentence).split(r"\ ")
|
|
2035
|
+
lines = [
|
|
2036
|
+
re.sub(r"[^\S\n]*" + r"\s+".join(words), "", line).rstrip()
|
|
2037
|
+
for line in text.split("\n")
|
|
2038
|
+
]
|
|
2039
|
+
return "\n".join(line for line in lines if line.strip())
|
|
2040
|
+
|
|
2041
|
+
|
|
2042
|
+
def _before_next_subsection(text: str) -> str:
|
|
2043
|
+
end = _OTHER_HAZARDS.search(text)
|
|
2044
|
+
return text[: end.start()] if end else text
|
|
2045
|
+
|
|
2046
|
+
|
|
2047
|
+
_OTHER_HAZARDS = re.compile(r"^\s*[·•∙.]?\s*2\.3\b", re.MULTILINE)
|
|
2048
|
+
|
|
2049
|
+
|
|
2050
|
+
def _supplemental_passages(hazards_text: str) -> list[str]:
|
|
2051
|
+
"""What section 2 prints under a heading of supplemental label elements.
|
|
2052
|
+
|
|
2053
|
+
Up to the next numbered heading, at most _PASSAGE_LIMIT characters. One sheet
|
|
2054
|
+
prints "Besondere Vorschriften für ergänzende
|
|
2055
|
+
Kennzeichnungselemente", then "2.3 Sonstige Gefahren" and under it
|
|
2056
|
+
"Wiederholter Kontakt kann zu spröder oder rissiger Haut führen." as an
|
|
2057
|
+
effect, not as EUH066: the heading ends the passage before that.
|
|
2058
|
+
"""
|
|
2059
|
+
passages: list[str] = []
|
|
2060
|
+
for heading in _SUPPLEMENTAL_HEADING.finditer(hazards_text):
|
|
2061
|
+
rest = hazards_text[heading.end() : heading.end() + _PASSAGE_LIMIT]
|
|
2062
|
+
end = _PASSAGE_END.search(rest)
|
|
2063
|
+
passages.append(rest[: end.start()] if end else rest)
|
|
2064
|
+
return passages
|
|
2065
|
+
|
|
2066
|
+
|
|
2067
|
+
def _euh_wordings_outside(
|
|
2068
|
+
hazards_text: str, labels: Sequence[str], on_label: set[str]
|
|
2069
|
+
) -> list[Message]:
|
|
2070
|
+
"""An EUH wording section 2 prints only outside the label (SDB-28).
|
|
2071
|
+
|
|
2072
|
+
It does not count -- under 2.3 it can describe an effect rather than label
|
|
2073
|
+
one ("Wiederholter Kontakt kann zu spröder oder
|
|
2074
|
+
rissiger Haut führen." under "Mögliche schädliche Wirkungen") -- but where
|
|
2075
|
+
it stands is the supplier's to put right, so it is reported: one
|
|
2076
|
+
sealant prints EUH208 under "2.4 Zusätzliche Hinweise".
|
|
2077
|
+
"""
|
|
2078
|
+
outside = hazards_text
|
|
2079
|
+
for label in labels:
|
|
2080
|
+
outside = outside.replace(label, " ")
|
|
2081
|
+
return [
|
|
2082
|
+
Message(EUH_WORDING_OUTSIDE_LABEL, {"statement": code, "sentence": sentence})
|
|
2083
|
+
for code, sentence in euh_statements_by_wording(
|
|
2084
|
+
outside, skip=on_label, partial=True
|
|
2085
|
+
)
|
|
2086
|
+
]
|
|
2087
|
+
|
|
2088
|
+
|
|
2089
|
+
@dataclass(slots=True)
|
|
2090
|
+
class _LabelReading:
|
|
2091
|
+
"""What one sheet's label carries."""
|
|
2092
|
+
|
|
2093
|
+
hazard_codes: set[str]
|
|
2094
|
+
precautionary_codes: set[str]
|
|
2095
|
+
supplemental: set[str]
|
|
2096
|
+
# EUH statements the label prints without their code, as (code, sentence).
|
|
2097
|
+
worded_supplemental: list[tuple[str, str]]
|
|
2098
|
+
signal_word: str | None
|
|
2099
|
+
pictograms: set[str]
|
|
2100
|
+
warnings: list[Message]
|
|
2101
|
+
|
|
2102
|
+
|
|
2103
|
+
def _strongest_signal_word(words: Iterable[str | None]) -> str | None:
|
|
2104
|
+
"""The stronger word: CLP Article 20(3) never prints Achtung beside Gefahr."""
|
|
2105
|
+
present = set(words)
|
|
2106
|
+
for word in ("Gefahr", "Achtung"):
|
|
2107
|
+
if word in present:
|
|
2108
|
+
return word
|
|
2109
|
+
return None
|
|
2110
|
+
|
|
2111
|
+
|
|
2112
|
+
def printed_hazard_codes(label_text: str) -> set[str]:
|
|
2113
|
+
"""The H-codes a label prints as its own, one code per element (SDB-5).
|
|
2114
|
+
|
|
2115
|
+
Codes standing only inside a classification entry are not label elements.
|
|
2116
|
+
Measured over the corpus this affects five sheets, and on every one of
|
|
2117
|
+
them the removed code is exactly what Article 26 lets the label omit --
|
|
2118
|
+
H400 beside H410, H318 beside H314. A code that also appears outside such
|
|
2119
|
+
an entry is kept, so a label that really does carry it is unaffected.
|
|
2120
|
+
|
|
2121
|
+
A combination is split into its codes: "H300+H310+H330" is H300, H310 and
|
|
2122
|
+
H330. Codes read off the wording (SDB-6) are not printed ones.
|
|
2123
|
+
"""
|
|
2124
|
+
return set(HAZARD_CODE_PATTERN.findall(CLASSIFICATION_ENTRY.sub(" ", label_text)))
|
|
2125
|
+
|
|
2126
|
+
|
|
2127
|
+
# A label that prints R or S phrases (SDB-120): their heading, or a phrase's
|
|
2128
|
+
# number in brackets behind its sentence ("… gelangen.(S2)").
|
|
2129
|
+
_OLD_PHRASES_PRINTED = re.compile(
|
|
2130
|
+
r"\b[RS][^\S\n]?-[^\S\n]?S[äa]tze\b|\([RS][^\S\n]?\d{1,2}(?:[/-][A-Z\d]{1,3})*\)"
|
|
2131
|
+
)
|
|
2132
|
+
|
|
2133
|
+
# A label that refers to the classification for its hazard statements
|
|
2134
|
+
# (SDB-119): "Gefahrenhinweis/e: siehe Abschnitt 2.1".
|
|
2135
|
+
_REFERS_TO_CLASSIFICATION = re.compile(
|
|
2136
|
+
r"Gefahrenhinweis(?:/e|e)?[^\S\n]*:?[^\S\n]*siehe[^\S\n]+"
|
|
2137
|
+
r"(?:(?:Unter)?[Aa]bschnitt|Punkt|Kapitel|Ziffer)?[^\S\n]*2\.1\b",
|
|
2138
|
+
re.IGNORECASE,
|
|
2139
|
+
)
|
|
2140
|
+
|
|
2141
|
+
|
|
2142
|
+
def _classification_referred_to(part: str, label: str) -> str | None:
|
|
2143
|
+
"""The classification of the sheet *part*, if its *label* refers to it (SDB-119)."""
|
|
2144
|
+
if not _REFERS_TO_CLASSIFICATION.search(label):
|
|
2145
|
+
return None
|
|
2146
|
+
hazards = split_sections(part).get(Section.HAZARDS, "")
|
|
2147
|
+
return "\n".join(_classification_parts(hazards))
|
|
2148
|
+
|
|
2149
|
+
|
|
2150
|
+
def _read_label(
|
|
2151
|
+
hazard_section_text: str, classification: str | None = None
|
|
2152
|
+
) -> _LabelReading:
|
|
2153
|
+
"""What a label carries; *classification* is the text of 2.1 where the
|
|
2154
|
+
label names it as the place of its hazard statements (SDB-119)."""
|
|
2155
|
+
warnings: list[Message] = []
|
|
2156
|
+
# What a sheet says it does not label with is no label (SDB-78).
|
|
2157
|
+
hazard_section_text = _LABEL_DECLINED.sub("", hazard_section_text)
|
|
2158
|
+
|
|
2159
|
+
hazard_codes = sorted(printed_hazard_codes(hazard_section_text))
|
|
2160
|
+
if not hazard_codes and classification is not None:
|
|
2161
|
+
hazard_codes = sorted(set(HAZARD_CODE_PATTERN.findall(classification)))
|
|
2162
|
+
precautionary_codes = sorted(
|
|
2163
|
+
set(PRECAUTION_CODE_PATTERN.findall(hazard_section_text))
|
|
2164
|
+
)
|
|
2165
|
+
|
|
2166
|
+
# A label that names no code at all is read by its sentences instead. Twenty
|
|
2167
|
+
# sheets here print the wording and nothing else -- "Gefahrenhinweise :
|
|
2168
|
+
# Giftig für Wasserorganismen mit langfristiger Wirkung." -- and the codes
|
|
2169
|
+
# are unambiguous from the regulation's own table.
|
|
2170
|
+
#
|
|
2171
|
+
# Only when nothing was found, never alongside. A label that names codes is
|
|
2172
|
+
# using codes, and the ones it names are the ones it means; adding to them
|
|
2173
|
+
# from a sentence somewhere in the block is how the ingredient table gets
|
|
2174
|
+
# onto a product. Measured over the corpus, every sheet this helps had an
|
|
2175
|
+
# empty list, so the narrower rule costs nothing and claims less.
|
|
2176
|
+
#
|
|
2177
|
+
# And never from the phrases of the rules before CLP (SDB-120): "S2 Darf
|
|
2178
|
+
# nicht in die Hände von Kindern gelangen." is worded like P102 and is not it.
|
|
2179
|
+
if (not hazard_codes or not precautionary_codes) and not (
|
|
2180
|
+
_OLD_PHRASES_PRINTED.search(hazard_section_text)
|
|
2181
|
+
or old_classification(hazard_section_text) is not None
|
|
2182
|
+
):
|
|
2183
|
+
by_wording = codes_from_wording(hazard_section_text)
|
|
2184
|
+
if not hazard_codes:
|
|
2185
|
+
# "H", not "not EUH": EUH statements have their own field, and P
|
|
2186
|
+
# statements are somebody else's list entirely.
|
|
2187
|
+
hazard_codes = sorted(c for c in by_wording if c.startswith("H"))
|
|
2188
|
+
if not precautionary_codes:
|
|
2189
|
+
precautionary_codes = sorted(c for c in by_wording if c.startswith("P"))
|
|
2190
|
+
supplemental = sorted(
|
|
2191
|
+
set(
|
|
2192
|
+
code.upper()
|
|
2193
|
+
for code in SUPPLEMENTAL_CODE_PATTERN.findall(hazard_section_text)
|
|
2194
|
+
)
|
|
2195
|
+
)
|
|
2196
|
+
# EUH statements by their wording, whatever codes the label prints (SDB-28):
|
|
2197
|
+
# 165 current sheets print EUH208 as "Enthält … Kann allergische Reaktionen
|
|
2198
|
+
# hervorrufen." without its code, among other codes or none, and nothing
|
|
2199
|
+
# else read it. A code the label prints is read with its own wording.
|
|
2200
|
+
# Not past a 2.3 the label's end did not catch ("· 2.3 Sonstige Gefahren"):
|
|
2201
|
+
# "Produkt reagiert heftig mit Wasser" there describes, it does not label.
|
|
2202
|
+
worded_supplemental = euh_statements_by_wording(
|
|
2203
|
+
_before_next_subsection(hazard_section_text), skip=set(supplemental)
|
|
2204
|
+
)
|
|
2205
|
+
supplemental = sorted(set(supplemental) | {c for c, _ in worded_supplemental})
|
|
2206
|
+
|
|
2207
|
+
signal_word, signal_word_stated = read_signal_word(hazard_section_text)
|
|
2208
|
+
|
|
2209
|
+
pictograms = sorted(
|
|
2210
|
+
set(code.upper() for code in PICTOGRAM_PATTERN.findall(hazard_section_text))
|
|
2211
|
+
)
|
|
2212
|
+
|
|
2213
|
+
h_code_set = set(hazard_codes)
|
|
2214
|
+
|
|
2215
|
+
# Fallback: derive pictograms from H-codes when not found in PDF text
|
|
2216
|
+
if not pictograms and h_code_set:
|
|
2217
|
+
derived = pictograms_for(h_code_set)
|
|
2218
|
+
if derived:
|
|
2219
|
+
pictograms = sorted(derived)
|
|
2220
|
+
warnings.append(Message("pictograms-derived", {"pictograms": pictograms}))
|
|
2221
|
+
else:
|
|
2222
|
+
# Always enforce CLP Art. 26 precedence rules, even on PDF-extracted codes.
|
|
2223
|
+
pdf_set = set(pictograms)
|
|
2224
|
+
enforced = pictograms_for(h_code_set) if h_code_set else pdf_set
|
|
2225
|
+
# A pictogram listed in the PDF or implied by the codes can suppress;
|
|
2226
|
+
# PDF-listed codes our mapping does not know are kept as they are.
|
|
2227
|
+
candidates = pdf_set | enforced
|
|
2228
|
+
removed = candidates - suppress_pictograms(candidates, h_code_set)
|
|
2229
|
+
if removed & pdf_set:
|
|
2230
|
+
pictograms = sorted(pdf_set - removed)
|
|
2231
|
+
|
|
2232
|
+
# Derive only where the sheet says nothing. A sheet that prints "Kein
|
|
2233
|
+
# Signalwort" has answered, and overriding that with a derived word would
|
|
2234
|
+
# replace what the supplier labelled with what our table computes.
|
|
2235
|
+
if not signal_word_stated and (h_code_set or supplemental):
|
|
2236
|
+
signal_word = get_signal_word(h_code_set | set(supplemental))
|
|
2237
|
+
if signal_word:
|
|
2238
|
+
warnings.append(Message("signal-word-derived"))
|
|
2239
|
+
|
|
2240
|
+
return _LabelReading(
|
|
2241
|
+
hazard_codes=h_code_set,
|
|
2242
|
+
precautionary_codes=set(precautionary_codes),
|
|
2243
|
+
supplemental=set(supplemental),
|
|
2244
|
+
worded_supplemental=worded_supplemental,
|
|
2245
|
+
signal_word=signal_word,
|
|
2246
|
+
pictograms=set(pictograms),
|
|
2247
|
+
warnings=warnings,
|
|
2248
|
+
)
|
|
2249
|
+
|
|
2250
|
+
|
|
2251
|
+
def _page_break_beside(lines: list[str], index: int, furniture: frozenset[str]) -> bool:
|
|
2252
|
+
"""Whether a page number or the page header stands next to line *index*."""
|
|
2253
|
+
for neighbour in (index - 1, index + 1):
|
|
2254
|
+
if 0 <= neighbour < len(lines):
|
|
2255
|
+
line = lines[neighbour].strip()
|
|
2256
|
+
if line in furniture or _PAGE_NUMBER.match(line):
|
|
2257
|
+
return True
|
|
2258
|
+
return False
|
|
2259
|
+
|
|
2260
|
+
|
|
2261
|
+
def _ends_statement(line: str) -> bool:
|
|
2262
|
+
"""Whether *line* begins something other than the statement before it."""
|
|
2263
|
+
return bool(
|
|
2264
|
+
STATEMENT_CODE_PREFIX.match(line)
|
|
2265
|
+
or _ANY_STATEMENT_CODE.search(line)
|
|
2266
|
+
or _NUMBERED_SUBHEADING.match(line)
|
|
2267
|
+
or _LABEL_HEADING.match(line)
|
|
2268
|
+
or _CLASSIFICATION_LINE.search(line)
|
|
2269
|
+
)
|
|
2270
|
+
|
|
2271
|
+
|
|
2272
|
+
def _is_classification_entry(line: str) -> bool:
|
|
2273
|
+
"""Whether a hazard class stands before the first code of *line* (SDB-41).
|
|
2274
|
+
|
|
2275
|
+
"Aquatic Chronic 3, H412" lists 2.1's classification, not a statement. The
|
|
2276
|
+
shape identifies it, not where it stands: in the file's order one supplier's
|
|
2277
|
+
two-column sheets put it, and the boilerplate after it ("Das Produkt ist als
|
|
2278
|
+
gefährlich eingestuft gemäß …"), sometimes before the 2.2 heading and
|
|
2279
|
+
sometimes after. In print order it stands in 2.1, which the statement texts
|
|
2280
|
+
are cut from as well (SDB-10).
|
|
2281
|
+
"""
|
|
2282
|
+
code = _ANY_STATEMENT_CODE.search(line)
|
|
2283
|
+
if code is None:
|
|
2284
|
+
return False
|
|
2285
|
+
before = line[: code.start()].strip().rstrip(",;:(").strip()
|
|
2286
|
+
# "Reproduktionstoxizität: Repr. 1B (H360D)": the class behind its name.
|
|
2287
|
+
return any(
|
|
2288
|
+
part and _CLASSIFICATION_LINE.search(part)
|
|
2289
|
+
for part in (before, before.rpartition(":")[2].strip())
|
|
2290
|
+
)
|
|
2291
|
+
|
|
2292
|
+
|
|
2293
|
+
# What a supplier adds where the regulation's wording ends in a hole (SDB-108):
|
|
2294
|
+
# the organs and the route of exposure, named or in brackets.
|
|
2295
|
+
_HOLE_NAME = re.compile(
|
|
2296
|
+
r"(?:Expositionsweg|Zielorgan|Betroffene\s+Organe)", re.IGNORECASE
|
|
2297
|
+
)
|
|
2298
|
+
_HOLE_FILLING = re.compile(
|
|
2299
|
+
r"(?:\(|Expositionsweg|Zielorgan|Betroffene\s+Organe)", re.IGNORECASE
|
|
2300
|
+
)
|
|
2301
|
+
# How many words an organ's name on a line of its own may have.
|
|
2302
|
+
_ORGAN_WORDS = 2
|
|
2303
|
+
|
|
2304
|
+
|
|
2305
|
+
@dataclass(slots=True)
|
|
2306
|
+
class _StatementReader:
|
|
2307
|
+
"""The statements of one kind, collected line by line."""
|
|
2308
|
+
|
|
2309
|
+
pattern: re.Pattern[str]
|
|
2310
|
+
statements: list[str] = field(default_factory=list)
|
|
2311
|
+
current: list[str] = field(default_factory=list)
|
|
2312
|
+
capturing: bool = False
|
|
2313
|
+
# How many of *current*'s lines the statement held when its wording was
|
|
2314
|
+
# last complete; what stands behind is kept only if it completes again.
|
|
2315
|
+
complete: int = 0
|
|
2316
|
+
# And how many characters, joined.
|
|
2317
|
+
whole: int = 0
|
|
2318
|
+
# Whether whole sentences without a code behind a statement are kept
|
|
2319
|
+
# beside it (SDB-56), and the lines read behind the one that is over.
|
|
2320
|
+
keeps_sentences: bool = False
|
|
2321
|
+
behind: list[str] = field(default_factory=list)
|
|
2322
|
+
following: bool = False
|
|
2323
|
+
# How many lines the statement held at the first page break read past.
|
|
2324
|
+
page_break: int | None = None
|
|
2325
|
+
|
|
2326
|
+
def close(self) -> None:
|
|
2327
|
+
if self.current:
|
|
2328
|
+
kept = self.current[: self.complete] if self.complete else self.current
|
|
2329
|
+
self.statements.extend(_split_statements(" ".join(kept), self.pattern))
|
|
2330
|
+
if self.keeps_sentences and self.complete:
|
|
2331
|
+
# Lines that began the wording again and did not finish it;
|
|
2332
|
+
# behind a page break they are the next page's.
|
|
2333
|
+
end = self.page_break
|
|
2334
|
+
if end is not None and end < self.complete:
|
|
2335
|
+
end = None
|
|
2336
|
+
left = [*self.current[self.complete : end], *self.behind]
|
|
2337
|
+
self.statements.extend(_whole_sentences(left))
|
|
2338
|
+
self.current.clear()
|
|
2339
|
+
self.behind.clear()
|
|
2340
|
+
self.complete = self.whole = 0
|
|
2341
|
+
self.capturing = self.following = False
|
|
2342
|
+
self.page_break = None
|
|
2343
|
+
|
|
2344
|
+
def pass_page_break(self) -> None:
|
|
2345
|
+
if self.page_break is None:
|
|
2346
|
+
self.page_break = len(self.current)
|
|
2347
|
+
|
|
2348
|
+
def follow(self, value: str) -> None:
|
|
2349
|
+
"""Take *value* as a line behind the statement, which is over."""
|
|
2350
|
+
self.following = True
|
|
2351
|
+
self.behind.append(value)
|
|
2352
|
+
|
|
2353
|
+
def begin(self, value: str) -> None:
|
|
2354
|
+
self.close()
|
|
2355
|
+
self.capturing = True
|
|
2356
|
+
self.add(_statement_start(value))
|
|
2357
|
+
|
|
2358
|
+
def add(self, value: str, *, whole: bool = False) -> None:
|
|
2359
|
+
# "Reakti-" before the page header, "onen hervorrufen." after it: one
|
|
2360
|
+
# word, joined as the reader joins it within a page (SDB-54).
|
|
2361
|
+
joined = (
|
|
2362
|
+
join_broken_words(f"{self.current[-1]}\n{value}") if self.current else ""
|
|
2363
|
+
)
|
|
2364
|
+
if joined and "\n" not in joined:
|
|
2365
|
+
self.current[-1] = joined
|
|
2366
|
+
else:
|
|
2367
|
+
self.current.append(value)
|
|
2368
|
+
statement = " ".join(self.current)
|
|
2369
|
+
if whole or _wording_complete(statement, value, self.whole):
|
|
2370
|
+
self.complete = len(self.current)
|
|
2371
|
+
self.whole = len(statement)
|
|
2372
|
+
|
|
2373
|
+
def fills_its_hole(self, line: str, below: Sequence[str]) -> bool:
|
|
2374
|
+
"""Whether *line* is what the supplier adds to a wording that ends in
|
|
2375
|
+
a hole (SDB-108): in brackets, or under the hole's name, or a word or
|
|
2376
|
+
two right above that name ("Blut" / "Expositionsweg: oral")."""
|
|
2377
|
+
if not self.complete or self.complete != len(self.current):
|
|
2378
|
+
return False
|
|
2379
|
+
if not _ends_in_a_hole(" ".join(self.current)):
|
|
2380
|
+
return False
|
|
2381
|
+
if _HOLE_FILLING.match(line):
|
|
2382
|
+
return True
|
|
2383
|
+
following = next((b.strip() for b in below if b.strip()), "")
|
|
2384
|
+
return (
|
|
2385
|
+
len(line.split()) <= _ORGAN_WORDS
|
|
2386
|
+
and not any(c.isdigit() for c in line)
|
|
2387
|
+
and _HOLE_NAME.match(following) is not None
|
|
2388
|
+
)
|
|
2389
|
+
|
|
2390
|
+
def is_over_before(self, line: str) -> bool:
|
|
2391
|
+
"""Whether the statement is whole and *line* does not begin it again (SDB-51)."""
|
|
2392
|
+
if not self.complete or self.complete != len(self.current):
|
|
2393
|
+
return False
|
|
2394
|
+
return not _begins_again(" ".join(self.current), line)
|
|
2395
|
+
|
|
2396
|
+
@property
|
|
2397
|
+
def worded(self) -> bool:
|
|
2398
|
+
return bool(self.pattern.sub("", " ".join(self.current)).strip(" :-–+"))
|
|
2399
|
+
|
|
2400
|
+
def text(self, passages: Sequence[str]) -> str | None:
|
|
2401
|
+
"""The statements, one per line; *passages* is the text without a code."""
|
|
2402
|
+
unique: dict[str, str] = {}
|
|
2403
|
+
for statement in _without_strays(
|
|
2404
|
+
_with_wordings_apart(self.statements, passages)
|
|
2405
|
+
):
|
|
2406
|
+
if cleaned := _FOOTNOTE_MARK.sub("", statement.strip()):
|
|
2407
|
+
unique.setdefault(cleaned.casefold(), cleaned)
|
|
2408
|
+
return "\n".join(_without_shorter_readings(list(unique.values()))) or None
|
|
2409
|
+
|
|
2410
|
+
|
|
2411
|
+
def _without_shorter_readings(statements: list[str]) -> list[str]:
|
|
2412
|
+
"""Drop the shorter of two readings of a statement that ends in a hole (SDB-108).
|
|
2413
|
+
|
|
2414
|
+
2.1 prints "H373: Kann die Organe schädigen bei längerer oder wiederholter
|
|
2415
|
+
Exposition." and the label the same sentence with what the supplier adds
|
|
2416
|
+
to it, "(Zielorgan(e): Nieren; …)" or "… Exposition durch Verschlucken.":
|
|
2417
|
+
one statement, read twice, and the one that fills the hole is the whole
|
|
2418
|
+
one. Only for such a wording: behind any other, what follows the sentence
|
|
2419
|
+
is as often a cell of the table beside it ("… Hautreizungen.
|
|
2420
|
+
Berechnungsmethode.").
|
|
2421
|
+
"""
|
|
2422
|
+
|
|
2423
|
+
def parts(statement: str) -> tuple[str, str]:
|
|
2424
|
+
codes = _LEADING_STATEMENT_CODES.match(statement)
|
|
2425
|
+
if codes is None or not _ends_in_a_hole(statement):
|
|
2426
|
+
return "", ""
|
|
2427
|
+
return (
|
|
2428
|
+
re.sub(r"\s+", "", codes.group(1)),
|
|
2429
|
+
statement[codes.end() :].lstrip(" :-–").rstrip(" ."),
|
|
2430
|
+
)
|
|
2431
|
+
|
|
2432
|
+
def fills(shorter: str, longer: str) -> bool:
|
|
2433
|
+
if not shorter or not longer.startswith(shorter) or longer == shorter:
|
|
2434
|
+
return False
|
|
2435
|
+
rest = longer[len(shorter) :]
|
|
2436
|
+
added = rest.lstrip(" .")
|
|
2437
|
+
# In the same sentence, or what the hole asks for behind it.
|
|
2438
|
+
return bool(added) and (
|
|
2439
|
+
not rest.startswith(".") or _HOLE_FILLING.match(added) is not None
|
|
2440
|
+
)
|
|
2441
|
+
|
|
2442
|
+
read = [parts(statement) for statement in statements]
|
|
2443
|
+
return [
|
|
2444
|
+
statement
|
|
2445
|
+
for statement, (codes, body) in zip(statements, read, strict=True)
|
|
2446
|
+
if not (
|
|
2447
|
+
codes
|
|
2448
|
+
and any(
|
|
2449
|
+
other_codes == codes and fills(body, other)
|
|
2450
|
+
for other_codes, other in read
|
|
2451
|
+
)
|
|
2452
|
+
)
|
|
2453
|
+
]
|
|
2454
|
+
|
|
2455
|
+
|
|
2456
|
+
# The line that names what the label must name ("Enthält: Zement, Portland,
|
|
2457
|
+
# Klinker"): no part of an H or a P statement above it (SDB-127). An EUH
|
|
2458
|
+
# statement can begin so.
|
|
2459
|
+
_CONTENTS_LINE = re.compile(r"^Enthält[^\S\n]*:")
|
|
2460
|
+
# A footnote's number set against a statement's last full stop (SDB-127):
|
|
2461
|
+
# "… Kann allergische Reaktionen hervorrufen.6".
|
|
2462
|
+
_FOOTNOTE_MARK = re.compile(r"(?<=[a-zäöüß][.!?])\d$")
|
|
2463
|
+
|
|
2464
|
+
|
|
2465
|
+
def _extract_statement_text(
|
|
2466
|
+
text: str, pattern: re.Pattern[str], furniture: frozenset[str]
|
|
2467
|
+
) -> str | None:
|
|
2468
|
+
"""The statements *pattern* finds in *text*, one per line (SDB-27, SDB-51).
|
|
2469
|
+
|
|
2470
|
+
A statement starts at its code and takes the lines after it until a line
|
|
2471
|
+
that belongs to something else: another code, a heading, a row of the label
|
|
2472
|
+
table, a hazard class. Page numbers, the page header and the statement's
|
|
2473
|
+
own heading repeated on the next page are read past, so a statement broken
|
|
2474
|
+
by a page carries on behind them. Once the statement holds the end of its
|
|
2475
|
+
regulation wording and its line or sentence is over, the next line is no
|
|
2476
|
+
longer its own -- unless the wording begins again there.
|
|
2477
|
+
"""
|
|
2478
|
+
lines = _joined_combinations(_merge_broken_pairs(text.split("\n")))
|
|
2479
|
+
own_heading = _OWN_HEADING[pattern]
|
|
2480
|
+
reader = _StatementReader(
|
|
2481
|
+
pattern, keeps_sentences=pattern is SUPPLEMENTAL_CODE_PATTERN
|
|
2482
|
+
)
|
|
2483
|
+
label_broken = False
|
|
2484
|
+
|
|
2485
|
+
for index, raw_line in enumerate(lines):
|
|
2486
|
+
stripped = raw_line.strip()
|
|
2487
|
+
normalized = stripped.lstrip("-*•·!\t ")
|
|
2488
|
+
if not stripped or _is_classification_entry(normalized):
|
|
2489
|
+
# A classification entry begins no statement, and what follows it
|
|
2490
|
+
# up to the next code belongs to none (SDB-41); like an empty line
|
|
2491
|
+
# it ends the one before (SDB-27).
|
|
2492
|
+
reader.close()
|
|
2493
|
+
continue
|
|
2494
|
+
behind = _CODE_BEHIND_ITS_WORDING.match(normalized)
|
|
2495
|
+
if behind and _all_of(pattern, behind.group(2)):
|
|
2496
|
+
# "Zur Vermeidung von Risiken … einhalten. (EUH401)": the code in
|
|
2497
|
+
# brackets closes its sentence instead of opening it.
|
|
2498
|
+
reader.close()
|
|
2499
|
+
sentence = _sentence_before_its_code(
|
|
2500
|
+
lines, index, behind.group(1), behind.group(2)
|
|
2501
|
+
)
|
|
2502
|
+
if sentence:
|
|
2503
|
+
reader.statements.append(
|
|
2504
|
+
f"{behind.group(2)} {_clean_value(' '.join(sentence))}"
|
|
2505
|
+
)
|
|
2506
|
+
continue
|
|
2507
|
+
match = pattern.search(normalized)
|
|
2508
|
+
if match:
|
|
2509
|
+
head = _clean_value(normalized[: match.start()])
|
|
2510
|
+
if _finishes_the_statement(head, reader):
|
|
2511
|
+
# "… auch nicht nach" / "Gebrauch. P261-Einatmen von …": the
|
|
2512
|
+
# statement above ends on this line, before the next code.
|
|
2513
|
+
reader.add(head)
|
|
2514
|
+
reader.begin(
|
|
2515
|
+
_clean_value(_before_other_codes(normalized[match.start() :], pattern))
|
|
2516
|
+
)
|
|
2517
|
+
label_broken = _BROKEN_LABEL.search(head) is not None
|
|
2518
|
+
continue
|
|
2519
|
+
if not reader.capturing or _ONLY_PICTOGRAM_WORDS.fullmatch(normalized):
|
|
2520
|
+
continue
|
|
2521
|
+
if label_broken and _LABEL_TAIL.fullmatch(normalized):
|
|
2522
|
+
# "Ergänzende Gefahrenhin-: EUH401 …" and, after the page break,
|
|
2523
|
+
# "weise": the rest of the label, not a word of the statement
|
|
2524
|
+
# (SDB-110).
|
|
2525
|
+
label_broken = False
|
|
2526
|
+
continue
|
|
2527
|
+
if reader.following:
|
|
2528
|
+
# Behind a statement that is over: up to what ends one, or to the
|
|
2529
|
+
# page's end (SDB-56).
|
|
2530
|
+
if (
|
|
2531
|
+
_ends_statement(normalized)
|
|
2532
|
+
or stripped in furniture
|
|
2533
|
+
or _PAGE_NUMBER.match(stripped)
|
|
2534
|
+
or _CONTINUED.fullmatch(stripped)
|
|
2535
|
+
):
|
|
2536
|
+
reader.close()
|
|
2537
|
+
else:
|
|
2538
|
+
reader.follow(_clean_value(normalized))
|
|
2539
|
+
continue
|
|
2540
|
+
# The header counts once the statement has its wording: before that,
|
|
2541
|
+
# the line may be the wording, which a short sheet repeats as often.
|
|
2542
|
+
if (
|
|
2543
|
+
(reader.worded and stripped in furniture)
|
|
2544
|
+
or _PAGE_NUMBER.match(stripped)
|
|
2545
|
+
or _CONTINUED.fullmatch(stripped)
|
|
2546
|
+
or (
|
|
2547
|
+
own_heading.match(normalized)
|
|
2548
|
+
and _page_break_beside(lines, index, furniture)
|
|
2549
|
+
)
|
|
2550
|
+
):
|
|
2551
|
+
reader.pass_page_break()
|
|
2552
|
+
continue
|
|
2553
|
+
if _ends_statement(normalized) or (
|
|
2554
|
+
not reader.keeps_sentences and _CONTENTS_LINE.match(normalized)
|
|
2555
|
+
):
|
|
2556
|
+
reader.close()
|
|
2557
|
+
continue
|
|
2558
|
+
if reader.fills_its_hole(normalized, lines[index + 1 :]):
|
|
2559
|
+
reader.add(_clean_value(normalized), whole=True)
|
|
2560
|
+
continue
|
|
2561
|
+
if reader.is_over_before(normalized):
|
|
2562
|
+
if reader.keeps_sentences:
|
|
2563
|
+
reader.follow(_clean_value(normalized))
|
|
2564
|
+
else:
|
|
2565
|
+
reader.close()
|
|
2566
|
+
continue
|
|
2567
|
+
reader.add(_clean_value(normalized))
|
|
2568
|
+
|
|
2569
|
+
reader.close()
|
|
2570
|
+
return reader.text(_passages(lines, furniture))
|
|
2571
|
+
|
|
2572
|
+
|
|
2573
|
+
_ONE_CODE = r"(?:EUH\d{3}|[HP]\d{3})"
|
|
2574
|
+
# A combination left open at a line's end, "P305+P351+" over "P338 BEI …", or
|
|
2575
|
+
# continued at the next one's start, "P305 + P351" over "+ P338".
|
|
2576
|
+
_OPEN_COMBINATION = re.compile(rf"{_ONE_CODE}\s*\+\s*$")
|
|
2577
|
+
_CODE_AT_END = re.compile(rf"{_ONE_CODE}\s*$")
|
|
2578
|
+
_CONTINUED_COMBINATION = re.compile(rf"^\+\s*{_ONE_CODE}")
|
|
2579
|
+
# The codes a statement begins with: one, a combination, or several joined by
|
|
2580
|
+
# a comma, a semicolon, a dash, a slash or a space ("H222-H229 Extrem …").
|
|
2581
|
+
_LEADING_STATEMENT_CODES = re.compile(
|
|
2582
|
+
rf"^({_ONE_CODE}(?:(?:\s*[-–+,;/]\s*|\s+){_ONE_CODE})*)"
|
|
2583
|
+
)
|
|
2584
|
+
# "… einhalten. (EUH401)": a sentence with its code in brackets behind it, a
|
|
2585
|
+
# combination or several codes too ("… bersten. (H222, H229)", "… Weiter
|
|
2586
|
+
# spülen. (P305+P351+P338)"), or the brackets alone on the line below the
|
|
2587
|
+
# sentence ("… fernhalten. Nicht rauchen." / "(P210)").
|
|
2588
|
+
_CODE_BEHIND_ITS_WORDING = re.compile(
|
|
2589
|
+
rf"^(?:(\S.*[.!])\s*)?\(({_ONE_CODE}(?:\s*[+,]\s*{_ONE_CODE})*)\)\.?$"
|
|
2590
|
+
)
|
|
2591
|
+
# A sentence's end at a line's end, brackets and quotation marks behind it.
|
|
2592
|
+
_SENTENCE_END = re.compile(r"[.!?][)\]\"“”]*$")
|
|
2593
|
+
|
|
2594
|
+
|
|
2595
|
+
# What suppliers hang on a code: the bracket of "(EUH208) Enthält …", the
|
|
2596
|
+
# number of their own variant ("P264.1 Nach Gebrauch …"), and the letters CLP
|
|
2597
|
+
# sets behind H360 and H361 for the effect ("H361d Kann vermutlich das Kind im
|
|
2598
|
+
# Mutterleib schädigen."), which the wording says in full.
|
|
2599
|
+
_CODE_MARK = re.compile(rf"^({_ONE_CODE})(?:\)|\.\d\b|[dDfF]{{1,2}}(?=[\s:\-–]|$))\s*")
|
|
2600
|
+
# A line that begins with a label and its colon.
|
|
2601
|
+
_LABELLED_LINE = re.compile(r"^[A-ZÄÖÜ][^:.!?]{2,60}:\s")
|
|
2602
|
+
# A statement that is codes and nothing else: one, a row, a list.
|
|
2603
|
+
_ONLY_CODES = re.compile(
|
|
2604
|
+
rf"^{_ONE_CODE}(?:\s*\+\s*{_ONE_CODE})*"
|
|
2605
|
+
rf"(?:\s*[-–,;]\s*{_ONE_CODE}(?:\s*\+\s*{_ONE_CODE})*)*\s*[,;:]?$"
|
|
2606
|
+
)
|
|
2607
|
+
|
|
2608
|
+
|
|
2609
|
+
# "Enthält Biozidprodukte: Pyridin-2-thiol-1-oxid, Natriumsalz, …": a list,
|
|
2610
|
+
# which no sentence end closes.
|
|
2611
|
+
_BIOCIDES = re.compile(r"^Enthält\s+Biozidprodukte?\b", re.IGNORECASE)
|
|
2612
|
+
# How many words make a sentence in its own right (SDB-52, SDB-56).
|
|
2613
|
+
_SENTENCE_WORDS = 4
|
|
2614
|
+
|
|
2615
|
+
|
|
2616
|
+
def _whole_sentences(lines: Sequence[str]) -> list[str]:
|
|
2617
|
+
"""The whole sentences among the *lines* behind an EUH statement (SDB-56).
|
|
2618
|
+
|
|
2619
|
+
Label text without a code: "Ohne ausreichende Lüftung Bildung
|
|
2620
|
+
explosionsfähiger Gemische möglich.", the warning for grill lighters. A
|
|
2621
|
+
sentence runs to the line that ends one. What reaches no end is not a
|
|
2622
|
+
sentence and goes -- a substance's name, "UFI: …", a list of ingredients
|
|
2623
|
+
-- except the list of a treated article's biocides.
|
|
2624
|
+
"""
|
|
2625
|
+
sentences: list[str] = []
|
|
2626
|
+
current: list[str] = []
|
|
2627
|
+
for index, line in enumerate(lines):
|
|
2628
|
+
if not current and _BIOCIDES.match(line):
|
|
2629
|
+
sentences.append(_joined(lines[index:]))
|
|
2630
|
+
break
|
|
2631
|
+
ends = _SENTENCE_END.search(line) is not None
|
|
2632
|
+
if not current and not ends and len(line.split()) == 1:
|
|
2633
|
+
# "Bemerkung" over the sentence it heads.
|
|
2634
|
+
continue
|
|
2635
|
+
current.append(line)
|
|
2636
|
+
if ends:
|
|
2637
|
+
sentence = _joined(current)
|
|
2638
|
+
current = []
|
|
2639
|
+
if sentence[0].isupper() and len(sentence.split()) >= _SENTENCE_WORDS:
|
|
2640
|
+
sentences.append(sentence)
|
|
2641
|
+
return sentences
|
|
2642
|
+
|
|
2643
|
+
|
|
2644
|
+
def _joined(lines: Sequence[str]) -> str:
|
|
2645
|
+
"""*lines* as one, a word broken at a line's end joined (SDB-54)."""
|
|
2646
|
+
return join_broken_words("\n".join(lines)).replace("\n", " ")
|
|
2647
|
+
|
|
2648
|
+
|
|
2649
|
+
# A label broken at its line's end before the statement's code, and the rest
|
|
2650
|
+
# of it on a later line (SDB-110).
|
|
2651
|
+
_BROKEN_LABEL = re.compile(r"[a-zäöüß]-\s*:?\s*$")
|
|
2652
|
+
_LABEL_TAIL = re.compile(r"[a-zäöüß]{2,15}:?")
|
|
2653
|
+
# A code in a list behind a comma or a semicolon.
|
|
2654
|
+
_LISTED_CODE = re.compile(rf"\s*[,;]\s*({_ONE_CODE})(?!\d)")
|
|
2655
|
+
|
|
2656
|
+
|
|
2657
|
+
def _before_other_codes(text: str, pattern: re.Pattern[str]) -> str:
|
|
2658
|
+
"""*text* up to the first listed code of another kind (SDB-109).
|
|
2659
|
+
|
|
2660
|
+
"H373 (Blut), EUH210, EUH401" under "Gefahrenhinweise:" lists the label's
|
|
2661
|
+
codes of every kind; the hazard statement ends before the first EUH code.
|
|
2662
|
+
"""
|
|
2663
|
+
for listed in _LISTED_CODE.finditer(text):
|
|
2664
|
+
if not pattern.fullmatch(listed.group(1)):
|
|
2665
|
+
return text[: listed.start()]
|
|
2666
|
+
return text
|
|
2667
|
+
|
|
2668
|
+
|
|
2669
|
+
# What a pictogram of the protective equipment leaves in the text behind its
|
|
2670
|
+
# statement's code (SDB-127): "P280 gloves" over "Schutzhandschuhe tragen."
|
|
2671
|
+
_PICTOGRAM_WORD = r"(?:hands|eyes|gloves|face|clothing|mask)"
|
|
2672
|
+
_PICTOGRAM_WORDS = re.compile(
|
|
2673
|
+
rf"^((?:{_ONE_CODE})(?:\s*\+\s*{_ONE_CODE})*)"
|
|
2674
|
+
rf"(?:[^\S\n]+{_PICTOGRAM_WORD})+(?=[^\S\n]|$)"
|
|
2675
|
+
)
|
|
2676
|
+
_ONLY_PICTOGRAM_WORDS = re.compile(rf"{_PICTOGRAM_WORD}(?:[^\S\n]+{_PICTOGRAM_WORD})*")
|
|
2677
|
+
|
|
2678
|
+
|
|
2679
|
+
def _statement_start(text: str) -> str:
|
|
2680
|
+
"""The first line of a statement, its code freed of what suppliers hang on it."""
|
|
2681
|
+
text = _PICTOGRAM_WORDS.sub(r"\1", text)
|
|
2682
|
+
mark = _CODE_MARK.match(text)
|
|
2683
|
+
if mark is None:
|
|
2684
|
+
return text
|
|
2685
|
+
rest = text[mark.end() :]
|
|
2686
|
+
return f"{mark.group(1)}{'' if rest[:1] in tuple('-–:') else ' '}{rest}".strip()
|
|
2687
|
+
|
|
2688
|
+
|
|
2689
|
+
def _all_of(pattern: re.Pattern[str], codes: str) -> bool:
|
|
2690
|
+
"""Whether every code of *codes* ("P305+P351+P338", "H222, H229") is one of *pattern*'s."""
|
|
2691
|
+
return all(pattern.fullmatch(code.strip()) for code in re.split(r"[+,]", codes))
|
|
2692
|
+
|
|
2693
|
+
|
|
2694
|
+
def _finishes_the_statement(head: str, reader: _StatementReader) -> bool:
|
|
2695
|
+
"""Whether *head*, the text of a line before its first code, is the end of
|
|
2696
|
+
the statement being read: a sentence's end behind a line that ended none."""
|
|
2697
|
+
return (
|
|
2698
|
+
bool(head)
|
|
2699
|
+
and reader.capturing
|
|
2700
|
+
and not reader.following
|
|
2701
|
+
and bool(reader.current)
|
|
2702
|
+
and _SENTENCE_END.search(head) is not None
|
|
2703
|
+
and not _SENTENCE_END.search(reader.current[-1])
|
|
2704
|
+
and not _LABELLED_LINE.match(head)
|
|
2705
|
+
)
|
|
2706
|
+
|
|
2707
|
+
|
|
2708
|
+
def _sentence_before_its_code(
|
|
2709
|
+
lines: list[str], index: int, on_the_line: str | None, codes: str
|
|
2710
|
+
) -> list[str]:
|
|
2711
|
+
"""The lines of the sentence whose code stands in brackets at *index*.
|
|
2712
|
+
|
|
2713
|
+
On the code's own line, or on the line above where the brackets stand
|
|
2714
|
+
alone; with the lines above that it began on.
|
|
2715
|
+
"""
|
|
2716
|
+
code = re.split(r"[+,]", codes)[0].strip() if "," in codes else codes
|
|
2717
|
+
if on_the_line:
|
|
2718
|
+
return [*_sentence_begun_above(lines, index, code), on_the_line]
|
|
2719
|
+
last = next(
|
|
2720
|
+
(i for i in range(index - 1, max(-1, index - 3), -1) if lines[i].strip()),
|
|
2721
|
+
None,
|
|
2722
|
+
)
|
|
2723
|
+
if last is None:
|
|
2724
|
+
return []
|
|
2725
|
+
line = lines[last].strip().lstrip("-*•·!\t ")
|
|
2726
|
+
if not _SENTENCE_END.search(line) or _ends_statement(line):
|
|
2727
|
+
return []
|
|
2728
|
+
return [*_sentence_begun_above(lines, last, code), line]
|
|
2729
|
+
|
|
2730
|
+
|
|
2731
|
+
def _sentence_begun_above(lines: list[str], index: int, code: str) -> list[str]:
|
|
2732
|
+
"""The lines above *index* that the sentence of *code* began on, at most three.
|
|
2733
|
+
|
|
2734
|
+
"Zur Vermeidung von Risiken für Mensch und Umwelt die" over
|
|
2735
|
+
"Gebrauchsanleitung einhalten. (EUH401)": a line that ends no sentence
|
|
2736
|
+
carries on into the next. Only from where the wording begins: the heading
|
|
2737
|
+
above it ends no sentence either.
|
|
2738
|
+
"""
|
|
2739
|
+
bounds = wording_bounds(code)
|
|
2740
|
+
above: list[str] = []
|
|
2741
|
+
for raw_line in reversed(lines[max(0, index - 3) : index]):
|
|
2742
|
+
line = raw_line.strip().lstrip("-*•·!\t ")
|
|
2743
|
+
if not line or _SENTENCE_END.search(line) or _ends_statement(line):
|
|
2744
|
+
break
|
|
2745
|
+
above.insert(0, line)
|
|
2746
|
+
for first, line in enumerate(above):
|
|
2747
|
+
if bounds and bounds.opening.match(line):
|
|
2748
|
+
return above[first:]
|
|
2749
|
+
return []
|
|
2750
|
+
|
|
2751
|
+
|
|
2752
|
+
def _codes_of(statement: str) -> list[str]:
|
|
2753
|
+
"""The codes a statement of codes alone lists, a combination as one."""
|
|
2754
|
+
joined = re.sub(r"\s*\+\s*", "+", statement.strip().rstrip(",;:"))
|
|
2755
|
+
return [code for code in re.split(r"\s*[-–,;]\s*", joined) if code]
|
|
2756
|
+
|
|
2757
|
+
|
|
2758
|
+
def _listed(codes: str) -> list[str]:
|
|
2759
|
+
"""The codes a statement begins with, however it joins them."""
|
|
2760
|
+
return _codes_of(re.sub(r"\s*/\s*|(?<=\d)\s+(?=[EHP])", ",", codes))
|
|
2761
|
+
|
|
2762
|
+
|
|
2763
|
+
def _begins_again(statement: str, line: str) -> bool:
|
|
2764
|
+
"""Whether *line* begins the wording of *statement*, whole as it is, once more.
|
|
2765
|
+
|
|
2766
|
+
A second "Enthält …" under EUH208 does; under a row of codes it is the
|
|
2767
|
+
wording of any of them, the sheet's sentence for the row (SDB-30).
|
|
2768
|
+
"""
|
|
2769
|
+
if _ONLY_CODES.match(statement):
|
|
2770
|
+
return holds_a_wording_of(line, _codes_of(statement))
|
|
2771
|
+
wording = _wording_of(statement)
|
|
2772
|
+
return wording is not None and wording[2].match(line) is not None
|
|
2773
|
+
|
|
2774
|
+
|
|
2775
|
+
def _passages(lines: list[str], furniture: frozenset[str]) -> list[str]:
|
|
2776
|
+
"""The text of *lines* that carries no code, cut where a sentence cannot run on.
|
|
2777
|
+
|
|
2778
|
+
That is at an empty line, at a line with a code, at a numbered heading and
|
|
2779
|
+
at a heading of the label, whose own line may begin the next passage:
|
|
2780
|
+
"Prävention: Freisetzung in die Umwelt vermeiden." (SDB-52). What stands
|
|
2781
|
+
under 2.3 is no passage.
|
|
2782
|
+
"""
|
|
2783
|
+
passages: list[str] = []
|
|
2784
|
+
current: list[str] = []
|
|
2785
|
+
|
|
2786
|
+
def cut() -> None:
|
|
2787
|
+
if current:
|
|
2788
|
+
passages.append(" ".join(current))
|
|
2789
|
+
current.clear()
|
|
2790
|
+
|
|
2791
|
+
for raw_line in lines:
|
|
2792
|
+
line = raw_line.strip().lstrip("-*•·!\t ")
|
|
2793
|
+
if _OTHER_HAZARDS.match(line):
|
|
2794
|
+
# The label's sentences stand in the label, not under 2.3.
|
|
2795
|
+
break
|
|
2796
|
+
if line in furniture or _PAGE_NUMBER.match(line) or _CONTINUED.fullmatch(line):
|
|
2797
|
+
continue
|
|
2798
|
+
if _LABEL_HEADING.match(line) and not _ANY_STATEMENT_CODE.search(line):
|
|
2799
|
+
cut()
|
|
2800
|
+
_, colon, behind = line.partition(":")
|
|
2801
|
+
if colon and behind.strip():
|
|
2802
|
+
current.append(behind.strip())
|
|
2803
|
+
elif (
|
|
2804
|
+
not line
|
|
2805
|
+
or _ANY_STATEMENT_CODE.search(line)
|
|
2806
|
+
or _NUMBERED_SUBHEADING.match(line)
|
|
2807
|
+
or _CLASSIFICATION_LINE.search(line)
|
|
2808
|
+
):
|
|
2809
|
+
cut()
|
|
2810
|
+
else:
|
|
2811
|
+
if _LABELLED_LINE.match(line) and (
|
|
2812
|
+
not current or _SENTENCE_END.search(current[-1])
|
|
2813
|
+
):
|
|
2814
|
+
# "Gefährliche Inhaltsstoffe: Aceton" is no part of the
|
|
2815
|
+
# sentence before it, and "Bei anhaltender Augenreizung: …"
|
|
2816
|
+
# begins one of its own. Behind a line that ends no sentence
|
|
2817
|
+
# the colon is the wording's: "BEI KONTAKT MIT DEN" over
|
|
2818
|
+
# "AUGEN: Einige Minuten …".
|
|
2819
|
+
cut()
|
|
2820
|
+
current.append(line)
|
|
2821
|
+
cut()
|
|
2822
|
+
return passages
|
|
2823
|
+
|
|
2824
|
+
|
|
2825
|
+
def _with_wordings_apart(statements: list[str], passages: Sequence[str]) -> list[str]:
|
|
2826
|
+
"""*statements*, a row of codes replaced by what it stands for (SDB-52).
|
|
2827
|
+
|
|
2828
|
+
A sheet may print the codes apart from their sentences: a column of codes
|
|
2829
|
+
over the paragraph of their wordings, or the wordings under
|
|
2830
|
+
"Allgemein:", "Prävention:" … and the codes as a list below. Such a run of
|
|
2831
|
+
codes gives way to the statements already read with a wording where each
|
|
2832
|
+
of its codes has one, and otherwise takes the sheet's sentences from
|
|
2833
|
+
*passages*, all of them or none.
|
|
2834
|
+
"""
|
|
2835
|
+
worded = {
|
|
2836
|
+
code
|
|
2837
|
+
for statement in statements
|
|
2838
|
+
if _has_a_sentence_of_its_own(statement)
|
|
2839
|
+
and (leading := _LEADING_STATEMENT_CODES.match(statement))
|
|
2840
|
+
for code in _listed(leading.group(1))
|
|
2841
|
+
}
|
|
2842
|
+
result: list[str] = []
|
|
2843
|
+
index = 0
|
|
2844
|
+
while index < len(statements):
|
|
2845
|
+
end = index
|
|
2846
|
+
while end < len(statements) and _ONLY_CODES.match(statements[end]):
|
|
2847
|
+
end += 1
|
|
2848
|
+
codes = [code for row in statements[index:end] for code in _codes_of(row)]
|
|
2849
|
+
if not codes:
|
|
2850
|
+
result.append(statements[index])
|
|
2851
|
+
index += 1
|
|
2852
|
+
continue
|
|
2853
|
+
# The paragraph under a column of codes was read as the wording of
|
|
2854
|
+
# the last one: it is the column's where it begins with the first's.
|
|
2855
|
+
last = statements[end] if end < len(statements) else ""
|
|
2856
|
+
first = wording_bounds(codes[0])
|
|
2857
|
+
last_codes = _LEADING_STATEMENT_CODES.match(last)
|
|
2858
|
+
if (
|
|
2859
|
+
first
|
|
2860
|
+
and last_codes
|
|
2861
|
+
and last_codes.group(1) not in codes
|
|
2862
|
+
and first.opening.match(last[last_codes.end() :].lstrip(" :-–"))
|
|
2863
|
+
):
|
|
2864
|
+
apart = wordings_apart([*codes, last_codes.group(1)], passages)
|
|
2865
|
+
if apart is not None:
|
|
2866
|
+
result.extend(apart)
|
|
2867
|
+
index = end + 1
|
|
2868
|
+
continue
|
|
2869
|
+
if len(codes) > 1 and all(code in worded for code in codes):
|
|
2870
|
+
index = end
|
|
2871
|
+
continue
|
|
2872
|
+
apart = wordings_apart(codes, passages) if len(codes) > 1 else None
|
|
2873
|
+
result.extend(apart if apart is not None else statements[index:end])
|
|
2874
|
+
index = end
|
|
2875
|
+
return result
|
|
2876
|
+
|
|
2877
|
+
|
|
2878
|
+
def _has_a_sentence_of_its_own(statement: str) -> bool:
|
|
2879
|
+
"""Whether *statement* is a code with a sentence, not with what a table left.
|
|
2880
|
+
|
|
2881
|
+
The sentence begins as the regulation words the code, or it is one in its
|
|
2882
|
+
own right, four words or more and brought to an end: "P103: Lesen Sie
|
|
2883
|
+
sämtliche Anweisungen aufmerksam und befolgen Sie diese." is the newer
|
|
2884
|
+
wording, "H319 Berechnungsverfahren." a cell of 2.1's table.
|
|
2885
|
+
"""
|
|
2886
|
+
codes = _LEADING_STATEMENT_CODES.match(statement)
|
|
2887
|
+
if codes is None or _ONLY_CODES.match(statement):
|
|
2888
|
+
return False
|
|
2889
|
+
wording = statement[codes.end() :].lstrip(" :-–")
|
|
2890
|
+
bounds = wording_bounds(_listed(codes.group(1))[-1])
|
|
2891
|
+
if bounds is None or bounds.opening.match(wording):
|
|
2892
|
+
return bool(wording)
|
|
2893
|
+
return (
|
|
2894
|
+
len(wording.split()) >= _SENTENCE_WORDS
|
|
2895
|
+
and _SENTENCE_END.search(wording) is not None
|
|
2896
|
+
)
|
|
2897
|
+
|
|
2898
|
+
|
|
2899
|
+
def _without_strays(statements: list[str]) -> list[str]:
|
|
2900
|
+
"""*statements* without what a table left under a code that has its sentence.
|
|
2901
|
+
|
|
2902
|
+
"H410" over "Berechnungsverfahren." in 2.1's table of classifications
|
|
2903
|
+
reads as a statement, and so does the first half of one cut by a column:
|
|
2904
|
+
"H410 Sehr giftig für Wasserorganismen, mit lang-". Where the same code
|
|
2905
|
+
has a statement that begins as the regulation words it, one that does not
|
|
2906
|
+
goes, and so does one without its end that another of the code begins
|
|
2907
|
+
with.
|
|
2908
|
+
"""
|
|
2909
|
+
proper: dict[str, list[str]] = {}
|
|
2910
|
+
parsed: list[tuple[str, str | None, str, bool]] = []
|
|
2911
|
+
for statement in statements:
|
|
2912
|
+
codes = _LEADING_STATEMENT_CODES.match(statement)
|
|
2913
|
+
code = codes.group(1) if codes else None
|
|
2914
|
+
wording = statement[codes.end() :].lstrip(" :-–") if codes else ""
|
|
2915
|
+
bounds = wording_bounds(code) if code else None
|
|
2916
|
+
begins = bool(bounds and bounds.opening.match(wording))
|
|
2917
|
+
if code and begins:
|
|
2918
|
+
proper.setdefault(code, []).append(wording.casefold())
|
|
2919
|
+
parsed.append((statement, code, wording, begins))
|
|
2920
|
+
kept: list[str] = []
|
|
2921
|
+
for statement, code, wording, begins in parsed:
|
|
2922
|
+
others = proper.get(code or "", [])
|
|
2923
|
+
folded = wording.casefold().rstrip("-")
|
|
2924
|
+
if wording and others:
|
|
2925
|
+
if not begins and wording_bounds(code or "") is not None:
|
|
2926
|
+
continue
|
|
2927
|
+
if not _SENTENCE_END.search(wording) and any(
|
|
2928
|
+
other != wording.casefold() and other.startswith(folded)
|
|
2929
|
+
for other in others
|
|
2930
|
+
):
|
|
2931
|
+
continue
|
|
2932
|
+
kept.append(statement)
|
|
2933
|
+
return kept
|
|
2934
|
+
|
|
2935
|
+
|
|
2936
|
+
def _joined_combinations(lines: list[str]) -> list[str]:
|
|
2937
|
+
"""Lines with a combination left open joined to the line that closes it (SDB-51)."""
|
|
2938
|
+
joined: list[str] = []
|
|
2939
|
+
for line in lines:
|
|
2940
|
+
stripped = line.strip()
|
|
2941
|
+
if (
|
|
2942
|
+
joined
|
|
2943
|
+
and stripped
|
|
2944
|
+
and (
|
|
2945
|
+
_OPEN_COMBINATION.search(joined[-1])
|
|
2946
|
+
or (
|
|
2947
|
+
_CODE_AT_END.search(joined[-1])
|
|
2948
|
+
and _CONTINUED_COMBINATION.match(stripped)
|
|
2949
|
+
)
|
|
2950
|
+
)
|
|
2951
|
+
):
|
|
2952
|
+
joined[-1] = joined[-1].rstrip() + stripped
|
|
2953
|
+
continue
|
|
2954
|
+
joined.append(line)
|
|
2955
|
+
return joined
|
|
2956
|
+
|
|
2957
|
+
|
|
2958
|
+
def _wording_of(statement: str) -> tuple[int, re.Pattern[str], re.Pattern[str]] | None:
|
|
2959
|
+
"""Where a statement's codes end, and how its wording ends and begins.
|
|
2960
|
+
|
|
2961
|
+
The wording is the last code's where a line lists several ("H222, H229:
|
|
2962
|
+
Extrem entzündbares Aerosol. Behälter steht unter Druck: …"). None for a
|
|
2963
|
+
statement whose code the regulation does not word, or words without an end.
|
|
2964
|
+
"""
|
|
2965
|
+
codes = _LEADING_STATEMENT_CODES.match(statement)
|
|
2966
|
+
if codes is None:
|
|
2967
|
+
return None
|
|
2968
|
+
last = _listed(codes.group(1))[-1]
|
|
2969
|
+
bounds = wording_bounds(last)
|
|
2970
|
+
closing = bounds.closing or open_end(last) if bounds else None
|
|
2971
|
+
if bounds is None or closing is None:
|
|
2972
|
+
return None
|
|
2973
|
+
return codes.end(), closing, bounds.opening
|
|
2974
|
+
|
|
2975
|
+
|
|
2976
|
+
def _ends_in_a_hole(statement: str) -> bool:
|
|
2977
|
+
"""Whether the regulation leaves the end of *statement*'s wording open."""
|
|
2978
|
+
codes = _LEADING_STATEMENT_CODES.match(statement)
|
|
2979
|
+
return codes is not None and open_end(_listed(codes.group(1))[-1]) is not None
|
|
2980
|
+
|
|
2981
|
+
|
|
2982
|
+
def _wording_complete(statement: str, last_line: str, whole_before: int) -> bool:
|
|
2983
|
+
"""Whether *statement* holds its wording's end and stops there (SDB-51).
|
|
2984
|
+
|
|
2985
|
+
It stops where its last line ends on the wording's last words or on a
|
|
2986
|
+
sentence's end: "… zuführen! Größere Produktreste zur Problemstoff-
|
|
2987
|
+
sammelstelle" goes on to "bringen.", and "Schutzhandschuhe tragen.
|
|
2988
|
+
Augenschutz oder Gesichtsschutz tragen." is whole with its line. A
|
|
2989
|
+
statement that was whole *whole_before* characters in has to reach the
|
|
2990
|
+
wording's end once more behind them: a second "Enthält Citral. Kann
|
|
2991
|
+
allergische Reaktionen hervorrufen." does, "Enthält 6,8 Massenprozent
|
|
2992
|
+
entzündbare Bestandteile." does not.
|
|
2993
|
+
"""
|
|
2994
|
+
if _ONLY_CODES.match(statement):
|
|
2995
|
+
# A row or a list of codes is whole on its line (SDB-30): what follows
|
|
2996
|
+
# is theirs only where it begins a wording of one of them.
|
|
2997
|
+
return len(_codes_of(statement)) > 1
|
|
2998
|
+
wording = _wording_of(statement)
|
|
2999
|
+
if wording is None:
|
|
3000
|
+
return False
|
|
3001
|
+
position, closing, _ = wording
|
|
3002
|
+
found = list(closing.finditer(statement, max(position, whole_before)))
|
|
3003
|
+
if not found:
|
|
3004
|
+
return False
|
|
3005
|
+
last = found[-1]
|
|
3006
|
+
return not statement[last.end() :].strip() or bool(
|
|
3007
|
+
_SENTENCE_END.search(last_line.rstrip())
|
|
3008
|
+
)
|
|
3009
|
+
|
|
3010
|
+
|
|
3011
|
+
def _split_statements(statement: str, pattern: re.Pattern[str]) -> list[str]:
|
|
3012
|
+
"""*statement*, and the next one where a line holds two (SDB-51).
|
|
3013
|
+
|
|
3014
|
+
"P101: … bereithalten. P102: Darf nicht …" on one line: a code *pattern*
|
|
3015
|
+
finds behind the end of the first wording begins the second statement.
|
|
3016
|
+
"""
|
|
3017
|
+
wording = _wording_of(statement)
|
|
3018
|
+
if wording is None:
|
|
3019
|
+
return [statement]
|
|
3020
|
+
position, closing, _ = wording
|
|
3021
|
+
found = closing.search(statement, position)
|
|
3022
|
+
if found is None:
|
|
3023
|
+
return [statement]
|
|
3024
|
+
following = pattern.search(statement, found.end())
|
|
3025
|
+
if following is None:
|
|
3026
|
+
return [statement]
|
|
3027
|
+
return [
|
|
3028
|
+
statement[: following.start()].rstrip(),
|
|
3029
|
+
*_split_statements(_statement_start(statement[following.start() :]), pattern),
|
|
3030
|
+
]
|
|
3031
|
+
|
|
3032
|
+
|
|
3033
|
+
def _statements_within(text: str | None, codes: set[str]) -> str | None:
|
|
3034
|
+
"""Keep the statements of *text* whose every H-code is one of *codes*.
|
|
3035
|
+
|
|
3036
|
+
Every one, not any: "H300+H310+H330" is a single statement, and a line that
|
|
3037
|
+
runs on into a code the label does not carry is not the label's line.
|
|
3038
|
+
"""
|
|
3039
|
+
if text is None:
|
|
3040
|
+
return None
|
|
3041
|
+
kept = [
|
|
3042
|
+
statement
|
|
3043
|
+
for statement in text.split("\n")
|
|
3044
|
+
if set(HAZARD_CODE_PATTERN.findall(statement)) <= codes
|
|
3045
|
+
]
|
|
3046
|
+
return "\n".join(kept) or None
|
|
3047
|
+
|
|
3048
|
+
|
|
3049
|
+
def _extract_statements_from_sections(
|
|
3050
|
+
sections: dict[Section, str],
|
|
3051
|
+
*,
|
|
3052
|
+
allowed_sections: set[Section],
|
|
3053
|
+
pattern: re.Pattern[str],
|
|
3054
|
+
furniture: frozenset[str],
|
|
3055
|
+
) -> str | None:
|
|
3056
|
+
relevant_chunks = [
|
|
3057
|
+
sections[section] for section in allowed_sections if sections.get(section)
|
|
3058
|
+
]
|
|
3059
|
+
if not relevant_chunks:
|
|
3060
|
+
return None
|
|
3061
|
+
|
|
3062
|
+
combined = "\n\n".join(relevant_chunks)
|
|
3063
|
+
return _extract_statement_text(combined, pattern, furniture)
|
|
3064
|
+
|
|
3065
|
+
|
|
3066
|
+
def _parse_contact_info(
|
|
3067
|
+
section_text: str, furniture: frozenset[str]
|
|
3068
|
+
) -> tuple[ContactInfo, list[Message]]:
|
|
3069
|
+
warnings: list[Message] = []
|
|
3070
|
+
|
|
3071
|
+
if not section_text:
|
|
3072
|
+
warnings.append(Message("no-identification-section"))
|
|
3073
|
+
return ContactInfo(), warnings
|
|
3074
|
+
|
|
3075
|
+
contact = _read_supplier_of(section_text, furniture)
|
|
3076
|
+
contact.emergency_phone = _emergency_phone(section_text)
|
|
3077
|
+
|
|
3078
|
+
if not contact.supplier_name:
|
|
3079
|
+
warnings.append(Message("no-supplier-name"))
|
|
3080
|
+
if not contact.supplier_address:
|
|
3081
|
+
warnings.append(Message("no-supplier-address"))
|
|
3082
|
+
if not contact.supplier_phone:
|
|
3083
|
+
warnings.append(Message("no-supplier-phone"))
|
|
3084
|
+
if not contact.emergency_phone:
|
|
3085
|
+
warnings.append(Message("no-emergency-phone"))
|
|
3086
|
+
|
|
3087
|
+
return contact, warnings
|
|
3088
|
+
|
|
3089
|
+
|
|
3090
|
+
# The heading of subsection 1.4, numbered or not, behind a bullet or bare:
|
|
3091
|
+
# "1.4 Notrufnummer", "1,4 Notrufnummer", "· Notfallauskunft:", "Emergency
|
|
3092
|
+
# telephone number".
|
|
3093
|
+
_EMERGENCY_HEADING = re.compile(
|
|
3094
|
+
r"^[\s·•*\-–]*(?:1\s*[.,]\s*4\.?\s*[:\-–]?\s*)?(?:Notruf|Notfall|Emergency)",
|
|
3095
|
+
re.IGNORECASE,
|
|
3096
|
+
)
|
|
3097
|
+
# Another subsection of section 1: where the file draws 1.4 before it, what
|
|
3098
|
+
# follows is the supplier's block, and its numbers are the office's.
|
|
3099
|
+
_OTHER_SUBSECTION = re.compile(
|
|
3100
|
+
r"^[\s·•*\-–]*1\s*[.,]\s*[1-3]\.?(?:\s|$)|Einzelheiten\s+zum\s+Lieferanten",
|
|
3101
|
+
re.IGNORECASE,
|
|
3102
|
+
)
|
|
3103
|
+
# How many lines below its heading the number may stand: a poison centre's
|
|
3104
|
+
# name and address come first on some sheets.
|
|
3105
|
+
_EMERGENCY_REACH = 12
|
|
3106
|
+
_SHORT_EMERGENCY_NUMBER = re.compile(r"(?<![\d.,/])11[02](?![\d.,/])")
|
|
3107
|
+
# Numbers of section 1 that are no telephone: a REACH registration
|
|
3108
|
+
# ("01-2100000000-00-XXXX"), a CAS or EC number.
|
|
3109
|
+
_NO_TELEPHONE = re.compile(r"^\d{2}-\d{10}-\d{2}|^\d{2,7}-\d{2}-\d$|^\d{3}-\d{3}-\d$")
|
|
3110
|
+
# What stands behind the number on its line and is not it.
|
|
3111
|
+
_FAX_BEHIND = re.compile(r"\s*[-–,;/|]?\s*(?:Tele)?Fax\b.*$", re.IGNORECASE)
|
|
3112
|
+
|
|
3113
|
+
|
|
3114
|
+
def _emergency_number(text: str) -> re.Match[str] | None:
|
|
3115
|
+
"""The first telephone number in *text* as an emergency line prints one.
|
|
3116
|
+
|
|
3117
|
+
It begins with a plus, a zero or a bracket, as a number dialled in or to
|
|
3118
|
+
Germany does: "1907/2006" is the regulation, and a date is a date. "112"
|
|
3119
|
+
and "110" count as they stand.
|
|
3120
|
+
"""
|
|
3121
|
+
for found in _PHONE_NUMBER.finditer(text):
|
|
3122
|
+
number = found.group(0).strip()
|
|
3123
|
+
digits = sum(c.isdigit() for c in number)
|
|
3124
|
+
if (
|
|
3125
|
+
digits >= 6
|
|
3126
|
+
and number[0] in "+0("
|
|
3127
|
+
and not _DATE_ONLY.match(number)
|
|
3128
|
+
and not _DATE_ONLY.match(number.strip("()"))
|
|
3129
|
+
and not _NO_TELEPHONE.match(number)
|
|
3130
|
+
):
|
|
3131
|
+
return found
|
|
3132
|
+
return _SHORT_EMERGENCY_NUMBER.search(text)
|
|
3133
|
+
|
|
3134
|
+
|
|
3135
|
+
def _emergency_phone(section_text: str) -> str | None:
|
|
3136
|
+
"""The emergency number of subsection 1.4, as printed (SDB-117).
|
|
3137
|
+
|
|
3138
|
+
Behind the heading on its line, or on the first line below it that
|
|
3139
|
+
carries a telephone number; never past another subsection's heading.
|
|
3140
|
+
What stands there without a number -- a company's name, "siehe 1.3" --
|
|
3141
|
+
is no emergency number.
|
|
3142
|
+
"""
|
|
3143
|
+
lines = section_text.split("\n")
|
|
3144
|
+
headings = [i for i, line in enumerate(lines) if _EMERGENCY_HEADING.match(line)]
|
|
3145
|
+
# A number on the heading's own line first, wherever that heading stands:
|
|
3146
|
+
# where the file draws "1.4 Notrufnummer" above the supplier's block, the
|
|
3147
|
+
# lines below it are the office's, and the number follows its own label.
|
|
3148
|
+
for index in headings:
|
|
3149
|
+
if (value := _emergency_value(lines[index])) is not None:
|
|
3150
|
+
return value
|
|
3151
|
+
for index in headings:
|
|
3152
|
+
for below in lines[index + 1 : index + 1 + _EMERGENCY_REACH]:
|
|
3153
|
+
if _OTHER_SUBSECTION.search(below):
|
|
3154
|
+
break
|
|
3155
|
+
if (value := _emergency_value(below)) is not None:
|
|
3156
|
+
return value
|
|
3157
|
+
return None
|
|
3158
|
+
|
|
3159
|
+
|
|
3160
|
+
def _emergency_value(line: str) -> str | None:
|
|
3161
|
+
"""What *line* gives as the emergency number, or None without a number.
|
|
3162
|
+
|
|
3163
|
+
The line as printed, without what only labels it: the heading in front
|
|
3164
|
+
("Notrufnummer 1:"), a telephone label ("Tel.:") and a fax number behind.
|
|
3165
|
+
A poison centre's name stays: it says whose number it is.
|
|
3166
|
+
"""
|
|
3167
|
+
text = line.strip().lstrip("|·•*").strip()
|
|
3168
|
+
if _EMERGENCY_HEADING.match(text):
|
|
3169
|
+
_, colon, behind = text.partition(":")
|
|
3170
|
+
if colon and _emergency_number(behind):
|
|
3171
|
+
text = behind.strip()
|
|
3172
|
+
elif found := _emergency_number(text):
|
|
3173
|
+
text = text[found.start() :]
|
|
3174
|
+
else:
|
|
3175
|
+
return None
|
|
3176
|
+
elif _emergency_number(text) is None:
|
|
3177
|
+
return None
|
|
3178
|
+
text = _FAX_BEHIND.sub("", _PHONE_LABEL.sub("", text, count=1))
|
|
3179
|
+
return _clean_value(text) if _emergency_number(text) else None
|
|
3180
|
+
|
|
3181
|
+
|
|
3182
|
+
# The supplier block of section 1 (SDB-49, SDB-50): "1.3 Einzelheiten zum
|
|
3183
|
+
# Lieferanten, der das Sicherheitsdatenblatt bereitstellt", numbered "1.3",
|
|
3184
|
+
# "1.3." or "1,3" -- or titled so under another number, as a kit's sheet
|
|
3185
|
+
# does ("1.2 Einzelheiten zum Lieferanten der das Kit-Sicherheitsdatenblatt
|
|
3186
|
+
# bereitstellt").
|
|
3187
|
+
_SUPPLIER_HEADING = re.compile(
|
|
3188
|
+
r"^1\s*[.,]\s*3\.?\s*[:\-–]?(?:\s*(?-i:[A-ZÄÖÜ])[a-zäöüß]{3,}|\s*$)"
|
|
3189
|
+
r"|Einzelheiten\s+zum\s+Lieferanten",
|
|
3190
|
+
re.IGNORECASE,
|
|
3191
|
+
)
|
|
3192
|
+
_BARE_SUPPLIER_NUMBER = re.compile(r"^1\s*[.,]\s*3\.?\s*[:\-–]?\s*$")
|
|
3193
|
+
# What ends the block: the next numbered subsection, not one of 1.3's own.
|
|
3194
|
+
_NUMBERED_SUBSECTION = re.compile(r"^\d{1,2}\s*[.,]\s*\d{1,2}\b")
|
|
3195
|
+
_SUPPLIER_SUBSECTION = re.compile(r"^1\s*[.,]\s*\d\s*[.,]\s*\d{1,2}\.?\s*")
|
|
3196
|
+
# What is left of the heading's title on the lines below the number.
|
|
3197
|
+
# The end of the heading's title, broken by a hyphen and printed beside the
|
|
3198
|
+
# block's second line: "… das Sicherheitsdatenblatt bereit-" / "F. Eichhof
|
|
3199
|
+
# GmbH & Co. KG" / "stellt: Erlenweg 48".
|
|
3200
|
+
_SUPPLIER_TITLE_TAIL = re.compile(r"^(?:bereit)?stellt\s*:\s*", re.IGNORECASE)
|
|
3201
|
+
# A subsection's number before a supplier's label, where the sheet numbers
|
|
3202
|
+
# its lines and has no "1.3 Einzelheiten zum Lieferanten": "1.2
|
|
3203
|
+
# Inverkehrbringer: Birk Creations GmbH".
|
|
3204
|
+
_NUMBER_BEFORE_SUPPLIER_LABEL = re.compile(
|
|
3205
|
+
r"^1\s*[.,]\s*\d\.?\s+(?=(?:Angaben\s+zu[mr]\s+)?(?:Hersteller|Lieferant|Firma"
|
|
3206
|
+
r"|Firmenname|Firmenbezeichnung|Inverkehrbringer)\w*\s*[:/])",
|
|
3207
|
+
re.IGNORECASE,
|
|
3208
|
+
)
|
|
3209
|
+
_SUPPLIER_TITLE = re.compile(
|
|
3210
|
+
r"^(?:Einzelheiten\b|der\s+das\b|das\s+Sicherheitsdatenblatt\b"
|
|
3211
|
+
r"|Sicherheitsdatenblatt(?:\s+bereitstellt|\s*$)|bereitstellt\b"
|
|
3212
|
+
r"|zum\s+Lieferanten\b|Lieferanten,\s+der\s+das\b)",
|
|
3213
|
+
re.IGNORECASE,
|
|
3214
|
+
)
|
|
3215
|
+
# The label a supplier's name stands under or beside. "Anschrift" and
|
|
3216
|
+
# "Adresse" also head the poison centre's table under 1.4, so outside the
|
|
3217
|
+
# block they count only where no other label does.
|
|
3218
|
+
#
|
|
3219
|
+
# "Angaben zum Hersteller:", "Angaben zum Lieferanten:" are the same labels
|
|
3220
|
+
# in a sheet without numbered subsections.
|
|
3221
|
+
_SUPPLIER_LABEL = re.compile(
|
|
3222
|
+
r"^(?:Angaben\s+zu[mr]\s+)?(?:Herstellers?|Lieferant(?:en)?|Firma|Firmenname"
|
|
3223
|
+
r"|Firmenbezeichnung|Firmenidentifikation|Unternehmen|Inverkehrbringer|Händler"
|
|
3224
|
+
r"|Fabrik|Bezeichnung\s+des\s+Unternehmens)\b",
|
|
3225
|
+
re.IGNORECASE,
|
|
3226
|
+
)
|
|
3227
|
+
# A label that heads the name's line and says nothing more: "Name" over the
|
|
3228
|
+
# company in a table of the block.
|
|
3229
|
+
_BARE_NAME_LABEL = re.compile(r"^Name\s*:?$", re.IGNORECASE)
|
|
3230
|
+
# The same label with the company behind it (SDB-151): "Name: Musterchemie GmbH".
|
|
3231
|
+
_NAME_LABEL_WITH_VALUE = re.compile(r"^Name\s*:\s*(?=\S)", re.IGNORECASE)
|
|
3232
|
+
# "1.3 Verwendung des Produkts": a sheet that numbers section 1 its own way
|
|
3233
|
+
# has something else under 1.3 (SDB-132).
|
|
3234
|
+
_OTHER_TITLE_UNDER_1_3 = re.compile(
|
|
3235
|
+
r"^1\s*[.,]\s*3\.?\s*[:\-–]?\s*(?:Verwendung|Relevante|Notruf|Produkt)",
|
|
3236
|
+
re.IGNORECASE,
|
|
3237
|
+
)
|
|
3238
|
+
_ADDRESS_HEADING = re.compile(r"^(?:Anschrift|Adresse)\b", re.IGNORECASE)
|
|
3239
|
+
# The words of such a label, alone or joined by slashes: "Hersteller/Lieferant".
|
|
3240
|
+
_LABEL_WORDS = re.compile(
|
|
3241
|
+
r"^(?:Angaben\s+zu[mr]\s+)?(?:Herstellers?|Lieferant(?:en)?|Firma|Firmenname"
|
|
3242
|
+
r"|Firmenbezeichnung|Unternehmen|Inverkehrbringer|Händler|Anschrift|Adresse)"
|
|
3243
|
+
r"(?:\s*/\s*(?:Hersteller|Lieferant|Firma|Importeur|Vertrieb|Händler))*\b",
|
|
3244
|
+
re.IGNORECASE,
|
|
3245
|
+
)
|
|
3246
|
+
# "T" and "F" stand for telephone and fax only before a number, with no word
|
|
3247
|
+
# in lower case between: "T +49 8621 400", "T UK FREEPHONE 0800 006 1010",
|
|
3248
|
+
# not "F. Eichhof GmbH & Co. KG" or "F.W.Kiefer".
|
|
3249
|
+
_LETTER_LABEL = r"[TF]\b(?=(?-i:[^a-zäöüß\n]*)\d)"
|
|
3250
|
+
# A line of contact details, not of the address.
|
|
3251
|
+
_CONTACT_LINE = re.compile(
|
|
3252
|
+
rf"^(?:Tel(?:efon\w*)?\b\.?|Tlf\b\.?|Telefax\b|Fax\w*\b\.?|{_LETTER_LABEL}|Phone\b|Fon\b|E-?Mail|Mail\b"
|
|
3253
|
+
r"|Nicht-Notfall-Rufnummer"
|
|
3254
|
+
r"|Internet|Webseite|Website|Homepage|Web\b|URL\b|www\.|https?://|[\w.+-]+@)",
|
|
3255
|
+
re.IGNORECASE,
|
|
3256
|
+
)
|
|
3257
|
+
_PHONE_LABEL = re.compile(
|
|
3258
|
+
r"^(?:Tel(?:efon\w*)?\b\.?|Tlf\b\.?|T\b(?=(?-i:[^a-zäöüß\n]*)\d)|Phone\b|Fon\b"
|
|
3259
|
+
r"|Nicht-Notfall-Rufnummer\w*)"
|
|
3260
|
+
r"(?:[-\s]?Nr\.?)?\s*:?\s*",
|
|
3261
|
+
re.IGNORECASE,
|
|
3262
|
+
)
|
|
3263
|
+
# The same label behind other words of its line, with its colon. Not behind
|
|
3264
|
+
# a slash: "Telefax:/Telefon: 0551 153467" does not say whose number it is.
|
|
3265
|
+
_PHONE_LABEL_INSIDE = re.compile(
|
|
3266
|
+
r"(?<!/)\b(?:Tel(?:efon)?\.?|Tlf\.?|Fon)(?:[-\s]?Nr\.?)?\s*:\s*(?=[+(\d])",
|
|
3267
|
+
re.IGNORECASE,
|
|
3268
|
+
)
|
|
3269
|
+
_PICTOGRAM_LABELS = {
|
|
3270
|
+
"📞": "Telefon",
|
|
3271
|
+
"☎": "Telefon",
|
|
3272
|
+
"✆": "Telefon",
|
|
3273
|
+
"📠": "Telefax",
|
|
3274
|
+
"📧": "E-Mail",
|
|
3275
|
+
"✉": "E-Mail",
|
|
3276
|
+
"🌐": "Internet",
|
|
3277
|
+
}
|
|
3278
|
+
_CONTACT_PICTOGRAM = re.compile(
|
|
3279
|
+
"^([" + "".join(_PICTOGRAM_LABELS) + "])\ufe0f?\\s*:?\\s*"
|
|
3280
|
+
)
|
|
3281
|
+
# A telephone line that is not the supplier's office: a poison centre, an
|
|
3282
|
+
# emergency line.
|
|
3283
|
+
_NOT_THE_OFFICE = re.compile(r"Gift|Notruf|(?<!Nicht-)Notfall|Emergency", re.IGNORECASE)
|
|
3284
|
+
# Numbers that cost the caller or lead to a service desk: 0180x, 0900, 0800,
|
|
3285
|
+
# 0137. The office number is preferred where the block has one.
|
|
3286
|
+
_SERVICE_NUMBER = re.compile(r"^(?:00)?(?:49)?0?(?:180\d|900|800|137)")
|
|
3287
|
+
_DATE_ONLY = re.compile(r"^\d{1,2}[./]\d{1,2}[./]\d{2,4}$")
|
|
3288
|
+
# Any label of a telephone or fax number: without one in the block, a line
|
|
3289
|
+
# that is only a number is the telephone.
|
|
3290
|
+
_NUMBER_LABEL = re.compile(
|
|
3291
|
+
rf"^(?:(?:Tel(?:efon\w*)?|Tlf|Telefax|Fax\w*|Phone|Fon)\b|{_LETTER_LABEL})",
|
|
3292
|
+
re.IGNORECASE,
|
|
3293
|
+
)
|
|
3294
|
+
# A heading of section 1 where a name would stand: a table printed apart from
|
|
3295
|
+
# its headings leaves it there.
|
|
3296
|
+
_NOT_A_NAME = re.compile(
|
|
3297
|
+
r"^(?:Relevante\s+identifizierte|Verwendung(?:en)?\b|Notrufnummer|Produktidentifikator"
|
|
3298
|
+
r"|Bezeichnung\s+des|Einzelheiten\b)",
|
|
3299
|
+
re.IGNORECASE,
|
|
3300
|
+
)
|
|
3301
|
+
# A line that begins with a postcode and its town is an address line.
|
|
3302
|
+
_POSTCODE_FIRST = re.compile(r"^(?:[A-Z]{1,2}\s?[-–]?\s?)?\d{4,5}\s+[A-ZÄÖÜ]")
|
|
3303
|
+
# An address line in Germany (SDB-55): five digits first, "D-" or "DE-" before
|
|
3304
|
+
# them or not, or the country's name.
|
|
3305
|
+
_GERMAN_ADDRESS_LINE = re.compile(
|
|
3306
|
+
r"^(?:(?:D|DE)\s*[-–]?\s*)?\d{5}\b|\b(?:Deutschland|Germany|GERMANY)\b"
|
|
3307
|
+
)
|
|
3308
|
+
# How many lines may part the street from its town (see _read_supplier).
|
|
3309
|
+
_LINES_BETWEEN = 2
|
|
3310
|
+
# "(DE)" before one of several companies a block lists by country.
|
|
3311
|
+
_COUNTRY_TAG = re.compile(r"^\([A-Z]{2}\)\s*")
|
|
3312
|
+
# A legal form on the line below a name that ends "… GmbH & Co.".
|
|
3313
|
+
_LEGAL_FORM_ALONE = re.compile(r"(?:KG|KGaA|OHG|GmbH|AG|mbH)\.?,?")
|
|
3314
|
+
_NAME_LEFT_OPEN = re.compile(r"(?:&|\bCo\.?|\bund|\bu\.)$")
|
|
3315
|
+
# "(+49) 231 1450-0": the country code may stand in brackets, plus included.
|
|
3316
|
+
_PHONE_NUMBER = re.compile(r"\(?(?:\+\s?)?\(?\d[\d\s()/\-–.]{4,}\d")
|
|
3317
|
+
# A line that is a telephone number and nothing else: contact, not address.
|
|
3318
|
+
_PHONE_ONLY = re.compile(r"^\(?(?:\+\s?)?[\d\s()/\-–.]{6,}$")
|
|
3319
|
+
# Where the company's own details end: a department, a person, a hotline.
|
|
3320
|
+
_SUPPLIER_PART_END = re.compile(
|
|
3321
|
+
r"^(?:Auskunft|Auskunftgebender|Auskunftsgebender|Technische\s+Auskunft"
|
|
3322
|
+
r"|Ansprechpartner|Kontakt|Abteilung|Zuständig|Beratung|Notruf|Giftnotruf"
|
|
3323
|
+
r"|Giftinformation|GIZ|Notfall)",
|
|
3324
|
+
re.IGNORECASE,
|
|
3325
|
+
)
|
|
3326
|
+
# A poison centre or emergency line: its number is nobody's office phone.
|
|
3327
|
+
_EMERGENCY_PART = re.compile(
|
|
3328
|
+
r"^(?:Notruf|Giftnotruf|Giftinformation|GIZ|Notfall)", re.IGNORECASE
|
|
3329
|
+
)
|
|
3330
|
+
# Labels of an address line, dropped from it: "Straße: Seeuferstraße 4".
|
|
3331
|
+
_ADDRESS_LABEL = re.compile(
|
|
3332
|
+
r"^(?:(?:Straße|Strasse|Ort|PLZ|Postleitzahl|Postfach|Land|Stadt|Nat\.-Kenn\.)"
|
|
3333
|
+
r"(?:\s*/\s*(?:Straße|Strasse|Ort|PLZ|Postleitzahl|Postfach|Land|Stadt))*"
|
|
3334
|
+
r"|Anschrift|Adresse|Hauptsitz|Sitz)\s*:\s*",
|
|
3335
|
+
re.IGNORECASE,
|
|
3336
|
+
)
|
|
3337
|
+
# The same labels alone on their line, the value below: a table of the block.
|
|
3338
|
+
_BARE_ADDRESS_LABEL = re.compile(
|
|
3339
|
+
r"^(?:Straße|Strasse|Ort|PLZ|Postleitzahl|Land|Stadt|Anschrift"
|
|
3340
|
+
r"|Adresse|Geschäftsadresse|Postadresse|Hauptsitz|Sitz)$",
|
|
3341
|
+
re.IGNORECASE,
|
|
3342
|
+
)
|
|
3343
|
+
# A company's register or tax number: behind the address, not part of it.
|
|
3344
|
+
_REGISTER_LABEL = re.compile(r"^(?:Id-?Nr|USt-?Id-?Nr|Ust\.?-?Id)\b", re.IGNORECASE)
|
|
3345
|
+
# The second half of a legal form at the start of the address line: "…
|
|
3346
|
+
# GmbH" / "KG . Haselring 4 . 81602 Neufeld".
|
|
3347
|
+
_LEGAL_FORM_BEFORE_ADDRESS = re.compile(r"^(KG|KGaA|OHG)\s*[.,]\s*(?=\S)")
|
|
3348
|
+
_EMAIL = re.compile(r"[\w.+-]+@[\w-]+(?:\.[\w-]+)+")
|
|
3349
|
+
_WEBSITE = re.compile(r"\b(?:https?://|www\.)[^\s,;]+", re.IGNORECASE)
|
|
3350
|
+
# A company's legal form: a name without one may go on on the next line.
|
|
3351
|
+
_LEGAL_FORM = re.compile(
|
|
3352
|
+
r"\b(?:GmbH|AG|KG|KGaA|mbH|SE|OHG|GbR|e\.\s?K|e\.\s?V|B\.\s?V|N\.\s?V|S\.\s?A|S\.\s?p\.\s?A"
|
|
3353
|
+
r"|S\.\s?r\.\s?l|[Ss]rl|SRL|SAS|Ltd|Inc|LLC|plc|A/S|AB|Oy|Co)\b\.?",
|
|
3354
|
+
)
|
|
3355
|
+
|
|
3356
|
+
|
|
3357
|
+
def _read_supplier_of(section_text: str, furniture: frozenset[str]) -> ContactInfo:
|
|
3358
|
+
"""The supplier of section 1 (SDB-49, SDB-50).
|
|
3359
|
+
|
|
3360
|
+
Read from the block under 1.3; where that names nobody -- one supplier prints
|
|
3361
|
+
1.3's content as a table further down, after 1.4 -- from the first label
|
|
3362
|
+
of the supplier in section 1, and failing that from an address label.
|
|
3363
|
+
"""
|
|
3364
|
+
lines = [
|
|
3365
|
+
part
|
|
3366
|
+
for line in _without_page_breaks(section_text.split("\n"), furniture)
|
|
3367
|
+
for part in _supplier_lines(line)
|
|
3368
|
+
]
|
|
3369
|
+
starts: list[int] = []
|
|
3370
|
+
heading = next(
|
|
3371
|
+
(
|
|
3372
|
+
i
|
|
3373
|
+
for i, line in enumerate(lines)
|
|
3374
|
+
if _SUPPLIER_HEADING.search(line) and not _OTHER_TITLE_UNDER_1_3.match(line)
|
|
3375
|
+
),
|
|
3376
|
+
None,
|
|
3377
|
+
)
|
|
3378
|
+
labels = [
|
|
3379
|
+
label
|
|
3380
|
+
for pattern in (_SUPPLIER_LABEL, _ADDRESS_HEADING)
|
|
3381
|
+
if (
|
|
3382
|
+
label := next(
|
|
3383
|
+
(i for i, line in enumerate(lines) if _is_label(line, pattern)), None
|
|
3384
|
+
)
|
|
3385
|
+
)
|
|
3386
|
+
is not None
|
|
3387
|
+
]
|
|
3388
|
+
if heading is not None and _is_untitled(lines, heading):
|
|
3389
|
+
# "1.3" alone, no title below it: on a sheet from before REACH's
|
|
3390
|
+
# numbering that is "Zubereitung: …", and the supplier stands under
|
|
3391
|
+
# "1.4 Hersteller/Lieferant:".
|
|
3392
|
+
starts = [*labels[:1], heading + 1, *labels[1:]]
|
|
3393
|
+
else:
|
|
3394
|
+
starts = [*([heading + 1] if heading is not None else []), *labels]
|
|
3395
|
+
first: ContactInfo | None = None
|
|
3396
|
+
for start in starts:
|
|
3397
|
+
supplier = _german_supplier(_supplier_block(lines, start))
|
|
3398
|
+
if supplier.supplier_name:
|
|
3399
|
+
return supplier
|
|
3400
|
+
first = first or supplier
|
|
3401
|
+
return first or ContactInfo()
|
|
3402
|
+
|
|
3403
|
+
|
|
3404
|
+
def _is_untitled(lines: list[str], heading: int) -> bool:
|
|
3405
|
+
"""Whether the heading at *heading* is the bare number, its title not below."""
|
|
3406
|
+
if not _BARE_SUPPLIER_NUMBER.match(lines[heading]):
|
|
3407
|
+
return False
|
|
3408
|
+
below = lines[heading + 1] if heading + 1 < len(lines) else ""
|
|
3409
|
+
return not (_SUPPLIER_TITLE.match(below) or _SUPPLIER_HEADING.search(below))
|
|
3410
|
+
|
|
3411
|
+
|
|
3412
|
+
def _german_supplier(block: list[str]) -> ContactInfo:
|
|
3413
|
+
"""The supplier among the companies *block* names: the German one (SDB-55).
|
|
3414
|
+
|
|
3415
|
+
The first company, unless its address lies abroad and a later one's in
|
|
3416
|
+
Germany: one sheet names its Danish maker, then "Nationaler Kontakt" over
|
|
3417
|
+
the German company. E-mail and website are that company's own
|
|
3418
|
+
where the block gives them from its name on, else the first company's.
|
|
3419
|
+
"""
|
|
3420
|
+
first = _read_supplier(block)
|
|
3421
|
+
if not first.supplier_address or _in_germany(first.supplier_address):
|
|
3422
|
+
return first
|
|
3423
|
+
for index, line in enumerate(block[1:], start=1):
|
|
3424
|
+
if _EMERGENCY_PART.match(line):
|
|
3425
|
+
break
|
|
3426
|
+
if not _begins_a_company(line):
|
|
3427
|
+
continue
|
|
3428
|
+
company = _read_supplier(block[index:])
|
|
3429
|
+
if company.supplier_address and _in_germany(company.supplier_address):
|
|
3430
|
+
company.supplier_email = company.supplier_email or first.supplier_email
|
|
3431
|
+
company.information_email = (
|
|
3432
|
+
company.information_email or first.information_email
|
|
3433
|
+
)
|
|
3434
|
+
company.supplier_website = (
|
|
3435
|
+
company.supplier_website or first.supplier_website
|
|
3436
|
+
)
|
|
3437
|
+
return company
|
|
3438
|
+
return first
|
|
3439
|
+
|
|
3440
|
+
|
|
3441
|
+
def _begins_a_company(line: str) -> bool:
|
|
3442
|
+
"""Whether *line* names a company: a legal form, bare or behind a supplier's label."""
|
|
3443
|
+
labelled, value = _label_and_value(line)
|
|
3444
|
+
if labelled:
|
|
3445
|
+
return bool(_LEGAL_FORM.search(value))
|
|
3446
|
+
return ":" not in line and bool(_LEGAL_FORM.search(line))
|
|
3447
|
+
|
|
3448
|
+
|
|
3449
|
+
def _in_germany(address: str) -> bool:
|
|
3450
|
+
"""Whether a line of *address* begins with a German postcode or names the country."""
|
|
3451
|
+
return any(
|
|
3452
|
+
_GERMAN_ADDRESS_LINE.search(part.strip())
|
|
3453
|
+
for part in re.split(r", | \. | - ", address)
|
|
3454
|
+
)
|
|
3455
|
+
|
|
3456
|
+
|
|
3457
|
+
def _without_page_breaks(lines: list[str], furniture: frozenset[str]) -> list[str]:
|
|
3458
|
+
"""*lines* without a page number and the page header beside it.
|
|
3459
|
+
|
|
3460
|
+
A page that ends inside the block puts its footer and the next page's
|
|
3461
|
+
header between the supplier's lines. The header alone does not tell: it
|
|
3462
|
+
often carries the supplier's name. Beside a page number it is the page's.
|
|
3463
|
+
"""
|
|
3464
|
+
stripped = [line.strip() for line in lines]
|
|
3465
|
+
dropped: set[int] = set()
|
|
3466
|
+
for index, line in enumerate(stripped):
|
|
3467
|
+
if not _PAGE_NUMBER.match(line):
|
|
3468
|
+
continue
|
|
3469
|
+
dropped.add(index)
|
|
3470
|
+
for step in (-1, 1):
|
|
3471
|
+
neighbour = index + step
|
|
3472
|
+
while 0 <= neighbour < len(stripped) and stripped[neighbour] in furniture:
|
|
3473
|
+
dropped.add(neighbour)
|
|
3474
|
+
neighbour += step
|
|
3475
|
+
return [line for index, line in enumerate(lines) if index not in dropped]
|
|
3476
|
+
|
|
3477
|
+
|
|
3478
|
+
def _supplier_lines(line: str) -> list[str]:
|
|
3479
|
+
"""A line's parts, without bullet and without a number of 1.3's own ("1.3.1").
|
|
3480
|
+
|
|
3481
|
+
A sheet that sets its lines side by side parts them with a bar:
|
|
3482
|
+
"GERMANY | Tel. +49 2054 4048-70, Fax. +49 2054 4048-790".
|
|
3483
|
+
"""
|
|
3484
|
+
parts: list[str] = []
|
|
3485
|
+
for part in line.split(" | "):
|
|
3486
|
+
stripped = part.strip().lstrip("·•*-–. ").strip()
|
|
3487
|
+
stripped = _SUPPLIER_SUBSECTION.sub("", stripped).strip()
|
|
3488
|
+
stripped = _NUMBER_BEFORE_SUPPLIER_LABEL.sub("", stripped)
|
|
3489
|
+
pictogram = _CONTACT_PICTOGRAM.match(stripped)
|
|
3490
|
+
if pictogram:
|
|
3491
|
+
# A telephone, a letter and a globe for the labels.
|
|
3492
|
+
label = _PICTOGRAM_LABELS[pictogram.group(1)]
|
|
3493
|
+
stripped = f"{label}: {stripped[pictogram.end() :]}"
|
|
3494
|
+
if stripped:
|
|
3495
|
+
parts.append(stripped)
|
|
3496
|
+
return parts
|
|
3497
|
+
|
|
3498
|
+
|
|
3499
|
+
def _is_label(line: str, pattern: re.Pattern[str]) -> bool:
|
|
3500
|
+
"""Whether *line* is, or begins with, a label *pattern* names."""
|
|
3501
|
+
head = line.partition(":")[0]
|
|
3502
|
+
return (
|
|
3503
|
+
bool(pattern.match(head))
|
|
3504
|
+
and not any(c.isdigit() for c in head)
|
|
3505
|
+
and len(head) <= 120
|
|
3506
|
+
)
|
|
3507
|
+
|
|
3508
|
+
|
|
3509
|
+
def _supplier_block(lines: list[str], start: int) -> list[str]:
|
|
3510
|
+
"""The lines from *start* to the next numbered subsection, the title left out."""
|
|
3511
|
+
block: list[str] = []
|
|
3512
|
+
for line in lines[start:]:
|
|
3513
|
+
if _NUMBERED_SUBSECTION.match(line) and not _SUPPLIER_HEADING.search(line):
|
|
3514
|
+
break
|
|
3515
|
+
if (
|
|
3516
|
+
not block
|
|
3517
|
+
and _SUPPLIER_TITLE.match(line)
|
|
3518
|
+
and not any(c.isdigit() for c in line)
|
|
3519
|
+
):
|
|
3520
|
+
continue
|
|
3521
|
+
if not block and line.startswith("(") and line.endswith(")"):
|
|
3522
|
+
# "(Hersteller/Importeur/Alleinvertreter/nachgeschalteter
|
|
3523
|
+
# Anwender/Händler)": the heading's remark, nobody's name.
|
|
3524
|
+
continue
|
|
3525
|
+
line = _SUPPLIER_TITLE_TAIL.sub("", line)
|
|
3526
|
+
if line and not _BARE_SUPPLIER_NUMBER.match(line):
|
|
3527
|
+
block.append(line)
|
|
3528
|
+
return block
|
|
3529
|
+
|
|
3530
|
+
|
|
3531
|
+
def _label_and_value(line: str) -> tuple[bool, str]:
|
|
3532
|
+
"""Whether *line* is a supplier's label, and the value it carries.
|
|
3533
|
+
|
|
3534
|
+
Behind a colon, or behind the label itself where the value is a company:
|
|
3535
|
+
"Hersteller/Lieferant Haselwerk AG & Co. KGaA".
|
|
3536
|
+
"""
|
|
3537
|
+
named = _NAME_LABEL_WITH_VALUE.match(line)
|
|
3538
|
+
if named:
|
|
3539
|
+
return True, line[named.end() :].strip()
|
|
3540
|
+
if not (_is_label(line, _SUPPLIER_LABEL) or _is_label(line, _ADDRESS_HEADING)):
|
|
3541
|
+
return False, ""
|
|
3542
|
+
_, colon, value = line.partition(":")
|
|
3543
|
+
if colon:
|
|
3544
|
+
# "Händler Tel.:: Ahornwerk GmbH": a second label's colon before it.
|
|
3545
|
+
return True, value.strip().lstrip(":").strip()
|
|
3546
|
+
behind = _LABEL_WORDS.sub("", line).strip()
|
|
3547
|
+
return True, behind if _LEGAL_FORM.search(behind) else ""
|
|
3548
|
+
|
|
3549
|
+
|
|
3550
|
+
@dataclass(slots=True)
|
|
3551
|
+
class _Phones:
|
|
3552
|
+
"""The telephone numbers of a supplier block, by how they are marked."""
|
|
3553
|
+
|
|
3554
|
+
labelled: list[str] = field(default_factory=list)
|
|
3555
|
+
bare: list[str] = field(default_factory=list)
|
|
3556
|
+
# Behind a label that does not begin its line.
|
|
3557
|
+
inside: list[str] = field(default_factory=list)
|
|
3558
|
+
pending: bool = False
|
|
3559
|
+
|
|
3560
|
+
def read(self, line: str) -> None:
|
|
3561
|
+
if self.pending and not _CONTACT_LINE.match(line):
|
|
3562
|
+
self.pending = False
|
|
3563
|
+
self._keep(self.labelled, line)
|
|
3564
|
+
elif _PHONE_LABEL.match(line):
|
|
3565
|
+
number = _PHONE_NUMBER.search(line)
|
|
3566
|
+
if _NOT_THE_OFFICE.search(line[: number.start()] if number else line):
|
|
3567
|
+
return
|
|
3568
|
+
value = _PHONE_LABEL.sub("", line, count=1)
|
|
3569
|
+
self.pending = not self._keep(self.labelled, value)
|
|
3570
|
+
elif _PHONE_ONLY.match(line):
|
|
3571
|
+
self._keep(self.bare, line)
|
|
3572
|
+
elif (
|
|
3573
|
+
inside := _PHONE_LABEL_INSIDE.search(line)
|
|
3574
|
+
) and not _NOT_THE_OFFICE.search(line[: inside.start()]):
|
|
3575
|
+
# "Deutschland Telefon: 0800 - 6536776, Fax: 0800 - 6367628"
|
|
3576
|
+
self._keep(self.inside, line[inside.end() :])
|
|
3577
|
+
|
|
3578
|
+
@staticmethod
|
|
3579
|
+
def _keep(numbers: list[str], text: str) -> bool:
|
|
3580
|
+
number = _phone_number(text)
|
|
3581
|
+
if number is not None:
|
|
3582
|
+
numbers.append(number)
|
|
3583
|
+
return number is not None
|
|
3584
|
+
|
|
3585
|
+
def chosen(self, block: list[str]) -> str | None:
|
|
3586
|
+
"""The office number: labelled before bare, a service number last (SDB-50)."""
|
|
3587
|
+
numbers = list(self.labelled)
|
|
3588
|
+
if not numbers and not any(_NUMBER_LABEL.match(line) for line in block):
|
|
3589
|
+
numbers = self.bare[:1]
|
|
3590
|
+
numbers = numbers or self.inside
|
|
3591
|
+
office = [
|
|
3592
|
+
number
|
|
3593
|
+
for number in numbers
|
|
3594
|
+
if not _SERVICE_NUMBER.match("".join(c for c in number if c.isdigit()))
|
|
3595
|
+
]
|
|
3596
|
+
return next(iter(office or numbers), None)
|
|
3597
|
+
|
|
3598
|
+
|
|
3599
|
+
def _read_supplier(block: list[str]) -> ContactInfo:
|
|
3600
|
+
"""Name, address, phone, e-mail and website from a supplier block."""
|
|
3601
|
+
contact = ContactInfo()
|
|
3602
|
+
name: list[str] = []
|
|
3603
|
+
address: list[str] = []
|
|
3604
|
+
phones = _Phones()
|
|
3605
|
+
stage = "name"
|
|
3606
|
+
behind_address = 0
|
|
3607
|
+
named_at = len(block)
|
|
3608
|
+
block = _without_label_remarks(block)
|
|
3609
|
+
for index, line in enumerate(block):
|
|
3610
|
+
if stage == "name":
|
|
3611
|
+
labelled, value = _label_and_value(line)
|
|
3612
|
+
if labelled:
|
|
3613
|
+
if not value or value.endswith(":"):
|
|
3614
|
+
# No value, or another label where one would stand.
|
|
3615
|
+
continue
|
|
3616
|
+
stage = "after name"
|
|
3617
|
+
line = value
|
|
3618
|
+
elif _CONTACT_LINE.match(line) or _SUPPLIER_PART_END.match(line):
|
|
3619
|
+
stage = "contacts"
|
|
3620
|
+
elif (
|
|
3621
|
+
":" in line and not _LEGAL_FORM.search(line.partition(":")[0])
|
|
3622
|
+
) or _NOT_A_NAME.match(line):
|
|
3623
|
+
# "Zubereitung: Organisch-mineralischer NPK-Dünger": another
|
|
3624
|
+
# label's value, not a name. "XYZ GmbH & Co. KG, Dieselstr.
|
|
3625
|
+
# 3, D-61417 Birkenhofen, Tel.: +49 7537 4180 0" is one.
|
|
3626
|
+
continue
|
|
3627
|
+
else:
|
|
3628
|
+
stage = "after name"
|
|
3629
|
+
if (
|
|
3630
|
+
stage == "after name"
|
|
3631
|
+
and _POSTCODE_FIRST.match(line)
|
|
3632
|
+
and not _LEGAL_FORM.search(line)
|
|
3633
|
+
):
|
|
3634
|
+
# No name before the address: the block's table lost it. A
|
|
3635
|
+
# company's name may begin like a postcode: "2022 ES
|
|
3636
|
+
# Deutschland GmbH".
|
|
3637
|
+
stage = "address"
|
|
3638
|
+
elif stage == "after name":
|
|
3639
|
+
first, rest = _name_and_address(_COUNTRY_TAG.sub("", line))
|
|
3640
|
+
name = [_first_company(first)]
|
|
3641
|
+
named_at = index
|
|
3642
|
+
address.extend(_places_beside_a_name(rest, phones))
|
|
3643
|
+
continue
|
|
3644
|
+
if stage != "name" and _is_label(line, _SUPPLIER_LABEL):
|
|
3645
|
+
# A second "Lieferant": the next company, with a telephone of
|
|
3646
|
+
# its own (one supplier lists Germany, then Austria).
|
|
3647
|
+
break
|
|
3648
|
+
if stage == "after name":
|
|
3649
|
+
part, line = _name_part_of(name, address, line)
|
|
3650
|
+
name.extend(part)
|
|
3651
|
+
if not line:
|
|
3652
|
+
continue
|
|
3653
|
+
stage = "address"
|
|
3654
|
+
if stage == "address":
|
|
3655
|
+
head, colon, value = line.partition(":")
|
|
3656
|
+
number = value.partition(":")[0].strip()
|
|
3657
|
+
if colon and any(c.isdigit() for c in head) and _PHONE_ONLY.match(number):
|
|
3658
|
+
# A pictogram for a label: "D-49731 Talstadt (Unterland):
|
|
3659
|
+
# +49 (0) 5173 / 960 90 999", more pictograms' values behind.
|
|
3660
|
+
address.append(head.strip())
|
|
3661
|
+
line = number
|
|
3662
|
+
stage = "contacts"
|
|
3663
|
+
elif _ends_the_address(line):
|
|
3664
|
+
stage = "contacts"
|
|
3665
|
+
else:
|
|
3666
|
+
# "Anschrift:" or "Hauptsitz:" alone heads the lines below.
|
|
3667
|
+
value = _ADDRESS_LABEL.sub("", line).strip().rstrip(",;").strip()
|
|
3668
|
+
if value and not _BARE_ADDRESS_LABEL.match(value):
|
|
3669
|
+
address.append(value)
|
|
3670
|
+
continue
|
|
3671
|
+
if stage == "contacts":
|
|
3672
|
+
if (
|
|
3673
|
+
_EMERGENCY_PART.match(line)
|
|
3674
|
+
or _COUNTRY_TAG.match(line)
|
|
3675
|
+
# "Gipswerk GmbH Schweiz, Seestrasse 4, 3011 Bergingen,
|
|
3676
|
+
# Schweiz Telefon: +41 …": the next company and its number.
|
|
3677
|
+
or _names_another_company(line)
|
|
3678
|
+
):
|
|
3679
|
+
break
|
|
3680
|
+
behind_address += 1
|
|
3681
|
+
if (
|
|
3682
|
+
behind_address <= _LINES_BETWEEN + 1
|
|
3683
|
+
and _POSTCODE_FIRST.match(line)
|
|
3684
|
+
and not any(_POSTCODE_FIRST.match(part) for part in address)
|
|
3685
|
+
):
|
|
3686
|
+
# The town, parted from the street by a line or two of the
|
|
3687
|
+
# column beside them: "Haselring 4" / "E-Mail
|
|
3688
|
+
# sachkundige Person:" / "sds@sdb-dienst.example" / "81602 Neufeld -
|
|
3689
|
+
# Germany".
|
|
3690
|
+
address.append(line)
|
|
3691
|
+
continue
|
|
3692
|
+
phones.read(line)
|
|
3693
|
+
contact.supplier_name = _without_marks(" ".join(name)) or None
|
|
3694
|
+
contact.supplier_address = ", ".join(line for line in address if line) or None
|
|
3695
|
+
contact.supplier_phone = phones.chosen(block)
|
|
3696
|
+
(
|
|
3697
|
+
contact.supplier_email,
|
|
3698
|
+
contact.supplier_website,
|
|
3699
|
+
contact.information_email,
|
|
3700
|
+
) = _contact_addresses(block, named_at)
|
|
3701
|
+
return contact
|
|
3702
|
+
|
|
3703
|
+
|
|
3704
|
+
def _without_label_remarks(block: list[str]) -> list[str]:
|
|
3705
|
+
"""*block* without what only explains a label (SDB-133).
|
|
3706
|
+
|
|
3707
|
+
"Name" alone over the name, and the rest of a remark in brackets that a
|
|
3708
|
+
supplier's label opens and a later line closes: "Lieferant
|
|
3709
|
+
(Hersteller/Importeur/Alleinvertreter/nachgeschalteter" /
|
|
3710
|
+
"Anwender/Händler)". The label's own line stays; it carries no name.
|
|
3711
|
+
"""
|
|
3712
|
+
kept: list[str] = []
|
|
3713
|
+
remark_open = False
|
|
3714
|
+
for line in block:
|
|
3715
|
+
if remark_open:
|
|
3716
|
+
remark_open = ")" not in line
|
|
3717
|
+
continue
|
|
3718
|
+
if _BARE_NAME_LABEL.match(line):
|
|
3719
|
+
continue
|
|
3720
|
+
kept.append(line)
|
|
3721
|
+
remark_open = _label_and_value(line)[0] and line.count("(") > line.count(")")
|
|
3722
|
+
return kept
|
|
3723
|
+
|
|
3724
|
+
|
|
3725
|
+
def _name_part_of(
|
|
3726
|
+
name: list[str], address: list[str], line: str
|
|
3727
|
+
) -> tuple[list[str], str]:
|
|
3728
|
+
"""What of *line* below the name still belongs to it, and the rest of it.
|
|
3729
|
+
|
|
3730
|
+
The name's second line (SDB-49), or the second half of a legal form set
|
|
3731
|
+
before the address: "… GmbH" / "KG . Haselring 4 . 81602 Neufeld".
|
|
3732
|
+
"""
|
|
3733
|
+
if len(name) != 1 or address:
|
|
3734
|
+
return [], line
|
|
3735
|
+
if _continues_name(name[0], line):
|
|
3736
|
+
return [line.rstrip(",")], ""
|
|
3737
|
+
form = _LEGAL_FORM_BEFORE_ADDRESS.match(line)
|
|
3738
|
+
if form and _LEGAL_FORM.search(name[0]):
|
|
3739
|
+
return [form.group(1)], line[form.end() :]
|
|
3740
|
+
return [], line
|
|
3741
|
+
|
|
3742
|
+
|
|
3743
|
+
# A first name's full stop with a blank behind it, in front of the address:
|
|
3744
|
+
# "E-Mail (fachkundige Person): Vorname. Nachname@firma.example".
|
|
3745
|
+
_SPLIT_LOCAL_PART = re.compile(r"(?<=:\s)([A-Za-z]+\.)\s(?=[\w.+-]+@)")
|
|
3746
|
+
# A web address printed against the e-mail address with nothing between.
|
|
3747
|
+
_GLUED_WEBSITE = re.compile(r"(?<=[a-z])(?=www\.[\w-]+\.)")
|
|
3748
|
+
|
|
3749
|
+
|
|
3750
|
+
# What a sheet calls whoever answers for it (SDB-136), on the address's line,
|
|
3751
|
+
# on a line above, or heading the part of the block the address stands in:
|
|
3752
|
+
# first the competent person Annex II asks for, then an office that gives
|
|
3753
|
+
# information.
|
|
3754
|
+
_INFORMATION_LABELS = (
|
|
3755
|
+
re.compile(
|
|
3756
|
+
r"sachkundig|fachkundig|zuständig|verantwortlich"
|
|
3757
|
+
r"|zum\s+(?:Sicherheitsdatenblatt|SDB)\b",
|
|
3758
|
+
re.IGNORECASE,
|
|
3759
|
+
),
|
|
3760
|
+
re.compile(
|
|
3761
|
+
r"auskunft|auskünfte|ansprechpartner|kontaktperson|kontaktstelle"
|
|
3762
|
+
r"|technische[nr]?\s+information|regulatory|produktsicherheit",
|
|
3763
|
+
re.IGNORECASE,
|
|
3764
|
+
),
|
|
3765
|
+
)
|
|
3766
|
+
|
|
3767
|
+
|
|
3768
|
+
def _contact_addresses(
|
|
3769
|
+
block: list[str], named_at: int
|
|
3770
|
+
) -> tuple[str | None, str | None, str | None]:
|
|
3771
|
+
"""The block's first e-mail address, its first website, and the address
|
|
3772
|
+
of whoever answers for the sheet (SDB-50, SDB-134, SDB-136).
|
|
3773
|
+
|
|
3774
|
+
None of them from an emergency part below the name, at line *named_at*:
|
|
3775
|
+
what stands under "Notruf" or "Giftnotruf" there is the poison centre's.
|
|
3776
|
+
"""
|
|
3777
|
+
end = next(
|
|
3778
|
+
(
|
|
3779
|
+
index
|
|
3780
|
+
for index, line in enumerate(block)
|
|
3781
|
+
if index > named_at and _EMERGENCY_PART.match(line)
|
|
3782
|
+
),
|
|
3783
|
+
len(block),
|
|
3784
|
+
)
|
|
3785
|
+
own = [
|
|
3786
|
+
_GLUED_WEBSITE.sub(" ", _SPLIT_LOCAL_PART.sub(r"\1", line))
|
|
3787
|
+
if "@" in line
|
|
3788
|
+
else line
|
|
3789
|
+
for line in block[:end]
|
|
3790
|
+
]
|
|
3791
|
+
text = "\n".join(own)
|
|
3792
|
+
email = _EMAIL.search(text)
|
|
3793
|
+
website = _WEBSITE.search(text)
|
|
3794
|
+
return (
|
|
3795
|
+
email.group(0) if email else None,
|
|
3796
|
+
website.group(0).rstrip(".") if website else None,
|
|
3797
|
+
_information_email(own),
|
|
3798
|
+
)
|
|
3799
|
+
|
|
3800
|
+
|
|
3801
|
+
def _information_email(lines: list[str]) -> str | None:
|
|
3802
|
+
"""The address under a label of whoever answers for the sheet, or None.
|
|
3803
|
+
|
|
3804
|
+
An address belongs to the label last named at or before it. The
|
|
3805
|
+
competent person's goes before an information office's; an address under
|
|
3806
|
+
no such label is not one.
|
|
3807
|
+
"""
|
|
3808
|
+
found: list[tuple[int, str]] = []
|
|
3809
|
+
rank = len(_INFORMATION_LABELS)
|
|
3810
|
+
for line in lines:
|
|
3811
|
+
email = _EMAIL.search(line)
|
|
3812
|
+
label = line[: email.start()] if email else line
|
|
3813
|
+
rank = next(
|
|
3814
|
+
(
|
|
3815
|
+
index
|
|
3816
|
+
for index, pattern in enumerate(_INFORMATION_LABELS)
|
|
3817
|
+
if pattern.search(label)
|
|
3818
|
+
),
|
|
3819
|
+
rank,
|
|
3820
|
+
)
|
|
3821
|
+
if email and rank < len(_INFORMATION_LABELS):
|
|
3822
|
+
found.append((rank, email.group(0)))
|
|
3823
|
+
return min(found, key=lambda entry: entry[0])[1] if found else None
|
|
3824
|
+
|
|
3825
|
+
|
|
3826
|
+
def _places_beside_a_name(parts: list[str], phones: _Phones) -> list[str]:
|
|
3827
|
+
"""The address parts of a name's line; a telephone among them goes to *phones*."""
|
|
3828
|
+
places: list[str] = []
|
|
3829
|
+
for part in parts:
|
|
3830
|
+
# "D-91140 Nürnberg. Tel.: +49 (0)911 9410000 0"
|
|
3831
|
+
inside = _PHONE_LABEL_INSIDE.search(part)
|
|
3832
|
+
place = part[: inside.start()].rstrip(" .,;") if inside else part
|
|
3833
|
+
# "Tel.: …" alone; "54246 Lindendorf, D-Telefon: …" leaves "D-".
|
|
3834
|
+
if _CONTACT_LINE.match(part) or place in ("D-", "D"):
|
|
3835
|
+
place = ""
|
|
3836
|
+
if place:
|
|
3837
|
+
places.append(place)
|
|
3838
|
+
if place != part:
|
|
3839
|
+
phones.read(part)
|
|
3840
|
+
return places
|
|
3841
|
+
|
|
3842
|
+
|
|
3843
|
+
def _ends_the_address(line: str) -> bool:
|
|
3844
|
+
"""Whether *line* is the first behind the address: a contact, a department,
|
|
3845
|
+
a country's tag, or any label but an address line's own."""
|
|
3846
|
+
return bool(
|
|
3847
|
+
_CONTACT_LINE.match(line)
|
|
3848
|
+
or _PHONE_ONLY.match(line)
|
|
3849
|
+
or _SUPPLIER_PART_END.match(line)
|
|
3850
|
+
or _COUNTRY_TAG.match(line)
|
|
3851
|
+
or _REGISTER_LABEL.match(line)
|
|
3852
|
+
or (":" in line and not _ADDRESS_LABEL.match(line))
|
|
3853
|
+
)
|
|
3854
|
+
|
|
3855
|
+
|
|
3856
|
+
def _names_another_company(line: str) -> bool:
|
|
3857
|
+
"""Whether a line among the contact details opens a company of its own:
|
|
3858
|
+
a legal form before any colon, and an address behind a comma."""
|
|
3859
|
+
head = line.partition(":")[0]
|
|
3860
|
+
return (
|
|
3861
|
+
"," in head
|
|
3862
|
+
and _LEGAL_FORM.search(head.partition(",")[0]) is not None
|
|
3863
|
+
and not _CONTACT_LINE.match(line)
|
|
3864
|
+
and not _SUPPLIER_PART_END.match(line)
|
|
3865
|
+
)
|
|
3866
|
+
|
|
3867
|
+
|
|
3868
|
+
def _continues_name(name: str, line: str) -> bool:
|
|
3869
|
+
"""Whether *line* is the second line of the company's *name* (SDB-49)."""
|
|
3870
|
+
if _NAME_LEFT_OPEN.search(name) and _LEGAL_FORM_ALONE.fullmatch(line):
|
|
3871
|
+
return True
|
|
3872
|
+
# "Alfred Kirsch" over "Lackfabrik GmbH & Co. KG"; not "Custom Parts
|
|
3873
|
+
# Europe" over "The Old Mill", the first line of its address.
|
|
3874
|
+
return (
|
|
3875
|
+
not _LEGAL_FORM.search(name)
|
|
3876
|
+
and _LEGAL_FORM.search(line) is not None
|
|
3877
|
+
and not any(c.isdigit() for c in line)
|
|
3878
|
+
and not _CONTACT_LINE.match(line)
|
|
3879
|
+
and not _SUPPLIER_PART_END.match(line)
|
|
3880
|
+
and ":" not in line
|
|
3881
|
+
)
|
|
3882
|
+
|
|
3883
|
+
|
|
3884
|
+
def _name_and_address(line: str) -> tuple[str, list[str]]:
|
|
3885
|
+
"""A name line that carries the address too, split into the two.
|
|
3886
|
+
|
|
3887
|
+
"Ahornwerk Germany AG; Auenweg 22; 95044 Seestadt / Fluss" at its semicolons;
|
|
3888
|
+
"Buchfeld GmbH, Am Hafen 74, D- 29327 Feldstadt" at its commas, where
|
|
3889
|
+
the first part ends in a legal form and a number follows.
|
|
3890
|
+
"""
|
|
3891
|
+
if ";" in line:
|
|
3892
|
+
head, *parts = (part.strip() for part in line.split(";"))
|
|
3893
|
+
return head, [part for part in parts if part]
|
|
3894
|
+
for separator in (" – ", ", ", " . ", " - "):
|
|
3895
|
+
head, found, tail = line.partition(separator)
|
|
3896
|
+
if found and _LEGAL_FORM.search(head) and any(c.isdigit() for c in tail):
|
|
3897
|
+
return head, [
|
|
3898
|
+
part.strip() for part in tail.split(separator) if part.strip()
|
|
3899
|
+
]
|
|
3900
|
+
return line, []
|
|
3901
|
+
|
|
3902
|
+
|
|
3903
|
+
# A company's legal form with what belongs to it: "GmbH & Co. KG".
|
|
3904
|
+
_LEGAL_FORM_RUN = re.compile(
|
|
3905
|
+
rf"{_LEGAL_FORM.pattern}"
|
|
3906
|
+
rf"(?:\s*(?:&|\+|und|u\.)?\s*(?:Co\b\.?\s*)?{_LEGAL_FORM.pattern})*"
|
|
3907
|
+
)
|
|
3908
|
+
# An abbreviation's full stop at a name's end: "Co.", "B.V.", "a.s.".
|
|
3909
|
+
_ENDS_IN_ABBREVIATION = re.compile(
|
|
3910
|
+
r"(?:\b(?:Co|Inc|Ltd|Corp|Cie)|\b(?:[A-Za-z]\.\s?)+[A-Za-z])\.$"
|
|
3911
|
+
)
|
|
3912
|
+
|
|
3913
|
+
|
|
3914
|
+
def _first_company(name: str) -> str:
|
|
3915
|
+
"""*name* up to its first company where the line carries two (SDB-133).
|
|
3916
|
+
|
|
3917
|
+
A table with a column per country prints them side by side: "Birkenhof
|
|
3918
|
+
GmbH Birkenhof AG". The first ends with its legal form; what follows
|
|
3919
|
+
is another where it begins like the first and has a legal form of its own.
|
|
3920
|
+
"""
|
|
3921
|
+
form = _LEGAL_FORM_RUN.search(name)
|
|
3922
|
+
if form is None:
|
|
3923
|
+
return name
|
|
3924
|
+
behind = name[form.end() :].split()
|
|
3925
|
+
if (
|
|
3926
|
+
not behind
|
|
3927
|
+
or behind[0].casefold() != name.split()[0].casefold()
|
|
3928
|
+
or not _LEGAL_FORM.search(" ".join(behind[1:]))
|
|
3929
|
+
):
|
|
3930
|
+
return name
|
|
3931
|
+
return name[: form.end()]
|
|
3932
|
+
|
|
3933
|
+
|
|
3934
|
+
def _without_marks(name: str) -> str:
|
|
3935
|
+
"""*name* without a footnote's asterisk and stray punctuation at its end."""
|
|
3936
|
+
name = name.strip()
|
|
3937
|
+
while name:
|
|
3938
|
+
if not (
|
|
3939
|
+
name[-1] in "*,;:"
|
|
3940
|
+
or name.endswith((" .", "..", " -", " –"))
|
|
3941
|
+
or (name.endswith(".") and not _ENDS_IN_ABBREVIATION.search(name))
|
|
3942
|
+
):
|
|
3943
|
+
break
|
|
3944
|
+
name = name[:-1].rstrip()
|
|
3945
|
+
return name
|
|
3946
|
+
|
|
3947
|
+
|
|
3948
|
+
def _phone_number(text: str) -> str | None:
|
|
3949
|
+
"""The first telephone number in *text*, nothing behind it ("Telefax …")."""
|
|
3950
|
+
match = _PHONE_NUMBER.search(text)
|
|
3951
|
+
if match is None:
|
|
3952
|
+
return None
|
|
3953
|
+
number = match.group(0).strip()
|
|
3954
|
+
if number.count("(") > number.count(")"):
|
|
3955
|
+
if number.rindex("(") == 0:
|
|
3956
|
+
# "(+49 231 1450-0)": the bracket holds the number, and closes
|
|
3957
|
+
# behind it.
|
|
3958
|
+
number = number[1:].strip()
|
|
3959
|
+
else:
|
|
3960
|
+
# "+49 (0) 1806/31-9000 (0,14 €/Min. …)": the bracket opens a remark.
|
|
3961
|
+
number = number[: number.rindex("(")].strip()
|
|
3962
|
+
if _DATE_ONLY.match(number):
|
|
3963
|
+
return None
|
|
3964
|
+
digits = "".join(c for c in number if c.isdigit())
|
|
3965
|
+
# 19240 is the number every German poison centre shares (0551 19240,
|
|
3966
|
+
# 030 19240 …): a sheet's emergency line, never the supplier's office.
|
|
3967
|
+
if len(digits) < 6 or digits.endswith("19240"):
|
|
3968
|
+
return None
|
|
3969
|
+
return number
|
|
3970
|
+
|
|
3971
|
+
|
|
3972
|
+
# Where subsection 1.1 ends: the heading of 1.2 or 1.3, with or without number.
|
|
3973
|
+
_AFTER_PRODUCT_IDENTIFIER = re.compile(
|
|
3974
|
+
r"^\W*(?:1\s*[.,]\s*[2-4]\b"
|
|
3975
|
+
r"|Relevante identifizierte Verwendungen"
|
|
3976
|
+
r"|Verwendung des Stoff"
|
|
3977
|
+
r"|Einzelheiten zum Lieferanten"
|
|
3978
|
+
r"|Angaben zum (?:Hersteller|Lieferanten))",
|
|
3979
|
+
re.IGNORECASE,
|
|
3980
|
+
)
|
|
3981
|
+
# A label that names the product by the name it is sold under (SDB-131):
|
|
3982
|
+
# each of its parts, between slashes or "oder", is one of these.
|
|
3983
|
+
_TRADE_NAME_PART = re.compile(
|
|
3984
|
+
r"^(?:handelsnamen?(?: des (?:produkte?s|gemischs?|stoffe?s))?"
|
|
3985
|
+
r"|handelsbezeichnung|produktnamen?|produktbezeichnung|markenname"
|
|
3986
|
+
r"|artikelbezeichnung|bezeichnung"
|
|
3987
|
+
r"|(?:die )?bezeichnung des (?:produkte?s|gemischs?|artikels)"
|
|
3988
|
+
r"|(?:ghs )?produkt-?(?:identi?fikator(?:en)?|indikator)(?: handelsnamen?)?)$"
|
|
3989
|
+
)
|
|
3990
|
+
# A label that names the product in other words: its trade name goes first.
|
|
3991
|
+
_OTHER_NAME_PART = re.compile(
|
|
3992
|
+
r"^(?:(?:die )?(?:name|bezeichnung)"
|
|
3993
|
+
r"(?: de[sr] (?:produkte?s|gemischs?|stoffe?s|artikels|zubereitung))?"
|
|
3994
|
+
r"|der zubereitung|produkt|stoffname|substanzname)$"
|
|
3995
|
+
)
|
|
3996
|
+
# What stands in front of a label: a bullet, the subsection's number.
|
|
3997
|
+
_BEFORE_LABEL = re.compile(r"^[^\w(]*ꞏ?\s*(?:\d+\s*[.,]\s*)*\d*\s*\.?[^\w(]*")
|
|
3998
|
+
# A bullet or dash in front of a value; a quote or bracket belongs to it.
|
|
3999
|
+
_BULLET = re.compile(r"^(?:[^\w\"„“”»«‚‘'(\[<]|ꞏ)+")
|
|
4000
|
+
# A trade name's label and the name on one line, with no colon between; one
|
|
4001
|
+
# letter too many on the label is the sheet's slip (SDB-148).
|
|
4002
|
+
_LABEL_THEN_NAME = re.compile(
|
|
4003
|
+
r"^(?:Handelsname[a-z]?(?:\s*/\s*Bezeichnung)?|Handelsbezeichnung"
|
|
4004
|
+
r"|Produktnamen?|Produktbezeichnung)\s+(?=\S)",
|
|
4005
|
+
re.IGNORECASE,
|
|
4006
|
+
)
|
|
4007
|
+
# A line that is a label of something else and carries no value: it ends in a
|
|
4008
|
+
# colon, or it is one of the labels sheets print without one.
|
|
4009
|
+
_OTHER_LABEL = re.compile(
|
|
4010
|
+
r"^(?:.*:"
|
|
4011
|
+
r"|UFI\b.*|.*(?:\b(?:Nr|Code|Codes)|nummern?)\.?"
|
|
4012
|
+
r"|Produkt(?:form|art|typ|gruppe|code|nummer)(?:\s*\([^()]*\))?"
|
|
4013
|
+
r"|Stoff-?\s*/\s*Produktidentifikation|Andere Bezeichnungen"
|
|
4014
|
+
r"|Weitere Handelsnamen.*|Geltungsbereich|Synonyme?"
|
|
4015
|
+
r"|Seite\s*:?\s*\d+.*"
|
|
4016
|
+
# A formula identifier, also with a character too many in a group, as
|
|
4017
|
+
# text recognition reads it (SDB-148).
|
|
4018
|
+
r"|[A-Z0-9]{4,5}(?:-[A-Z0-9]{4,}){3}"
|
|
4019
|
+
# Labels sheets print without a colon, the value elsewhere (SDB-148).
|
|
4020
|
+
r"|(?:REACH[-\s]?)?Registrier(?:ungs)?[-\s]?(?:nummer|nr\.?)\b.*"
|
|
4021
|
+
r"|Eindeutiger\s+Rezepturidentifikator.*"
|
|
4022
|
+
r"|Tele(?:fon|fax)|Fax|E-?Mail(?:-Adresse)?"
|
|
4023
|
+
# The title of section 1 in another language, where its heading left it.
|
|
4024
|
+
r"|Identification\s+of\s+the\s+(?:substance|mixture|product)\b.*)$",
|
|
4025
|
+
re.IGNORECASE,
|
|
4026
|
+
)
|
|
4027
|
+
# "Produktform: Gemisch", "UFI: …": a label of something else with its value;
|
|
4028
|
+
# a registration number's label may be longer, with the substance it belongs
|
|
4029
|
+
# to ("REACH-Registrierungs-Nr. von n-Butylacetat: 01-…", SDB-148).
|
|
4030
|
+
_LABELLED_VALUE = re.compile(
|
|
4031
|
+
r"^(?:[^:\d()]{2,40}|(?:REACH[-\s]?)?Registrier[^:]{2,80}):\s*\S", re.IGNORECASE
|
|
4032
|
+
)
|
|
4033
|
+
# An article number with its label in front of the name on the name's line
|
|
4034
|
+
# (SDB-148): "Art.-Nr. 12.345 Musterdünger".
|
|
4035
|
+
_ARTICLE_NUMBER_FIRST = re.compile(
|
|
4036
|
+
r"^Art(?:ikel)?\.?\s?-?\s?(?:Nr|Nummer)\b\.?:?\s*[\w./-]*\d[\w./-]*\s+(?=\S)",
|
|
4037
|
+
re.IGNORECASE,
|
|
4038
|
+
)
|
|
4039
|
+
# Or its label behind the name, the number on the next line (SDB-148):
|
|
4040
|
+
# "Musterspray 200 ml Art.Nr." Two words at least stand before it.
|
|
4041
|
+
_ARTICLE_LABEL_LAST = re.compile(
|
|
4042
|
+
r"^(\S*[^\W\d_]\S*\s+\S+(?:\s+\S+)*?)\s+Art(?:ikel)?\.?\s?-?\s?(?:Nr|Nummer)\b\.?:?$",
|
|
4043
|
+
re.IGNORECASE,
|
|
4044
|
+
)
|
|
4045
|
+
# A supplier's label, alone or with its value: where 1.1 ends on a sheet
|
|
4046
|
+
# that prints no heading for 1.2 or 1.3 (SDB-148).
|
|
4047
|
+
_SUPPLIER_LABEL_LINE = re.compile(
|
|
4048
|
+
r"^(?:Hersteller|Lieferant|Firma|Firmenname|Firmenbezeichnung|Inverkehrbringer)"
|
|
4049
|
+
r"(?:\s*/\s*(?:Hersteller|Lieferant|Firma|Importeur|Vertrieb|Händler))*"
|
|
4050
|
+
r"\s*(?::.*)?$",
|
|
4051
|
+
re.IGNORECASE,
|
|
4052
|
+
)
|
|
4053
|
+
# The number of a subsection alone on its line.
|
|
4054
|
+
_BARE_1_1 = re.compile(r"^1\s*[.,]\s*1\.?$")
|
|
4055
|
+
_BARE_1_2 = re.compile(r"^1\s*[.,]\s*2\.?$")
|
|
4056
|
+
# "Produkt: Musterreiniger" in a sheet's header (SDB-150).
|
|
4057
|
+
_PRODUCT_LABEL = re.compile(r"^produkt$")
|
|
4058
|
+
# A header line that is not the product's name (SDB-150): the sheet's title
|
|
4059
|
+
# or what it was made by, its version, a date, a page.
|
|
4060
|
+
_HEADER_LINE = re.compile(
|
|
4061
|
+
r"^(?:EU-|EG-)?Sicherheitsdatenbl[aä]tt|^gem(?:äß|äss|\.)\s"
|
|
4062
|
+
r"|^(?:nach|laut|entsprechend)\s+(?:der\s+)?(?:Verordnung|Richtlinie)"
|
|
4063
|
+
r"|^(?:Version|Revision|Fassung|Stand|Datum|Druck|Seite|Page|Ersetzt|Ersatz"
|
|
4064
|
+
r"|Überarbeit|Bearbeit|Erstell|Ausgabe|Ausstell|Gültig)"
|
|
4065
|
+
r"|\b\d{1,2}\s?[./]\s?\d{1,2}\s?[./]\s?(?:\d{4}|\d{2})\b"
|
|
4066
|
+
r"|\b(?:1907/2006|2020/878|453/2010|2015/830)\b",
|
|
4067
|
+
re.IGNORECASE,
|
|
4068
|
+
)
|
|
4069
|
+
# The heading of 1.2 behind a name, where the text lost the line break.
|
|
4070
|
+
_HEADING_BEHIND_NAME = re.compile(r"\s*:?\s*1\s*\.\s*2\b\.?\s+Relevante\b.*$")
|
|
4071
|
+
|
|
4072
|
+
|
|
4073
|
+
def _label_parts(key: str) -> list[str]:
|
|
4074
|
+
key = re.sub(r"\([^)]*\)", " ", _BEFORE_LABEL.sub("", key.casefold()))
|
|
4075
|
+
key = re.sub(r",?\s*ufi\b", " ", key)
|
|
4076
|
+
return [" ".join(part.split()) for part in re.split(r"/| oder ", key)]
|
|
4077
|
+
|
|
4078
|
+
|
|
4079
|
+
def _is_trade_name_label(key: str) -> bool:
|
|
4080
|
+
return all(_TRADE_NAME_PART.match(part) for part in _label_parts(key))
|
|
4081
|
+
|
|
4082
|
+
|
|
4083
|
+
def _is_name_label(key: str) -> bool:
|
|
4084
|
+
return all(
|
|
4085
|
+
_TRADE_NAME_PART.match(part) or _OTHER_NAME_PART.match(part)
|
|
4086
|
+
for part in _label_parts(key)
|
|
4087
|
+
)
|
|
4088
|
+
|
|
4089
|
+
|
|
4090
|
+
def _name_value(value: str) -> str:
|
|
4091
|
+
"""The name without the bullet or dash a sheet sets in front of it.
|
|
4092
|
+
|
|
4093
|
+
Nor the article number a sheet prints in front of it with its label
|
|
4094
|
+
(SDB-148): "Art.-Nr. 12.345 Musterdünger".
|
|
4095
|
+
"""
|
|
4096
|
+
name = _BULLET.sub("", _clean_value(value))
|
|
4097
|
+
name = _ARTICLE_NUMBER_FIRST.sub("", name)
|
|
4098
|
+
return _HEADING_BEHIND_NAME.sub("", name).strip()
|
|
4099
|
+
|
|
4100
|
+
|
|
4101
|
+
# A reference to another place of the sheet where the name should stand
|
|
4102
|
+
# (SDB-153): "Handelsname: siehe oben".
|
|
4103
|
+
_REFERENCE = re.compile(r"^siehe\s+oben\.?$", re.IGNORECASE)
|
|
4104
|
+
|
|
4105
|
+
|
|
4106
|
+
def _is_no_name(line: str) -> bool:
|
|
4107
|
+
"""Whether *line* under a name's label is something other than the name."""
|
|
4108
|
+
return bool(
|
|
4109
|
+
_REFERENCE.match(_BULLET.sub("", line).strip())
|
|
4110
|
+
or _OTHER_LABEL.match(_BULLET.sub("", line))
|
|
4111
|
+
or _LABELLED_VALUE.match(_BULLET.sub("", line))
|
|
4112
|
+
or _is_name_label(line.rstrip(": "))
|
|
4113
|
+
or not re.search(r"[^\W\d_]", line)
|
|
4114
|
+
)
|
|
4115
|
+
|
|
4116
|
+
|
|
4117
|
+
def _extract_product_identifier(section_text: str) -> str | None:
|
|
4118
|
+
"""The product's name as subsection 1.1 prints it (SDB-131).
|
|
4119
|
+
|
|
4120
|
+
Behind a label that names the product, the trade name's first, on its
|
|
4121
|
+
line or on the next one that is no label; a heading "Produktidentifikator"
|
|
4122
|
+
is such a label. Further down in section 1 only behind a label with the
|
|
4123
|
+
name on its line.
|
|
4124
|
+
"""
|
|
4125
|
+
if not section_text:
|
|
4126
|
+
return None
|
|
4127
|
+
|
|
4128
|
+
lines = [
|
|
4129
|
+
_normalize_colon_spacing(line).strip()
|
|
4130
|
+
for line in _merge_broken_pairs(section_text.split("\n"))
|
|
4131
|
+
]
|
|
4132
|
+
lines = [line for line in lines if line]
|
|
4133
|
+
end = _end_of_1_1(lines)
|
|
4134
|
+
first = lines[:end]
|
|
4135
|
+
for is_label in (_is_trade_name_label, _is_name_label):
|
|
4136
|
+
for line in first:
|
|
4137
|
+
key, _, value = line.partition(":")
|
|
4138
|
+
name = _name_value(value)
|
|
4139
|
+
if is_label(key) and _REFERENCE.match(name):
|
|
4140
|
+
# "Handelsname: siehe oben": the sheet names no product here,
|
|
4141
|
+
# and which line it means it does not say (SDB-153).
|
|
4142
|
+
return None
|
|
4143
|
+
if is_label(key) and name and not _is_name_label(name):
|
|
4144
|
+
return name
|
|
4145
|
+
for index, line in enumerate(first):
|
|
4146
|
+
if not _is_name_label(line.rstrip(": ")) and not _LABEL_THEN_NAME.match(
|
|
4147
|
+
_BULLET.sub("", line)
|
|
4148
|
+
):
|
|
4149
|
+
continue
|
|
4150
|
+
for later in first[index:]:
|
|
4151
|
+
bare = _BULLET.sub("", later)
|
|
4152
|
+
joined = _LABEL_THEN_NAME.match(bare)
|
|
4153
|
+
if joined and not _is_name_label(bare.rstrip(": ")):
|
|
4154
|
+
# "Produktbezeichnung Musterreiniger": label and name, no colon.
|
|
4155
|
+
return _name_value(bare[joined.end() :])
|
|
4156
|
+
before_number = _ARTICLE_LABEL_LAST.match(bare)
|
|
4157
|
+
if before_number:
|
|
4158
|
+
# "Musterspray 200 ml Art.Nr.": the number stands below.
|
|
4159
|
+
return _name_value(before_number.group(1))
|
|
4160
|
+
if not _is_no_name(later):
|
|
4161
|
+
return _name_value(later)
|
|
4162
|
+
for line in lines[end:]:
|
|
4163
|
+
key, _, value = line.partition(":")
|
|
4164
|
+
if _is_trade_name_label(key) and _label_parts(key) != ["bezeichnung"]:
|
|
4165
|
+
name = _name_value(value)
|
|
4166
|
+
if name:
|
|
4167
|
+
return name
|
|
4168
|
+
return None
|
|
4169
|
+
|
|
4170
|
+
|
|
4171
|
+
def _end_of_1_1(lines: list[str]) -> int:
|
|
4172
|
+
"""Where subsection 1.1 ends among the lines of section 1 (SDB-131, SDB-148).
|
|
4173
|
+
|
|
4174
|
+
At the heading of a later subsection, or at a supplier's label on a sheet
|
|
4175
|
+
that prints none. "1.2" alone under "1.1" alone is no heading: the sheet
|
|
4176
|
+
set both numbers in front of what follows.
|
|
4177
|
+
"""
|
|
4178
|
+
for index, line in enumerate(lines):
|
|
4179
|
+
if not index:
|
|
4180
|
+
continue
|
|
4181
|
+
if _BARE_1_2.match(line) and _BARE_1_1.match(lines[index - 1]):
|
|
4182
|
+
continue
|
|
4183
|
+
if _AFTER_PRODUCT_IDENTIFIER.match(line) or _SUPPLIER_LABEL_LINE.match(
|
|
4184
|
+
_BULLET.sub("", line)
|
|
4185
|
+
):
|
|
4186
|
+
return index
|
|
4187
|
+
return len(lines)
|
|
4188
|
+
|
|
4189
|
+
|
|
4190
|
+
def _value_before_label(text: str) -> str | None:
|
|
4191
|
+
"""The name a sheet prints above its label (SDB-149).
|
|
4192
|
+
|
|
4193
|
+
": Musterreiniger" / "Handelsname": the line with the colon and the value
|
|
4194
|
+
stands directly above the trade name's label, which is alone on its line
|
|
4195
|
+
and has no value below. Read from the sheet's text as it stands, up to
|
|
4196
|
+
the heading of 1.2: the sections' text has such a value joined to the
|
|
4197
|
+
label above it already.
|
|
4198
|
+
"""
|
|
4199
|
+
lines = [line.strip() for line in text.split("\n") if line.strip()]
|
|
4200
|
+
for index, line in enumerate(lines):
|
|
4201
|
+
if index and _AFTER_PRODUCT_IDENTIFIER.match(line):
|
|
4202
|
+
break
|
|
4203
|
+
if not index or ":" in line or not _is_trade_name_label(line):
|
|
4204
|
+
continue
|
|
4205
|
+
above = lines[index - 1]
|
|
4206
|
+
below = lines[index + 1] if index + 1 < len(lines) else ""
|
|
4207
|
+
if above.startswith(":") and not below.startswith(":"):
|
|
4208
|
+
name = _name_value(above.lstrip(": "))
|
|
4209
|
+
if name and not _is_no_name(name):
|
|
4210
|
+
return name
|
|
4211
|
+
return None
|
|
4212
|
+
|
|
4213
|
+
|
|
4214
|
+
def _lines_above_section_1(text: str) -> list[str]:
|
|
4215
|
+
"""The lines of a sheet's header: what stands above its first section."""
|
|
4216
|
+
above: list[str] = []
|
|
4217
|
+
for line in _merge_split_headings(_merge_broken_pairs(text.split("\n"))):
|
|
4218
|
+
stripped = line.strip()
|
|
4219
|
+
if not stripped:
|
|
4220
|
+
continue
|
|
4221
|
+
if _read_section_heading(stripped) is not None:
|
|
4222
|
+
break
|
|
4223
|
+
subsection = SUBSECTION_PATTERN.match(stripped)
|
|
4224
|
+
if subsection and SUBSECTION_TITLES[int(subsection.group(1))].search(
|
|
4225
|
+
subsection.group(3)
|
|
4226
|
+
):
|
|
4227
|
+
break
|
|
4228
|
+
above.append(stripped)
|
|
4229
|
+
return above
|
|
4230
|
+
|
|
4231
|
+
|
|
4232
|
+
def _name_in_header(section_text: str, above: list[str]) -> str | None:
|
|
4233
|
+
"""The product's name from the sheet's header, where 1.1 gives none (SDB-150).
|
|
4234
|
+
|
|
4235
|
+
Behind a label of the trade name, or "Produkt", with the name on its line.
|
|
4236
|
+
Else, where 1.1 prints the trade name's label and leaves its field empty,
|
|
4237
|
+
the last line of the header, directly above section 1: the headline such
|
|
4238
|
+
a sheet sets its product's name in. Not a line that is the sheet's title,
|
|
4239
|
+
a date, a version, a page or another label's value.
|
|
4240
|
+
"""
|
|
4241
|
+
for line in above:
|
|
4242
|
+
key, colon, value = _normalize_colon_spacing(line).partition(":")
|
|
4243
|
+
if not colon or not (
|
|
4244
|
+
_is_trade_name_label(key) or _PRODUCT_LABEL.match(key.strip().casefold())
|
|
4245
|
+
):
|
|
4246
|
+
continue
|
|
4247
|
+
name = _name_value(value)
|
|
4248
|
+
if name and not _is_no_name(name):
|
|
4249
|
+
return name
|
|
4250
|
+
if not above or not _has_empty_name_field(section_text):
|
|
4251
|
+
return None
|
|
4252
|
+
last = above[-1]
|
|
4253
|
+
if _is_no_name(last) or _HEADER_LINE.search(last):
|
|
4254
|
+
return None
|
|
4255
|
+
return _name_value(last)
|
|
4256
|
+
|
|
4257
|
+
|
|
4258
|
+
def _has_empty_name_field(section_text: str) -> bool:
|
|
4259
|
+
"""Whether 1.1 prints the trade name's label with nothing behind it."""
|
|
4260
|
+
lines = [
|
|
4261
|
+
_normalize_colon_spacing(line).strip()
|
|
4262
|
+
for line in section_text.split("\n")
|
|
4263
|
+
if line.strip()
|
|
4264
|
+
]
|
|
4265
|
+
return any(
|
|
4266
|
+
line.endswith(":") and _is_trade_name_label(line.rstrip(": "))
|
|
4267
|
+
for line in lines[: _end_of_1_1(lines)]
|
|
4268
|
+
)
|
|
4269
|
+
|
|
4270
|
+
|
|
4271
|
+
def _extract_version(text: str) -> str | None:
|
|
4272
|
+
"""
|
|
4273
|
+
Extract the document's own version from SDS text.
|
|
4274
|
+
|
|
4275
|
+
REACH Annex II puts the revision indication on the first page, and in
|
|
4276
|
+
practice suppliers repeat it in a running header or footer on every page.
|
|
4277
|
+
Section 16 then lists the *superseded* versions once each. Searching the
|
|
4278
|
+
whole document therefore turns up several numbers, so the one repeated
|
|
4279
|
+
most often -- the running header -- is the document's own version, and a
|
|
4280
|
+
single mention is a reference to something else.
|
|
4281
|
+
|
|
4282
|
+
Args:
|
|
4283
|
+
text: The full SDS text to search.
|
|
4284
|
+
|
|
4285
|
+
Returns:
|
|
4286
|
+
The version string (e.g., "3.3") or None if not found.
|
|
4287
|
+
"""
|
|
4288
|
+
for pattern in (
|
|
4289
|
+
VERSION_BEHIND_DATE_PATTERN,
|
|
4290
|
+
VERSION_PATTERN,
|
|
4291
|
+
REVISION_NUMBER_PATTERN,
|
|
4292
|
+
):
|
|
4293
|
+
matches = [
|
|
4294
|
+
m.group(1).replace(",", ".").replace("-", ".")
|
|
4295
|
+
for m in pattern.finditer(text)
|
|
4296
|
+
# "(ersetzt Version 1.03)" sits in the same repeated footer line as
|
|
4297
|
+
# the current number, so it survives the frequency count. Reading
|
|
4298
|
+
# the words just before the label is what separates them.
|
|
4299
|
+
if not SUPERSEDED_MARKER.search(text[max(0, m.start() - 40) : m.start()])
|
|
4300
|
+
# "Letzte Version: 06.03.2023" names a day, not a number.
|
|
4301
|
+
and not _DATE_AS_VERSION.fullmatch(m.group(1))
|
|
4302
|
+
]
|
|
4303
|
+
if not matches:
|
|
4304
|
+
continue
|
|
4305
|
+
counts = Counter(matches)
|
|
4306
|
+
top = max(counts.values())
|
|
4307
|
+
# Ties keep document order so the result stays deterministic.
|
|
4308
|
+
for value in matches:
|
|
4309
|
+
if counts[value] == top:
|
|
4310
|
+
return value
|
|
4311
|
+
return None
|
|
4312
|
+
|
|
4313
|
+
|
|
4314
|
+
def _parse_date_parts(day: str, month: str, year: str) -> date | None:
|
|
4315
|
+
"""Build a date from "23", "Nov", "2023" or "23", "11", "2023"."""
|
|
4316
|
+
if month.isdigit():
|
|
4317
|
+
month_number = int(month)
|
|
4318
|
+
else:
|
|
4319
|
+
month_number = _MONTH_NAMES.get(month.casefold().rstrip(".")[:3], 0)
|
|
4320
|
+
try:
|
|
4321
|
+
return date(int(year), month_number, int(day))
|
|
4322
|
+
except ValueError:
|
|
4323
|
+
logger.warning("sds.date.invalid", value=f"{day}.{month}.{year}")
|
|
4324
|
+
return None
|
|
4325
|
+
|
|
4326
|
+
|
|
4327
|
+
def _latest_dated_match(text: str, pattern: re.Pattern[str]) -> date | None:
|
|
4328
|
+
"""Return the most recent date *pattern* finds, or None.
|
|
4329
|
+
|
|
4330
|
+
Section 16 lists superseded revisions with their older dates, and a sheet
|
|
4331
|
+
can carry more than one dated label, so the newest is the one that
|
|
4332
|
+
describes the document in hand.
|
|
4333
|
+
"""
|
|
4334
|
+
found = [
|
|
4335
|
+
parsed
|
|
4336
|
+
for match in pattern.finditer(text)
|
|
4337
|
+
if (parsed := _date_of(match)) is not None
|
|
4338
|
+
]
|
|
4339
|
+
return max(found) if found else None
|
|
4340
|
+
|
|
4341
|
+
|
|
4342
|
+
def _date_of(match: re.Match[str]) -> date | None:
|
|
4343
|
+
"""The date a _DATED fragment matched, written day first or year first."""
|
|
4344
|
+
day, month, year, iso_year, iso_month, iso_day = match.groups()
|
|
4345
|
+
if day is None:
|
|
4346
|
+
return _parse_date_parts(iso_day, iso_month, iso_year)
|
|
4347
|
+
return _parse_date_parts(day, month, year)
|
|
4348
|
+
|
|
4349
|
+
|
|
4350
|
+
def _extract_revision_date(text: str) -> date | None:
|
|
4351
|
+
"""Extract the revision date ("Überarbeitet am: 16.11.2020") from SDS text."""
|
|
4352
|
+
found = [
|
|
4353
|
+
parsed
|
|
4354
|
+
for pattern in (
|
|
4355
|
+
REVISION_DATE_BEFORE_VERSION_PATTERN,
|
|
4356
|
+
REVISION_DATE_BEFORE_LABEL_PATTERN,
|
|
4357
|
+
REVISION_DATE_GRID_PATTERN,
|
|
4358
|
+
VERSION_CREATED_PATTERN,
|
|
4359
|
+
VERSION_CREATED_ABOVE_PATTERN,
|
|
4360
|
+
)
|
|
4361
|
+
if (parsed := _latest_dated_match(text, pattern)) is not None
|
|
4362
|
+
]
|
|
4363
|
+
for match in REVISION_DATE_PATTERN.finditer(text):
|
|
4364
|
+
if _dates_another_version(text, match):
|
|
4365
|
+
continue
|
|
4366
|
+
dated: re.Match[str] | None = match
|
|
4367
|
+
while dated is not None:
|
|
4368
|
+
parsed = _date_of(dated)
|
|
4369
|
+
if parsed is not None:
|
|
4370
|
+
found.append(parsed)
|
|
4371
|
+
dated = _FURTHER_DATE.match(text, dated.end())
|
|
4372
|
+
return max(found) if found else None
|
|
4373
|
+
|
|
4374
|
+
|
|
4375
|
+
def _dates_another_version(text: str, match: re.Match[str]) -> bool:
|
|
4376
|
+
"""Whether *match* dates a version the sheet only names (SDB-57)."""
|
|
4377
|
+
if not _VERSION_LABEL_START.match(match.group(0)):
|
|
4378
|
+
return False
|
|
4379
|
+
before = text[max(0, match.start() - 60) : match.start()]
|
|
4380
|
+
return _OTHER_VERSION_BEFORE.search(before) is not None
|
|
4381
|
+
|
|
4382
|
+
|
|
4383
|
+
def _extract_issue_date(text: str) -> date | None:
|
|
4384
|
+
"""Extract the issue date ("Ausstellungsdatum: 11.07.2013") from SDS text."""
|
|
4385
|
+
return _latest_dated_match(text, ISSUE_DATE_PATTERN)
|
|
4386
|
+
|
|
4387
|
+
|
|
4388
|
+
# A header table read column by column (SDB-92): the values first, each on a
|
|
4389
|
+
# line of its own, then their labels in the same order.
|
|
4390
|
+
#
|
|
4391
|
+
# 02.01.2013 Erstfassung
|
|
4392
|
+
# 08.04.2020 Überarbeitet am
|
|
4393
|
+
_DATE_LABEL_LINE = re.compile(
|
|
4394
|
+
r"[^\S\n]*(?:(überarbeitet\s+am|überarbeitungsdatum|revisionsdatum)"
|
|
4395
|
+
r"|erstfassung|erstellt\s+am|erstellungsdatum|ausgabedatum|druckdatum)"
|
|
4396
|
+
r"[^\S\n]*:?[^\S\n]*",
|
|
4397
|
+
re.IGNORECASE,
|
|
4398
|
+
)
|
|
4399
|
+
_DATE_ONLY_LINE = re.compile(rf"[^\S\n]*{_DATE_FRAGMENT}[^\S\n]*")
|
|
4400
|
+
# How far above its labels a column of values may stand.
|
|
4401
|
+
_COLUMN_REACH = 15
|
|
4402
|
+
|
|
4403
|
+
|
|
4404
|
+
def _extract_date_from_columns(text: str) -> date | None:
|
|
4405
|
+
"""The revision date of a header table whose values precede their labels.
|
|
4406
|
+
|
|
4407
|
+
Only where the count agrees: as many dates in a row as date labels in a
|
|
4408
|
+
row, so each date has its label.
|
|
4409
|
+
"""
|
|
4410
|
+
lines = text.split("\n")
|
|
4411
|
+
found = []
|
|
4412
|
+
for index, line in enumerate(lines):
|
|
4413
|
+
label = _DATE_LABEL_LINE.fullmatch(line)
|
|
4414
|
+
if label is None or not label.group(1):
|
|
4415
|
+
continue
|
|
4416
|
+
first = index
|
|
4417
|
+
while first > 0 and _DATE_LABEL_LINE.fullmatch(lines[first - 1]):
|
|
4418
|
+
first -= 1
|
|
4419
|
+
last = index
|
|
4420
|
+
while last + 1 < len(lines) and _DATE_LABEL_LINE.fullmatch(lines[last + 1]):
|
|
4421
|
+
last += 1
|
|
4422
|
+
end = first
|
|
4423
|
+
while end > max(0, first - _COLUMN_REACH) and not _DATE_ONLY_LINE.fullmatch(
|
|
4424
|
+
lines[end - 1]
|
|
4425
|
+
):
|
|
4426
|
+
end -= 1
|
|
4427
|
+
start = end
|
|
4428
|
+
while start > 0 and _DATE_ONLY_LINE.fullmatch(lines[start - 1]):
|
|
4429
|
+
start -= 1
|
|
4430
|
+
if end - start != last - first + 1 or end == start:
|
|
4431
|
+
continue
|
|
4432
|
+
match = _DATE_ONLY_LINE.fullmatch(lines[start + index - first])
|
|
4433
|
+
parsed = _parse_date_parts(*match.groups()) if match else None
|
|
4434
|
+
if parsed is not None:
|
|
4435
|
+
found.append(parsed)
|
|
4436
|
+
return max(found) if found else None
|
|
4437
|
+
|
|
4438
|
+
|
|
4439
|
+
# How many lines below its label a revision date may stand (SDB-103).
|
|
4440
|
+
_LABEL_REACH = 3
|
|
4441
|
+
|
|
4442
|
+
|
|
4443
|
+
def _extract_date_below_label(text: str) -> date | None:
|
|
4444
|
+
"""The revision date that stands a few lines below its label (SDB-103).
|
|
4445
|
+
|
|
4446
|
+
A header whose cells reach the text one after another: "Überarbeitet am:",
|
|
4447
|
+
"Artikel-Nr.: 2297PR", "Seite 1 von 6", "17.07.2020". The lines between
|
|
4448
|
+
are complete in themselves; an empty line or another label without its
|
|
4449
|
+
value ends the search, because the date may be that label's.
|
|
4450
|
+
"""
|
|
4451
|
+
lines = text.split("\n")
|
|
4452
|
+
found = []
|
|
4453
|
+
for index, line in enumerate(lines):
|
|
4454
|
+
label = _DATE_LABEL_LINE.fullmatch(line)
|
|
4455
|
+
if label is None or not label.group(1):
|
|
4456
|
+
continue
|
|
4457
|
+
for below in lines[index + 1 : index + 1 + _LABEL_REACH]:
|
|
4458
|
+
match = _DATE_ONLY_LINE.fullmatch(below)
|
|
4459
|
+
if match:
|
|
4460
|
+
parsed = _parse_date_parts(*match.groups())
|
|
4461
|
+
if parsed is not None:
|
|
4462
|
+
found.append(parsed)
|
|
4463
|
+
break
|
|
4464
|
+
if not below.strip() or below.rstrip().endswith(":"):
|
|
4465
|
+
break
|
|
4466
|
+
return max(found) if found else None
|
|
4467
|
+
|
|
4468
|
+
|
|
4469
|
+
# A month and a year (SDB-90): "STAND: 06/2022", "Stand: Februar 2003", or
|
|
4470
|
+
# "03 / 2012" alone on a line. Last of all, and narrow: the number of a
|
|
4471
|
+
# regulation looks the same ("Verordnung Nr. 8/1985", "NN 1/2021").
|
|
4472
|
+
# Also behind the revision label (SDB-94): "Überarbeitet am: März 2013",
|
|
4473
|
+
# "Überarbeitet am: April/2017".
|
|
4474
|
+
_MONTH_YEAR_LABEL = (
|
|
4475
|
+
r"\b(?:Stand|Überarbeitet[^\S\n]+am|Überarbeitungsdatum|Revisionsdatum)"
|
|
4476
|
+
r"[^\S\n]*:[^\S\n]*"
|
|
4477
|
+
)
|
|
4478
|
+
MONTH_YEAR_PATTERN = re.compile(
|
|
4479
|
+
rf"{_MONTH_YEAR_LABEL}(0?[1-9]|1[0-2])[^\S\n]?/[^\S\n]?((?:19|20)\d{{2}})\b"
|
|
4480
|
+
rf"|{_MONTH_YEAR_LABEL}([A-Za-zä]{{3,9}})\.?(?:[^\S\n]+|[^\S\n]?/[^\S\n]?)"
|
|
4481
|
+
r"((?:19|20)\d{2})\b"
|
|
4482
|
+
r"|^[^\S\n]*(0[1-9]|1[0-2])[^\S\n]?/[^\S\n]?((?:19|20)\d{2})[^\S\n]*\.?[^\S\n]*$"
|
|
4483
|
+
# "Stand 10.2013" in a footer (SDB-100).
|
|
4484
|
+
r"|^[^\S\n]*Stand[^\S\n]*:?[^\S\n]*(0[1-9]|1[0-2])\.((?:19|20)\d{2})\b",
|
|
4485
|
+
re.IGNORECASE | re.MULTILINE,
|
|
4486
|
+
)
|
|
4487
|
+
|
|
4488
|
+
|
|
4489
|
+
def _extract_month_year_date(text: str) -> date | None:
|
|
4490
|
+
"""The newest month-and-year date of a sheet that states no other, as the
|
|
4491
|
+
first of its month, or None."""
|
|
4492
|
+
found = []
|
|
4493
|
+
for match in MONTH_YEAR_PATTERN.finditer(text):
|
|
4494
|
+
month, year = next(
|
|
4495
|
+
(match.group(i), match.group(i + 1)) for i in (1, 3, 5, 7) if match.group(i)
|
|
4496
|
+
)
|
|
4497
|
+
if not month.isdigit() and month.casefold()[:3] not in _MONTH_NAMES:
|
|
4498
|
+
continue
|
|
4499
|
+
parsed = _parse_date_parts("1", month, year)
|
|
4500
|
+
if parsed is not None:
|
|
4501
|
+
found.append(parsed)
|
|
4502
|
+
return max(found) if found else None
|
|
4503
|
+
|
|
4504
|
+
|
|
4505
|
+
def _extract_short_year_date(text: str) -> date | None:
|
|
4506
|
+
"""Read a date whose year is written with two digits, or None.
|
|
4507
|
+
|
|
4508
|
+
See SHORT_YEAR_DATE_PATTERN: a last resort for a document that has yielded
|
|
4509
|
+
no date at all, never a substitute for one that has.
|
|
4510
|
+
"""
|
|
4511
|
+
found = [
|
|
4512
|
+
parsed
|
|
4513
|
+
for match in SHORT_YEAR_DATE_PATTERN.finditer(text)
|
|
4514
|
+
if (
|
|
4515
|
+
parsed := _parse_date_parts(
|
|
4516
|
+
match.group(1), match.group(2), _expand_year(match.group(3))
|
|
4517
|
+
)
|
|
4518
|
+
)
|
|
4519
|
+
is not None
|
|
4520
|
+
]
|
|
4521
|
+
return max(found) if found else None
|
|
4522
|
+
|
|
4523
|
+
|
|
4524
|
+
def _expand_year(two_digits: str) -> str:
|
|
4525
|
+
"""Expand a two-digit year, with the century boundary fixed at 90."""
|
|
4526
|
+
number = int(two_digits)
|
|
4527
|
+
return str(1900 + number if number >= 90 else 2000 + number)
|
|
4528
|
+
|
|
4529
|
+
|
|
4530
|
+
def _extract_effective_date(text: str) -> date | None:
|
|
4531
|
+
"""Extract the date a sheet states it takes effect, or None.
|
|
4532
|
+
|
|
4533
|
+
See EFFECTIVE_DATE_PATTERN: this is a fallback for sheets that state no
|
|
4534
|
+
revision and no issue date, never a substitute for either.
|
|
4535
|
+
"""
|
|
4536
|
+
return _latest_dated_match(text, EFFECTIVE_DATE_PATTERN)
|
|
4537
|
+
|
|
4538
|
+
|
|
4539
|
+
def _merge_broken_pairs(lines: Iterable[str]) -> list[str]:
|
|
4540
|
+
merged: list[str] = []
|
|
4541
|
+
lines_list = list(lines)
|
|
4542
|
+
i = 0
|
|
4543
|
+
while i < len(lines_list):
|
|
4544
|
+
current = lines_list[i].rstrip()
|
|
4545
|
+
|
|
4546
|
+
# Look ahead for colon, skipping blank lines
|
|
4547
|
+
j = i + 1
|
|
4548
|
+
while j < len(lines_list) and not lines_list[j].strip():
|
|
4549
|
+
j += 1
|
|
4550
|
+
|
|
4551
|
+
next_line = lines_list[j].strip() if j < len(lines_list) else ""
|
|
4552
|
+
|
|
4553
|
+
if next_line.startswith(":") and current.strip():
|
|
4554
|
+
value_text = next_line.lstrip(": ")
|
|
4555
|
+
|
|
4556
|
+
# If colon line has no value, look for value on next line
|
|
4557
|
+
if not value_text:
|
|
4558
|
+
k = j + 1
|
|
4559
|
+
while k < len(lines_list) and not lines_list[k].strip():
|
|
4560
|
+
k += 1
|
|
4561
|
+
if k < len(lines_list):
|
|
4562
|
+
value_text = lines_list[k].strip()
|
|
4563
|
+
# A key whose value the layout left empty, last in its
|
|
4564
|
+
# section, is followed by the next section's heading -- and
|
|
4565
|
+
# taking that as the value buries it mid-line, where no
|
|
4566
|
+
# heading is read. "Das Produkt entspricht den Kriterien
|
|
4567
|
+
# für PBT- oder vPvB-Stoffen ... 1907/2006 :" closes 2.3 on
|
|
4568
|
+
# one supplier's sheets, and their section 3 opened a page
|
|
4569
|
+
# late, at the running header, with the ingredient table
|
|
4570
|
+
# filed under section 2.
|
|
4571
|
+
if _is_section_heading(value_text):
|
|
4572
|
+
merged.append(f"{current.strip()}:")
|
|
4573
|
+
i = k
|
|
4574
|
+
continue
|
|
4575
|
+
merged.append(f"{current.strip()}: {value_text}")
|
|
4576
|
+
i = k + 1
|
|
4577
|
+
continue
|
|
4578
|
+
|
|
4579
|
+
merged.append(f"{current.strip()}: {value_text}")
|
|
4580
|
+
i = j + 1
|
|
4581
|
+
continue
|
|
4582
|
+
|
|
4583
|
+
merged.append(current)
|
|
4584
|
+
i += 1
|
|
4585
|
+
|
|
4586
|
+
return merged
|
|
4587
|
+
|
|
4588
|
+
|
|
4589
|
+
def _is_section_heading(line: str) -> bool:
|
|
4590
|
+
return (
|
|
4591
|
+
SECTION_HEADER_PATTERN.search(line) is not None
|
|
4592
|
+
or read_numbered_section_heading(line) is not None
|
|
4593
|
+
)
|
|
4594
|
+
|
|
4595
|
+
|
|
4596
|
+
def _normalize_colon_spacing(line: str) -> str:
|
|
4597
|
+
return re.sub(r"\s*:\s*", ": ", line.strip())
|
|
4598
|
+
|
|
4599
|
+
|
|
4600
|
+
def _clean_value(value: str) -> str:
|
|
4601
|
+
return re.sub(r"\s+", " ", value).strip()
|