-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
186 lines (154 loc) · 6.12 KB
/
Copy pathapp.py
File metadata and controls
186 lines (154 loc) · 6.12 KB
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
import streamlit as st
import pandas as pd
from gensim.models import Word2Vec
from greek_normalisation.normalise import Normaliser, Norm
import random
import matplotlib.pyplot as plt
import seaborn as sns
import math
@st.experimental_singleton
def load_model():
loaded_model = Word2Vec.load("gensim-model-word2vec-August")
# st.write("Cache miss!")
return loaded_model
@st.experimental_singleton
def start_normaliser():
normaliser = Normaliser().normalise
return normaliser
@st.experimental_singleton
def load_glosses():
defs = pd.read_csv("normalised_glosses.tsv", sep="\t")
return defs
def get_gloss(lemma):
try:
y = glosses.loc[glosses["lemma"] == lemma].iloc[0]["def"]
return y
except IndexError:
return "-"
def random_search():
search = vocab[random.randrange(0, len(vocab))]
def update_search(new_search):
st.session_state.search_updated = True
st.session_state.search_default = new_search
normalise = start_normaliser()
glosses = load_glosses()
model = load_model()
vocab = model.wv.index_to_key
st.write("Based on a Word2Vec model of the Diorisis corpus:")
st.write("Vatri, Alessandro (2020): The Diorisis Ancient Greek Corpus (JSON). figshare. Dataset. https://doi.org/10.6084/m9.figshare.12251468.v6")
st.write("Vatri, A., and B. McGillivray. \"The Diorisis Ancient Greek Corpus\", Research Data Journal for the Humanities and Social Sciences 3, 1 (2018): 55-65")
st.write("Credit for the short definitions goes to Perseus and Logeion: https://github.com/helmadik/shortdefs")
if "search_updated" not in st.session_state:
st.session_state["search_updated"] = False
if "search_default" not in st.session_state:
st.session_state["search_default"] = "λέγω"
if st.session_state.search_updated is True:
search = st.multiselect("Choose your starting word:", vocab, default=st.session_state.search_default)#, index=random.randrange(0, len(vocab)))
else:
search = st.multiselect("Choose your starting word:", vocab, default="λέγω")#, index=random.randrange(0, len(vocab)))
if st.checkbox("Do you want to add a negative search term?"):
antisearch = st.multiselect("Negative word(s):", vocab)
else:
antisearch = None
if st.button("Random"):
seeking = True
while seeking == True:
rand_word = vocab[random.randrange(0, len(vocab))]
if get_gloss(rand_word) != "-":
seeking = False
search = [rand_word]
if len(search) is not 0:
sim = model.wv.most_similar(positive=search, negative=antisearch, topn=50)
results = [(res[0], get_gloss(normalise(res[0])[0]), res[1], vocab.index(res[0])+1) for res in sim]
df = pd.DataFrame.from_records(results, columns=("Related Words", "Gloss", "Relatedness", "Word Frequency"))
st.subheader("You searched for words related to:")
cols = st.columns(len(search))
for search_term, col in zip(search, cols):
with col:
search_gloss = get_gloss(normalise(search_term)[0])
st.metric(search_gloss, search_term, f"Frequency Rank: {vocab.index(search_term)+1}")
if antisearch is not None and len(antisearch) is not 0:
st.subheader("And unrelated to:")
anticols = st.columns(len(antisearch))
for search_term, col in zip(antisearch, anticols):
with col:
search_gloss = get_gloss(normalise(search_term)[0])
st.metric(search_gloss, search_term, f"Frequency Rank: {vocab.index(search_term)+1}")
# st.table(df.style.hide().background_gradient(subset="Word Frequency", gmap=[1/x for x in df["Word Frequency"]]))
header_cols = st.columns(4)
with header_cols[0]:
st.subheader("Related Words")
with header_cols[1]:
st.subheader("Gloss")
with header_cols[2]:
st.subheader("Relatedness")
with header_cols[3]:
st.subheader("Frequency")
for related_word, gloss, relatedness, frequency in results:
result_cols = st.columns(4)
with result_cols[0]:
st.button(related_word, on_click=update_search, kwargs={"new_search": related_word})
with result_cols[1]:
if gloss is not "-":
st.write(f"[{gloss}](https://logeion.uchicago.edu/{related_word})")
else:
st.write(f"[\[No short definition found\]](https://logeion.uchicago.edu/{related_word})")
with result_cols[2]:
# num = [relatedness]
# hovertemp = f"{relatedness}"
# fig = px.bar(x=num, range_x = [0, 1], width=300, height=200)
# fig.update_traces(hovertemplate=hovertemp)
# fig.update_layout(showlegend=False)
# fig.update_xaxes(visible=False)
# fig.update_yaxes(visible=False)
num = [relatedness]
word = ["Relatedness"]
fig = plt.figure()
fig.set_size_inches(1,.15)
ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.set_xlim(0,2)
ax.set_axis_off()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
fig.add_axes(ax)
hbar = ax.barh(word, num, color="grey")
label = [float(str(num[0])[:4])]
ax.bar_label(hbar, label, label_type="edge", padding=6, fontsize=6)
st.pyplot(fig, bbox_inches=0, transparent=False)
with result_cols[3]:
pass
num = [math.exp(-0.0002*frequency)]
word = ["Frequency"]
plt.autoscale()
fig2 = plt.figure()
fig2.set_size_inches(1, .15)
ax2 = plt.Axes(fig2, [0., 0., 1., 1.])
ax2.set_xlim(0,2)
ax2.set_axis_off()
ax2.get_xaxis().set_visible(False)
ax2.get_yaxis().set_visible(False)
fig2.add_axes(ax2)
my_cmap=plt.get_cmap("cool")
hbar = ax2.barh(word, num, color=my_cmap(num))
label2 = [frequency]
ax2.bar_label(hbar, label2, label_type="edge", fontsize=6, padding=6)
st.pyplot(fig2, bbox_inches=0, transparent=False)
# for search_term in search:
# search_gloss = get_gloss(normalise(search_term)[0])
# st.metric(search_gloss, search_term, f"Frequency Rank: {vocab.index(search_term)+1}")
# st.write(f"{vocab.index(search)+1} {search} {search_gloss}")
# st.metric(search_gloss, search[0], f"Frequency Rank: {vocab.index(search[0])+1}")
# st.write(get_gloss(normalise(search)[0]))
# search = "ὀπτάω"
# antisearch = None#"Ζεύς"#
# sim = wv.most_similar(positive=search, negative=antisearch, topn=50)
# print(f"results for {search} : {get_gloss(normalise(search)[0])}")
# print()
# for result in sim:
# norm = normalise(result[0])[0]
# print(result[0], "\t", get_gloss(norm))
# df = pd.DataFrame({
# 'first column': [1, 2, 3, 4],
# 'second column': [10, 20, 30, 40]
# })
# df