Codex commited on
Commit
176f0fc
·
1 Parent(s): 83088ec

Fix cramped Circa binary market lanes

Browse files
Files changed (2) hide show
  1. src/market-scanner.js +123 -31
  2. test/market-scanner.test.js +15 -0
src/market-scanner.js CHANGED
@@ -879,6 +879,7 @@ function extractBinaryYesNoEntriesV2(line, section) {
879
  function extractOddsTokensFromSegment(segment) {
880
  const cleaned = normalizeWhitespace(segment)
881
  .toUpperCase()
 
882
  .replace(/\//g, ' ')
883
  .replace(/\s+/g, ' ');
884
  const tokens = [...cleaned.matchAll(/(?:\d{4,6}|[0-9BOSI]{4,6})\s+([+\-~]?[0-9BOSI]{2,4})/g)]
@@ -891,6 +892,90 @@ function extractOddsTokensFromSegment(segment) {
891
  return [...cleaned.matchAll(/([+\-~][0-9BOSI]{2,4})/g)].map((match) => match[1]);
892
  }
893
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
894
  function extractBinaryYesNoEntriesV3(line, section) {
895
  const entries = [];
896
  const normalizedLine = normalizeWhitespace(line)
@@ -919,37 +1004,13 @@ function extractBinaryYesNoEntriesV3(line, section) {
919
  const yesOdds = normalizeOcrOddsTokenForSide(oddsTokens[0], 'yes');
920
  const noOdds = normalizeOcrOddsTokenForSide(oddsTokens[1], 'no');
921
 
922
- for (const sideEntry of [
923
- { side: section.side ?? 'yes', oddsInput: yesOdds },
924
- { side: 'no', oddsInput: noOdds },
925
- ]) {
926
- if (!sideEntry.oddsInput || !isReasonableCircaOdds(sideEntry.oddsInput, section)) {
927
- continue;
928
- }
929
-
930
- const impliedProbability = americanToImpliedProbability(sideEntry.oddsInput);
931
- if (impliedProbability === null) {
932
- continue;
933
- }
934
-
935
- const entry = {
936
- source: 'circa',
937
- book: 'Circa',
938
- eventName: 'Circa MLB',
939
- eventId: null,
940
- team,
941
- playerName,
942
- playerKey: normalizePlayerName(playerName),
943
- marketType: section.type,
944
- marketLabel: section.label,
945
- side: sideEntry.side,
946
- lineValue: section.lineValue,
947
- oddsInput: sideEntry.oddsInput,
948
- impliedProbability,
949
- rawLabel: segment,
950
- };
951
- entry.marketKey = buildMarketKey(entry);
952
- entries.push(entry);
953
  }
954
  }
955
 
@@ -1160,9 +1221,40 @@ export function parseCircaOcrText(text) {
1160
  .filter((line) => line.trim().length > 0);
1161
 
1162
  const columnSections = new Map();
 
1163
  const overUnderStates = new Map();
1164
 
1165
  for (const rawLine of rawLines) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1166
  const segments = splitCircaLayoutColumns(rawLine);
1167
  if (segments.length === 0) {
1168
  continue;
 
879
  function extractOddsTokensFromSegment(segment) {
880
  const cleaned = normalizeWhitespace(segment)
881
  .toUpperCase()
882
+ .replace(/([+\-~][0-9BOSI]{3})(\d{4,6})/g, '$1 $2')
883
  .replace(/\//g, ' ')
884
  .replace(/\s+/g, ' ');
885
  const tokens = [...cleaned.matchAll(/(?:\d{4,6}|[0-9BOSI]{4,6})\s+([+\-~]?[0-9BOSI]{2,4})/g)]
 
892
  return [...cleaned.matchAll(/([+\-~][0-9BOSI]{2,4})/g)].map((match) => match[1]);
893
  }
894
 
895
+ function buildCircaBinaryEntry(playerName, team, section, side, oddsInput, rawLabel) {
896
+ if (!playerName || playerName.length < 3 || !oddsInput || !isReasonableCircaOdds(oddsInput, section)) {
897
+ return null;
898
+ }
899
+
900
+ const impliedProbability = americanToImpliedProbability(oddsInput);
901
+ if (impliedProbability === null) {
902
+ return null;
903
+ }
904
+
905
+ const entry = {
906
+ source: 'circa',
907
+ book: 'Circa',
908
+ eventName: 'Circa MLB',
909
+ eventId: null,
910
+ team,
911
+ playerName,
912
+ playerKey: normalizePlayerName(playerName),
913
+ marketType: section.type,
914
+ marketLabel: section.label,
915
+ side,
916
+ lineValue: section.lineValue,
917
+ oddsInput,
918
+ impliedProbability,
919
+ rawLabel,
920
+ };
921
+ entry.marketKey = buildMarketKey(entry);
922
+ return entry;
923
+ }
924
+
925
+ function extractCombinedBinaryLaneEntries(line, sections) {
926
+ const normalizedLine = normalizeWhitespace(line)
927
+ .toUpperCase()
928
+ .replace(/\//g, ' ')
929
+ .replace(/\s+/g, ' ');
930
+ const playerPattern = /([A-Z][A-Z.'\- ]{2,40}\([A-Z]{2,3}\))/g;
931
+ const playerMatches = [...normalizedLine.matchAll(playerPattern)];
932
+
933
+ if (playerMatches.length < sections.length) {
934
+ return [];
935
+ }
936
+
937
+ const entries = [];
938
+ for (let index = 0; index < sections.length; index += 1) {
939
+ const current = playerMatches[index];
940
+ const next = playerMatches[index + 1];
941
+ const segmentStart = current.index ?? 0;
942
+ const segmentEnd = next?.index ?? normalizedLine.length;
943
+ const segment = normalizedLine.slice(segmentStart, segmentEnd);
944
+ const { playerName, team } = extractPlayerAndTeam(current[1]);
945
+ const oddsTokens = extractOddsTokensFromSegment(segment);
946
+ if (oddsTokens.length < 2) {
947
+ continue;
948
+ }
949
+
950
+ const section = sections[index];
951
+ const yesEntry = buildCircaBinaryEntry(
952
+ playerName,
953
+ team,
954
+ section,
955
+ section.side ?? 'yes',
956
+ normalizeOcrOddsTokenForSide(oddsTokens[0], 'yes'),
957
+ segment,
958
+ );
959
+ const noEntry = buildCircaBinaryEntry(
960
+ playerName,
961
+ team,
962
+ section,
963
+ 'no',
964
+ normalizeOcrOddsTokenForSide(oddsTokens[1], 'no'),
965
+ segment,
966
+ );
967
+
968
+ if (yesEntry) {
969
+ entries.push(yesEntry);
970
+ }
971
+ if (noEntry) {
972
+ entries.push(noEntry);
973
+ }
974
+ }
975
+
976
+ return entries;
977
+ }
978
+
979
  function extractBinaryYesNoEntriesV3(line, section) {
980
  const entries = [];
981
  const normalizedLine = normalizeWhitespace(line)
 
1004
  const yesOdds = normalizeOcrOddsTokenForSide(oddsTokens[0], 'yes');
1005
  const noOdds = normalizeOcrOddsTokenForSide(oddsTokens[1], 'no');
1006
 
1007
+ const yesEntry = buildCircaBinaryEntry(playerName, team, section, section.side ?? 'yes', yesOdds, segment);
1008
+ const noEntry = buildCircaBinaryEntry(playerName, team, section, 'no', noOdds, segment);
1009
+ if (yesEntry) {
1010
+ entries.push(yesEntry);
1011
+ }
1012
+ if (noEntry) {
1013
+ entries.push(noEntry);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1014
  }
1015
  }
1016
 
 
1221
  .filter((line) => line.trim().length > 0);
1222
 
1223
  const columnSections = new Map();
1224
+ let combinedBinarySections = null;
1225
  const overUnderStates = new Map();
1226
 
1227
  for (const rawLine of rawLines) {
1228
+ const normalizedRawLine = normalizeWhitespace(rawLine).toUpperCase();
1229
+ if (
1230
+ normalizedRawLine.includes('RECORD A HIT')
1231
+ && (normalizedRawLine.includes('RECORD AN RBI') || normalizedRawLine.includes('RECORD A RBI'))
1232
+ && normalizedRawLine.includes('SCORE A RUN')
1233
+ ) {
1234
+ combinedBinarySections = [
1235
+ detectCircaSection('RECORD A HIT'),
1236
+ detectCircaSection('RECORD AN RBI'),
1237
+ detectCircaSection('SCORE A RUN'),
1238
+ ].filter(Boolean);
1239
+ columnSections.clear();
1240
+ continue;
1241
+ }
1242
+
1243
+ if (combinedBinarySections?.length) {
1244
+ if (normalizedRawLine.includes('YES NO')) {
1245
+ continue;
1246
+ }
1247
+ if (isLikelySectionBoundary(rawLine)) {
1248
+ combinedBinarySections = null;
1249
+ } else {
1250
+ const combinedEntries = extractCombinedBinaryLaneEntries(rawLine, combinedBinarySections);
1251
+ if (combinedEntries.length > 0) {
1252
+ entries.push(...combinedEntries);
1253
+ continue;
1254
+ }
1255
+ }
1256
+ }
1257
+
1258
  const segments = splitCircaLayoutColumns(rawLine);
1259
  if (segments.length === 0) {
1260
  continue;
test/market-scanner.test.js CHANGED
@@ -129,6 +129,21 @@ test('parses side-by-side binary circa sections without mixing markets', () => {
129
  assert.ok(!entries.some((entry) => entry.marketType === 'hits' && entry.playerName === 'BOBBY WITT JR' && entry.oddsInput === '+185'));
130
  });
131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
  test('filters impossible binary hit prices caused by OCR corruption', () => {
133
  const entries = parseCircaOcrText([
134
  'WILL [PLAYER] RECORD A HIT?',
 
129
  assert.ok(!entries.some((entry) => entry.marketType === 'hits' && entry.playerName === 'BOBBY WITT JR' && entry.oddsInput === '+185'));
130
  });
131
 
132
+ test('parses cramped combined hit-rbi-run rows without mixing neighboring markets', () => {
133
+ const entries = parseCircaOcrText([
134
+ 'WILL [PLAYER] RECORD A HIT? WILL [PLAYER] RECORD AN RBI? WILL [PLAYER] SCORE A RUN?',
135
+ 'YES NO YES NO YES NO',
136
+ 'JAC CAGLIANONE (KC) 85801 -140 85802 +110 BOBBY WITT JR (KC) 86001 +18586002 -230 BOBBY WITT JR (KC) 86201 +120/86202 -150',
137
+ 'JONATHAN INDIA (KC) 85803 -140 85804 +110 VINNIE PASQUANTINO (KC) 86003 +170 86005 -210 SALVADOR PEREZ (KC) 86203 +150 86204 -185',
138
+ 'ANDREW BENINTENDI (CHW) 85805 -165 85806 +135 MIGUEL VARGAS (CHW) 86005 +240 86006 -300 CHASE MEIDROTH (CHW) 86205 +130 86206 -160',
139
+ ].join('\n'));
140
+
141
+ assert.ok(entries.some((entry) => entry.marketType === 'hits' && entry.playerName === 'JAC CAGLIANONE' && entry.oddsInput === '-140'));
142
+ assert.ok(entries.some((entry) => entry.marketType === 'rbis' && entry.playerName === 'BOBBY WITT JR' && entry.oddsInput === '+185'));
143
+ assert.ok(entries.some((entry) => entry.marketType === 'runs' && entry.playerName === 'BOBBY WITT JR' && entry.oddsInput === '+120'));
144
+ assert.ok(!entries.some((entry) => entry.marketType === 'hits' && entry.playerName === 'BOBBY WITT JR' && entry.oddsInput === '+120'));
145
+ });
146
+
147
  test('filters impossible binary hit prices caused by OCR corruption', () => {
148
  const entries = parseCircaOcrText([
149
  'WILL [PLAYER] RECORD A HIT?',