jin2li commited on
Commit
8339ec9
ยท
verified ยท
1 Parent(s): c766eb3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +135 -15
app.py CHANGED
@@ -528,17 +528,11 @@ def is_broad_lookup_term(term):
528
  return False
529
 
530
 
 
531
  def build_chemical_name_variants(term):
532
  """
533
- ํ™”ํ•™๋ช… ๊ณ„์—ด ์ž๋™ alias ํ™•์žฅ.
534
- ๊ทœ์ œ ํŒŒ์ผ์—์„œ ์ž์ฃผ ์“ฐ๋Š” ์ด์„ฑ์งˆ์ฒด/๋ณต์ˆ˜ํ˜•/๊ตญ๋ฌธ ์Œ์—ญ ํ‘œ๊ธฐ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
535
-
536
- ์˜ˆ:
537
- Dichlorobenzene
538
- -> Dichlorobenzenes
539
- -> 1,2-Dichlorobenzene / 1,3-Dichlorobenzene / 1,4-Dichlorobenzene
540
- -> o-Dichlorobenzene / m-Dichlorobenzene / p-Dichlorobenzene
541
- -> ๋””ํด๋กœ๋กœ๋ฒค์   / 1,4-๋””ํด๋กœ๋กœ๋ฒค์   / p-๋””ํด๋กœ๋กœ๋ฒค์  
542
  """
543
  term = safe_str(term)
544
  if not term:
@@ -560,7 +554,6 @@ def build_chemical_name_variants(term):
560
  if low.endswith("benzenes"):
561
  add(term[:-1])
562
 
563
- translit = term
564
  replacements = [
565
  ("Dichloro", "๋””ํด๋กœ๋กœ"), ("dichloro", "๋””ํด๋กœ๋กœ"),
566
  ("Trichloro", "ํŠธ๋ฆฌํด๋กœ๋กœ"), ("trichloro", "ํŠธ๋ฆฌํด๋กœ๋กœ"),
@@ -568,6 +561,7 @@ def build_chemical_name_variants(term):
568
  ("Chloro", "ํด๋กœ๋กœ"), ("chloro", "ํด๋กœ๋กœ"),
569
  ("Dibromo", "๋””๋ธŒ๋กœ๋ชจ"), ("dibromo", "๋””๋ธŒ๋กœ๋ชจ"),
570
  ("Bromo", "๋ธŒ๋กœ๋ชจ"), ("bromo", "๋ธŒ๋กœ๋ชจ"),
 
571
  ("Fluoro", "ํ”Œ๋ฃจ์˜ค๋กœ"), ("fluoro", "ํ”Œ๋ฃจ์˜ค๋กœ"),
572
  ("Iodo", "์•„์ด์˜ค๋„"), ("iodo", "์•„์ด์˜ค๋„"),
573
  ("Benzene", "๋ฒค์  "), ("benzene", "๋ฒค์  "),
@@ -575,6 +569,7 @@ def build_chemical_name_variants(term):
575
  ("Phenol", "ํŽ˜๋†€"), ("phenol", "ํŽ˜๋†€"),
576
  ]
577
 
 
578
  for a, b in replacements:
579
  translit = translit.replace(a, b)
580
 
@@ -589,6 +584,7 @@ def build_chemical_name_variants(term):
589
  ("chlorobenzene", "Chlorobenzene", "ํด๋กœ๋กœ๋ฒค์  "),
590
  ("dibromobenzene", "Dibromobenzene", "๋””๋ธŒ๋กœ๋ชจ๋ฒค์  "),
591
  ("bromobenzene", "Bromobenzene", "๋ธŒ๋กœ๋ชจ๋ฒค์  "),
 
592
  ("fluorobenzene", "Fluorobenzene", "ํ”Œ๋ฃจ์˜ค๋กœ๋ฒค์  "),
593
  ("iodobenzene", "Iodobenzene", "์•„์ด์˜ค๋„๋ฒค์  "),
594
  ]
@@ -598,19 +594,122 @@ def build_chemical_name_variants(term):
598
  add(en)
599
  add(en + "s")
600
  add(ko)
 
601
 
602
- for pos in ["1,2", "1,3", "1,4", "o", "m", "p", "ortho", "meta", "para"]:
603
- add(f"{pos}-{en}")
604
- add(f"{pos} {en}")
605
- add(f"{pos}-{ko}")
606
- add(f"{pos} {ko}")
 
