Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Module4_Labs/.DS_Store
Module4_Labs/Lab2_Doubly_Linked_List/.DS_Store
Module4_Labs/Lab3_File_System/.DS_Store
Expand Down
6 changes: 3 additions & 3 deletions Module3_Labs/Lab3_Minesweeper/Cards/2.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

Here are a couple examples of how your output should look:

![Capture](C:\Users\kevin\Documents\Programming\Darlene\Rewritten\Minesweeper\Minesweeper\Capture.PNG)
![Capture](./Capture.PNG)

After the first move "55", then the board looks like this:

![Capture(2)](C:\Users\kevin\Documents\Programming\Darlene\Rewritten\Minesweeper\Minesweeper\Capture(2).PNG)
![Capture(2)](./Capture(2).PNG)

A little further down the game, I have made some more moves, and I know a flag is at "36". If I want to place a flag there, it would look like this:

![Capture(3)](C:\Users\kevin\Documents\Programming\Darlene\Rewritten\Minesweeper\Minesweeper\Capture(3).PNG)
![Capture(3)](./Capture(3).PNG)

Notice the "F" at column 3, row 6.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,3 @@ class BinHeap:





Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!--title={Visualizing Tweets}-->

For this lab, we will be analyzing tweets using Twitter's API and `tweepy` package in Python by creating a Word Cloud to observe the frequency of words being used in a celebrity's tweets.

Analyzing the words used by a celebrity in an increasingly cluttered social media world has many uses.

In this day and age, having a prominent social media presence can mean the difference for celebrities' public persona. A celebrity can use social media to generate excitement from millions of fans on Twitter, and if done right, propel their fame to new heights. Conversely, celebrities have to be careful about what they post on a site like Twitter, because one offensive tweet will get viral for the wrong reasons and destroy their reputation, not just in social media but in real life as well. Therefore, the words that celebrities choose when tweeting are vitally important, to cultivate an online persona and propel their own fame. Seeing a word cloud of the words in their tweets can start to help us find common trends in their tweets and determine what kind of persona they wish to conjure on social media.

To do this in Python using `tweepy`, you should start off by importing our necessary libraries. The libraries we will be using are:

`tweepy`, `pandas`, `sys`, `csv`, `WordCloud` and `STOPWORDS` from `wordcloud`, `matplotlib`, `matplotlib.pyplot`, `string`, `re`, `PIL`

The next thing we want is to be able to use the python-twitter API client. To do that, you need to acquire a declare a set of application tokens. Name the tokens `consumer_key`, `consumer_secret`, `access_token_key`, and `access_token_secret`.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!--title={Importing Libraries}-->

Import the following libraries:

- `tweepy`

- `pandas`

- `sys`

- `csv`

- `WordCloud` and `STOPWORDS` from `wordcloud`

- `matplotlib`

- `matplotlib.pyplot`

- `string`

- `re`

- `PIL`

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!--title={Importing Libraries}-->

An example of importing libraries is as below:

```python
import math
```

You can import a library and give it a preferred name, as below:

```python
import math as mt
```

Import the following libraries:

- `tweepy`

- `pandas`

- `sys`

- `csv`

- `WordCloud` and `STOPWORDS` from `wordcloud`

- `matplotlib`

- `matplotlib.pyplot`

- `string`

- `re`

- `PIL`

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!--title={Obtaining Authorization Keys}-->

Head over to your Twitter Developer Account and create an app. After your app is created, you will see a new page that shows all the information you need.

