Skip to content

Commit

Permalink
No more empty similarity
Browse files Browse the repository at this point in the history
  • Loading branch information
ishefi committed Apr 17, 2024
1 parent 3075b22 commit a8f3f18
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion common/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class UserHistory(SQLModel, table=True):
id: int = Field(default=None, primary_key=True)
user_id: int = Field(foreign_key="user.id", index=True)
guess: str = HebrewString(32)
similarity: float | None
similarity: float
distance: int
egg: str | None = NoFinalHebrewString(default=None)
game_date: datetime.date
Expand Down
4 changes: 2 additions & 2 deletions logic/game_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ async def get_secret_vector(self) -> np_float_arr:
self._secret_cache[self.date] = vector
return self._secret_cache[self.date]

async def get_similarity(self, word: str) -> float | None:
async def get_similarity(self, word: str) -> float:
word_vector = await self.get_vector(word)
if word_vector is None:
return None
raise HSError("Word not found", code=100796)
secret_vector = await self.get_secret_vector()
return await self.calc_similarity(secret_vector, word_vector)

Expand Down
22 changes: 10 additions & 12 deletions routers/game_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
async def distance(
request: Request,
word: str = Query(default=..., min_length=2, max_length=24, regex=r"^[א-ת ']+$"),
) -> schemas.DistanceResponse | list[schemas.DistanceResponse]:
) -> list[schemas.DistanceResponse]:
word = word.replace("'", "")
if egg := EasterEggLogic.get_easter_egg(word):
response = schemas.DistanceResponse(
Expand All @@ -42,17 +42,15 @@ async def distance(
distance=cache_score,
solver_count=solver_count,
)
if request.headers.get("x-sh-version", "2022-02-20") >= "2023-09-10":
if request.state.user:
history_logic = UserHistoryLogic(
request.app.state.session,
request.state.user,
get_date(request.app.state.days_delta),
)
return await history_logic.update_and_get_history(response)
else:
return [response]
return response
if request.state.user:
history_logic = UserHistoryLogic(
request.app.state.session,
request.state.user,
get_date(request.app.state.days_delta),
)
return await history_logic.update_and_get_history(response)
else:
return [response]


@game_router.get("/api/clue")
Expand Down
7 changes: 5 additions & 2 deletions scripts/semantle.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import sys
from datetime import datetime

from common.error import HSError

base = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
sys.path.extend([base])

Expand Down Expand Up @@ -33,8 +35,9 @@ async def main() -> None:
)
inp = input(">")
print(inp[::-1])
similarity = await logic.get_similarity(inp)
if similarity is None or similarity < 0:
try:
similarity = await logic.get_similarity(inp)
except HSError:
print("I don't know this word!")
else:
cache_score = await cache_logic.get_cache_score(inp)
Expand Down

0 comments on commit a8f3f18

Please sign in to comment.