607
 
608
  add(f"1,4-{ko}(p-{ko})")
609
  add(f"1,4-{en} (p-{en})")
 
 
610
 
611
  return variants
612
 
613
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
614
  def build_ingredient_lookup_terms(material=None, component=None, extra_terms=None):
615
  material = material or {}
616
  component = component or {}
@@ -746,6 +845,17 @@ def validate_country_file_match_context(context, component, lookup_terms):
746
  if len(norm) >= 4 and norm in compact:
747
  return True
748
 
 
 
 
 
 
 
 
 
 
 
 
749
  if context_has_group_regulation_for_terms(context, lookup_terms):
750
  return True
751
 
@@ -2585,6 +2695,16 @@ def evaluate_component_from_country_files_only(country, material, component, tar
2585
  )
2586
 
2587
  if not matches:
 
 
 
 
 
 
 
 
 
 
2588
  result = {
2589
  "๋Œ€ํ‘œ INCI": representative,
2590
  "๊ฒฐ๊ณผ": NO_EVIDENCE_TEXT,
 
528
  return False
529
 
530
 
531
+
532
  def build_chemical_name_variants(term):
533
  """
534
+ ํ™”ํ•™๋ช… ํ‘œ๊ธฐ ์ฐจ์ด ๋ณด์ •์šฉ alias ์ƒ์„ฑ.
535
+ ์ด์„ฑ์งˆ์ฒด/๋ณต์ˆ˜ํ˜•/๊ตญ๋ฌธ ์Œ์—ญ ํ‘œ๊ธฐ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
 
 
 
 
 
 
 
536
  """
537
  term = safe_str(term)
538
  if not term:
 
554
  if low.endswith("benzenes"):
555
  add(term[:-1])
556
 
 
557
  replacements = [
558
  ("Dichloro", "๋””ํด๋กœ๋กœ"), ("dichloro", "๋””ํด๋กœ๋กœ"),
559
  ("Trichloro", "ํŠธ๋ฆฌํด๋กœ๋กœ"), ("trichloro", "ํŠธ๋ฆฌํด๋กœ๋กœ"),
 
561
  ("Chloro", "ํด๋กœ๋กœ"), ("chloro", "ํด๋กœ๋กœ"),
562
  ("Dibromo", "๋””๋ธŒ๋กœ๋ชจ"), ("dibromo", "๋””๋ธŒ๋กœ๋ชจ"),
563
  ("Bromo", "๋ธŒ๋กœ๋ชจ"), ("bromo", "๋ธŒ๋กœ๋ชจ"),
564
+ ("Difluoro", "๋””ํ”Œ๋ฃจ์˜ค๋กœ"), ("difluoro", "๋””ํ”Œ๋ฃจ์˜ค๋กœ"),
565
  ("Fluoro", "ํ”Œ๋ฃจ์˜ค๋กœ"), ("fluoro", "ํ”Œ๋ฃจ์˜ค๋กœ"),
566
  ("Iodo", "์•„์ด์˜ค๋„"), ("iodo", "์•„์ด์˜ค๋„"),
567
  ("Benzene", "๋ฒค์  "), ("benzene", "๋ฒค์  "),
 
569
  ("Phenol", "ํŽ˜๋†€"), ("phenol", "ํŽ˜๋†€"),
570
  ]
571
 
572
+ translit = term
573
  for a, b in replacements:
574
  translit = translit.replace(a, b)
575
 
 
584
  ("chlorobenzene", "Chlorobenzene", "ํด๋กœ๋กœ๋ฒค์  "),
585
  ("dibromobenzene", "Dibromobenzene", "๋””๋ธŒ๋กœ๋ชจ๋ฒค์  "),
586
  ("bromobenzene", "Bromobenzene", "๋ธŒ๋กœ๋ชจ๋ฒค์  "),
587
+ ("difluorobenzene", "Difluorobenzene", "๋””ํ”Œ๋ฃจ์˜ค๋กœ๋ฒค์  "),
588
  ("fluorobenzene", "Fluorobenzene", "ํ”Œ๋ฃจ์˜ค๋กœ๋ฒค์  "),
589
  ("iodobenzene", "Iodobenzene", "์•„์ด์˜ค๋„๋ฒค์  "),
590
  ]
 
594
  add(en)
595
  add(en + "s")
