jin2li commited on
Commit
e184017
ยท
verified ยท
1 Parent(s): a3e82e7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -78
app.py CHANGED
@@ -506,21 +506,30 @@ def preview_input_file(file):
506
  except Exception as e: return pd.DataFrame({"์˜ค๋ฅ˜":[str(e)]})
507
 
508
 
509
- def build_ingredient_lookup_terms(material=None, component=None, extra_terms=None):
510
- """
511
- ๊ตญ๊ฐ€๋ณ„ ํŒŒ์ผ ์ง์ ‘ ๊ฒ€์ƒ‰์šฉ ์„ฑ๋ถ„ ๊ฒ€์ƒ‰์–ด ํ™•์žฅ.
512
 
513
- ๋ชฉ์ :
514
- - ์ „์„ฑ๋ถ„๋ช…๊ณผ ๊ทœ์ œํŒŒ์ผ๋ช…์ด ์™„์ „ํžˆ ๊ฐ™์ง€ ์•Š์•„๋„ ๋งค์นญ๋˜๊ฒŒ ํ•œ๋‹ค.
515
- - ์˜ˆ: Disodium EDTA -> EDTA
516
- - ์˜ˆ: Butylated Hydroxytoluene (BHT) -> BHT
517
- - ์˜ˆ: Sodium Benzoate -> Benzoate
 
 
 
518
 
519
- ๋‹จ, water/aqua์ฒ˜๋Ÿผ ๋„ˆ๋ฌด ์ผ๋ฐ˜์ ์ธ ์„ฑ๋ถ„์€ ๊ธฐ์กด always-pass ๊ทœ์น™์—์„œ ๋จผ์ € ๋น ์ง„๋‹ค.
520
- """
 
 
 
 
 
 
 
 
 
 
521
  material = material or {}
522
  component = component or {}
523
-
524
  raw_terms = []
525
 
