-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
168 lines (131 loc) · 5.69 KB
/
app.py
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
from distutils.log import debug
from shiny import App, render, ui, reactive
import matplotlib.pyplot as plt
import matplotlib
import seaborn as sns
import pandas as pd
import numpy as np
# read data
cnstr_shiny = pd.read_csv("data/cnstr_shiny_sense_pred.csv")
# define selections
cnstr_forms = cnstr_shiny['Form'].unique().tolist()
relation_choices = ['吸引 (Attraction)', '不吸引 (Repulsion)']
column_choices = ['構式形式', '構式例子', '上下文', '關聯', '搭配強度',
'構式義(人工標記)', '構式義(機器預測)', 'PTT 板']
app_ui = ui.page_fluid(
ui.panel_title("Collostruction Analysis"),
ui.layout_sidebar(
ui.panel_sidebar(
ui.input_text("word", "輸入字詞", placeholder="什麼"),
ui.input_select("cnstr_form", "構式", cnstr_forms),
ui.input_radio_buttons("relation", "詞組與構式關係", relation_choices),
ui.input_checkbox_group("column", "表格欄位", column_choices),
ui.input_numeric("n", "顯示數目", value=10),
width = 3
),
ui.panel_main(
ui.navset_tab(
ui.nav("Table", ui.output_table("table")),
ui.nav("Plot", ui.output_plot("plot"))
),
),
),
)
def server(input, output, session):
# update cnstr form choices based on input words
@reactive.Effect
def _():
x = input.word()
form_choices = []
for form in cnstr_forms:
if x is None:
form_choices = []
elif x in form:
form_choices.append(form)
ui.update_select(
"cnstr_form",
label="構式",
choices=form_choices
)
@output
@render.table
def table() -> object:
cnstr_form = input.cnstr_form()
relation = input.relation()
n = input.n()
column = input.column()
column_mapping = {'構式形式': 'Form', '構式例子': 'Construction',
'上下文': 'Context', '關聯': 'Relation', '搭配強度': 'Collostruction_strength',
'構式義(人工標記)': 'Sense_annotated',
'構式義(機器預測)': 'Sense_predicted', 'PTT 板': 'Boardname'}
# filter cnstr form
cnstr_table = cnstr_shiny[cnstr_shiny['Form'] == cnstr_form]
# check relation
if relation == '吸引 (Attraction)':
cnstr_table = cnstr_table[cnstr_table['Relation'] == 'attraction']
else:
cnstr_table = cnstr_table[cnstr_table['Relation'] == 'repulsion']
# sort by collostruction strength
cnstr_table = cnstr_table.sort_values(by=['Collostruction_strength'], ascending=False)
# # select columns
# column_display = [column_mapping.get(key) for key in column]
# cnstr_table = cnstr_table[column_display]
# slice first n rows
cnstr_table = cnstr_table[0:n]
# select columns
if len(column) == 0:
cnstr_table = cnstr_table[['Form', 'Construction', 'Context', 'Collostruction_strength']]
else:
column_display = [column_mapping.get(key) for key in column]
cnstr_table = cnstr_table[column_display]
return cnstr_table
@output
@render.plot
def plot() -> object:
cnstr_form = input.cnstr_form()
relation = input.relation()
n = input.n()
cnstr_table = cnstr_shiny[cnstr_shiny['Form'] == cnstr_form]
# check relation
if relation == '吸引 (Attraction)':
cnstr_table = cnstr_table[cnstr_table['Relation'] == 'attraction']
else:
cnstr_table = cnstr_table[cnstr_table['Relation'] == 'repulsion']
# select columns
cnstr_table = cnstr_table[['pair', 'obs.w1_2.in_c', 'delta.p.constr.to.word',
'delta.p.word.to.constr', 'Collostruction_strength']]
# get distinct rows
cnstr_table = cnstr_table.drop_duplicates()
# sort by collostruction strength
cnstr_table = cnstr_table.sort_values(by=['Collostruction_strength'], ascending=False)
# slice first n rows
cnstr_table = cnstr_table[0:n]
# pivot_longer
cnstr_table_long = cnstr_table.melt(id_vars='pair',
value_vars=["obs.w1_2.in_c", "delta.p.constr.to.word",
"delta.p.word.to.constr", "Collostruction_strength"],
var_name='metric',
value_name='strength',
ignore_index=True)
cnstr_table_long = cnstr_table_long.sort_values(['metric','strength'],ascending=False)
# plot settings
sns.set_theme()
matplotlib.font_manager.fontManager.addfont('data/TaipeiSansTCBeta-Regular.ttf') # 新增字體
matplotlib.rc('font', family = 'Taipei Sans TC Beta') # 將 font-family 設為台北思源黑體
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
fig, axes = plt.subplots(2, 2)
# Adjust the subplot layout parameters
fig.subplots_adjust(hspace=0.125, wspace=0.125)
# plot bar charts
g = sns.catplot(data=cnstr_table_long,
col_wrap=2,
kind="bar",
orient = "h",
sharex=False,
y="pair", x="strength", col="metric", ci=None
)
g.set_axis_labels("吸引程度", "詞組")
g.set_titles('{col_name}')
return g
app = App(app_ui, server, debug=True)