596
  add(ko)
597
+ add(ko + "๋ฅ˜")
598
 
599
+ for pos in ["1,2", "1,3", "1,4", "1,2-", "1,3-", "1,4-", "o", "m", "p", "ortho", "meta", "para"]:
600
+ pos_clean = pos.rstrip("-")
601
+ add(f"{pos_clean}-{en}")
602
+ add(f"{pos_clean} {en}")
603
+ add(f"{pos_clean}-{ko}")
604
+ add(f"{pos_clean} {ko}")
605
 
606
  add(f"1,4-{ko}(p-{ko})")
607
  add(f"1,4-{en} (p-{en})")
608
+ add(f"p-{ko}")
609
+ add(f"p-{en}")
610
 
611
  return variants
612
 
613
 
614
+ def find_chemical_variant_match_in_country_text(country, material, component, target_id=""):
615
+ """
616
+ ๊ตญ๊ฐ€๋ณ„ ์„น์…˜ ์ธ๋ฑ์Šค ๋งค์นญ์ด ์‹คํŒจํ•œ ๊ฒฝ์šฐ๋ฅผ ์œ„ํ•œ ํ™”ํ•™๋ช… ๋ณ€ํ˜• fallback.
617
+ ์ „์ฒด ๊ตญ๊ฐ€ ํŒŒ์ผ ์›๋ฌธ์—์„œ ํ™”ํ•™๋ช… ๋ณ€ํ˜• alias๋ฅผ ์ง์ ‘ ์ฐพ๊ณ ,
618
+ ๊ทธ ์„น์…˜/๋ฌธ๋งฅ์ด ๊ธˆ์ง€ ๋˜๋Š” ์ œํ•œ ํŒŒ์ผ์ด๋ฉด ํŒ์ •ํ•œ๋‹ค.
619
+ """
620
+ lookup_terms = build_ingredient_lookup_terms(
621
+ material=material,
622
+ component=component,
623
+ extra_terms=[
624
+ component.get("INCI Name", ""),
625
+ component.get("ํ•œ๊ธ€ ์„ฑ๋ถ„๋ช…", ""),
626
+ component.get("CAS.No", ""),
627
+ ],
628
+ )
629
+
630
+ expanded = []
631
+ for t in lookup_terms:
632
+ for v in build_chemical_name_variants(t):
633
+ if safe_str(v) and v not in expanded:
634
+ expanded.append(v)
635
+
636
+ # ๋„ˆ๋ฌด ์ผ๋ฐ˜์ ์ธ alias ์ œ๊ฑฐ
637
+ expanded = [
638
+ x for x in expanded
639
+ if len(normalize_compact(x)) >= 4 and not is_broad_lookup_term(x)
640
+ ]
641
+
642
+ if not expanded:
643
+ return None
644
+
645
+ sections, status = get_country_section_index_cached(country)
646
+ country_col = display_country(country)
647
+ representative = safe_str(component.get("INCI Name", "")) or safe_str(component.get("ํ•œ๊ธ€ ์„ฑ๋ถ„๋ช…", "")) or "-"
648
+
649
+ for section in sections:
650
+ text = safe_str(section.get("text", ""))
651
+ text_norm = normalize_compact(text)
652
+ header = safe_str(section.get("header", ""))
653
+ reg_type = infer_regulation_type_from_source_text(header + "\n" + text[:1500], country=country)
654
+ reg_kind = reg_type.get("type", "unknown")
655
+ reg_label = reg_type.get("label", "๊ทœ์ œ ์œ ํ˜• ๋ฏธ๋ถ„๋ฅ˜")
656
+
657
+ hit_terms = [v for v in expanded if normalize_compact(v) in text_norm]
658
+ if not hit_terms:
659
+ continue
660
+
661
+ lines = [safe_str(x) for x in text.splitlines() if safe_str(x)]
662
+ context_lines = []
663
+ seen = set()
664
+
665
+ for idx, line in enumerate(lines):
666
+ line_norm = normalize_compact(line)
667
+ if any(normalize_compact(v) in line_norm for v in hit_terms):
668
+ for j in range(max(0, idx - 4), min(len(lines), idx + 5)):
669
+ if j not in seen:
670
+ seen.add(j)
671
+ context_lines.append(lines[j])
672
+
673
+ context = (header + "\n" + "\n".join(context_lines))[:5000]
674
+ context_low = context.lower()
675
+
676
+ prohibited_signal = (
677
+ reg_kind == "prohibited"
678
+ or any(k in context for k in ["๋ฐฐํ•ฉ๊ธˆ์ง€", "์‚ฌ์šฉ๊ธˆ์ง€", "์‚ฌ์šฉ ๊ธˆ์ง€", "๊ธˆ์ง€์›๋ฃŒ", "๊ธˆ์ง€ ์„ฑ๋ถ„", "๊ธˆ์ง€์„ฑ๋ถ„"])
679
+ or any(k in context_low for k in ["prohibited", "banned", "not allowed", "shall not be used", "must not be used"])
680
+ )
681
+
682
+ restricted_signal = (
683
+ reg_kind == "restricted"
684
+ or any(k in context for k in ["์‚ฌ์šฉ์ œํ•œ", "์‚ฌ์šฉ ์ œํ•œ", "๋ฐฐํ•ฉํ•œ๋„", "์ œํ•œ์„ฑ๋ถ„", "์ œํ•œ ์„ฑ๋ถ„"])
685
+ or any(k in context_low for k in ["restricted", "restriction", "maximum", "limit"])
686
+ )
687
+
688
+ if prohibited_signal:
689
+ result = {
690
+ "๋Œ€ํ‘œ INCI": representative,
691
+ "๊ฒฐ๊ณผ": f"{country_col} {reg_label}์—์„œ ํ™”ํ•™๋ช… ๋ณ€ํ˜• alias ํ™•์ธ",
692
+ "ํ‘œ์‹œ๋ฌธ๊ตฌ": "๋ฐฐํ•ฉ๊ธˆ์ง€",
693
+ "๋น„๊ณ ": context[:1000],
694
+ }
695
+ log_search_context(target_id, country, component, result["๊ฒฐ๊ณผ"], "COUNTRY_FILE_CHEMICAL_ALIAS_PROHIBITED", context)
696
+ return result
697
+
698
+ if restricted_signal:
699
+ limit_text, condition_text = extract_limit_condition_from_context(context)
700
+ display = f"๋ฐฐํ•ฉํ•œ๋„: {limit_text}" if limit_text else NEED_CHECK_TEXT
701
+ result = {
702
+ "๋Œ€ํ‘œ INCI": representative,
703
+ "๊ฒฐ๊ณผ": f"{country_col} {reg_label}์—์„œ ํ™”ํ•™๋ช… ๋ณ€ํ˜• alias ํ™•์ธ" + (f" / ์กฐ๊ฑด: {condition_text}" if condition_text else ""),
704
+ "ํ‘œ์‹œ๋ฌธ๊ตฌ": display,
705
+ "๋น„๊ณ ": context[:1000],
706
+ }
707
+ log_search_context(target_id, country, component, result["๊ฒฐ๊ณผ"], "COUNTRY_FILE_CHEMICAL_ALIAS_RESTRICTED", context)
708
+ return result
709
+
710
+ return None
711
+
712
+
713
  def build_ingredient_lookup_terms(material=None, component=None, extra_terms=None):
714
  material = material or {}
715
  component = component or {}
 
845
  if len(norm) >= 4 and norm in compact:
846
  return True
847
 
848
+ # ํ™”ํ•™๋ช… ๋ณ€ํ˜• alias fallback
849
+ for term in [
850
+ component.get("INCI Name", ""),
851
+ component.get("ํ•œ๊ธ€ ์„ฑ๋ถ„๋ช…", ""),
852
+ component.get("CAS.No", ""),
853
+ ]:
854
+ for variant in build_chemical_name_variants(term):
855
+ norm = normalize_compact(variant)
856
+ if len(norm) >= 4 and norm in compact:
857
+ return True
858
+
859
  if context_has_group_regulation_for_terms(context, lookup_terms):
860
  return True
861
 
 
2695
  )
2696
 
2697
  if not matches:
2698
+ alias_result = find_chemical_variant_match_in_country_text(
2699
+ country=country,
2700
+ material=material,
2701
+ component=component,
2702
+ target_id=target_id,
2703
+ )
2704
+
2705
+ if alias_result is not None:
2706
+ return alias_result
2707
+
2708
  result = {
2709
  "๋Œ€ํ‘œ INCI": representative,
2710
  "๊ฒฐ๊ณผ": NO_EVIDENCE_TEXT,