![alt](https://python-twitter.readthedocs.io/en/latest/_images/python-twitter-app-creation-part2.png)

Copy the information needed and declare `consumer_key`, `consumer_secret`, `access_token_key`, and `access_token_secret`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!--title={Creating An App}-->

Head over to your Twitter Developer Account and create an app. Fill out the fields on the next page that looks like this:

![alt](https://python-twitter.readthedocs.io/en/latest/_images/python-twitter-app-creation-part1.png)

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!--title={Key In Authorization Keys}-->

After your app is created, you will see a new page that shows all the information you need.

![alt](https://python-twitter.readthedocs.io/en/latest/_images/python-twitter-app-creation-part2.png)

Copy the information needed and declare the keys as follows:

```python
consumer_key = '<your_consumer_key>'
consumer_secret = '<your_consumer_secret>'
access_token_key = '<your_access_token_key>'
access_token_secret = '<your_access_token_secret>'
```



Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!--title={Obtaining Tweet Data}-->

The next thing we want to do is to create a function to extract tweets. Define a function `get_tweets(username)` that obtains the tweets from the user with username indicated in the parenthesis.

In our `get_tweets(username)` function, we first get authorization to our consumer key and consumer secret by declaring the variable `auth` using the `tweepy.OAuthHandler` function imported from `tweepy`. Then, we want to access to the user's access key and access secret by using the`auth.set_acess_token` function.

After we are done with that, we call the API by declaring the variable `api` using the `tweepy.API` function.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!--title={Authorization Keys and Calling API}-->

Define a function called `get_tweet()` which takes the parameter `username`. Then, gain authorization to the consumer key and consumer secret by using the function `OAuthHandler()` from the `tweepy` library.

The next thing we want to do is to gain access to the access key and access secret. We do so by using the `set_access_token()` function.

Once we are done with the authorization procedure, we move on by calling the API by calling `tweepy.API()`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!--title={Authorization Keys}-->

Define a function called `get_tweet()` which takes the parameter `username` as below:

```python
def get_tweet(username):
```

Under that function, we gain authorization to the consumer key and consumer secret by using the function `OAuthHandler()` from the `tweepy` library as below:

```python
auth = tweepy.OAuthHandler(consumer_key,consumer_secret)
```

After that, we want to gain access to the access key and the access secret. Use the `set_access_token()` function to do the following:

```python
auth.set_access_token(access_token_key,access_token_secret)
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!--title={Calling API}-->

The next thing we want to move on to is to call the Twitter API. Do so by the following:

```python
api = tweepy.API(auth)
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!--title={Extracting Tweets Into .csv File}-->

For our next step, we want to obtain a number of tweets from the user and write it to a new csv file from the list of tweets.

To do that, we first declare an empty list and name it `tfile`. Then, create a for loop to access the items in `tweepy.Cursor()` and append tweet data into the `tfile` list. The information that we want to append into `tfile` are `username`, `tweet.id_str`, `tweet.source`, `tweet.created_at`, `tweet.retweet_count`, `tweet.favourite_count`, and `tweet.text.encode("utf-8")`.

Once we have all our data we need in our `tfile` list, we copy them into a new csv file. Declare a variable `outfile` that names our new .csv file. Copy the data from `tfile` into the .csv file by using the `open` and `writerow` functions.

Once we are done with this, we have completed our `get_tweet()` function and we can move on to define our main function.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!--title={Extracting Tweets}-->

Create a list called `tfile`. Then, create a `for` loop to access the items in the user's timeline by calling `tweepy.Cursor(api.user_timeline,screen_name= username).items()`. Within the `for` loop, use the `append()` function on `tfile` to append `username`, `tweet.id_str`, `tweet.source`, `tweet.created_at`, `tweet.retweet_count`, `tweet.favourite_count`, and `tweet.text.encode("utf-8")`.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!--title={Extracting Tweets}-->

Create a list and name it `tfile` by doing the following:

```python
tfile = []
```

Then, use a for loop to access the items in the user's timeline:

```python
for tweet in tweepy.Cursor(api.user_timeline,screen_name=username).itms():
```

In the for loop, append the `tfile` with the append function:

```python
tfile.append(<example>)
```

The data that we want to append are `username`, `tweet.id_str`, `tweet.source`, `tweet.created_at`, `tweet.retweet_count`, `tweet.favourite_count`, and `tweet.text.encode("utf-8")`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!--title={Copying Tweets To .csv File}-->

After obtaining tweet data into `tfile`, we want to copy the data into a .csv file. To do so, we create a .csv file and open it by using the following code:

```python
with open(file,'w+') as file:
```

Then, copy the data from `tfile` by using the `writerows(tfile)` function.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!--title={Creating A .csv File}-->

To create a .csv file, we declare a variable `outfile` and store the name of the .csv file as `username + "_tweets_V1.csv"`. Then type `print("writing to " + outfile)` in the following line to make sure that we are writing to our .csv file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!--title={Copying Tweets To .csv File}-->

Open and write in the .csv file by using the following line of code:

```python
with open(outfile,'w+') as file:
```

Under the opened file, use the `csv.writer(file,delimeter)`function to specify how our data should be separated. In this case, we want them to be separated by a comma. Declare this function in a variable called `writer`.

Using `writer`, we want to write in our .csv file. To make our data tidy and easy to understand, we write the categories on the first row of the .csv file and then add the data from `tfile` in the rows below it as shown:

```python
writer.writerow(['User_Name','Tweet_ID','Source','Created_date','Retweet_count',
'Favourite_count','Tweet'])
writer.writerow(tfile)
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!--title={Cleansing Tweets}-->

The next thing we will move on to is to create our main function. This function utilizes the tweets we obtained into a .csv file in the previous function, cleanse them, and output a Wordcloud based on the highest number of repeated words.

To do that, we first define our `main()` function. In that function, we start by obtaining the tweet-filled .csv file with the `get_tweets()` function we defined earlier.

Then, you should pick a celebrity that you want to examine the tweets of, and pass the company's Twitter handle into `get_tweets()`.

Then, use the `read_csv()` function from the `pandas`library.

Please leave the code utilizing the `re` library inside of `main()`. Also please make sure you name your object from `read_csv` "bg". Bear in mind the "cleaned" data will be in the DataFrame "bg3".

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!--title={Reading A .csv File}-->

The next step we want to do is to define a `main()` function and do the rest of our tasks there. To read the .csv file that we generated from the `get_tweets()` function, we declare a variable `bg` that calls the function `read_csv()` from the `pandas` library. Print out the first 5 rows of data from `bg` using the `head()` function.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!--title={Generating .csv File Of Tweets}-->

The next thing we want to do is to define a `main()` function as follows:

```python
def main():
```



To generate the .csv file under the `main()` function that obtains tweets from a certain user, call the function `get_tweets()` as below:

```python
get_tweets(<username_used>)
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!--title={Reading The .csv File Of Tweets}-->

Read the .csv file generated by the `get_tweets()` function by declaring a variable that calls the `read_csv()` function. The `read_csv()` function works like this:

```python
bg = pd.read_csv(<csv_filename>,encoding='utf-8')
```

Print the first `n` rows from your .csv file to make sure everything is going smoothly by using the `print()` and `head()` function like this:

```python
print(bg.head(n))
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!--title={Creating A Data Frame Of Tweets}-->

After we have obtained our cleansed tweets in `bg2`, we create a new variable `bg3` that makes `bg2` into a data frame using the `DataFrame` function from the `pandas` library. Print out `bg3` to check for the right data frame output.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!--title={Creating A Data Frame Of Tweets}-->

After obtaining our cleansed tweets in `bg2`, we create a new variable `bg3` to form a data frame for `bg2` as follows:

```python
bg3 = pd.DataFrame(bg2, columns = ['tweet'])
```

Additionally, print out `bg3` to make sure we have the data frame output that we want.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!--title={Displaying Word Cloud}-->

Our last main step is to create a Wordcloud based on the data frame of cleansed tweets. To do that, use the functions from `matplotlib`, `wordcloud`, and `matplotlib.pyplot` libraries. After that, compile your entire code and you are done!
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!--title={Setting Parameters Of Wordcloud Figure}-->

We start by setting the parameters of our Wordcloud plot. Use the `rcParams()` function from the `matplotlib` library to do so. The parameters we want to set are `figure.figsize`, `font.size`, `savefig.dpi`, and `figure.subplot.bottom`.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!--title={Setting Parameters Of Wordcloud Figure}-->

We start by setting the parameters of our Wordcloud plot. Use the `rcParams()` function from the `matplotlib` library to do so, as follows:

```python
mpl.rcParams['<parameter>'] = <set_your_parameters_here>
```

The parameters we want to set are `figure.figsize`, `font.size`, `savefig.dpi`, and `figure.subplot.bottom`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!--title={Creating Wordcloud}-->

The next thing we want to do is to create the Wordcloud using `STOPWORDS` and `WordCloud` from the `wordcloud` library.

Set the stopwords using `set(STOPWORDS)`. Then, create a variable `text` to join all the tweets in `bg3`.

Create the wordcloud using the function `WordCloud().generate(str(text))`. The parameters we want to edit in the `WordCloud()` function are `background_color`, `stopwords`, `max_words`, `max_font_size`, and `random_state`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!--title={Combining Words From Tweets}-->

Now, we want to create the Wordcloud using `STOPWORDS` and `WordCloud` from the `wordcloud` library. We start by setting the stopwords, as follows:

```python
stopwords = set(STOPWORDS)
```

Next, create a variable `text` that joins all the tweets in `bg3`, separated with a space:

```python
text = " ".join(<every_tweet_in_bg3>)
```



Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!--title={Creating The Wordcloud}-->

We want to set the parameters in our wordcloud by doing the following:

```python
cloud = WordCloud(
background_color = <your_preferred_color>,
stopwords = stopwords,
max_words = <your_preferred_max_words>,
max_font_size = <your_preferred_max_size>,
random_state = <your_preferred_value>)
```

After that, we generate the wordcloud as follows:

```python
wordcloud = cloud.generate(str(text))
```



Loading