Update trade listings
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
"""Parse explicit, fully specified unit bundles from asking listings."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections import Counter
|
||||
import re
|
||||
|
||||
from calculate_interest_index import variants
|
||||
from extract_exchange_quotes import FOR, parse_side
|
||||
|
||||
|
||||
SEPARATOR = re.compile(r"\s*(?:\+|,|\band\b)\s*", re.IGNORECASE)
|
||||
OWNER = re.compile(r"^(?:my|your|ur)\s+", re.IGNORECASE)
|
||||
|
||||
|
||||
def parse_bundle(text: str) -> tuple[Counter[str], str | None] | None:
|
||||
text = text.strip(" \t.,!?")
|
||||
owner_match = OWNER.match(text)
|
||||
owner = owner_match.group().strip().casefold() if owner_match else None
|
||||
if owner_match:
|
||||
text = text[owner_match.end():]
|
||||
parts = SEPARATOR.split(text)
|
||||
if not parts or len(parts) > 8:
|
||||
return None
|
||||
result: Counter[str] = Counter()
|
||||
for part in parts:
|
||||
parsed = parse_side(part)
|
||||
if parsed is None or parsed[2] is not None:
|
||||
return None
|
||||
result[parsed[0]] += parsed[1]
|
||||
return result, owner
|
||||
|
||||
|
||||
def extract_bundle_quotes(rows: list[dict[str, str]]) -> list[dict]:
|
||||
quotes = []
|
||||
seen = set()
|
||||
for row_number, row in enumerate(rows, start=2):
|
||||
author = (row.get("author_hash") or "").strip()
|
||||
if not author:
|
||||
continue
|
||||
inventory = variants(row.get("offer") or "")
|
||||
requests = [x for x in (row.get("lf") or "", row.get("mlf") or "") if x.strip()]
|
||||
candidates = []
|
||||
if len(requests) == 1 and "|" not in (row.get("offer") or ""):
|
||||
candidates.append(("listing_pair", row.get("offer") or "", requests[0],
|
||||
f"{row.get('offer') or ''} -> {requests[0]}"))
|
||||
for request in requests:
|
||||
for segment in request.split("|"):
|
||||
halves = FOR.split(segment)
|
||||
if len(halves) == 2:
|
||||
candidates.append(("explicit_for", halves[0], halves[1], segment.strip()))
|
||||
for kind, left_text, right_text, source_text in candidates:
|
||||
left, right = parse_bundle(left_text), parse_bundle(right_text)
|
||||
if left is None or right is None:
|
||||
continue
|
||||
if len(left[0]) == len(right[0]) == 1:
|
||||
continue # Already handled by extract_exchange_quotes.py.
|
||||
if kind == "listing_pair":
|
||||
offered, requested = left[0], right[0]
|
||||
elif left[1] == "my" and right[1] != "my":
|
||||
offered, requested = left[0], right[0]
|
||||
elif right[1] == "my" and left[1] != "my":
|
||||
offered, requested = right[0], left[0]
|
||||
elif set(left[0]) <= inventory and not set(right[0]) <= inventory:
|
||||
offered, requested = left[0], right[0]
|
||||
elif set(right[0]) <= inventory and not set(left[0]) <= inventory:
|
||||
offered, requested = right[0], left[0]
|
||||
else:
|
||||
continue
|
||||
if not set(offered) <= inventory or set(offered) & set(requested):
|
||||
continue
|
||||
key = (author, tuple(sorted(offered.items())), tuple(sorted(requested.items())))
|
||||
if key in seen:
|
||||
continue
|
||||
seen.add(key)
|
||||
quotes.append({"source_row": row_number, "quote_type": kind,
|
||||
"offered": dict(offered), "requested": dict(requested),
|
||||
"source_text": source_text})
|
||||
return quotes
|
||||
Reference in New Issue
Block a user