File size: 10,715 Bytes
ebaa0d1
 
 
 
 
 
 
 
 
 
 
 
 
aa64aba
ebaa0d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa64aba
 
 
 
 
 
 
ebaa0d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa64aba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebaa0d1
 
 
aa64aba
 
 
 
ebaa0d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/env python3
"""Extract Poucher's measured duration-of-evaporation coefficients.

The source table is in Poucher Vol II, Chapter 4, "Duration of evaporation
table" (parsed source pages 70-74). The numeric coefficient is the target:
it is measured by olfaction on smelling strips, not computed from molecular
features.
"""
from __future__ import annotations

import json
import re
from collections import defaultdict
from statistics import median
from pathlib import Path

from extract_poucher_tiers import match_name_to_cas

DATA = Path("data")
PAGES = DATA / "literature_flat" / "literature_pages.jsonl"
OUT = DATA / "poucher_substantivity.jsonl"
PUBCHEM_MAP = DATA / "poucher_pubchem_name_to_cas.json"


# Page 70 and page 74 are two-column layouts where the existing OCR text flattens
# columns out of visual order. These groups were verified against the page images.
PAGE_70_GROUPS: dict[int, list[str]] = {
    1: [
        "Acetophenone", "Almonds", "Amyl acetate", "Benzaldehyde",
        "Benzyl acetate", "Ethyl acetate", "Ethyl acetoacetate",
        "Iso-butyl acetate", "Methyl benzoate", "Niaouli", "Octyl acetate",
        "Phenylethyl acetate", "Phenylethyl formate",
        "Phenylethyl propionate", "Phenylethyl salicylate",
    ],
    2: [
        "Benzyl formate", "Bois de rose", "Ethyl benzoate",
        "Limes distilled", "Linalol", "Mandarin", "Methyl salicylate",
    ],
    3: [
        "Benzyl cinnamate", "Coriander", "Para cresyl methyl ether",
        "Para cresyl acetate", "Para cresyl iso-butyrate",
        "Cuminic aldehyde", "Cyclohexanyl butyrate", "Decyl formate",
        "Dimethyl benzyl carbinol", "Dimethyl benzyl acetate",
        "Ethyl decine carbonate", "Ethyl salicylate",
    ],
}

PAGE_74_GROUPS: dict[int, list[str]] = {
    54: ["Cedryl acetate"],
    55: ["Nerolidol"],
    60: ["Benzyl phenylacetate", "Citral", "Rhodinyl formate"],
    62: ["Amyl phenylacetate"],
    65: ["Cinnamic alcohol nat."],
    70: ["Linalyl salicylate", "Jasmin decolore"],
    73: ["Cassie absolute, Farnesiana"],
    77: ["Methyl naphthyl ketone"],
    79: ["Civette absolute"],
    80: ["Hydroxy citronellal"],
    85: ["Phenylacetaldehyde dimethyl acetal"],
    87: ["Octyl aldehyde"],
    88: ["Ethyl methyl phenyl glycidate"],
    89: ["Cyclamen aldehyde"],
    90: [
        "Galbanum resin", "Opoponax resin", "Orris oleo resin",
        "Rhodinyl acetate", "Santal W.A.", "Tarragon",
        "Jasmin chassis incolore",
    ],
    91: ["Phenylethyl phenylacetate", "Undecalactone"],
    94: ["Angelica root", "Birch bud"],
    96: ["Arnica flowers"],
    100: [
        "Acet eugenol", "Ambergris extract, 3 per cent",
        "Amyl cinnamic aldehyde", "Amyloxy iso-eugenol", "Benzoin",
        "Benzophenone", "Birch tar", "Castoreum absolute",
        "Cinnamic alcohol synthetic", "Costus", "Coumarin", "Cypress",
        "Decyl aldehyde", "Ethyl vanillin", "Gamma nonyl lactone",
        "Guaiyl esters", "Immortelle absolute", "Iso-eugenol",
        "Iso-eugenol phenylacetate", "Labdanum",
        "Linalyl phenylacetate", "Methyl nonyl acetaldehyde",
        "Musks artificial", "Oakmoss", "Olibanum oil and resin",
        "Patchouli", "Pepper", "Peru balsam", "Phenylacetic acid",
        "Phenylacetic aldehyde", "Pimento", "Rhodinyl phenylacetate",
        "Santalwood E.1.", "Storax resin", "Santalyl phenylacetate",
        "Tolu balsam", "Tonka resinoid",
        "Trichlor phenyl methyl carbinyl acetate", "Undecylic aldehyde",
        "Vanillin", "Vetivert",
    ],
}

