返回 last30days-skill
test_polymarket_entity_match.py
根目录 / tests / test_polymarket_entity_match.py
1 """The entity matcher every Polymarket relevance gate shares.
2
3 A market names the entity when a topic term (or a host alias) appears as a
4 whole phrase in that market's question, its ``groupItemTitle``, one of its
5 non-Yes/No outcome labels, or its event title. Related names (a team's
6 players) count only on the market's own fields and are reported apart.
7 """
8
9 import pytest
10
11 from lib import polymarket
12
13
14 def _market(question="", label="", outcomes='["Yes", "No"]'):
15 return {"question": question, "groupItemTitle": label, "outcomes": outcomes}
16
17
18 NINERS = _market(
19 "Will the San Francisco 49ers win the 2027 NFL league championship?",
20 "San Francisco 49ers",
21 )
22
23
24 def _match(topic, market, title="", **hints):
25 return polymarket.EntityMatcher(topic, polymarket.PolymarketHints.coerce(hints or None)).match(
26 market, title,
27 )
28
29
30 def test_topic_matches_group_item_title_and_question():
31 found = _match("49ers", NINERS, "Pro Football: 2027 Champion")
32 assert found is not None
33 assert found.field == "market"
34 assert found.related is None
35 assert _match("49ers", _market(label="San Francisco 49ers"), "Pro Football: 2027 Champion")
36 assert _match(
37 "49ers",
38 _market("Will the San Francisco 49ers win the 2027 NFL league championship?"),
39 "Pro Football: 2027 Champion",
40 )
41
42
43 def test_a_sibling_market_naming_the_topic_does_not_qualify_another_market():
44 chargers = _market(
45 "Will the Los Angeles Chargers win the 2027 NFL league championship?",
46 "Los Angeles Chargers",
47 )
48 assert _match("49ers", chargers, "Pro Football: 2027 Champion") is None
49
50
51 def test_event_title_match_is_reported_as_a_title_match():
52 found = _match("49ers", _market("8.5+ Wins", "8.5+ Wins"), "Pro Football: 49ers 2026 Win Total")
53 assert found is not None
54 assert found.field == "title"
55
56
57 @pytest.mark.parametrize("topic, market, title", [
58 ("Kanye West", _market("Will the 49ers win the NFC West?", "San Francisco 49ers"),
59 "Pro Football: NFC West Champion"),
60 ("Muse", _market("Will Lorenzo Musetti win the 2026 ATP Finals?", "Lorenzo Musetti"),
61 "ATP Finals Winner"),
62 ("June Oven", _market("Will China invade Taiwan by June 30, 2027?"),
63 "Will China invade Taiwan by June 30, 2027?"),
64 ])
65 def test_single_word_collisions_do_not_match(topic, market, title):
66 assert _match(topic, market, title) is None
67
68
69 @pytest.mark.parametrize("topic, question, title", [
70 ("Nvidia earnings", "Will Nvidia beat quarterly earnings?", "Nvidia quarterly earnings beat?"),
71 ("2028 presidential election", "Will JD Vance win the 2028 US Presidential Election?",
72 "Presidential Election Winner 2028"),
73 ("Sam Altman OpenAI", "Sam Altman CEO OpenAI?", "Sam Altman CEO OpenAI"),
74 ("Fed rate cut", "Will the Fed cut rates in December?", "Fed decision in December"),
75 ("tariffs on China", "Will the US raise its tariff on China?", "US tariff on China"),
76 ])
77 def test_descriptive_topic_matches_with_its_words_in_any_order(topic, question, title):
78 assert _match(topic, _market(question), title)
79
80
81 def test_outcome_label_matches_and_yes_no_labels_do_not():
82 best_model = _market(
83 "Which company has best AI model end of 2026?",
84 outcomes='["Google", "Anthropic", "OpenAI"]',
85 )
86 assert _match("Anthropic", best_model, "Which company has best AI model end of 2026?")
87 assert _match("Yes", _market("Will it rain?"), "Weather") is None
88
89
90 def test_alias_matches_like_the_topic():
91 action = _market("Will Kimi Antonelli win F1 Action of the Year?", "Kimi Antonelli")
92 title = "F1: Action of the Year"
93 assert _match("Formula 1", action, title) is None
94 found = _match("Formula 1", action, title, aliases=["F1"])
95 assert found is not None
96 assert found.related is None
97
98
99 @pytest.mark.parametrize("topic, text", [
100 ("Apple", "Apple's Market Cap end of 2026?"),
101 ("Apple", "Apple’s Market Cap end of 2026?"),
102 ("Apple's", "Will Apple be the largest company?"),
103 ("the Lakers", "Will the Los Angeles Lakers win the 2027 NBA Finals?"),
104 ])
105 def test_possessive_and_leading_article_are_ignored(topic, text):
106 assert _match(topic, _market(text), "Some event")
107
108
109 def test_related_name_matches_only_as_a_related_match():
110 lendeborg = _market(
111 "Will Yaxel Lendeborg win the 2026-27 NBA Rookie of the Year?", "Yaxel Lendeborg",
112 )
113 title = "NBA: 2026-27 Rookie of the Year"
114 assert _match("Warriors", lendeborg, title) is None
115 found = _match(
116 "Warriors", lendeborg, title,
117 kind="team", related=[{"name": "Yaxel Lendeborg", "relation": "player"}],
118 )
119 assert found is not None
120 assert found.related == "Yaxel Lendeborg"
121 assert found.field == "market"
122
123
124 def test_related_name_in_the_event_title_alone_does_not_match():
125 hints = {"related": [{"name": "Stephen Curry", "relation": "player"}]}
126 found = _match("Warriors", _market("Over 29.5 points?", "Over 29.5"),
127 "Stephen Curry points vs Lakers", **hints)
128 assert found is None
129
130
131 def test_entity_match_wins_over_related_match():
132 market = _market("Will the Golden State Warriors and Stephen Curry ...?", "Golden State Warriors")
133 found = _match("Warriors", market, "NBA", related=[{"name": "Stephen Curry"}])
134 assert found is not None
135 assert found.related is None
136
137
138 def test_comparison_topic_matches_either_side():
139 market = _market("Will Anthropic have the best AI model?", "Anthropic")
140 assert _match("OpenAI vs Anthropic", market, "Which company has best AI model end of 2026?")
141
142
143 def test_acronym_title_matches_spelled_out_topic():
144 """Market titles use shorthand ("AGI by 2030?") while topics arrive spelled out."""
145 for title in ("OpenAI announces it has achieved AGI before 2027?", "AGI by 2030?"):
146 assert _match("artificial general intelligence", _market(title), title)
147 assert _match("impact of artificial general intelligence", _market("AGI by 2030?"), "AGI by 2030?")
148
149
150 def test_acronym_credit_does_not_widen_the_match():
151 assert _match("artificial general intelligence", _market("Premier League top scorer 2026"),
152 "Premier League top scorer 2026") is None
153 assert _match("Mill.com food recycler", _market("Meek Mill announces tour"),
154 "Meek Mill announces tour") is None
155 # Two letters are too ambiguous to stand for a spelled-out phrase.
156 assert _match("machine learning", _market("ML market cap above $1 billion?"),
157 "ML market cap above $1 billion?") is None
158
159
160 def test_soft_domain_sweep_matches_its_domain_word():
161 """A sweep topic ("AI frontier developments") is about the domain itself."""
162 title = "Will AI models beat humans at coding?"
163 assert _match("AI frontier developments", _market(title), title)
164 assert _match("MCP protocol benchmark", _market("Will the Kyoto Protocol survive?"),
165 "Will the Kyoto Protocol survive?") is None
166
166 lines PYTHON