Newer
Older
"source": [
"from lib.xgta import Xgta\n",
"import connection_psql as creds\n",
"xgta = Xgta(\n",
" creds=creds, \n",
" streaming=True, # Streaming uses less RAM, but is slower. Container or notebook may fail/shut down if RAM limit is exceeded.\n",
")"
]
},
{
"cell_type": "markdown",
"id": "063036c2-cf05-4ec0-90ad-0c80e636a82f",
"metadata": {},
"source": [
"------\n",
"\n",
"Calculate frequencey of tweets per day that contain certain (case insensitive) keywords with:\n",
"\n",
"```python\n",
"r1 = xgta.frequency_of_tweets_per_day_containing(keywords)\n",
"```\n",
"\n",
"The `keywords` variable is case insensitive and performs a regex search. Stringing multiple keywords together is possible with the `|`-Operator.\n",
"\n",
"For example, tweets contianing the words \"Rassismus\" and / or \"Diskriminierung\" use the following):"
]
},
{
"cell_type": "code",
"id": "9e6416d0-8abb-4342-a7e3-b5b4e6103c80",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"r1 = xgta.frequency_of_tweets_per_day_containing(\"rassismus|diskriminierung\")"
]
},
{
"cell_type": "markdown",
"id": "dd320383-f444-4804-8cc3-68cbca5a0d2f",
"metadata": {},
"source": [
"The returned object (in the example above `r1`) contains the `Polars.DataFrame` as `.df` (e.g., `r1.df`) and a method `.plot_frequency_of()` to plot a timeseries of the dataframe."
]
},
{
"cell_type": "code",
"source": [
"r1.df # Returns the DataFrame itself"
]
},
{
"cell_type": "code",
"source": [
"r1.plot_frequency_of(['search_term:all_tweets']) # Plots a timeseries of all tweets fitting the search criteria."
]
},
{
"cell_type": "markdown",
"id": "7798f830-5a90-4075-9028-cc29a2407a2e",
"metadata": {},
"source": [
"It is also possibel to plot multiple columns of the DataFrame in one figure. (Uncomment or comment with `#`)"
]
},
{
"cell_type": "code",
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
"source": [
"r1.plot_frequency_of([\n",
" 'search_term:all_tweets',\n",
" 'search_term:is_retweet',\n",
" 'search_term:is_original_tweet',\n",
" 'percent:all_tweets',\n",
" 'percent:is_retweet',\n",
" 'percent:is_original_tweet',\n",
" # 'all_tweets',\n",
" # 'is_retweet',\n",
" # 'is_original_tweet',\n",
"])"
]
},
{
"cell_type": "markdown",
"id": "72e4b2b7-0bab-4c56-9f4e-039ad4db952d",
"metadata": {},
"source": [
"Plots are interactive: \n",
"\n",
"- Deselect columns by clicking on the ledgend.\n",
"- Draw rectangles to zoom in.\n",
"- Double click to reset plots to their default view.\n",
"\n",
"---\n",
"\n",
"To keep previous results, and search for new terms, add save results in a new return-object, e.g., `r2`:"
]
},
{
"cell_type": "code",
"source": [
"r2 = xgta.frequency_of_tweets_per_day_containing(\"krise\")\n",
"r2.plot_frequency_of(['search_term:is_retweet','search_term:is_original_tweet'])\n",
"r2.df.describe()"
]
},
{
"cell_type": "markdown",
"id": "c645b864-d166-4881-b096-1d174e22ccee",
"metadata": {},
"source": [
"Compare multiple results with each other:"
]
},
{
"cell_type": "code",
"id": "69054b41-b209-48e6-a3ae-9a31e535648b",
"source": [
"xgta.plot_frequency_of(\n",
" results=[r1, r2], # Two or more results in an array []\n",
" plot=[\n",
" 'search_term:is_retweet',\n",
" 'search_term:is_original_tweet',\n",
" ],\n",
" shared_xaxes=True, # Optional\n",
" shared_yaxes=True, # Optional\n",
")"
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
},
{
"cell_type": "markdown",
"id": "462cfbdb-18d9-461b-8287-f2020d371571",
"metadata": {},
"source": [
"---\n",
"\n",
"## Access dataset with polars directly"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b861d8c1-59e4-45d5-8500-a442a459b120",
"metadata": {},
"outputs": [],
"source": [
"import polars as pl"
]
},
{
"cell_type": "markdown",
"id": "3f3842fe-cc49-4725-8c02-9d0d488730e2",
"metadata": {},
"source": [
"Get list of available columns."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7008c916-16da-4445-b236-ef232afdd201",
"metadata": {},
"outputs": [],
"source": [
"xgta.df_xgta.collect_schema().names()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f130c9eb-1ee8-49e8-b60b-55a9a27a8e26",
"metadata": {},
"outputs": [],
"source": [
"pl.Config(fmt_str_lengths=350)\n",
"\n",
"q = (\n",
" xgta.df_xgta\n",
" .limit(1_000_000) # Use limit to develop your queries. It greatly speeds up the development time.\n",
" .filter(\n",
" pl.col('text').str.to_lowercase().str.contains(r\"\\bwir\\b\")\n",
" &\n",
" pl.col('isretweet').not_()\n",
" )\n",
" .select([\"postdate\", \"text\"])\n",
")\n",
"\n",
"q.collect()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e8635cf5-e11f-4939-86de-1410f9c128ae",
"metadata": {},
"outputs": [],
"source": []
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",