neoai-kterasawa commited on
Commit
3fcb62a
·
1 Parent(s): 9e16e32
Files changed (3) hide show
  1. app.py +5 -2
  2. src/func.py +0 -4
  3. src/model/odds.py +42 -35
app.py CHANGED
@@ -18,7 +18,10 @@ def calculate(budget: int, text: str) -> tuple[str, list[list]]:
18
  raise gr.Error(e)
19
 
20
 
21
- with gr.Blocks() as demo:
 
 
 
22
  with gr.Row():
23
  with gr.Column():
24
  budget_input = gr.Number(value=DEFAULT_AMOUNT, label="予算")
@@ -37,7 +40,7 @@ with gr.Blocks() as demo:
37
  col_count=(5, "fixed"),
38
  )
39
 
40
- # click
41
  calculate_button.click(
42
  calculate,
43
  inputs=[budget_input, text_input],
 
18
  raise gr.Error(e)
19
 
20
 
21
+ with gr.Blocks(
22
+ theme=gr.themes.Default(primary_hue="green", secondary_hue="lime")
23
+ ) as demo:
24
+ # flontend ------------------------------
25
  with gr.Row():
26
  with gr.Column():
27
  budget_input = gr.Number(value=DEFAULT_AMOUNT, label="予算")
 
40
  col_count=(5, "fixed"),
41
  )
42
 