526
  for v in [
@@ -538,106 +547,68 @@ def build_ingredient_lookup_terms(material=None, component=None, extra_terms=Non
538
  raw_terms.append(safe_str(v))
539
 
540
  expanded = []
 
 
 
 
 
 
541
 
542
- def add(term):
543
  term = safe_str(term)
544
  if not term:
545
  return
546
-
547
- norm = normalize_compact(term)
548
-
549
- # ๋„ˆ๋ฌด ์งง๊ฑฐ๋‚˜ ์ผ๋ฐ˜์ ์ธ ๋‹จ์–ด๋Š” ์˜คํƒ ๋ฐฉ์ง€
550
- blocked = {
551
- "acid", "oil", "water", "aqua", "extract", "powder", "solution",
552
- "sodium", "potassium", "calcium", "magnesium", "zinc", "ammonium",
553
- "and", "or", "the", "of", "for", "skin", "agent",
554
- "์• ์”จ๋“œ", "์˜ค์ผ", "์ถ”์ถœ๋ฌผ", "์ •์ œ์ˆ˜", "์ˆ˜", "๋ฌผ",
555
- }
556
-
557
- if len(norm) < 3:
558
  return
559
-
560
- if normalize_text(term) in blocked or norm in blocked:
561
  return
562
-
563
  if term not in expanded:
564
  expanded.append(term)
565
 
566
- salt_prefixes = [
567
- "disodium", "trisodium", "tetrasodium", "sodium",
568
- "dipotassium", "tripotassium", "tetrapotassium", "potassium",
569
- "calcium disodium", "calcium", "magnesium", "zinc", "ammonium",
570
- "mono", "di", "tri", "tetra",
571
- ]
572
-
573
  for term in raw_terms:
574
  t = safe_str(term)
575
  add(t)
 
 
576
 
577
- # ๊ด„ํ˜ธ ์•ˆ ์•ฝ์–ด/๋ณ„์นญ
578
  for p in re.findall(r"\(([^)]+)\)", t):
579
- add(p)
 
 
580
 
581
- # ๋Œ€๋ฌธ์ž ์•ฝ์–ด: EDTA, BHT, BHA, MEA ๋“ฑ
582
  for abbr in re.findall(r"\b[A-Z][A-Z0-9]{2,}\b", t):
583
- add(abbr)
584
-
585
- # ํ•˜์ดํ”ˆ/์Šฌ๋ž˜์‹œ ๋ณ€ํ˜•
586
- add(t.replace("-", " "))
587
- add(t.replace("/", " "))
588
 
589
  low = t.lower().strip()
590
-
591
- # ์—ผ/๊ธˆ์†์—ผ prefix ์ œ๊ฑฐ: Disodium EDTA -> EDTA
592
  for prefix in salt_prefixes:
593
  if low.startswith(prefix + " "):
594
  base = t[len(prefix):].strip(" -_/")
595
- add(base)
 
 
596
 
597
- # Calcium Disodium EDTA์ฒ˜๋Ÿผ ๋‘ ๋‹จ๊ณ„ prefix ์ œ๊ฑฐ
598
  tokens = re.split(r"[\s\-/]+", t)
599
- if len(tokens) >= 2:
600
- for i in range(1, min(4, len(tokens))):
601
- base = " ".join(tokens[i:])
602
- if len(normalize_compact(base)) >= 3:
603
- add(base)
604
-
605
- # ์ผ๋ฐ˜ INCI ์ ‘๋ฏธ/๊ทธ๋ฃน ํ›„๋ณด
606
- # Sodium Benzoate -> Benzoate, Potassium Sorbate -> Sorbate
607
- if len(tokens) >= 2 and tokens[0].lower() in {
608
- "sodium", "potassium", "calcium", "magnesium", "zinc", "ammonium",
609
- "disodium", "trisodium", "tetrasodium", "dipotassium",
610
- }:
611
- add(" ".join(tokens[1:]))
612
-
613
- # compact ๊ธฐ์ค€ ์ค‘๋ณต ์ œ๊ฑฐ
614
  seen = set()
615
  final_terms = []
616
-
617
  for term in expanded:
618
  norm = normalize_compact(term)
619
-
620
  if norm in seen:
621
  continue
622
-
623
  seen.add(norm)
624
  final_terms.append(term)
625
 
626
- # ๊ธด ๊ฒ€์ƒ‰์–ด ๋จผ์ €, ์งง์€ ํ•ต์‹ฌ์–ด ๋‚˜์ค‘
627
- final_terms = sorted(final_terms, key=lambda x: len(normalize_compact(x)), reverse=True)
628
-
629
- return final_terms
630
-
631
 
632
  def context_has_group_regulation_for_terms(context, lookup_terms):
633
- """
634
- 'EDTA and derivatives', 'X and salts', 'X derivatives' ๊ฐ™์€ ๊ทธ๋ฃนํ˜• ๋ฌธ๊ตฌ ํ™•์ธ.
635
- ํŠน์ • ์„ฑ๋ถ„ ํ•˜๋“œ์ฝ”๋”ฉ ์—†์ด alias์™€ ๋ฌธ๋งฅ ํŒจํ„ด์œผ๋กœ ํŒ๋‹จํ•œ๋‹ค.
636
- """
637
  text = safe_str(context)
638
  compact = normalize_compact(text)
639
  low = text.lower()
640
-
641
  if not text or not lookup_terms:
642
  return False
643
 
@@ -646,22 +617,43 @@ def context_has_group_regulation_for_terms(context, lookup_terms):
646
  "and its salts", "and their salts", "and derivatives", "and its derivatives",
647
  "๋ฐ์œ ๋„์ฒด", "์œ ๋„์ฒด", "์—ผ๋ฅ˜", "๊ทธ์—ผ", "๊ทธ ์œ ๋„์ฒด",
648
  ]
649
-
650
  if not any(normalize_compact(w) in compact or w in low for w in group_words):
651
  return False
652
 
653
  for term in lookup_terms:
 
 
654
  norm = normalize_compact(term)
 
 
 
655
 
656
- if len(norm) < 3:
657
- continue
 
 
 
658
 
659
- if norm in compact:
 
 
660
  return True
661
 
 
 
 
 
 
 
 
 
 
 
662
  return False
663
 
664
 
 
 
665
  # country db
666
 
667
  def resolve_country_folder(country):
@@ -2517,6 +2509,27 @@ def evaluate_component_from_country_files_only(country, material, component, tar
2517
  evidence = context[:5000]
2518
  lower = evidence.lower()
2519
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2520
  # 1. Annex II / Prohibited / Negative List
2521
  # ํ•ต์‹ฌ: ์„ฑ๋ถ„ ์ฃผ๋ณ€์— ๊ธˆ์ง€๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์—†์–ด๋„ ํ•ด๋‹น ํŒŒ์ผ์—์„œ ๋ฐœ๊ฒฌ๋˜๋ฉด ๋ฐฐํ•ฉ๊ธˆ์ง€.
2522
  if reg_kind == "restricted" or reg_kind.startswith("positive_"):
 
506
  except Exception as e: return pd.DataFrame({"์˜ค๋ฅ˜":[str(e)]})
507
 
508
 
 
 
 
509
 
510
+ BROAD_LOOKUP_TERMS = {
511
+ "acid", "acids", "alcohol", "alcohols", "glycol", "glycols", "oil", "oils",
512
+ "extract", "extracts", "water", "aqua", "powder", "solution", "agent",
513
+ "sodium", "potassium", "calcium", "magnesium", "zinc", "ammonium",
514
+ "skin", "conditioning", "cosmetic", "quality", "system",
515
+ "stem", "flower", "leaf", "root", "fruit", "seed", "peel",
516
+ "์• ์”จ๋“œ", "์•Œ์ฝ”์˜ฌ", "๊ธ€๋ผ์ด์ฝœ", "์˜ค์ผ", "์ถ”์ถœ๋ฌผ", "์ •์ œ์ˆ˜", "์ˆ˜", "๋ฌผ",
517
+ }
518
 
519
+ def is_broad_lookup_term(term):
520
+ norm = normalize_compact(term)
521
+ text = normalize_text(term)
522
+ if not norm or len(norm) < 4:
523
+ return True
524
+ if norm in {normalize_compact(x) for x in BROAD_LOOKUP_TERMS}:
525
+ return True
526
+ if text in BROAD_LOOKUP_TERMS:
527
+ return True
528
+ return False
529
+
530
+ def build_ingredient_lookup_terms(material=None, component=None, extra_terms=None):
531
  material = material or {}
532
  component = component or {}
 
533
  raw_terms = []
534
 
535
  for v in [
 
547
  raw_terms.append(safe_str(v))
548
 
549
  expanded = []
550
+ strong_abbrev_allowlist = {"EDTA", "BHT", "BHA", "MEA", "DEA", "TEA", "D4", "D5", "D6", "PFAS", "PFOA", "PFOS"}
551
+ salt_prefixes = [
552
+ "calcium disodium", "disodium", "trisodium", "tetrasodium", "sodium",
553
+ "dipotassium", "tripotassium", "tetrapotassium", "potassium",
554
+ "calcium", "magnesium", "zinc", "ammonium",
555
+ ]
556
 
557
+ def add(term, allow_broad=False):
558
  term = safe_str(term)
559
  if not term:
560
  return
561
+ if len(normalize_compact(term)) < 3:
 
 
 
 
 
 
 
 
 
 
 
562
  return
563
+ if not allow_broad and is_broad_lookup_term(term):
 
564
  return
 
565
  if term not in expanded:
566
  expanded.append(term)
567
 
 
 
 
 
 
 
 
568
  for term in raw_terms:
569
  t = safe_str(term)
570
  add(t)
571
+ add(t.replace("-", " "))
572
+ add(t.replace("/", " "))
573
 
 
574
  for p in re.findall(r"\(([^)]+)\)", t):
575
+ p = safe_str(p)
576
+ if p.upper() in strong_abbrev_allowlist or len(normalize_compact(p)) >= 5:
577
+ add(p, allow_broad=(p.upper() in strong_abbrev_allowlist))
578
 
 
579
  for abbr in re.findall(r"\b[A-Z][A-Z0-9]{2,}\b", t):
580
+ if abbr.upper() in strong_abbrev_allowlist:
581
+ add(abbr, allow_broad=True)
 
 
 
582
 
583
  low = t.lower().strip()
 
 
584
  for prefix in salt_prefixes:
585
  if low.startswith(prefix + " "):
586
  base = t[len(prefix):].strip(" -_/")
587
+ # Disodium EDTA -> EDTA๋Š” ํ—ˆ์šฉ, Sodium Benzoate -> Benzoate๋„ ํ—ˆ์šฉ
588
+ # ํ•˜์ง€๋งŒ Butylene Glycol -> Glycol์ฒ˜๋Ÿผ broad term๋งŒ ๋‚จ๊ธฐ๋Š” ํ™•์žฅ์€ ํ•˜์ง€ ์•Š์Œ
589
+ add(base, allow_broad=(base.upper() in strong_abbrev_allowlist))
590
 
 
591
  tokens = re.split(r"[\s\-/]+", t)
592
+ for i in range(1, min(4, len(tokens))):
593
+ base = " ".join(tokens[i:]).strip()
594
+ if base.upper() in strong_abbrev_allowlist:
595
+ add(base, allow_broad=True)
596
+
 
 
 
 
 
 
 
 
 
 
597
  seen = set()
598
  final_terms = []
 
599
  for term in expanded:
600
  norm = normalize_compact(term)
 
601
  if norm in seen:
602
  continue
 
603
  seen.add(norm)
604
  final_terms.append(term)
605
 
606
+ return sorted(final_terms, key=lambda x: len(normalize_compact(x)), reverse=True)
 
 
 
 
607
 
608
  def context_has_group_regulation_for_terms(context, lookup_terms):
 
 
 
 
609
  text = safe_str(context)
610
  compact = normalize_compact(text)
611
  low = text.lower()
 
612
  if not text or not lookup_terms:
613
  return False
614
 
 
617
  "and its salts", "and their salts", "and derivatives", "and its derivatives",
618
  "๋ฐ์œ ๋„์ฒด", "์œ ๋„์ฒด", "์—ผ๋ฅ˜", "๊ทธ์—ผ", "๊ทธ ์œ ๋„์ฒด",
619
  ]
 
620
  if not any(normalize_compact(w) in compact or w in low for w in group_words):
621
  return False
622
 
623
  for term in lookup_terms:
624
+ if is_broad_lookup_term(term):
625
+ continue
626
  norm = normalize_compact(term)
627
+ if len(norm) >= 4 and norm in compact:
628
+ return True
629
+ return False
630
 
631
+ def validate_country_file_match_context(context, component, lookup_terms):
632
+ text = safe_str(context)
633
+ compact = normalize_compact(text)
634
+ if not text:
635
+ return False
636
 
637
+ for term in [component.get("INCI Name", ""), component.get("ํ•œ๊ธ€ ์„ฑ๋ถ„๋ช…", ""), component.get("CAS.No", "")]:
638
+ norm = normalize_compact(term)
639
+ if len(norm) >= 4 and norm in compact:
640
  return True
641
 
642
+ for term in lookup_terms or []:
643
+ if is_broad_lookup_term(term):
644
+ continue
645
+ norm = normalize_compact(term)
646
+ if len(norm) >= 4 and norm in compact:
647
+ return True
648
+
649
+ if context_has_group_regulation_for_terms(context, lookup_terms):
650
+ return True
651
+
652
  return False
653
 
654
 
655
+
656
+
657
  # country db
658
 
659
  def resolve_country_folder(country):
 
2509
  evidence = context[:5000]
2510
  lower = evidence.lower()
2511
 
2512
+ if not validate_country_file_match_context(
2513
+ context=context,
2514
+ component=component,
2515
+ lookup_terms=lookup_terms if "lookup_terms" in locals() else [inci_name, korean_name, cas_no],
2516
+ ):
2517
+ result = {
2518
+ "๋Œ€ํ‘œ INCI": representative,
2519
+ "๊ฒฐ๊ณผ": NO_EVIDENCE_TEXT,
2520
+ "ํ‘œ์‹œ๋ฌธ๊ตฌ": "Pass",
2521
+ "๋น„๊ณ ": "๊ตญ๊ฐ€๋ณ„ ํŒŒ์ผ์—์„œ ๋„“์€ ๊ฒ€์ƒ‰์–ด๋งŒ ๋งค์นญ๋˜์–ด ์˜คํƒ์œผ๋กœ ์ œ์™ธ",
2522
+ }
2523
+ log_search_context(
2524
+ target_id,
2525
+ country,
2526
+ component,
2527
+ result["๊ฒฐ๊ณผ"],
2528
+ "COUNTRY_FILE_BROAD_MATCH_IGNORED",
2529
+ context,
2530
+ )
2531
+ return result
2532
+
2533
  # 1. Annex II / Prohibited / Negative List
2534
  # ํ•ต์‹ฌ: ์„ฑ๋ถ„ ์ฃผ๋ณ€์— ๊ธˆ์ง€๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์—†์–ด๋„ ํ•ด๋‹น ํŒŒ์ผ์—์„œ ๋ฐœ๊ฒฌ๋˜๋ฉด ๋ฐฐํ•ฉ๊ธˆ์ง€.
2535
  if reg_kind == "restricted" or reg_kind.startswith("positive_"):