Spaces:
Sleeping
Sleeping
File size: 9,175 Bytes
f4e0387 | 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 | """Rule-based preprocessing: Korean number→English unit conversion + article date injection."""
import re
from datetime import datetime
# Korean magnitude markers (cumulative within a compound number)
KO_MAGNITUDE = [("조", 10**12), ("억", 10**8), ("만", 10**4)]
# Recognized currency / counter units. Currency names follow international
# naming conventions ("Korean won", "Japanese yen", "Chinese yuan"). Counter
# units stay simple.
UNIT_MAP = {
"원": "Korean won",
"달러": "dollars",
"유로": "euros",
"엔": "Japanese yen",
"위안": "Chinese yuan",
"명": "people",
"개": "units",
"건": "cases",
"대": "vehicles",
"톤": "tons",
}
# Compound Korean number followed by unit. The number portion may contain any
# combination of 조 / 억 / 만 plus a tail digit run, optionally followed by an
# approximation marker (여 / 약 / 가량 / 여 등) before the unit. We require at
# least one digit-magnitude pair OR a digit-only tail to avoid matching bare
# units.
APPROX = r"(?:여|여\s+|가량|가량\s+|쯤|쯤\s+|약\s+|약)"
NUM_UNIT_PATTERN = re.compile(
r"(?P<num>"
r"(?:\d[\d,]*(?:\.\d+)?\s*조\s*)?"
r"(?:\d[\d,]*(?:\.\d+)?(?:\s*" + APPROX + r")?\s*억\s*)?"
r"(?:\d[\d,]*(?:\.\d+)?(?:\s*" + APPROX + r")?\s*만\s*)?"
r"(?:\d[\d,]*(?:\.\d+)?)?"
r")\s*(?P<unit>원|달러|유로|엔|위안|명|개|건|대|톤)"
)
# Percentage points. Matches all common Korean variants of "X%p / X%포인트 /
# X퍼센트 포인트". Must be detected and replaced BEFORE plain numbers so that
# "10%" doesn't slip through unattended (we want "10 percentage points", never
# just "10%").
PERCENT_POINT_PATTERN = re.compile(
r"(?P<num>\d[\d,]*(?:\.\d+)?)\s*"
r"(?:%\s*p|%\s*포인트|퍼센트\s*포인트|%\s*포인트)",
re.IGNORECASE,
)
def parse_korean_number(text: str) -> float | None:
"""Parse a compound Korean magnitude string (e.g., '1조4000억', '120억', '5000만',
'7800여억') → numeric value. Approximation markers (여 / 가량 / 쯤 / 약) are stripped."""
s = text.replace(",", "").replace(" ", "")
# Strip approximation markers anywhere they appear within the number string
s = re.sub(r"(여|가량|쯤|약)", "", s)
total = 0.0
matched = False
for marker, mult in KO_MAGNITUDE:
m = re.match(r"(\d+(?:\.\d+)?)" + marker, s)
if m:
total += float(m.group(1)) * mult
s = s[m.end():]
matched = True
if s:
try:
total += float(s)
matched = True
except ValueError:
pass
return total if matched else None
def format_english_amount(amount: float, unit_kor: str) -> str:
"""Format amount in English unit. Uses 'million/billion/trillion' for large numbers."""
unit_en = UNIT_MAP.get(unit_kor, unit_kor)
def _fmt(n: float) -> str:
# Preserve up to 4 decimals; trim trailing zeros; thousands-separate the integer part.
if abs(n - round(n)) < 1e-9:
return f"{int(round(n)):,}"
s = f"{n:.4f}".rstrip("0").rstrip(".")
if "." in s:
int_part, dec = s.split(".")
return f"{int(int_part):,}.{dec}"
return f"{int(s):,}"
if amount >= 10**12:
return f"{_fmt(amount / 10**12)} trillion {unit_en}"
if amount >= 10**9:
return f"{_fmt(amount / 10**9)} billion {unit_en}"
if amount >= 10**6:
return f"{_fmt(amount / 10**6)} million {unit_en}"
# < 10^6: write as thousand-separated integer (avoid awkward "135.574 thousand")
return f"{_fmt(amount)} {unit_en}"
def detect_korean_numbers(text: str) -> list[dict]:
"""Return list of {span, start, end, amount, unit, english} entries found in text."""
results = []
seen_spans = set()
for m in NUM_UNIT_PATTERN.finditer(text):
num_str = m.group("num").strip()
unit = m.group("unit")
if not re.search(r"\d", num_str):
continue # skip bare unit
amount = parse_korean_number(num_str)
if amount is None or amount == 0:
continue
full = f"{num_str}{unit}"
# Dedup identical spans (e.g., the same "100억원" appearing many times)
if full in seen_spans:
continue
seen_spans.add(full)
results.append({
"span": full,
"start": m.start(),
"end": m.end(),
"amount": amount,
"unit_ko": unit,
"english": format_english_amount(amount, unit),
})
return results
def parse_article_date(date_str: str) -> str | None:
"""Normalize an article date to a YYYY-MM-DD string, return None if unparseable."""
if not date_str:
return None
s = date_str.strip()
# Try a few common formats
for fmt in ("%Y-%m-%d", "%Y.%m.%d", "%Y/%m/%d", "%Y%m%d"):
try:
return datetime.strptime(s[:10], fmt).strftime("%Y-%m-%d")
except ValueError:
continue
return None
def _format_simple_number(num_str: str) -> str:
"""Format a plain digit string (with possible commas/decimals) keeping the value as-is."""
s = num_str.replace(",", "")
try:
n = float(s)
except ValueError:
return num_str
if abs(n - round(n)) < 1e-9:
return f"{int(round(n)):,}"
return f"{n:,g}"
def replace_numbers_inline(text: str) -> tuple[str, list[dict]]:
"""Replace Korean number+unit spans in `text` with their English equivalents.
Two passes (each non-overlapping internally), applied in priority order so
that longer / more specific patterns win:
1. Percentage points ("10%포인트" → "10 percentage points")
2. Korean magnitude+unit ("120억원" → "12 billion Korean won")
Returns (rewritten_text, list_of_replacements_with_offsets_in_original).
"""
matches: list[dict] = []
used_ranges: list[tuple[int, int]] = []
def overlaps(s: int, e: int) -> bool:
return any(not (e <= us or s >= ue) for us, ue in used_ranges)
# Pass 1: percentage points (highest priority)
for m in PERCENT_POINT_PATTERN.finditer(text):
s, e = m.start(), m.end()
if overlaps(s, e):
continue
num_str = m.group("num")
english = f"{_format_simple_number(num_str)} percentage points"
matches.append({
"start": s,
"end": e,
"span": text[s:e],
"amount": None,
"unit_ko": "%포인트",
"english": english,
})
used_ranges.append((s, e))
# Pass 2: Korean magnitude+currency/counter
for m in NUM_UNIT_PATTERN.finditer(text):
s, e = m.start(), m.end()
if overlaps(s, e):
continue
num_str = m.group("num").strip()
unit = m.group("unit")
if not re.search(r"\d", num_str):
continue
amount = parse_korean_number(num_str)
if amount is None or amount == 0:
continue
english = format_english_amount(amount, unit)
matches.append({
"start": s,
"end": e,
"span": text[s:e],
"amount": amount,
"unit_ko": unit,
"english": english,
})
used_ranges.append((s, e))
# Apply replacements right-to-left to preserve earlier offsets
matches.sort(key=lambda r: r["start"])
out = text
for r in sorted(matches, key=lambda r: r["start"], reverse=True):
out = out[:r["start"]] + r["english"] + out[r["end"]:]
return out, matches
def preprocess(text: str, article_date: str | None) -> tuple[str, dict]:
"""Inline-replace Korean numbers with English units inside the user text.
The article date is NOT injected into the user message; callers should append
it to the system prompt via `system_prompt_date_suffix()` instead.
Returns (rewritten_text, debug_info).
"""
norm_date = parse_article_date(article_date) if article_date else None
rewritten, replacements = replace_numbers_inline(text)
return rewritten, {"date": norm_date, "conversions": replacements}
def system_prompt_date_suffix(article_date: str | None) -> str:
"""Return the line to append to the system prompt for date anchoring.
Empty string if no date is available.
"""
norm_date = parse_article_date(article_date) if article_date else None
if not norm_date:
return ""
return f"\n\n[Article published date: {norm_date}]"
if __name__ == "__main__":
# Smoke test
samples = [
("1023억원과 5조4000억원, 그리고 300만원", "2025-03-15"),
("897억달러 매출", "2025-04-02"),
("지난 19일 회담이 열렸다", "2025-01-23"),
("100억원대 분쟁이 발생", "2024-11-01"),
("5000만원의 보너스를 받았다", "2025-02-10"),
]
for text, date in samples:
out, info = preprocess(text, date)
print(f"\n--- date={date} ---")
print(f"input: {text}")
print(f"detected: {info['conversions']}")
print(f"resolved date: {info['date']}")
print(f"augmented:\n{out}")
|