jin2li commited on
Commit
8f17412
ยท
verified ยท
1 Parent(s): 636e622

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -68
app.py CHANGED
@@ -738,15 +738,15 @@ def registry_terms_for_component(material=None, component=None, extra_terms=None
738
  material.get("Trade name(์˜๋ฌธ)", ""),
739
  ] + list(extra_terms or [])
740
 
741
- identity_norm = normalize_compact(" ".join(safe_str(x) for x in identity_values if safe_str(x)))
742
 
743
  hits = []
744
 
745
  for entry in REGULATORY_SYNONYM_REGISTRY:
746
  terms = entry.get("terms", [])
747
- term_norms = [normalize_compact(t) for t in terms if safe_str(t)]
748
 
749
- if any(tn and tn in identity_norm for tn in term_norms):
 
750
  for t in terms:
751
  if safe_str(t) and t not in hits:
752
  hits.append(safe_str(t))
@@ -768,17 +768,17 @@ def registry_direct_regulation_fallback(country, material, component, target_id=
768
  country_col = display_country(country)
769
  representative = safe_str(component.get("INCI Name", "")) or safe_str(component.get("ํ•œ๊ธ€ ์„ฑ๋ถ„๋ช…", "")) or registry_terms[0]
770
 
771
- registry_norms = [
772
- normalize_compact(t)
773
  for t in registry_terms
774
  if safe_str(t) and len(normalize_compact(t)) >= 3
775
  ]
776
 
777
  for section in sections:
778
  text = safe_str(section.get("text", ""))
779
- text_norm = normalize_compact(text)
780
 
781
- if not any(tn in text_norm for tn in registry_norms):
 
782
  continue
783
 
784
  header = safe_str(section.get("header", ""))
@@ -791,8 +791,7 @@ def registry_direct_regulation_fallback(country, material, component, target_id=
791
  seen = set()
792
 
793
  for idx, line in enumerate(lines):
794
- ln = normalize_compact(line)
795
- if any(tn in ln for tn in registry_norms):
796
  for j in range(max(0, idx - 5), min(len(lines), idx + 6)):
797
  if j not in seen:
798
  seen.add(j)
@@ -955,13 +954,12 @@ def find_chemical_variant_match_in_country_text(country, material, component, ta
955
 
956
  for section in sections:
957
  text = safe_str(section.get("text", ""))
958
- text_norm = normalize_compact(text)
959
  header = safe_str(section.get("header", ""))
960
  reg_type = infer_regulation_type_from_source_text(header + "\n" + text[:1500], country=country)
961
  reg_kind = reg_type.get("type", "unknown")
962
  reg_label = reg_type.get("label", "๊ทœ์ œ ์œ ํ˜• ๋ฏธ๋ถ„๋ฅ˜")
963
 
964
- hit_terms = [v for v in expanded if normalize_compact(v) in text_norm]
965
  if not hit_terms:
966
  continue
967
 
@@ -970,8 +968,7 @@ def find_chemical_variant_match_in_country_text(country, material, component, ta
970
  seen = set()
971
 
972
  for idx, line in enumerate(lines):
973
- line_norm = normalize_compact(line)
974
- if any(normalize_compact(v) in line_norm for v in hit_terms):
975
  for j in range(max(0, idx - 4), min(len(lines), idx + 5)):
976
  if j not in seen:
977
  seen.add(j)
@@ -1471,9 +1468,8 @@ def mfds_click_probable_result(page, term):
1471
  except Exception:
1472
  continue
1473
 
1474
- row_norm = normalize_compact(row_text)
1475
-
1476
- if term_norm and term_norm in row_norm:
1477
  row.click(timeout=3000)
1478
  page.wait_for_timeout(1000)
1479
  return True
@@ -1737,7 +1733,8 @@ def mfds_lookup_one_term_with_retry(page, term):
1737
  return [], ""
1738
 
1739
  # ๊ฒ€์ƒ‰์–ด๊ฐ€ ํ™”๋ฉด์— ์žˆ์ง€๋งŒ ๊ทœ์ œ ํ•„๋“œ๊ฐ€ ์—†์œผ๋ฉด ์ผ๋ฐ˜ ์›๋ฃŒ ์ƒ์„ธ/๋ฌด๊ทœ์ œ ๊ฒฐ๊ณผ๋กœ ํŒ๋‹จ
1740
- if term_norm and term_norm in body_norm:
 
1741
  return [], ""
1742
 
1743
  if not clicked:
@@ -2697,37 +2694,38 @@ def get_country_section_index_cached(country):
2697
  def find_matching_regulatory_sections_fast(country, ingredient_names, max_sections=30):
2698
  """
2699
  ๊ณ ์† ๋‹จ์ผ ๊ฒ€์ƒ‰.
2700
- get_country_section_index_cached()๊ฐ€ ๋งŒ๋“  ์„น์…˜ ์ธ๋ฑ์Šค์—์„œ ์„ฑ๋ถ„๋ช…/INCI/CAS๋ฅผ ์ง์ ‘ ๊ฒ€์ƒ‰ํ•œ๋‹ค.
 
 
 
 
2701
  """
2702
  sections, status = get_country_section_index_cached(country)
2703
 
2704
- names_norm = [
2705
- normalize_compact(x)
2706
  for x in ingredient_names
2707
- if safe_str(x) and len(normalize_compact(x)) >= 3
2708
  ]
2709
 
2710
- if not names_norm:
2711
  return [], status
2712
 
2713
  matches = []
2714
 
2715
  for section in sections:
2716
- text_norm = section.get("text_norm", "")
2717
 
2718
- if not any(name in text_norm for name in names_norm):
2719
  continue
2720
 
2721
- text = safe_str(section.get("text", ""))
2722
  header = safe_str(section.get("header", ""))
2723
  lines = [safe_str(x) for x in text.splitlines() if safe_str(x)]
2724
 
2725
  hit_indices = []
2726
 
2727
  for idx, line in enumerate(lines):
2728
- line_norm = normalize_compact(line)
2729
-
2730
- if any(name in line_norm for name in names_norm):
2731
  hit_indices.append(idx)
2732
 
2733
  if len(hit_indices) >= 10:
@@ -2752,7 +2750,6 @@ def find_matching_regulatory_sections_fast(country, ingredient_names, max_sectio
2752
  else:
2753
  context = header + "\n" + text[:2000]
2754
 
2755
- # ๋””์Šคํฌ ์บ์‹œ๊ฐ€ ์˜ค๋ž˜๋œ label์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ header ๊ธฐ์ค€์œผ๋กœ ํ•œ๋ฒˆ ๋” ๋ณด์ •
2756
  reg_type = infer_regulation_type_from_source_text(
2757
  header + "\n" + text[:1500],
2758
  country=country,
@@ -2769,65 +2766,62 @@ def find_matching_regulatory_sections_fast(country, ingredient_names, max_sectio
2769
 
2770
  return matches, status
2771
 
2772
-
2773
  def find_matching_regulatory_sections(raw_text, ingredient_names, max_sections=20):
2774
  """
2775
- ๊ตฌ๋ฒ„์ „ ํ˜ธํ™˜์šฉ. ์‹ ๊ทœ ๋‹จ์ผ ๊ฒ€์ƒ‰์€ find_matching_regulatory_sections_fast()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
2776
  """
2777
- names_norm = [
2778
- normalize_compact(x)
2779
  for x in ingredient_names
2780
- if safe_str(x) and len(normalize_compact(x)) >= 3
2781
  ]
2782
 
2783
- if not names_norm:
2784
  return []
2785
 
2786
  matches = []
2787
 
2788
  for section in split_regulatory_text_into_sections(raw_text):
2789
  text = section["text"]
2790
- text_norm = normalize_compact(text)
2791
 
2792
- if any(name in text_norm for name in names_norm):
2793
- lines = [safe_str(x) for x in text.splitlines() if safe_str(x)]
2794
- hit_indices = []
2795
 
2796
- for idx, line in enumerate(lines):
2797
- line_norm = normalize_compact(line)
2798
 
2799
- if any(name in line_norm for name in names_norm):
2800
- hit_indices.append(idx)
 
2801
 
2802
- context_lines = []
2803
- seen = set()
2804
 
2805
- for idx in hit_indices[:8]:
2806
- start = max(0, idx - 4)
2807
- end = min(len(lines), idx + 5)
2808
 
2809
- for j in range(start, end):
2810
- if j in seen:
2811
- continue
2812
 
2813
- seen.add(j)
2814
- context_lines.append(lines[j])
2815
 
2816
- context = section["header"] + "\n" + "\n".join(context_lines)
2817
- reg_type = infer_regulation_type_from_source_text(section["header"] + "\n" + text[:1200], country="")
2818
 
2819
- matches.append({
2820
- "header": section["header"],
2821
- "context": context[:8000],
2822
- "regulation_type": reg_type,
2823
- })
2824
 
2825
- if len(matches) >= max_sections:
2826
- break
2827
 
2828
  return matches
2829
 
2830
-
2831
  def pick_best_regulatory_match(matches):
2832
  """
2833
  ์—ฌ๋Ÿฌ ํŒŒ์ผ์—์„œ ๋™์‹œ์— ๋งค์นญ๋  ๋•Œ ์šฐ๏ฟฝ๏ฟฝ๏ฟฝ์ˆœ์œ„๋ฅผ ์ •ํ•œ๋‹ค.
@@ -3142,8 +3136,11 @@ def evaluate_component_from_uploaded_source_records(country, material, component
3142
  ๊ตญ๊ฐ€ ํด๋” ํŒŒ์ผ๋งŒ ๋ณด๊ณ  ํŒ์ •.
3143
  ํŒŒ์ผ record ์•ˆ์— ํ˜„์žฌ ์„ฑ๋ถ„๋ช…/๊ตญ๋ฌธ/CAS๊ฐ€ ์ง์ ‘ ์žˆ์„ ๋•Œ๋งŒ ํŒ์ •ํ•œ๋‹ค.
3144
  """
3145
- terms = source_match_terms_for_component(material, component)
3146
- term_norms = [normalize_compact(t) for t in terms if len(normalize_compact(t)) >= 3]
 
 
 
3147
 
3148
  representative = (
3149
  safe_str(component.get("INCI Name", ""))
@@ -3152,16 +3149,17 @@ def evaluate_component_from_uploaded_source_records(country, material, component
3152
  or "-"
3153
  )
3154
 
3155
- if not term_norms:
3156
  return None
3157
 
3158
  records, status = build_country_source_record_index(country)
3159
  hits = []
3160
 
3161
  for rec in records:
3162
- rec_norm = rec.get("text_norm", "")
3163
 
3164
- if any(tn in rec_norm for tn in term_norms):
 
3165
  hits.append(rec)
3166
 
3167
  if not hits:
 
738
  material.get("Trade name(์˜๋ฌธ)", ""),
739
  ] + list(extra_terms or [])
740
 
741
+ identity_raw = " ".join(safe_str(x) for x in identity_values if safe_str(x))
742
 
743
  hits = []
744
 
745
  for entry in REGULATORY_SYNONYM_REGISTRY:
746
  terms = entry.get("terms", [])
 
747
 
748
+ # ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ๋งค์นญ ๊ธˆ์ง€: biotin์ด meabiotin/biotinate์— ๊ฑธ๋ฆฌ๋Š” ๋ฌธ์ œ ๋ฐฉ์ง€
749
+ if any(exact_ingredient_match(t, identity_raw) for t in terms if safe_str(t)):
750
  for t in terms:
751
  if safe_str(t) and t not in hits:
752
  hits.append(safe_str(t))
 
768
  country_col = display_country(country)
769
  representative = safe_str(component.get("INCI Name", "")) or safe_str(component.get("ํ•œ๊ธ€ ์„ฑ๋ถ„๋ช…", "")) or registry_terms[0]
770
 
771
+ registry_terms = [
772
+ safe_str(t)
773
  for t in registry_terms
774
  if safe_str(t) and len(normalize_compact(t)) >= 3
775
  ]
776
 
777
  for section in sections:
778
  text = safe_str(section.get("text", ""))
 
779
 
780
+ # ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ๋งค์นญ ๊ธˆ์ง€
781
+ if not any(exact_ingredient_match(t, text) for t in registry_terms):
782
  continue
783
 
784
  header = safe_str(section.get("header", ""))
 
791
  seen = set()
792
 
793
  for idx, line in enumerate(lines):
794
+ if any(exact_ingredient_match(t, line) for t in registry_terms):
 
795
  for j in range(max(0, idx - 5), min(len(lines), idx + 6)):
796
  if j not in seen:
797
  seen.add(j)
 
954
 
955
  for section in sections:
956
  text = safe_str(section.get("text", ""))
 
957
  header = safe_str(section.get("header", ""))
958
  reg_type = infer_regulation_type_from_source_text(header + "\n" + text[:1500], country=country)
959
  reg_kind = reg_type.get("type", "unknown")
960
  reg_label = reg_type.get("label", "๊ทœ์ œ ์œ ํ˜• ๋ฏธ๋ถ„๋ฅ˜")
961
 
962
+ hit_terms = [v for v in expanded if exact_ingredient_match(v, text)]
963
  if not hit_terms:
964
  continue
965
 
 
968
  seen = set()
969
 
970
  for idx, line in enumerate(lines):
971
+ if any(exact_ingredient_match(v, line) for v in hit_terms):
 
972
  for j in range(max(0, idx - 4), min(len(lines), idx + 5)):
973
  if j not in seen:
974
  seen.add(j)
 
1468
  except Exception:
1469
  continue
1470
 
1471
+ # ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ํด๋ฆญ ๊ธˆ์ง€: biotin ๊ฒ€์ƒ‰ ์‹œ MEA-Biotinate ๊ฒฐ๊ณผ ํด๋ฆญ ๋ฐฉ์ง€
1472
+ if exact_ingredient_match(term, row_text):
 
1473
  row.click(timeout=3000)
1474
  page.wait_for_timeout(1000)
1475
  return True
 
1733
  return [], ""
1734
 
1735
  # ๊ฒ€์ƒ‰์–ด๊ฐ€ ํ™”๋ฉด์— ์žˆ์ง€๋งŒ ๊ทœ์ œ ํ•„๋“œ๊ฐ€ ์—†์œผ๋ฉด ์ผ๋ฐ˜ ์›๋ฃŒ ์ƒ์„ธ/๋ฌด๊ทœ์ œ ๊ฒฐ๊ณผ๋กœ ํŒ๋‹จ
1736
+ # ๋‹จ, ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์€ ๊ธˆ์ง€: biotin์ด meabiotin/biotinate์— ๊ฑธ๋ฆฌ๋ฉด ์•ˆ ๋จ
1737
+ if exact_ingredient_match(term, body_text):
1738
  return [], ""
1739
 
1740
  if not clicked:
 
2694
  def find_matching_regulatory_sections_fast(country, ingredient_names, max_sections=30):
2695
  """
2696
  ๊ณ ์† ๋‹จ์ผ ๊ฒ€์ƒ‰.
2697
+ ์„ฑ๋ถ„๋ช…/INCI/CAS๋ฅผ ๊ตญ๊ฐ€๋ณ„ ์„น์…˜ ์ธ๋ฑ์Šค์—์„œ ์ง์ ‘ ๊ฒ€์ƒ‰ํ•œ๋‹ค.
2698
+
2699
+ ์ค‘์š”:
2700
+ - normalize_compact ๊ธฐ๋ฐ˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ๊ฒ€์ƒ‰์„ ์“ฐ์ง€ ์•Š๋Š”๋‹ค.
2701
+ - Biotin ๊ฒ€์ƒ‰ ์‹œ MEA-Biotinate, Biotinate, Biotinyl- ๊ณ„์—ด์„ Biotin์œผ๋กœ ์˜ค์ธํ•˜์ง€ ์•Š๋Š”๋‹ค.
2702
  """
2703
  sections, status = get_country_section_index_cached(country)
2704
 
2705
+ names = [
2706
+ safe_str(x)
2707
  for x in ingredient_names
2708
+ if safe_str(x) and len(normalize_compact(x)) >= 3 and not is_broad_lookup_term(x)
2709
  ]
2710
 
2711
+ if not names:
2712
  return [], status
2713
 
2714
  matches = []
2715
 
2716
  for section in sections:
2717
+ text = safe_str(section.get("text", ""))
2718
 
2719
+ if not any(exact_ingredient_match(name, text) for name in names):
2720
  continue
2721
 
 
2722
  header = safe_str(section.get("header", ""))
2723
  lines = [safe_str(x) for x in text.splitlines() if safe_str(x)]
2724
 
2725
  hit_indices = []
2726
 
2727
  for idx, line in enumerate(lines):
2728
+ if any(exact_ingredient_match(name, line) for name in names):
 
 
2729
  hit_indices.append(idx)
2730
 
2731
  if len(hit_indices) >= 10:
 
2750
  else:
2751
  context = header + "\n" + text[:2000]
2752
 
 
2753
  reg_type = infer_regulation_type_from_source_text(
2754
  header + "\n" + text[:1500],
2755
  country=country,
 
2766
 
2767
  return matches, status
2768
 
 
2769
  def find_matching_regulatory_sections(raw_text, ingredient_names, max_sections=20):
2770
  """
2771
+ ๊ตฌ๋ฒ„์ „ ํ˜ธํ™˜์šฉ. ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ๊ฒ€์ƒ‰ ๊ธˆ์ง€.
2772
  """
2773
+ names = [
2774
+ safe_str(x)
2775
  for x in ingredient_names
2776
+ if safe_str(x) and len(normalize_compact(x)) >= 3 and not is_broad_lookup_term(x)
2777
  ]
2778
 
2779
+ if not names:
2780
  return []
2781
 
2782
  matches = []
2783
 
2784
  for section in split_regulatory_text_into_sections(raw_text):
2785
  text = section["text"]
 
2786
 
2787
+ if not any(exact_ingredient_match(name, text) for name in names):
2788
+ continue
 
2789
 
2790
+ lines = [safe_str(x) for x in text.splitlines() if safe_str(x)]
2791
+ hit_indices = []
2792
 
2793
+ for idx, line in enumerate(lines):
2794
+ if any(exact_ingredient_match(name, line) for name in names):
2795
+ hit_indices.append(idx)
2796
 
2797
+ context_lines = []
2798
+ seen = set()
2799
 
2800
+ for idx in hit_indices[:8]:
2801
+ start = max(0, idx - 4)
2802
+ end = min(len(lines), idx + 5)
2803
 
2804
+ for j in range(start, end):
2805
+ if j in seen:
2806
+ continue
2807
 
2808
+ seen.add(j)
2809
+ context_lines.append(lines[j])
2810
 
2811
+ context = section["header"] + "\n" + "\n".join(context_lines)
2812
+ reg_type = infer_regulation_type_from_source_text(section["header"] + "\n" + text[:1200], country="")
2813
 
2814
+ matches.append({
2815
+ "header": section["header"],
2816
+ "context": context[:8000],
2817
+ "regulation_type": reg_type,
2818
+ })
2819
 
2820
+ if len(matches) >= max_sections:
2821
+ break
2822
 
2823
  return matches
2824
 
 
2825
  def pick_best_regulatory_match(matches):
2826
  """
2827
  ์—ฌ๋Ÿฌ ํŒŒ์ผ์—์„œ ๋™์‹œ์— ๋งค์นญ๋  ๋•Œ ์šฐ๏ฟฝ๏ฟฝ๏ฟฝ์ˆœ์œ„๋ฅผ ์ •ํ•œ๋‹ค.
 
3136
  ๊ตญ๊ฐ€ ํด๋” ํŒŒ์ผ๋งŒ ๋ณด๊ณ  ํŒ์ •.
3137
  ํŒŒ์ผ record ์•ˆ์— ํ˜„์žฌ ์„ฑ๋ถ„๋ช…/๊ตญ๋ฌธ/CAS๊ฐ€ ์ง์ ‘ ์žˆ์„ ๋•Œ๋งŒ ํŒ์ •ํ•œ๋‹ค.
3138
  """
3139
+ terms = [
3140
+ safe_str(t)
3141
+ for t in source_match_terms_for_component(material, component)
3142
+ if safe_str(t) and len(normalize_compact(t)) >= 3 and not is_broad_lookup_term(t)
3143
+ ]
3144
 
3145
  representative = (
3146
  safe_str(component.get("INCI Name", ""))
 
3149
  or "-"
3150
  )
3151
 
3152
+ if not terms:
3153
  return None
3154
 
3155
  records, status = build_country_source_record_index(country)
3156
  hits = []
3157
 
3158
  for rec in records:
3159
+ rec_text = rec.get("text", "")
3160
 
3161
+ # ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ๋งค์นญ ๊ธˆ์ง€: biotin์ด MEA-Biotinate์— ๊ฑธ๋ฆฌ๋Š” ๋ฌธ์ œ ๋ฐฉ์ง€
3162
+ if any(exact_ingredient_match(t, rec_text) for t in terms):
3163
  hits.append(rec)
3164
 
3165
  if not hits: