WordGames / app.py
awacke1's picture
Update app.py
e14e2c9
import streamlit as st
import nltk
import requests
import lxml
import lxml.etree as xml
from nltk.corpus import words
from bs4 import BeautifulSoup as soup
st.markdown("🐲 Word Games πŸ‰ - Enter two to seven letters to get a high score of as many words as you can of that length. Leave a letter blank to confine word search to a smaller number of letters.")
st.markdown("Letters in Descending Frequency and Proportion in Words")
Vowels = "E, A, I, O, U, Y"
Consonants = "R, T, N, S, L, C, D, P, M, H, G, B, F, W, K, V, X, Z, J, Q"
st.write("Vowels: ", Vowels)
st.write("Consonants: ", Consonants)
@st.cache # cache download processes so they only execute once with same inputs
def download():
nltk.download('words')
download()
[a,b,c,d,e,f,g] = st.columns(7)
with a:
first_letter = st.text_input(label="1st",value = 'B').lower()
with b:
second_letter = st.text_input(label="2nd", value = 'A').lower()
with c:
third_letter = st.text_input(label="3rd", value = 'Y').lower()
with d:
fourth_letter = st.text_input(label="4th", value = 'O').lower()
with e:
fifth_letter = st.text_input(label="5th", value = 'U').lower()
with f:
sixth_letter = st.text_input(label="6th", value = '').lower()
with g:
seventh_letter = st.text_input(label="7th", value = '').lower()
clue2 = first_letter+second_letter
clue3 = first_letter+second_letter+third_letter
clue4 = first_letter+second_letter+third_letter+fourth_letter
clue5 = first_letter+second_letter+third_letter+fourth_letter+fifth_letter
clue6 = first_letter+second_letter+third_letter+fourth_letter+fifth_letter+sixth_letter
clue7 = first_letter+second_letter+third_letter+fourth_letter+fifth_letter+sixth_letter+seventh_letter
exclusions = st.text_input(label="exclusions", value = 'ERTHSINTHVCDL')
# Remove remaining choices after exclusions
for character in exclusions:
Vowels = Vowels.replace(character,'')
Consonants = Consonants.replace(character,'')
Vowels = Vowels.replace(' ', '').replace(',','')
Consonants = Consonants.replace(' ', '').replace(',','')
st.write("Vowels Remaining = ", (Vowels))
st.write("Consonants Remaining = ", (Consonants))
clue_result2 = []
clue_result3 = []
clue_result4 = []
clue_result5 = []
clue_result6 = []
clue_result7 = []
two_letters = [word for word in words.words() if len(word)==2 ]
three_letters = [word for word in words.words() if len(word)==3 ]
four_letters = [word for word in words.words() if len(word)==4 ]
five_letters = [word for word in words.words() if len(word)==5 ]
six_letters = [word for word in words.words() if len(word)==6 ]
seven_letters = [word for word in words.words() if len(word)==7 ]
#score
score2=0
score3=0
score4=0
score5=0
score6=0
score7=0
for word in two_letters:
if all(c in word for c in clue2) and not any(c in word for c in exclusions):
score2+=1
clue_result2.append(word)
for word in three_letters:
if all(c in word for c in clue3) and not any(c in word for c in exclusions):
score3+=1
clue_result3.append(word)
for word in four_letters:
if all(c in word for c in clue4) and not any(c in word for c in exclusions):
score4+=1
clue_result4.append(word)
for word in five_letters:
if all(c in word for c in clue5) and not any(c in word for c in exclusions):
score5+=1
clue_result5.append(word)
for word in six_letters:
if all(c in word for c in clue6) and not any(c in word for c in exclusions):
score6+=1
clue_result6.append(word)
for word in seven_letters:
if all(c in word for c in clue7) and not any(c in word for c in exclusions):
score7+=1
clue_result7.append(word)
tscore2=score2 * 2
tscore3=score3 * 4
tscore4=score4 * 8
tscore5=score5 * 16
tscore6=score6 * 32
tscore7=score7 * 64
totalScore =tscore2 + tscore3 + tscore4 + tscore5 + tscore6 + tscore7
st.markdown("### Words and Scores for Selected Letters")
st.write("Total Score = ", (totalScore))
st.write("Score for 7 letter words = ", (tscore7))
st.write("Words for ",clue7, clue_result7)
st.write("Score for 6 letter words = ", (tscore6))
st.write("Words for ",clue6, clue_result6)
st.write("Score for 5 letter words = ", (tscore5))
st.write("Words for ",clue5, clue_result5)
st.write("Score for 4 letter words = ", (tscore4))
st.write("Words for ",clue4, clue_result4)
st.write("Score for 3 letter words = ", (tscore3))
st.write("Words for ",clue3, clue_result3)
st.write("Score for 2 letter words = ", (tscore2))
st.write("Words for ",clue2, clue_result2)
@st.cache # cache download processes so they only execute once with same inputs
def define(word):
url = f'https://www.dictionary.com/browse/{word}?s=t'
r=requests.get(url)
s = soup(r.content, features="lxml")
for tag in s.find_all("meta"):
if tag.get("property", None) == "og:description":
content = tag.get("content", None)
return content
for word in clue_result7:
content = define(word)
st.write(content)