SKIP_LINES = {
    "PERFUMES, COSMETICS AND SOAPS",
    "ODOUR CLASSIFICATION AND FIXATION",
    "Duration of evaporation table",
    "Top notes",
    "Top notes-cont.",
    "Top notes-cant",
    "Middle notes",
    "Middle notes-cont.",
    "Bases",
}


def load_poucher_pages() -> dict[int, str]:
    pages: dict[int, str] = {}
    with open(PAGES) as f:
        for line in f:
            rec = json.loads(json.loads(line)["record"])
            if "Poucher" in rec.get("source", ""):
                pages[int(rec["page"])] = rec.get("text", "")
    return pages


def tier_for_coefficient(coefficient: int) -> str:
    if coefficient <= 14:
        return "top"
    if coefficient <= 60:
        return "mid"
    return "base"


def conflict_resolution_note(rows: list[dict]) -> str:
    names = {r["name"].lower() for r in rows}
    if len(names) == 1:
        return "same_material_repeat"
    return "same_cas_distinct_source_names_median_coefficient"


def clean_name(name: str) -> str:
    name = re.sub(r"\s+", " ", name).strip()
    name = name.strip(".,;:")
    return name


def load_pubchem_map() -> dict[str, dict]:
    if not PUBCHEM_MAP.exists():
        return {}
    with open(PUBCHEM_MAP) as f:
        return json.load(f).get("mappings", {})


def match_with_provenance(name: str, pubchem_map: dict[str, dict]) -> tuple[str | None, str, dict]:
    cas = match_name_to_cas(name)
    if cas:
        return cas, "poucher_name_map", {}
    mapped = pubchem_map.get(name)
    if mapped:
        return mapped["cas"], "pubchem_name_synonym", {
            "pubchem_cid": mapped.get("pubchem_cid"),
            "pubchem_name": mapped.get("pubchem_name"),
        }
    return None, "unmatched", {}


def parse_numbered_lines(text: str, page: int, initial_coefficient: int) -> tuple[list[dict], int]:
    entries: list[dict] = []
    current = initial_coefficient

    for raw in text.splitlines():
        line = clean_name(raw)
        if not line or line in SKIP_LINES or line.lower() in {s.lower() for s in SKIP_LINES}:
            continue
        if re.fullmatch(r"\d{1,3}", line):
            continue
        if re.fullmatch(r"\d{1,3}\s+PERFUMES, COSMETICS AND SOAPS", line):
            continue

        # OCR occasionally reads 8. as B.
        number_match = re.match(r"^(B|\d{1,3})\.\s*(.*)$", line)
        if number_match:
            raw_num = number_match.group(1)
            current = 8 if raw_num == "B" else int(raw_num)
            name = clean_name(number_match.group(2))
            if name:
                entries.append(entry(current, name, page))
            continue

        if current is not None and looks_like_material(line):
            entries.append(entry(current, line, page))

    return entries, current


def looks_like_material(line: str) -> bool:
    if len(line) < 2 or len(line) > 80:
        return False
    if line[0].isdigit():
        return False
    low = line.lower()
    if any(fragment in low for fragment in ["chapter", "page ", "classification", "table"]):
        return False
    return True


def entry(coefficient: int, name: str, page: int) -> dict:
    return {
        "name": clean_name(name),
        "poucher_coefficient": coefficient,
        "tier_band": tier_for_coefficient(coefficient),
        "page": page,
    }


