include optional verbs

This commit is contained in:
julius 2023-11-23 13:41:02 +01:00
parent b8b8e42b8e
commit 67d6e4322f
Signed by untrusted user who does not match committer: julius
GPG Key ID: 8AA3791362A8084A

27
main.py
View File

@ -25,11 +25,11 @@ app.add_middleware(
) )
@app.get("/allwords/", response_model=list[str]) # @app.get("/allwords/", response_model=list[str])
def words(): # def words():
with Session(engine) as session: # with Session(engine) as session:
results = session.exec(select(Word.word)).all() # results = session.exec(select(Word.word)).all()
return results # return results
@app.get("/randomword/", response_model=str) @app.get("/randomword/", response_model=str)
@ -48,11 +48,21 @@ def random_char():
@app.get("/phrases/", response_model=list[str]) @app.get("/phrases/", response_model=list[str])
def phrases(n: int = 4, nouns: int = 1, adjs: int = 2, pw: bool = False): def phrases(n: int = 4, nouns: int = 1, adjs: int = 2, pw: bool = False):
n, nouns, adjs = map(abs, [n, nouns, adjs]) n, nouns, adjs = map(abs, [n, nouns, adjs])
nouns = nouns if nouns <= 4 else 16 verbs = verbs if verbs <= 4 else 4
adjs = adjs if adjs <= 4 else 16 adjs = adjs if adjs <= 4 else 4
nouns = nouns if nouns <= 4 else 4
n = n if n <= 200 else 200 n = n if n <= 200 else 200
with Session(engine) as session: 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 = ( stmt = (
select(Sense.word) select(Sense.word)
.where(Sense.word_class == "adjective") .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() l_nouns = session.exec(stmt).all()
phrases = [ 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] + l_nouns[i * nouns : (i + 1) * nouns]
for i in range(n) for i in range(n)
] ]