File size: 7,058 Bytes
88d2f2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Classify each Polymarket question into one of six framing patterns.

The patterns are matched in priority order β€” the first regex that fires
wins. Order matters because, for example, ``"How many X by Y?"`` (P6)
also matches ``"Will X by Y?"`` only superficially, so we test P6 first.

Pattern legend:

    P1 "Will X by [date]?"            β€” classic deadline-bounded YES/NO
    P2 noun-phrase multi-outcome      β€” "Next president?", "Winner of X?"
                                        (lacks a verb-leading "Will" but
                                        ends with a question mark)
    P3 "[Asset] above [threshold]"    β€” price/threshold questions
    P4 "Who will be the next X?"      β€” open-ended "who" questions
    P5 "Will X happen between A and B?" β€” window-bounded YES/NO
    P6 "How many X by [date]?"         β€” count-by-date questions
"""
from __future__ import annotations

import argparse
import logging
import re
from collections import Counter
from dataclasses import dataclass
from pathlib import Path
from typing import Iterable, Optional

import pandas as pd

LOGGER = logging.getLogger(__name__)

PATTERN_LABELS = ("P1", "P2", "P3", "P4", "P5", "P6", "OTHER")

EXPECTED_DISTRIBUTION = {
    "P1": 0.45,
    "P2": 0.20,
    "P3": 0.15,
    "P4": 0.10,
    "P5": 0.10,
    "P6": 0.05,
}

# Compiled regexes β€” keep the order in sync with `classify_pattern`.
_RE_P6_HOW_MANY = re.compile(
    r"^\s*how\s+many\b.*?\bby\b.*\?\s*$", re.IGNORECASE
)
_RE_P5_BETWEEN = re.compile(
    r"^\s*will\b.*\bbetween\b.*\band\b.*\?\s*$", re.IGNORECASE
)
_RE_P4_WHO_NEXT = re.compile(
    r"^\s*who\s+(?:will\s+)?(?:be|win|become)\b.*\bnext\b.*\?\s*$",
    re.IGNORECASE,
)
_RE_P4_WHO_FALLBACK = re.compile(r"^\s*who\b.*\?\s*$", re.IGNORECASE)
_RE_P3_THRESHOLD = re.compile(
    r"(above|below|reach|exceed|hit|cross|over|under|"
    r"greater than|less than|>=?|<=?)\s*\$?\d",
    re.IGNORECASE,
)
_RE_P1_WILL_BY = re.compile(
    r"^\s*will\b.*\b(by|before|on|in|prior to|by the end of)\b.*\?\s*$",
    re.IGNORECASE,
)
_RE_P1_WILL_GENERIC = re.compile(r"^\s*will\b.*\?\s*$", re.IGNORECASE)
_RE_QUESTION_MARK = re.compile(r"\?\s*$")


@dataclass(frozen=True)
class PatternStats:
    """Counts + percentages for the corpus-wide pattern distribution."""

    counts: dict[str, int]
    total: int

    def percentages(self) -> dict[str, float]:
        if self.total == 0:
            return {label: 0.0 for label in PATTERN_LABELS}
        return {
            label: 100.0 * self.counts.get(label, 0) / self.total
            for label in PATTERN_LABELS
        }


def classify_pattern(question: str) -> str:
    """Return the label of the first pattern that matches the question."""

    if not question:
        return "OTHER"

    # Order matters β€” P6 first because it can be mistaken for P1.
    if _RE_P6_HOW_MANY.search(question):
        return "P6"
    if _RE_P5_BETWEEN.search(question):
        return "P5"
    if _RE_P4_WHO_NEXT.search(question):
        return "P4"
    if _RE_P3_THRESHOLD.search(question):
        return "P3"
    if _RE_P1_WILL_BY.search(question):
        return "P1"
    if _RE_P1_WILL_GENERIC.search(question):
        return "P1"
    if _RE_P4_WHO_FALLBACK.search(question):
        return "P4"
    if _RE_QUESTION_MARK.search(question):
        # Noun-phrase / leftover question with no leading verb -> P2.
        return "P2"
    return "OTHER"


def classify_dataframe(
    df: pd.DataFrame, *, question_col: str = "question"
) -> pd.Series:
    """Add a series of pattern labels for every row of ``df``."""

    return df[question_col].astype(str).map(classify_pattern)


def summarize_patterns(labels: Iterable[str]) -> PatternStats:
    counter: Counter[str] = Counter(labels)
    total = sum(counter.values())
    counts = {label: counter.get(label, 0) for label in PATTERN_LABELS}
    return PatternStats(counts=counts, total=total)


def stats_to_report(stats: PatternStats) -> str:
    """Render a Markdown summary contrasting actual and expected shares."""

    pcts = stats.percentages()
    lines = [
        "# Polymarket Question Framing Patterns",
        "",
        f"Sample size: **{stats.total}** binary questions.",
        "",
        "| Pattern | Description | Actual % | Expected % | Delta |",
        "|---|---|---|---|---|",
    ]
    descriptions = {
        "P1": "Will X by [date]?",
        "P2": "Noun-phrase multi-outcome",
        "P3": "[Asset] above [threshold]",
        "P4": "Who will be the next X?",
        "P5": "Will X happen between [start] and [end]?",
        "P6": "How many X by [date]?",
        "OTHER": "Unclassified",
    }
    for label in PATTERN_LABELS:
        actual = pcts.get(label, 0.0)
        expected = EXPECTED_DISTRIBUTION.get(label)
        if expected is None:
            expected_str = "β€”"
            delta_str = "β€”"
        else:
            expected_pct = expected * 100
            expected_str = f"{expected_pct:.1f}%"
            delta_str = f"{actual - expected_pct:+.1f} pp"
        lines.append(
            f"| {label} | {descriptions[label]} | "
            f"{actual:.1f}% | {expected_str} | {delta_str} |"
        )
    lines.extend(
        [
            "",
            "## Notes",
            "- Classification is regex-based; ties resolve to the highest-priority "
            "pattern (P6 > P5 > P4 > P3 > P1 > P2).",
            "- `OTHER` captures malformed or non-question rows (typos, missing "
            "trailing `?`, etc.).",
            "- Threshold detection (P3) requires a numeric literal after a "
            "comparison word so we don't mislabel ordinary `will-by-date` "
            "questions that mention prices in passing.",
        ]
    )
    return "\n".join(lines) + "\n"


# --------------------------------------------------------------------------- #
# CLI.                                                                        #
# --------------------------------------------------------------------------- #


def _parse_args(argv: Optional[list[str]] = None) -> argparse.Namespace:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "--parquet", default="corpus/polymarket_questions.parquet"
    )
    parser.add_argument("--out", default="corpus/patterns_report.md")
    parser.add_argument(
        "--log-level", default="INFO", choices=("DEBUG", "INFO", "WARNING")
    )
    return parser.parse_args(argv)


def main(argv: Optional[list[str]] = None) -> int:
    args = _parse_args(argv)
    logging.basicConfig(
        level=args.log_level,
        format="%(asctime)s %(levelname)s %(name)s %(message)s",
    )
    df = pd.read_parquet(args.parquet)
    labels = classify_dataframe(df)
    stats = summarize_patterns(labels)
    report = stats_to_report(stats)
    out_path = Path(args.out)
    out_path.parent.mkdir(parents=True, exist_ok=True)
    out_path.write_text(report)
    LOGGER.info("wrote pattern report -> %s", out_path)
    return 0


if __name__ == "__main__":  # pragma: no cover
    raise SystemExit(main())