43
+ # click ------------------------------
44
  calculate_button.click(
45
  calculate,
46
  inputs=[budget_input, text_input],
src/func.py CHANGED
@@ -1,9 +1,5 @@
1
- from typing import Final
2
-
3
  from src.model.odds import Choices
4
 
5
- DEFAULT_AMOUNT: Final[int] = 10_000
6
-
7
 
8
  def calculate(budget: int, raw_text: str) -> tuple[str, list[list]]:
9
  choices = Choices.from_text(raw_text)
 
 
 
1
  from src.model.odds import Choices
2
 
 
 
3
 
4
  def calculate(budget: int, raw_text: str) -> tuple[str, list[list]]:
5
  choices = Choices.from_text(raw_text)
src/model/odds.py CHANGED
@@ -1,4 +1,5 @@
1
  import re
 
2
 
3
  from pydantic import (
4
  BaseModel,
@@ -10,10 +11,12 @@ from pydantic import (
10
  field_validator,
11
  )
12
 
 
 
13
 
14
  class Choice(BaseModel):
15
  number: StrictStr
16
- name: StrictStr
17
  odds: StrictFloat
18
 
19
 
@@ -44,11 +47,9 @@ class Choices(RootModel[list[Choice]]):
44
  def to_choices_with_bet(self, budget: int) -> "ChoicesWithBet":
45
  inverse_ratios_sum = sum(1 / choice.odds for choice in self.root)
46
 
47
- # 重み計算
48
  choice_with_bet_list: list[ChoiceWithBet] = []
49
  for chice in self.root:
50
  weight = (1 / chice.odds) / inverse_ratios_sum
51
- # 逆比
52
  choice_with_bet_list.append(
53
  ChoiceWithBet(
54
  number=chice.number,
@@ -62,62 +63,68 @@ class Choices(RootModel[list[Choice]]):
62
 
63
  @classmethod
64
  def from_text(cls, text: str) -> "Choices":
 
 
 
 
 
 
 
 
65
  lines = text.split("\n")
66
- for strat_key in ("選択", "オッズ", "組み合わせ", "オッズを見て個別選択"):
67
- if strat_key in lines:
68
- lines = lines[lines.index(strat_key) + 1 :]
69
- text = "\n".join(lines)
 
 
 
 
 
 
70
  # pcの時
71
- is_pc = re.match(r"[\d\n]+", text)
72
  if is_pc:
73
- return cls._from_text_from_pc(text)
74
  # moduleの時
75
- return cls._from_text_from_mobile(text)
76
 
77
  @classmethod
78
- def _from_text_from_pc(cls, text: str) -> "Choices":
79
  choices_list: list[Choice] = []
80
- tmp_list: list[str] = []
81
- lines: list[str] = text.split("\n")
82
  for line in lines:
83
  is_odds = "." in line
84
  if is_odds:
85
  choices_list.append(
86
  Choice(
87
- number="-".join(tmp_list),
88
- name="",
89
  odds=float(line),
90
  )
91
  )
92
- tmp_list = []
93
  else:
94
- if line.strip():
95
- tmp_list.append(line.strip())
96
  return cls(root=choices_list)
97
 
98
  @classmethod
99
- def _from_text_from_mobile(cls, text: str) -> "Choices":
100
  choice_list: list[Choice] = []
101
- lines: list[str] = text.split("\n")
102
- for strat_key in ("選択", "オッズ", "組み合わせ", "オッズを見て個別選択"):
103
- if strat_key in lines:
104
- lines = lines[lines.index(strat_key) + 1 :]
105
- if "すべてを選択" in lines:
106
- lines = lines[: lines.index("すべてを選択")]
107
  for i in range(len(lines) // 3):
108
- # oddsが1つ目にある
109
- if "." in lines[3 * i + 1]:
110
- idx_odds = 1
111
- idx_name = 2
112
- else:
113
- idx_odds = 2
114
- idx_name = 1
115
 
116
  choice_list.append(
117
  Choice(
118
- number="-".join(lines[3 * i].split()),
119
- name="-".join(lines[3 * i + idx_name].split()),
120
- odds=float(lines[3 * i + idx_odds]),
121
  )
122
  )
123
  return cls(root=choice_list)
 
1
  import re
2
+ from typing import Final
3
 
4
  from pydantic import (
5
  BaseModel,
 
11
  field_validator,
12
  )
13
 
14
+ NUMBER_JOINER: Final[str] = "-"
15
+
16
 
17
  class Choice(BaseModel):
18
  number: StrictStr
19
+ name: StrictStr = ""
20
  odds: StrictFloat
21
 
22
 
 
47
  def to_choices_with_bet(self, budget: int) -> "ChoicesWithBet":
48
  inverse_ratios_sum = sum(1 / choice.odds for choice in self.root)
49
 
 
50
  choice_with_bet_list: list[ChoiceWithBet] = []
51
  for chice in self.root:
52
  weight = (1 / chice.odds) / inverse_ratios_sum
 
53
  choice_with_bet_list.append(
54
  ChoiceWithBet(
55
  number=chice.number,
 
63
 
64
  @classmethod
65
  def from_text(cls, text: str) -> "Choices":
66
+ FIRST_KEYS: Final[list[str]] = [
67
+ "選択",
68
+ "オッズ",
69
+ "組み合わせ",
70
+ "オッズを見て個別選択",
71
+ ]
72
+ LAST_KEYS: Final[list[str]] = ["すべてを選択"]
73
+
74
  lines = text.split("\n")
75
+ lines = [line.strip() for line in lines]
76
+
77
+ # 余計な行を削除 ----------------------------------------
78
+ for first_key in FIRST_KEYS:
79
+ if first_key in lines:
80
+ lines = lines[lines.index(first_key) + 1 :]
81
+ for last_key in LAST_KEYS:
82
+ if last_key in lines:
83
+ lines = lines[: lines.index(last_key)]
84
+
85
  # pcの時
86
+ is_pc = re.match(r"[\d\s]+", "".join(lines))
87
  if is_pc:
88
+ return cls._from_lines_from_pc(lines)
89
  # moduleの時
90
+ return cls._from_lines_from_mobile(lines)
91
 
92
  @classmethod
93
+ def _from_lines_from_pc(cls, lines: list[str]) -> "Choices":
94
  choices_list: list[Choice] = []
95
+ tmp_numbers: list[str] = []
 
96
  for line in lines:
97
  is_odds = "." in line
98
  if is_odds:
99
  choices_list.append(
100
  Choice(
101
+ number=NUMBER_JOINER.join(tmp_numbers),
 
102
  odds=float(line),
103
  )
104
  )
105
+ tmp_numbers = []
106
  else:
107
+ if line:
108
+ tmp_numbers.append(line)
109
  return cls(root=choices_list)
110
 
111
  @classmethod
112
+ def _from_lines_from_mobile(cls, lines: list[str]) -> "Choices":
113
  choice_list: list[Choice] = []
 
 
 
 
 
 
114
  for i in range(len(lines) // 3):
115
+ idx_number = 3 * i
116
+ if "." in lines[3 * i + 1]: # oddsが2つ目にある
117
+ idx_odds = 3 * i + 1
118
+ idx_name = 3 * i + 2
119
+ else: # oddsが3つ目にある
120
+ idx_odds = 3 * i + 2
121
+ idx_name = 3 * i + 1
122
 
123
  choice_list.append(
124
  Choice(
125
+ number=NUMBER_JOINER.join(lines[idx_number].split()),
126
+ name=NUMBER_JOINER.join(lines[idx_name].split()),
127
+ odds=float(lines[idx_odds]),
128
  )
129
  )
130
  return cls(root=choice_list)