Compare commits

...
2 Commits
Author SHA1 Message Date
atarasov 3502d3876c 0.5.1 - Move 2026-03-12 17:57:13 +03:00
atarasov 19365ce4f7 0.5 - Разбор нескольких моделей в одной строке 2026-03-12 17:56:49 +03:00
3 changed files with 26 additions and 130 deletions
+2 -15
View File
@@ -1,16 +1,3 @@
HYUNDAI Accent 1.3 [G4E-A] 08.1999-
HYUNDAI Accent 1.3 [G4EH] 08.2002-
HYUNDAI Accent 1.4 GL 11.2005-
HYUNDAI Accent 1.3 [G4E-A] 08.1999- рандомный \n text HYUNDAI Accent 1.3 [G4EH] 08.2002- вфы вфы HYUNDAI Accent 1.4 GL 11.2005-
HYUNDAI Accent 1.5 [G4EB] 08.2002-
HYUNDAI Accent 1.6 [G4ED-G] 12.2002-
HYUNDAI Accent 1.6 GLS 11.2005-
HYUNDAI Coupe 1.6 16V [G4ED-G] 03.2002-
HYUNDAI Coupe 2.0 16V [G4GF] 08.2002-
HYUNDAI Coupe 2.0 GLS 08.2002-
KIA Rio 1.6 16V [G4ED] 03.2005-
MERCEDES-BENZ CABRIOLET (W111) 280 SE 3.5 (111.025) [M 116.980] 01/1969 - 12/1971
MERCEDES-BENZ COUPE (W111) 280 SE 3.5 (111.026) [M 116.980] 01/1969 - 12/1971
MERCEDES-BENZ S-CLASS (W108, W109) 280 SE,SEL 3.5 (108.057, 108.058) [M 116.980] 03/1971 - 08/1972
MERCEDES-BENZ S-CLASS (W108, W109) 300 SEL 3.5 (109.056) [M 116.981] 01/1970 - 07/1972
MERCEDES-BENZ S-CLASS (W116) 350 SE,SEL (116.028, 116.029) [M 116.983] 08/1972 - 02/1976
MERCEDES-BENZ SL купе (C107) 350 SLC (107.023) [M 116.982, M 116.984] 01/1972 - 02/1976
HYUNDAI Accent 1.6
+24 -17
View File
@@ -31,7 +31,8 @@ SHOW_IO = os.environ.get("SHOW_IO", "1").lower() in ("1", "true", "yes")
RECORD_DELIMITERS = ("\n", "\r\n", "\r")
EXTRACT_PROMPT = """Extract brand, model, engine_code, date_range from the text.
Return JSON object only: {"brand":"","model":"","engine_code":"","date_range":""}
If 1 record — return JSON object: {"brand":"","model":"","engine_code":"","date_range":""}
If 2+ records on the line — return JSON array: [{"brand":"",...},{"brand":"",...}]
Empty fields as "". No other text."""
@@ -42,8 +43,17 @@ def prepare_records(content: str) -> list[str]:
return [r.strip() for r in content.split(RECORD_DELIMITERS[0]) if r.strip()]
def extract_record(client: Client, record: str) -> dict:
"""2) Отправка промпт + 1 запись в нейронку. 3) Получение ответа."""
def _normalize_obj(obj: dict) -> dict:
return {
"brand": obj.get("brand", ""),
"model": obj.get("model", ""),
"engine_code": obj.get("engine_code", ""),
"date_range": obj.get("date_range", ""),
}
def extract_record(client: Client, record: str) -> list[dict]:
"""2) Отправка промпт + 1 запись в нейронку. 3) Получение ответа (1 или более объектов)."""
for attempt in range(1, RETRY_COUNT + 1):
try:
response = client.chat(
@@ -59,24 +69,21 @@ def extract_record(client: Client, record: str) -> dict:
result = result.split("```")[1]
if result.strip().lower().startswith("json"):
result = result.strip()[4:].strip()
obj = json.loads(result)
return {
"brand": obj.get("brand", ""),
"model": obj.get("model", ""),
"engine_code": obj.get("engine_code", ""),
"date_range": obj.get("date_range", ""),
}
except (json.JSONDecodeError, KeyError) as e:
parsed = json.loads(result)
if isinstance(parsed, list):
return [_normalize_obj(o) for o in parsed]
return [_normalize_obj(parsed)]
except (json.JSONDecodeError, KeyError):
if attempt < RETRY_COUNT:
time.sleep(RETRY_DELAY)
else:
return {"brand": "", "model": "", "engine_code": "", "date_range": record}
return [{"brand": "", "model": "", "engine_code": "", "date_range": record}]
except Exception:
if attempt < RETRY_COUNT:
time.sleep(RETRY_DELAY)
else:
raise
return {"brand": "", "model": "", "engine_code": "", "date_range": record}
return [{"brand": "", "model": "", "engine_code": "", "date_range": record}]
def process_file(client: Client, filepath: pathlib.Path) -> bool:
@@ -96,18 +103,18 @@ def process_file(client: Client, filepath: pathlib.Path) -> bool:
for i, record in enumerate(records, 1):
print(f" [{i}/{total}] ", end="", flush=True)
obj = extract_record(client, record)
data.append(obj)
objs = extract_record(client, record)
data.extend(objs)
out_path.write_text(json.dumps(data, ensure_ascii=False, indent=2), encoding="utf-8")
if SHOW_IO:
inp = record[:60] + "" if len(record) > 60 else record
out = json.dumps(obj, ensure_ascii=False)
out = json.dumps(objs, ensure_ascii=False)
print(f"вход: {inp}", flush=True)
print(f" выход: {out}", flush=True)
else:
print("", flush=True)
elapsed = time.perf_counter() - t0
print(f" {filepath.name}{total} записей, {elapsed:.1f}s — OK")
print(f" {filepath.name}{len(data)} объектов, {elapsed:.1f}s — OK")
return True
-98
View File
@@ -1,98 +0,0 @@
[
{
"brand": "HYUNDAI",
"model": "Accent 1.3",
"engine_code": "G4E-A",
"date_range": "08.1999-"
},
{
"brand": "HYUNDAI",
"model": "Accent 1.3",
"engine_code": "G4EH",
"date_range": "08.2002-"
},
{
"brand": "HYUNDAI",
"model": "Accent 1.4 GL",
"engine_code": "",
"date_range": "11.2005-"
},
{
"brand": "HYUNDAI",
"model": "Accent 1.5",
"engine_code": "G4EB",
"date_range": "08.2002-"
},
{
"brand": "HYUNDAI",
"model": "Accent 1.6",
"engine_code": "G4ED-G",
"date_range": "12.2002-"
},
{
"brand": "HYUNDAI",
"model": "Accent 1.6 GLS",
"engine_code": "",
"date_range": "11.2005-"
},
{
"brand": "HYUNDAI",
"model": "Coupe 1.6 16V",
"engine_code": "G4ED-G",
"date_range": "03.2002-"
},
{
"brand": "HYUNDAI",
"model": "Coupe 2.0 16V",
"engine_code": "G4GF",
"date_range": "08.2002-"
},
{
"brand": "HYUNDAI",
"model": "Coupe 2.0 GLS",
"engine_code": "",
"date_range": "08.2002-"
},
{
"brand": "KIA",
"model": "Rio 1.6 16V",
"engine_code": "G4ED",
"date_range": "03.2005-"
},
{
"brand": "MERCEDES-BENZ",
"model": "CABRIOLET (W111) 280 SE 3.5",
"engine_code": "111.025",
"date_range": "01/1969 - 12/1971"
},
{
"brand": "MERCEDES-BENZ",
"model": "COUPE (W111) 280 SE 3.5",
"engine_code": "111.026",
"date_range": "01/1969 - 12/1971"
},
{
"brand": "MERCEDES-BENZ",
"model": "S-CLASS (W108, W109) 280 SE,SEL 3.5",
"engine_code": "M 116.980",
"date_range": "03/1971 - 08/1972"
},
{
"brand": "MERCEDES-BENZ",
"model": "S-CLASS (W108, W109) 300 SEL 3.5",
"engine_code": "109.056 M 116.981",
"date_range": "01/1970 - 07/1972"
},
{
"brand": "MERCEDES-BENZ",
"model": "S-CLASS (W116) 350 SE,SEL",
"engine_code": "M 116.983",
"date_range": "08/1972 - 02/1976"
},
{
"brand": "MERCEDES-BENZ",
"model": "SL купе (C107) 350 SLC (107.023)",
"engine_code": "M 116.982, M 116.984",
"date_range": "01/1972 - 02/1976"
}
]