Spaces:
Sleeping
Sleeping
# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022) | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
import streamlit as st | |
from streamlit.logger import get_logger | |
import pandas as pd | |
from utils import plotly_line_chart | |
import plotly.graph_objects as go | |
LOGGER = get_logger(__name__) | |
def run(): | |
st.set_page_config( | |
page_title="Restaurant review analysis", | |
page_icon="π", | |
) | |
st.write("# Restaurant review analysis") | |
start_year = st.sidebar.slider('Year', 2016, 2023, 2017) | |
smooth = st.sidebar.selectbox('Rolling window', [7, 14, 30, 60, 90], index=2) | |
reviews = pd.read_pickle('reviews.bin') | |
trend = pd.DataFrame( | |
{'sentiment': (reviews.groupby('period')['s'].sum().rolling(smooth).sum()/reviews.groupby('period')['s'].count().rolling(30).sum()), | |
'emotion': (reviews.groupby('period')['s'].count().rolling(smooth).sum()/reviews.groupby('period')['starRating'].count().rolling(30).sum()), | |
'rating': reviews.groupby('period')['rating'].mean().rolling(smooth).mean()} | |
)[str(start_year):] | |
trend.index = pd.to_datetime(trend.index.astype(str)) | |
with st.expander('Customer sentiment and emotions', expanded=True): | |
st.write('''emotion defined as percentage of response with review comment''') | |
plotly_line_chart(trend, columns=['sentiment', 'emotion'], | |
styles={'emotion': dict(dash='dot', color=('rgb(128, 128, 128)'))} | |
) | |
with st.expander('score rating', expanded=False): | |
plotly_line_chart(trend, columns=['rating'], | |
#styles={'sentiment': dict(dash='dot', color=('rgb(128, 128, 128)'))}, | |
yaxis={'sentiment': 'y2'} | |
) | |
st.write(trend) | |
absa = pd.read_pickle('stats.bin') | |
cols = st.sidebar.multiselect('customer view point', options=['food', 'service', 'atmosphere', 'staff', 'dish', 'price', 'restaurant', 'owner', 'cuisine', 'rice', 'drinks'], default=['food', 'service', 'price']) | |
positivity = absa.groupby('Period').sum().rolling(smooth).sum()/absa.groupby('Period').count().rolling(smooth).sum() | |
positivity = positivity[str(start_year):] | |
new_cols = [c for c in cols if c in positivity] | |
positivity.index = pd.to_datetime(positivity.index.astype(str)) | |
plotly_line_chart(positivity, columns=new_cols,) | |
top_words = st.slider('top X customer concerns', 5, 50, 30) | |
top_mentions = absa.count().sort_values(ascending=False).head(top_words) | |
with st.expander('Customer top mentions', expanded=True): | |
fig = go.Figure() | |
fig.add_trace( | |
go.Bar(x=top_mentions.index, y=top_mentions.values) | |
) | |
st.plotly_chart(fig) | |
#st.write(top_mentions) | |
if __name__ == "__main__": | |
run() | |