def test_lemmatized_text(): """Test lemmatized text works""" text = "She loves dogs" output = az.lemmatized_text(text) expect = "love dog" print(output) assert output == expect
def interactive(): """Page to allow nlp analysis from user input""" input_text = st.text_area("Enter text", "Type here") token_cb = st.checkbox("Show tokens") ner_cb = st.checkbox("Show named entities") sentiment_cb = st.checkbox("Show sentiment") summary_cb = st.checkbox("Show Summary") # st.success("Running Analysis") # if st.button("Analysis"): if token_cb: tokens = az.tokenize(input_text) st.write(tokens) if ner_cb: doc = az.get_nlp(input_text) named_entities = az.named_entity_recognization(input_text) if len(named_entities) > 0: html = spacy.displacy.render(doc, style="ent") # Newlines seem to mess with the rendering html = html.replace("\n", " ") HTML_WRAPPER = """<div style="overflow-x: auto; border: 1px solid \ #e6e9ef; border-radius: 0.25rem; padding: 1rem; margin-bottom: 2.5rem">\ {}</div>""" st.write(HTML_WRAPPER.format(html), unsafe_allow_html=True) else: st.info("No named entity recognized") if sentiment_cb: sentiments = TextBlob(az.lemmatized_text(input_text)) st.write(sentiments.sentiment) if summary_cb: summaries = sz.summarize_text(input_text) st.write(summaries)
def sentiment(): """Main function for sentiment analysis.""" senti_df = main_df.copy(deep=True) # Initializing the new columns with a numpy array, so the entire series is returned senti_df[cts.POSITIVE], senti_df[cts.NEGATIVE] = az.top_polarized_word( senti_df[cts.TOKEN].values) # calculate overall sentiment from the combined text senti_df[cts.SENTI] = senti_df[cts.COMBINED].apply( lambda x: TextBlob(az.lemmatized_text(x)).sentiment.polarity) senti_df = ut.return_assignment(senti_df, assign_id, assignments) senti_type = st.sidebar.selectbox("Type of sentiment analysis", ["Overall", "Student", "Question"]) if not assignments: st.warning("Please select an assignment for the analysis") elif senti_type == "Overall": st.sidebar.success( 'To continue see individual sentiment analysis select "Student"') st.header(f"Overall sentiment polarity in **{assignment_string}**") overall_senti(senti_df) elif senti_type == "Student": st.header( f"View sentiment by individual students in **{assignment_string}**" ) student_senti(senti_df) elif senti_type == "Question": st.header( f"View sentiment by individual questions in **{assignment_string}**" ) question_senti(senti_df)
def interactive(): """Page to allow nlp analysis from user input.""" input_text = st.text_area("Enter text", "Type here") token_cb = st.checkbox("Show tokens") ner_cb = st.checkbox("Show named entities") sentiment_cb = st.checkbox("Show sentiment") summary_cb = st.checkbox("Show Summary") # st.success("Running Analysis") # if st.button("Analysis"): if token_cb: tokens = az.tokenize(input_text) st.write(tokens) if ner_cb: displacy_renderer(az.get_nlp(input_text)) if sentiment_cb: sentiments = TextBlob(az.lemmatized_text(input_text)) st.write(sentiments.sentiment) if summary_cb: summaries = sz.summarize_text(input_text) st.write(summaries)
def sentiment(): """main function for sentiment analysis""" senti_df = main_df.copy(deep=True) # calculate overall sentiment from the combined text senti_df[cts.SENTI] = senti_df["combined"].apply( lambda x: TextBlob(az.lemmatized_text(x)).sentiment.polarity) senti_df = senti_df[senti_df[assign_id].isin(assignments)] senti_type = st.sidebar.selectbox("Type of sentiment analysis", ["Overall", "Student", "Question"]) if senti_type == "Overall": st.sidebar.success( 'To continue see individual sentiment analysis select "Student"') st.header(f"Overall sentiment polarity in **{assign_text}**") overall_senti(senti_df) elif senti_type == "Student": st.header( f"View sentiment by individual students in **{assign_text}**") student_senti(senti_df) elif senti_type == "Question": st.header( f"View sentiment by individual questions in **{assign_text}**") question_senti(senti_df)