def grouped_entries(groups: dict[int, list[str]], page: int) -> list[dict]:
    return [entry(coeff, name, page) for coeff, names in groups.items() for name in names]


def extract_entries() -> list[dict]:
    pages = load_poucher_pages()
    entries: list[dict] = []
    entries.extend(grouped_entries(PAGE_70_GROUPS, 70))

    current = 3
    for page in [71, 72, 73]:
        page_entries, current = parse_numbered_lines(pages[page], page, current)
        entries.extend(page_entries)

    entries.extend(grouped_entries(PAGE_74_GROUPS, 74))

    seen = set()
    unique = []
    for e in entries:
        key = (e["poucher_coefficient"], e["name"].lower())
        if key not in seen:
            seen.add(key)
            unique.append(e)
    return unique


def main() -> None:
    entries = extract_entries()
    pubchem_map = load_pubchem_map()

    by_cas: dict[str, dict] = {}
    unmatched: list[dict] = []
    conflicts: dict[str, list[dict]] = defaultdict(list)

    for e in entries:
        cas, match_source, match_extra = match_with_provenance(e["name"], pubchem_map)
        if not cas:
            unmatched.append(e)
            continue

        row = {
            "cas": cas,
            "name": e["name"],
            "poucher_coefficient": e["poucher_coefficient"],
            "tier_band": e["tier_band"],
            "source": "Poucher Vol II duration of evaporation table",
            "source_page": e["page"],
            "target_type": "measured_olfactive_duration_coefficient",
            "cas_match_source": match_source,
            **match_extra,
        }
        conflicts[cas].append(row)

    for cas, rows in conflicts.items():
        coeffs = sorted({r["poucher_coefficient"] for r in rows})
        source_rows = sorted(
            [
                {
                    "name": r["name"],
                    "poucher_coefficient": r["poucher_coefficient"],
                    "tier_band": r["tier_band"],
                    "source_page": r["source_page"],
                    "cas_match_source": r["cas_match_source"],
                }
                for r in rows
            ],
            key=lambda r: (r["poucher_coefficient"], r["name"]),
        )
        median_coefficient = int(round(median(r["poucher_coefficient"] for r in rows)))
        chosen = sorted(
            rows,
            key=lambda r: (
                abs(r["poucher_coefficient"] - median_coefficient),
                r["poucher_coefficient"],
                r["name"],
            ),
        )[0]
        chosen["poucher_coefficient"] = median_coefficient
        chosen["tier_band"] = tier_for_coefficient(median_coefficient)
        chosen["all_poucher_names"] = sorted({r["name"] for r in rows})
        chosen["all_poucher_coefficients"] = coeffs
        chosen["coefficient_conflict"] = len(coeffs) > 1
        chosen["conflict_resolution"] = (
            conflict_resolution_note(rows) if len(coeffs) > 1 else "none"
        )
        chosen["source_rows"] = source_rows
        by_cas[cas] = chosen

    OUT.write_text(
        "\n".join(json.dumps(row, sort_keys=True) for _, row in sorted(by_cas.items())) + "\n"
    )

    print(f"Extracted table entries: {len(entries)}")
    print(f"Matched CAS rows: {len(by_cas)}")
    print(f"Unmatched names: {len(unmatched)}")
    print(f"CAS with coefficient conflicts: {sum(1 for r in by_cas.values() if r['coefficient_conflict'])}")
    print("Tier-band distribution:")
    dist = defaultdict(int)
    for row in by_cas.values():
        dist[row["tier_band"]] += 1
    for tier in ["top", "mid", "base"]:
        print(f"  {tier}: {dist[tier]}")
    if unmatched:
        print("Unmatched sample:")
        for row in unmatched[:25]:
            print(f"  p{row['page']} c{row['poucher_coefficient']}: {row['name']}")
    print(f"Saved to {OUT}")


if __name__ == "__main__":
    main()