Nikhil Mane commited on
Commit
aa4aab4
1 Parent(s): e0fe974

Added basic viz

Browse files
Files changed (4) hide show
  1. app.py +120 -4
  2. exp_notebook.ipynb +0 -0
  3. requirements.txt +7 -0
  4. text.txt +71 -0
app.py CHANGED
@@ -1,8 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
 
 
 
 
 
2
 
3
- x = st.slider('Select a value')
4
- st.write(x, 'squared is', x * x)
 
5
 
6
- text = st.text_input("Paste your text", key="text")
7
 
8
- st.write(f"No. of words = {len(text)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import matplotlib.pyplot as plt
2
+ import pandas as pd
3
+ plt.rcParams["figure.figsize"] = (30,20)
4
+ import nltk
5
+ import spacy
6
+ from nltk.tokenize import sent_tokenize
7
+ from nltk.tokenize import word_tokenize
8
+ from nltk.probability import FreqDist
9
+ from nltk.corpus import stopwords
10
+ from wordcloud import WordCloud, ImageColorGenerator
11
+ from PIL import Image
12
  import streamlit as st
13
+ from nltk.corpus import stopwords
14
+ from sklearn.feature_extraction.text import CountVectorizer
15
+ from collections import Counter
16
+ import seaborn as sns
17
+ import plotly.express as px
18
 
19
+ from nltk.sentiment.vader import SentimentIntensityAnalyzer
20
+ nltk.download('vader_lexicon')
21
+ sid = SentimentIntensityAnalyzer()
22
 
23
+ # Settings
24
 
25
+ width = st.sidebar.slider("plot width", 1, 25, 10)
26
+ height = st.sidebar.slider("plot height", 1, 25, 4)
27
+
28
+ # Temp
29
+ st.title("Text Disection : Analyze your text")
30
+
31
+
32
+ # Input
33
+ default= """That is, life becomes meaningful only by adopting the path of service. Our government is engaged in making the life of the countrymen easier with this spirit of service. India is making every effort to fulfill the dreams of many generations which wanted them to come true.
34
+
35
+ Friends,
36
+
37
+ I have seen and experienced the problems of farmers very closely in my public life of five decades. Therefore, we gave topmost priority to development of agriculture and welfare of farmers when the country gave me an opportunity to serve as the Prime Minister in 2014.
38
+ This is bad news.
39
+ People are sad.
40
+ """
41
+ text = st.text_area("Paste your text", default)
42
+
43
+ # Baisc Info
44
+ tokenized_sent=sent_tokenize(text)
45
+ tokenized_word=word_tokenize(text)
46
+ fdist = FreqDist(tokenized_word)
47
+ st.subheader("Basic Information -")
48
+ st.markdown(f"#### Total Sentences in the text = {len(tokenized_sent)}")
49
+ st.markdown(f"#### Total words in the text = {len(tokenized_word)}")
50
+ # st.markdown(f"##### Most common words = {fdist.most_common(5)}")
51
+
52
+ # Word Cloud
53
+ st.subheader("Here is wordcloud -")
54
+ wc = WordCloud(background_color='black', colormap='RdYlGn').generate_from_text(text)
55
+ fig, ax = plt.subplots(figsize=(width, height))
56
+ plt.imshow(wc)
57
+ plt.axis('off')
58
+ st.pyplot(fig)
59
+
60
+ st.subheader("And N-Gram Anaysis is - ")
61
+ def plot_top_ngrams_barchart(text, n=2):
62
+ stop=set(stopwords.words('english'))
63
+
64
+ new= text.str.split()
65
+ new=new.values.tolist()
66
+ corpus=[word for i in new for word in i]
67
+
68
+ def _get_top_ngram(corpus, n=None):
69
+ vec = CountVectorizer(ngram_range=(n, n), stop_words=stop).fit(corpus)
70
+ bag_of_words = vec.transform(corpus)
71
+ sum_words = bag_of_words.sum(axis=0)
72
+ words_freq = [(word, sum_words[0, idx])
73
+ for word, idx in vec.vocabulary_.items()]
74
+ words_freq =sorted(words_freq, key = lambda x: x[1], reverse=True)
75
+ return words_freq[:10]
76
+
77
+ top_n_bigrams=_get_top_ngram(text,n)[:10]
78
+ x,y=map(list,zip(*top_n_bigrams))
79
+ # fig, ax = plt.subplots(figsize=(width, height))
80
+ # sns.barplot(x=y,y=x)
81
+ fig = px.bar(x=y,y=x, color=y)
82
+ st.plotly_chart(fig)
83
+ # st.pyplot()
84
+ st.markdown(f'##### Unigram:')
85
+ plot_top_ngrams_barchart(pd.Series([text]), 1)
86
+ st.markdown(f'##### bigram:')
87
+ plot_top_ngrams_barchart(pd.Series([text]), 2)
88
+ st.markdown(f'##### Trigram:')
89
+ plot_top_ngrams_barchart(pd.Series([text]), 3)
90
+
91
+
92
+ # Overall Sentiment
93
+
94
+ sentiment_dict = sid.polarity_scores(text)
95
+
96
+ st.subheader(f"Overall Sentiment score is = {sentiment_dict['compound']}")
97
+ # decide sentiment as positive, negative and neutral
98
+ if sentiment_dict['compound'] >= 0.05 :
99
+ st.markdown("** Sentence Overall Rated As Positive **")
100
+
101
+ elif sentiment_dict['compound'] <= - 0.05 :
102
+ st.markdown("** Sentence Overall Rated As Negative **")
103
+
104
+ else :
105
+ st.markdown("** Sentence Overall Rated As Neutral **")
106
+
107
+ # Temporal sentiment
108
+
109
+ temporal_sentiment = pd.DataFrame(columns =['sentence', 'sentiment'])
110
+
111
+ for sent in tokenized_sent:
112
+ sentiment_dict = sid.polarity_scores(sent)
113
+ sent
114
+ temporal_sentiment = temporal_sentiment.append({'sentence' : sent,'sentiment' :sentiment_dict['compound']},
115
+ ignore_index=True)
116
+
117
+ # st.write(temporal_sentiment)
118
+
119
+ fig, ax = plt.subplots(figsize=(width, height))
120
+ temporal_sentiment['sentiment'].plot(kind='bar', color=(temporal_sentiment['sentiment'] > 0).map({True: 'g',False: 'r'}))
121
+ fig = px.bar(temporal_sentiment, x=temporal_sentiment.index , y=temporal_sentiment['sentiment'],
122
+ hover_data=['sentiment', 'sentence'], color= (temporal_sentiment['sentiment'] > 0),
123
+ color_discrete_map={True: 'green',False: 'red'})
124
+ st.plotly_chart(fig)
exp_notebook.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ nltk
2
+ pandas
3
+ spacy
4
+ PIL
5
+ sklearn
6
+ seaborn
7
+ plotly
text.txt ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Namaskar, my dear countrymen!
2
+
3
+ Today is the holy festival of Dev-Deepawali. Today is also the holy festival of Prakash Purab of Guru Nanak Dev ji. I extend warm greetings to all the people of the world and to all the countrymen on this holy festival. It is also very gratifying that the Kartarpur Sahib Corridor has now reopened after a gap of one-and-a-half years.
4
+
5
+ Friends,
6
+
7
+ Guru Nanak ji has said: 'विच्‍च दुनिया सेव कमाइए ता दरगाह बैसन पाइए'
8
+
9
+ That is, life becomes meaningful only by adopting the path of service. Our government is engaged in making the life of the countrymen easier with this spirit of service. India is making every effort to fulfill the dreams of many generations which wanted them to come true.
10
+
11
+ Friends,
12
+
13
+ I have seen and experienced the problems of farmers very closely in my public life of five decades. Therefore, we gave topmost priority to development of agriculture and welfare of farmers when the country gave me an opportunity to serve as the Prime Minister in 2014.
14
+
15
+ Friends,
16
+
17
+ Many people are unaware of the fact that 80 out of 100 farmers in the country are small farmers. They have less than two hectares of land. Can you imagine the number of these small farmers is more than 10 crores? This small piece of land is the source of their entire life. This is their life and they make a living for themselves and their families with the help of this small land. The division of families from generation to generation is making this land smaller.
18
+
19
+ Therefore, we have worked all-round on providing seeds, insurance, markets and savings to overcome the challenges of small farmers of the country. Along with good quality seeds, the government has also provided facilities like neem coated urea, soil health cards, micro irrigation etc to farmers. We have given 22 crore soil health cards to farmers. As a result, agricultural production has also increased due to this scientific campaign.
20
+
21
+ Friends,
22
+
23
+ We have made the crop insurance scheme more effective. More farmers have been brought under this purview. Old rules were also changed so that more and more farmers could get compensation easily at the time of disaster. As a result, our farmer brothers and sisters have received compensation of more than one lakh crore rupees in the last four years. We have also extended insurance and pension facilities to small farmers and farm workers. To meet the needs of small farmers, 1.62 lakh crore rupees were transferred directly into their bank accounts.
24
+
25
+ Friends,
26
+
27
+ Many steps were also taken so that the farmers get the right price for their produce in return for their hard work. The country strengthened its rural market infrastructure. We have not only increased the MSP, but also created record government procurement centers. The procurement of the produce by our government has broken the records of the last several decades. We have given a platform to the farmers to sell their produce anywhere by connecting more than 1,000 mandis of the country with the e-NAM scheme. Along with this, we also spent crores of rupees on modernization of agricultural mandis across the country.
28
+
29
+ Friends,
30
+
31
+ Today the agriculture budget of the central government has increased five times as compared to earlier. Every year more than Rs.1.25 lakh crore is being spent on agriculture. Under the agriculture infrastructure fund worth one lakh crore rupees, arrangements are being made for storage of produce near the villages and farms and expansion of many facilities like making available agricultural equipment rapidly.
32
+
33
+ The campaign to create 10,000 FPOs (Farmers Producers Organisations) is also underway to empower small farmers. About 7,000 crore rupees are being spent on this too. The allocation of Micro Irrigation Fund has also been doubled to 10,000 crore rupees. We also doubled the crop loan, which will be Rs 16 lakh crore this year. Now our farmers associated with fish farming have also started getting the benefit of Kisan Credit Cards. That is, our government is taking every possible step in the interest of farmers. It is working sincerely to improve the economic condition of the farmers and strengthen their social status.
34
+
35
+ Friends,
36
+
37
+ Three agricultural laws were introduced as part of this great campaign to improve the condition of the farmers. The aim was that the farmers of the country, especially the small farmers, should be empowered and they should get the right price for their produce and more options to sell their produce. For years, this demand was continuously being made by the farmers, agricultural experts, agricultural economists and the farmers' organizations of the country. In the past also, many governments had brainstormed on this issue. This time also, there was debate in Parliament following which these laws were introduced. A large number of farmers and several farmers’ organizations across the country welcomed and supported it. I am very grateful to all of them and want to thank them.
38
+
39
+ Friends,
40
+
41
+ Our government brought in the new laws with a good intention, full sincerity and complete dedication for the welfare of farmers, especially for small farmers, in the interest of the agriculture and the country and for the bright future of the poor in villages. But we have not been able to explain to some farmers such a sacred thing which is absolutely pure and for the benefit of the farmers despite our efforts.
42
+
43
+ Even though only a section of farmers was protesting, it was still important for us. Agricultural economists, scientists, progressive farmers also tried hard to make them understand the importance of agricultural laws. We kept on explaining to them with utmost humility and with an open mind. Individual and group interactions also continued through various mediums. We did not leave any stone unturned to understand the arguments of the farmers.
44
+
45
+ The government also agreed to change the provisions of the laws on which they had objections. We also proposed suspending these laws for two years. In the meantime, this matter also went to the Hon'ble Supreme Court. All these things are in front of the country, so I will not go into further details.
46
+
47
+ Friends,
48
+
49
+ While apologizing to the countrymen, today I want to say sincerely that perhaps there must have been some deficiency in our penance that we could not explain the truth like the light of the lamp to the farmer brothers.
50
+
51
+ Today is the holy festival of Prakash Purab of Guru Nanak Dev ji. This is not the time to blame anyone. Today I want to tell you, the entire country, that we have decided to repeal all three agricultural laws. We will complete the constitutional process to repeal these three agricultural laws in the Parliament session that begins later this month.
52
+
53
+ Friends,
54
+
55
+ I urge all my agitating farmer companions that today is the holy day of Guru Purab and therefore you should return to your homes, fields and to your families. Let's make a fresh start. Let's move forward with a fresh beginning.
56
+
57
+ Friends,
58
+
59
+ Today, the government has taken another important decision related to the agriculture sector. A committee will be constituted to decide on matters like promotion of zero budgeting farming i.e. natural farming, scientifically change the crop pattern keeping in mind the changing requirements of the country and make MSP more effective and transparent. The committee will include representatives of the central government, state governments, farmers, agricultural scientists and agricultural economists.
60
+
61
+ Friends,
62
+
63
+ Our government has been working in the interest of farmers and will continue to do so. I will end my speech in the spirit of Guru Gobind Singh ji.
64
+
65
+ ‘देह सिवा बरु मोहि इहै सुभ करमन ते कबहूं न टरों।‘
66
+
67
+ O Goddess, grant me this boon that I will never back down from doing good deeds.
68
+
69
+ Whatever I did, I did for the farmers and whatever I am doing, I am doing for the country. With your blessings, there was no deficiency in my hard work even earlier. Today I assure you that I will work harder now so that your dreams can come true, the dreams of the country can come true.
70
+
71
+ Many thanks to you! Namaskar!