File size: 12,493 Bytes
0d0f07a 14c3a4f 51778ca 7b3478d dcc32dc c7d9cf9 554cc8f c7d9cf9 0d0f07a aa5c198 ee625fc aa5c198 cdb0a70 94e7f68 e99824c aa5c198 cdb0a70 e99824c aa5c198 703ce10 0d0f07a 703ce10 0d0f07a c7d9cf9 2605d63 0d0f07a 51778ca ee625fc 7088ca8 ee625fc 317c2f1 cdb0a70 910ea33 ca2c3e1 ee625fc 910ea33 0d0f07a cdb0a70 0d0f07a fcfa1a6 8fb441e fcfa1a6 14c3a4f 0d0f07a cdb0a70 910ea33 14c3a4f 910ea33 8fb441e 910ea33 b24ad56 910ea33 14c3a4f bdf0a5e 0d0f07a ee625fc cdb0a70 ee625fc ca2c3e1 ee625fc bdf0a5e ee625fc cdb0a70 ee625fc cdb0a70 bdf0a5e ee625fc cdb0a70 ee625fc bdf0a5e ee625fc cdb0a70 bdf0a5e 0d0f07a a1d0fa4 ca2c3e1 a1d0fa4 7b3478d aa5c198 7b3478d 0d0f07a eb0e921 7b3478d aa5c198 7b3478d eb0e921 7b3478d eb0e921 7b3478d 0d0f07a 7b3478d 88d7eed 7b3478d 88d7eed 7b3478d 05fa263 7b3478d c7d9cf9 7b3478d 0d0f07a c7d9cf9 0d0f07a a1d0fa4 ca2c3e1 a1d0fa4 94e7f68 dcc32dc 554cc8f a1d0fa4 554cc8f 94e7f68 554cc8f dcc32dc c7d9cf9 554cc8f 46e623d 8e13e1c 2605d63 8e13e1c 2605d63 703ce10 05fa263 703ce10 a1d0fa4 05fa263 a1d0fa4 46e623d c7d9cf9 dcc32dc 0d0f07a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
import streamlit as st
from streamlit_option_menu import option_menu
from word2vec import *
import pandas as pd
from autocomplete import *
from vector_graph import *
from plots import *
from lsj_dict import *
import json
from streamlit_tags import st_tags, st_tags_sidebar
st.set_page_config(page_title="Ancient Greek Word2Vec", layout="centered")
# Cache data
@st.cache_data
def load_lsj_dict():
return json.load(open('lsj_dict.json', 'r'))
@st.cache_data
def load_all_models_words():
return sorted(load_compressed_word_list('corpora/compass_filtered.pkl.gz'), key=custom_sort)
@st.cache_data
def load_models_for_word_dict():
return word_in_models_dict('corpora/compass_filtered.pkl.gz')
@st.cache_data
def load_all_lemmas():
return load_compressed_word_list('all_lemmas.pkl.gz')
@st.cache_data
def load_lemma_count_dict():
return count_lemmas('lemma_list_raw')
# Load compressed word list
all_models_words = load_all_models_words()
# Prepare lsj dictionary
lemma_dict = load_lsj_dict()
# Load dictionary with words as keys and eligible models as values
models_for_word_dict = load_models_for_word_dict()
lemma_counts = load_lemma_count_dict()
# Set styles for menu
styles = {
"container": {"display": "flex", "justify-content": "center"},
"nav": {"display": "flex", "gap": "2px", "margin": "5px"},
"nav-item": {"flex": "1", "font-family": "Sans-serif"},
"nav-link": {
"background-color": "#f0f0f0",
"border": "1px solid #ccc",
"border-radius": "5px",
"padding": "10px",
"width": "100px",
"height": "60px",
"display": "flex",
"align-items": "center",
"justify-content": "center",
"transition": "background-color 0.3s, color 0.3s",
"color": "black",
"text-decoration": "none"
},
"nav-link:hover": {
"background-color": "rgb(238, 238, 238)",
"color": "#000"
},
"nav-link-selected": {
"background-color": "rgb(254, 74, 75)",
"color": "white",
"font-weight": "bold"
},
"icon": {"display": "None"}
}
# Horizontal menu
active_tab = option_menu(None, ["Nearest neighbours", "Cosine similarity", "3D graph", 'Dictionary', 'About', 'FAQ'],
menu_icon="cast", default_index=0, orientation="horizontal", styles=styles)
# Adding CSS style to remove list-style-type
st.markdown("""
<style>
/* Define a class to remove list-style-type */
.no-list-style {
list-style-type: none;
}
</style>
""", unsafe_allow_html=True)
# Nearest neighbours tab
if active_tab == "Nearest neighbours":
# All models in a list
eligible_models = ["Archaic", "Classical", "Hellenistic", "Early Roman", "Late Roman"]
all_models_words = load_all_models_words()
with st.container():
st.markdown("## Nearest Neighbours")
st.markdown('Here you can extract the nearest neighbours to a chosen lemma. Please select one or more time slices and the preferred number of nearest neighbours.')
target_word = st.multiselect("Enter a word", options=all_models_words, max_selections=1)
if len(target_word) > 0:
target_word = target_word[0]
eligible_models = models_for_word_dict[target_word]
models = st.multiselect(
"Select models to search for neighbours",
eligible_models
)
n = st.slider("Number of neighbours", 1, 50, 15)
nearest_neighbours_button = st.button("Find nearest neighbours")
if nearest_neighbours_button:
if validate_nearest_neighbours(target_word, n, models) == False:
st.error('Please fill in all fields')
else:
# Rewrite models to list of all loaded models
models = load_selected_models(models)
nearest_neighbours = get_nearest_neighbours(target_word, n, models)
all_dfs = []
# Create dataframes
for model in nearest_neighbours.keys():
st.write(f"### {model}")
df = pd.DataFrame(
nearest_neighbours[model],
columns = ['Word', 'Cosine Similarity']
)
all_dfs.append((model, df))
st.table(df)
# Store content in a temporary file
tmp_file = store_df_in_temp_file(all_dfs)
# Open the temporary file and read its content
with open(tmp_file, "rb") as file:
file_byte = file.read()
# Create download button
st.download_button(
"Download results",
data=file_byte,
file_name = f'nearest_neighbours_{target_word}_TEST.xlsx',
mime='application/octet-stream'
)
# Cosine similarity tab
elif active_tab == "Cosine similarity":
all_models_words = load_all_models_words()
with st.container():
eligible_models_1 = []
eligible_models_2 = []
st.markdown("## Cosine similarity")
st.markdown('Here you can extract the cosine similarity between two lemmas. Please select a time slice for each lemma. You can also calculate the cosine similarity between two vectors of the same lemma in different time slices.')
col1, col2 = st.columns(2)
col3, col4 = st.columns(2)
with col1:
word_1 = st.multiselect("Enter a word", placeholder="πατήρ", max_selections=1, options=all_models_words)
if len(word_1) > 0:
word_1 = word_1[0]
eligible_models_1 = models_for_word_dict[word_1]
with col2:
time_slice_1 = st.selectbox("Time slice word 1", options = eligible_models_1)
with st.container():
with col3:
word_2 = st.multiselect("Enter a word", placeholder="μήτηρ", max_selections=1, options=all_models_words)
if len(word_2) > 0:
word_2 = word_2[0]
eligible_models_2 = models_for_word_dict[word_2]
with col4:
time_slice_2 = st.selectbox("Time slice word 2", eligible_models_2)
# Create button for calculating cosine similarity
cosine_similarity_button = st.button("Calculate cosine similarity")
# If the button is clicked, execute calculation
if cosine_similarity_button:
cosine_simularity_score = get_cosine_similarity(word_1, time_slice_1, word_2, time_slice_2)
st.write(cosine_simularity_score)
# 3D graph tab
elif active_tab == "3D graph":
st.markdown("## 3D graph")
st.markdown('Here you can generate a 3D representation of the semantic space surrounding a target lemma. Please choose the lemma and the time slice.')
col1, col2 = st.columns(2)
# Load compressed word list
all_models_words = load_all_models_words()
with st.container():
eligible_models = []
with col1:
word = st.multiselect("Enter a word", all_models_words, max_selections=1)
if len(word) > 0:
word = word[0]
eligible_models = models_for_word_dict[word]
with col2:
time_slice = st.selectbox("Time slice", eligible_models)
n = st.slider("Number of words", 1, 50, 15)
graph_button = st.button("Create 3D graph")
if graph_button:
time_slice_model = convert_time_name_to_model(time_slice)
nearest_neighbours_vectors = get_nearest_neighbours_vectors(word, time_slice_model, n)
fig, df = make_3d_plot_tSNE(nearest_neighbours_vectors, word, time_slice_model)
st.plotly_chart(fig)
# Dictionary tab
elif active_tab == "Dictionary":
with st.container():
st.markdown('## Dictionary')
st.markdown('Search a word in the Liddell-Scott-Jones dictionary (only Greek, no whitespaces).')
all_lemmas = load_all_lemmas()
# query_word = st.multiselect("Search a word in the LSJ dictionary", all_lemmas, max_selections=1)
query_tag = st_tags(label='',
text = '',
value = [],
suggestions = all_lemmas,
maxtags = 1,
key = '1'
)
# If a word has been selected by user
if query_tag:
st.write(f"### {query_tag[0]}")
# Display word information
if query_tag[0] in lemma_dict:
data = lemma_dict[query_tag[0]]
elif query_tag[0].capitalize() in lemma_dict: # Some words are capitalized in the dictionary
data = lemma_dict[query_tag[0].capitalize()]
else:
st.error("Word not found in dictionary")
# Put text in readable format
text = format_text(data)
st.markdown(format_text(data), unsafe_allow_html = True)
st.markdown("""
<style>
.tab {
display: inline-block;
margin-left: 4em;
}
.tr {
font-weight: bold;
}
.list-class {
list-style-type: none;
margin-top: 1em;
}
.primary-indicator {
font-weight: bold;
font-size: x-large;
}
.secondary-indicator {
font-weight: bold;
font-size: large;
}
.tertiary-indicator {
font-weight: bold;
font-size: medium;
}
.quaternary-indicator {
font-weight: bold;
font-size: medium;
}
.primary-class {
padding-left: 2em;
}
.secondary-class {
padding-left: 4em;
}
.tertiary-class {
padding-left: 6em;
}
.quaternary-class {
padding-left: 8em;
}
</style>
""", unsafe_allow_html=True)
# About tab
elif active_tab == "About":
st.markdown("""
## About
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam nec purus nec nunc ultricies ultricies.
""")
elif active_tab == "FAQ":
st.markdown("""
## FAQ
""")
with st.expander('''**Which models is this interface based on?**'''):
st.write(
"This interface is based on five language models. \
Language models are statistical models of language, \
which store statistical information about word co-occurrence during the training phase. \
During training they process a corpus of texts in the target language(s). \
Once trained, models can be used to extract information about the language \
(in this interface, we focus on the extraction of semantic information) or to perform specific linguistic tasks. \
The models on which this interface is based are Word Embedding models."
)
with st.expander('''**Which corpus was used to train the models?**'''):
st.write(
"The five models on which this interface is based were trained on five slices of the Diorisis Ancient Greek Corpus (Vatri & McGillivray 2018)."
)
|