Skip to content

Commit

Permalink
Add nlp words to sample sentence base on sense plot
Browse files Browse the repository at this point in the history
  • Loading branch information
pbong committed Oct 31, 2023
1 parent 391b57d commit bd07f26
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 36 deletions.
2 changes: 2 additions & 0 deletions src/callbacks/home/api_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def get_nlp_word(API_URL, word):
word['example_sentences'] = util.sanitize_sample_sentences(word['example_sentences'])

df = pd.DataFrame(word_db)
df = df.sort_values(by=['sense_id'])

return df
except Exception as e:
Expand All @@ -46,6 +47,7 @@ def get_word_db(API_URL, word):
word_db = res.json()

df = pd.DataFrame(word_db)
df = df.sort_values(by=['sense_id'])
return df
except:
return pd.DataFrame()
Expand Down
91 changes: 55 additions & 36 deletions src/callbacks/home/plot/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,29 +106,47 @@ def display_word_tooltip_in_the_source_plot(source_n_clicks, sense_n_clicks, wor
def display_sense_dropdown(word, word_exists):
if word and word_exists:
df = get_word_db(API_URL, word)
nlp_word_df = get_nlp_word(API_URL, word)

if len(df) >= 1:
checklist_options = {}
num_sense = 0

checklist_options = {}
num_sense = 0
# netsci word
if len(df) >= 1:
for i in range(len(df)):
sense_id = df.iloc[i]['sense_id']

pos = ''
if 'pos' in df.columns:
pos = df.iloc[i]['pos']
#if 'pos' in df.columns:
# pos = df.iloc[i]['pos']

checklist_options[sense_id] = sense_and_pos_text(
f'Sense {num_sense+1}', pos)
num_sense = num_sense + 1

# nlp word
if len(nlp_word_df) >= 1:
for i in range(len(nlp_word_df)):
sense_id = nlp_word_df.iloc[i]['sense_id']

pos = ''
#if 'pos' in nlp_word_df.columns:
# pos = nlp_word_df.iloc[i]['pos']

checklist_options[sense_id] = sense_and_pos_text(
f'Sense {num_sense+1}', pos) + "*"
num_sense = num_sense + 1


if len(df) >= 1 or len(nlp_word_df) >= 1:
selected_option = None
if not checklist_options:
checklist_options = {None: None}
else:
selected_option = list(checklist_options.keys())[0]

return checklist_options, selected_option

else:
# TODO: Handle case where word is not in database
raise PreventUpdate
Expand All @@ -145,40 +163,41 @@ def plot_update_sample_sentence_base_on_sense(sense_value, word, word_exists):

if word and sense_value and word_exists:
df = get_word_db(API_URL, word) # get_definition_list(word)

if len(df) > 0:
sense_id_df = df.loc[df['sense_id'] == sense_value.lower()]

sample_sentence_list = sense_id_df.iloc[0]['example_sentences']

sample_sentence = ''
if len(sample_sentence_list) > 0:
sample_sentence = sample_sentence_list[0]

sense_data = html.Div(
children=[
html.Br(),
#html.Div(
# children=[
# html.Span('Definition: '),
# html.Span(
# 'lorem ipsum',
# style={'color': 'gray'}
# )
# ]
#),
html.Div(
children=[
html.Span('Sample sentence: '),
html.Span(
sample_sentence,
style={'color': 'gray'}
)
]
),
]
)
return sense_data
if len(sense_id_df) >= 1:
sample_sentence_list = sense_id_df.iloc[0]['example_sentences']

sample_sentence = ''
if len(sample_sentence_list) > 0:
sample_sentence = sample_sentence_list[0]

sense_data = html.Div(
children=[
html.Br(),
#html.Div(
# children=[
# html.Span('Definition: '),
# html.Span(
# 'lorem ipsum',
# style={'color': 'gray'}
# )
# ]
#),
html.Div(
children=[
html.Span('Sample sentence: '),
html.Span(
sample_sentence,
style={'color': 'gray'}
)
]
),
]
)
return sense_data

raise PreventUpdate

Expand Down

0 comments on commit bd07f26

Please sign in to comment.