Spaces:
Sleeping
Sleeping
Update app.py
#1
by jin2li - opened
app.py
CHANGED
|
@@ -352,6 +352,33 @@ def normalize_compact(text):
|
|
| 352 |
return re.sub(r"\s+", "", normalize_text(text))
|
| 353 |
|
| 354 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 355 |
def country_key(country):
|
| 356 |
text = safe_str(country).upper().replace("_", " ").replace("-", " ")
|
| 357 |
text = re.sub(r"\s+", " ", text).strip()
|
|
@@ -360,35 +387,27 @@ def country_key(country):
|
|
| 360 |
"한국": "KOREA",
|
| 361 |
"KOREA": "KOREA",
|
| 362 |
"KR": "KOREA",
|
| 363 |
-
|
| 364 |
"EU": "EU",
|
| 365 |
"E.U.": "EU",
|
| 366 |
"EUROPE": "EU",
|
| 367 |
"유럽": "EU",
|
| 368 |
-
|
| 369 |
"EU FUTURE": "EU_FUTURE",
|
| 370 |
"EU (FUTURE)": "EU_FUTURE",
|
| 371 |
-
|
| 372 |
"JAPAN": "JAPAN",
|
| 373 |
"JP": "JAPAN",
|
| 374 |
"일본": "JAPAN",
|
| 375 |
-
|
| 376 |
"CHINA": "CHINA",
|
| 377 |
"CN": "CHINA",
|
| 378 |
"중국": "CHINA",
|
| 379 |
-
|
| 380 |
"USA": "FDA",
|
| 381 |
"US": "FDA",
|
| 382 |
"미국": "FDA",
|
| 383 |
"FDA": "FDA",
|
| 384 |
-
|
| 385 |
"ASEAN": "ASEAN",
|
| 386 |
"아세안": "ASEAN",
|
| 387 |
-
|
| 388 |
"UK": "UK",
|
| 389 |
"UNITED KINGDOM": "UK",
|
| 390 |
"영국": "UK",
|
| 391 |
-
|
| 392 |
"SEPHORA": "SEPHORA",
|
| 393 |
"TARGET": "TARGET",
|
| 394 |
"ULTA": "ULTA",
|
|
@@ -1108,7 +1127,6 @@ def clean_input_df(df, cols):
|
|
| 1108 |
row_values = [safe_str(v) for v in row.values]
|
| 1109 |
row_text = " ".join([v for v in row_values if v]).strip()
|
| 1110 |
row_text_lower = row_text.lower()
|
| 1111 |
-
row_compact = normalize_compact(row_text)
|
| 1112 |
|
| 1113 |
if not row_text:
|
| 1114 |
continue
|
|
@@ -1117,7 +1135,7 @@ def clean_input_df(df, cols):
|
|
| 1117 |
korean_text = safe_str(row.get(korean_ing_col, "")).lower() if korean_ing_col in df.columns else ""
|
| 1118 |
inci_text = safe_str(row.get(inci_col, "")).lower() if inci_col in df.columns else ""
|
| 1119 |
|
| 1120 |
-
if no_text in ["no", "번호", "순번"]:
|
| 1121 |
continue
|
| 1122 |
|
| 1123 |
if korean_text in ["한글 표준화 명칭", "한글 성분명", "한글성분명", "국문 성분명"]:
|
|
@@ -1127,20 +1145,10 @@ def clean_input_df(df, cols):
|
|
| 1127 |
continue
|
| 1128 |
|
| 1129 |
if (
|
| 1130 |
-
|
| 1131 |
-
or
|
| 1132 |
-
or
|
| 1133 |
-
or
|
| 1134 |
-
or korean_text.startswith("total")
|
| 1135 |
-
or inci_text.startswith("total")
|
| 1136 |
-
or no_text.strip() == "total"
|
| 1137 |
-
or korean_text.strip() == "total"
|
| 1138 |
-
or inci_text.strip() == "total"
|
| 1139 |
-
or row_compact in ["total", "합계"]
|
| 1140 |
-
or row_compact.startswith("total100")
|
| 1141 |
-
or row_compact.startswith("합계100")
|
| 1142 |
-
or row_compact.endswith("total100")
|
| 1143 |
-
or row_compact.endswith("합계100")
|
| 1144 |
):
|
| 1145 |
footer_started = True
|
| 1146 |
continue
|
|
@@ -1178,6 +1186,14 @@ def make_grouped_materials(df):
|
|
| 1178 |
korean_ing = safe_str(row.get(korean_ing_col, "")) if korean_ing_col in row else ""
|
| 1179 |
inci_name = safe_str(row.get(inci_col, "")) if inci_col in row else ""
|
| 1180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1181 |
if not korean_ing and not inci_name:
|
| 1182 |
continue
|
| 1183 |
|
|
@@ -2207,10 +2223,21 @@ def make_formula_statement_rows(row_items, country_cols):
|
|
| 2207 |
comp = item["component"]
|
| 2208 |
row_data = item["row_data"]
|
| 2209 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2210 |
row = {
|
| 2211 |
-
"NO.":
|
| 2212 |
-
"한글 표준화 명칭":
|
| 2213 |
-
"INGREDIENT NAME (INCI)":
|
| 2214 |
"FUNCTION": comp.get("FUNCTION", ""),
|
| 2215 |
"CAS.No": comp.get("CAS.No", ""),
|
| 2216 |
"SOURCE": comp.get("SOURCE", "ICID") or "ICID",
|
|
@@ -2227,7 +2254,7 @@ def make_formula_statement_rows(row_items, country_cols):
|
|
| 2227 |
row[country_col] = normalize_formula_country_text(
|
| 2228 |
raw_text,
|
| 2229 |
country_col=country_col,
|
| 2230 |
-
inci_name=
|
| 2231 |
amount_text=comp.get("In formula(w/w %)", ""),
|
| 2232 |
)
|
| 2233 |
|
|
@@ -2296,8 +2323,19 @@ def sum_formula_percent(formula_df):
|
|
| 2296 |
if formula_df is None or formula_df.empty:
|
| 2297 |
return total
|
| 2298 |
|
| 2299 |
-
for
|
| 2300 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2301 |
|
| 2302 |
if num is not None:
|
| 2303 |
total += num
|
|
|
|
| 352 |
return re.sub(r"\s+", "", normalize_text(text))
|
| 353 |
|
| 354 |
|
| 355 |
+
def is_total_like_text(text):
|
| 356 |
+
compact = normalize_compact(text)
|
| 357 |
+
return compact in ["total", "합계", "총합", "sum"] or compact.startswith("total") or compact.startswith("합계")
|
| 358 |
+
|
| 359 |
+
|
| 360 |
+
def row_has_total_like(row_values):
|
| 361 |
+
values = [safe_str(v) for v in row_values if safe_str(v)]
|
| 362 |
+
|
| 363 |
+
for value in values:
|
| 364 |
+
if is_total_like_text(value):
|
| 365 |
+
return True
|
| 366 |
+
|
| 367 |
+
row_text = " ".join(values)
|
| 368 |
+
row_compact = normalize_compact(row_text)
|
| 369 |
+
|
| 370 |
+
if (
|
| 371 |
+
row_compact in ["total", "합계", "총합", "sum"]
|
| 372 |
+
or row_compact.startswith("total")
|
| 373 |
+
or row_compact.startswith("합계")
|
| 374 |
+
or "total100" in row_compact
|
| 375 |
+
or "합계100" in row_compact
|
| 376 |
+
):
|
| 377 |
+
return True
|
| 378 |
+
|
| 379 |
+
return False
|
| 380 |
+
|
| 381 |
+
|
| 382 |
def country_key(country):
|
| 383 |
text = safe_str(country).upper().replace("_", " ").replace("-", " ")
|
| 384 |
text = re.sub(r"\s+", " ", text).strip()
|
|
|
|
| 387 |
"한국": "KOREA",
|
| 388 |
"KOREA": "KOREA",
|
| 389 |
"KR": "KOREA",
|
|
|
|
| 390 |
"EU": "EU",
|
| 391 |
"E.U.": "EU",
|
| 392 |
"EUROPE": "EU",
|
| 393 |
"유럽": "EU",
|
|
|
|
| 394 |
"EU FUTURE": "EU_FUTURE",
|
| 395 |
"EU (FUTURE)": "EU_FUTURE",
|
|
|
|
| 396 |
"JAPAN": "JAPAN",
|
| 397 |
"JP": "JAPAN",
|
| 398 |
"일본": "JAPAN",
|
|
|
|
| 399 |
"CHINA": "CHINA",
|
| 400 |
"CN": "CHINA",
|
| 401 |
"중국": "CHINA",
|
|
|
|
| 402 |
"USA": "FDA",
|
| 403 |
"US": "FDA",
|
| 404 |
"미국": "FDA",
|
| 405 |
"FDA": "FDA",
|
|
|
|
| 406 |
"ASEAN": "ASEAN",
|
| 407 |
"아세안": "ASEAN",
|
|
|
|
| 408 |
"UK": "UK",
|
| 409 |
"UNITED KINGDOM": "UK",
|
| 410 |
"영국": "UK",
|
|
|
|
| 411 |
"SEPHORA": "SEPHORA",
|
| 412 |
"TARGET": "TARGET",
|
| 413 |
"ULTA": "ULTA",
|
|
|
|
| 1127 |
row_values = [safe_str(v) for v in row.values]
|
| 1128 |
row_text = " ".join([v for v in row_values if v]).strip()
|
| 1129 |
row_text_lower = row_text.lower()
|
|
|
|
| 1130 |
|
| 1131 |
if not row_text:
|
| 1132 |
continue
|
|
|
|
| 1135 |
korean_text = safe_str(row.get(korean_ing_col, "")).lower() if korean_ing_col in df.columns else ""
|
| 1136 |
inci_text = safe_str(row.get(inci_col, "")).lower() if inci_col in df.columns else ""
|
| 1137 |
|
| 1138 |
+
if no_text in ["no", "no.", "번호", "순번"]:
|
| 1139 |
continue
|
| 1140 |
|
| 1141 |
if korean_text in ["한글 표준화 명칭", "한글 성분명", "한글성분명", "국문 성분명"]:
|
|
|
|
| 1145 |
continue
|
| 1146 |
|
| 1147 |
if (
|
| 1148 |
+
is_total_like_text(no_text)
|
| 1149 |
+
or is_total_like_text(korean_text)
|
| 1150 |
+
or is_total_like_text(inci_text)
|
| 1151 |
+
or row_has_total_like(row_values)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1152 |
):
|
| 1153 |
footer_started = True
|
| 1154 |
continue
|
|
|
|
| 1186 |
korean_ing = safe_str(row.get(korean_ing_col, "")) if korean_ing_col in row else ""
|
| 1187 |
inci_name = safe_str(row.get(inci_col, "")) if inci_col in row else ""
|
| 1188 |
|
| 1189 |
+
if (
|
| 1190 |
+
is_total_like_text(no_value)
|
| 1191 |
+
or is_total_like_text(korean_ing)
|
| 1192 |
+
or is_total_like_text(inci_name)
|
| 1193 |
+
or row_has_total_like(row.values)
|
| 1194 |
+
):
|
| 1195 |
+
continue
|
| 1196 |
+
|
| 1197 |
if not korean_ing and not inci_name:
|
| 1198 |
continue
|
| 1199 |
|
|
|
|
| 2223 |
comp = item["component"]
|
| 2224 |
row_data = item["row_data"]
|
| 2225 |
|
| 2226 |
+
no_value = safe_str(material.get("NO", ""))
|
| 2227 |
+
korean_name = safe_str(comp.get("한글 성분명", ""))
|
| 2228 |
+
inci_name = safe_str(comp.get("INCI Name", ""))
|
| 2229 |
+
|
| 2230 |
+
if (
|
| 2231 |
+
is_total_like_text(no_value)
|
| 2232 |
+
or is_total_like_text(korean_name)
|
| 2233 |
+
or is_total_like_text(inci_name)
|
| 2234 |
+
):
|
| 2235 |
+
continue
|
| 2236 |
+
|
| 2237 |
row = {
|
| 2238 |
+
"NO.": no_value,
|
| 2239 |
+
"한글 표준화 명칭": korean_name,
|
| 2240 |
+
"INGREDIENT NAME (INCI)": inci_name,
|
| 2241 |
"FUNCTION": comp.get("FUNCTION", ""),
|
| 2242 |
"CAS.No": comp.get("CAS.No", ""),
|
| 2243 |
"SOURCE": comp.get("SOURCE", "ICID") or "ICID",
|
|
|
|
| 2254 |
row[country_col] = normalize_formula_country_text(
|
| 2255 |
raw_text,
|
| 2256 |
country_col=country_col,
|
| 2257 |
+
inci_name=inci_name,
|
| 2258 |
amount_text=comp.get("In formula(w/w %)", ""),
|
| 2259 |
)
|
| 2260 |
|
|
|
|
| 2323 |
if formula_df is None or formula_df.empty:
|
| 2324 |
return total
|
| 2325 |
|
| 2326 |
+
for _, row in formula_df.iterrows():
|
| 2327 |
+
no_value = safe_str(row.get("NO.", ""))
|
| 2328 |
+
korean_name = safe_str(row.get("한글 표준화 명칭", ""))
|
| 2329 |
+
inci_name = safe_str(row.get("INGREDIENT NAME (INCI)", ""))
|
| 2330 |
+
|
| 2331 |
+
if (
|
| 2332 |
+
is_total_like_text(no_value)
|
| 2333 |
+
or is_total_like_text(korean_name)
|
| 2334 |
+
or is_total_like_text(inci_name)
|
| 2335 |
+
):
|
| 2336 |
+
continue
|
| 2337 |
+
|
| 2338 |
+
num = to_number(row.get("%(w/w)", ""))
|
| 2339 |
|
| 2340 |
if num is not None:
|
| 2341 |
total += num
|