From 67d6e4322f6daaf759c9c856ab419f0083116048 Mon Sep 17 00:00:00 2001 From: julius Date: Thu, 23 Nov 2023 13:41:02 +0100 Subject: [PATCH] include optional verbs --- main.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/main.py b/main.py index c7eff586..439fbe66 100644 --- a/main.py +++ b/main.py @@ -25,11 +25,11 @@ app.add_middleware( ) -@app.get("/allwords/", response_model=list[str]) -def words(): - with Session(engine) as session: - results = session.exec(select(Word.word)).all() - return results +# @app.get("/allwords/", response_model=list[str]) +# def words(): +# with Session(engine) as session: +# results = session.exec(select(Word.word)).all() +# return results @app.get("/randomword/", response_model=str) @@ -48,11 +48,21 @@ def random_char(): @app.get("/phrases/", response_model=list[str]) def phrases(n: int = 4, nouns: int = 1, adjs: int = 2, pw: bool = False): n, nouns, adjs = map(abs, [n, nouns, adjs]) - nouns = nouns if nouns <= 4 else 16 - adjs = adjs if adjs <= 4 else 16 + verbs = verbs if verbs <= 4 else 4 + adjs = adjs if adjs <= 4 else 4 + nouns = nouns if nouns <= 4 else 4 n = n if n <= 200 else 200 with Session(engine) as session: + stmt = ( + select(Sense.word) + .where(Sense.word_class == "verb") + .where(text("sense.word !~ '\s|-'")) + .order_by(text("random()")) + .limit(n * adjs) + ) + l_verbs = session.exec(stmt).all() + stmt = ( select(Sense.word) .where(Sense.word_class == "adjective") @@ -71,7 +81,8 @@ def phrases(n: int = 4, nouns: int = 1, adjs: int = 2, pw: bool = False): ) l_nouns = session.exec(stmt).all() phrases = [ - l_adjectives[i * adjs : (i + 1) * adjs] + l_verbs[i * verbs : (i + 1) * verbs] + + l_adjectives[i * adjs : (i + 1) * adjs] + l_nouns[i * nouns : (i + 1) * nouns] for i in range(n) ]