Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweet analyzing error #1

Open
mirsakhawathossain opened this issue Jan 26, 2017 · 2 comments
Open

Tweet analyzing error #1

mirsakhawathossain opened this issue Jan 26, 2017 · 2 comments

Comments

@mirsakhawathossain
Copy link

I have applied your codes I could collect tweets data but for visualization when I applied data in Python 3.5.2 I changed the code for Python 2 to Python 3.The code is following
`'''
Author: Adil Moujahid
Description: Script for analyzing tweets to compare the popularity of 3 programming languages: Python, Javascript and ruby
Reference: http://adilmoujahid.com/posts/2014/07/twitter-analytics/
'''

import json
import pandas as pd
import matplotlib.pyplot as plt
import re

def word_in_text(word, text):
word = word.lower()
text = text.lower()
match = re.search(word, text)
if match:
return True
return False

def extract_link(text):
regex = r'https?://[^\s<>"]+|www.[^\s<>"]+'
match = re.search(regex, text)
if match:
return match.group()
return ''

def main():

#Reading Tweets
print('Reading Tweets\n')
tweets_data_path = 'd:\\twitter.txt'

tweets_data = []
tweets_file = open(tweets_data_path, "r")
for line in tweets_file:
    try:
        tweet = json.loads(line)
        tweets_data.append(tweet)
    except:
        continue


#Structuring Tweets
print('Structuring Tweets\n')
tweets = pd.DataFrame()
tweets['text'] = map(lambda tweet: tweet['text'], tweets_data)
tweets['lang'] = map(lambda tweet: tweet['lang'], tweets_data)
tweets['country'] = map(lambda tweet: tweet['place']['country'] if tweet['place'] != None else None, tweets_data)


#Analyzing Tweets by Language
print('Analyzing tweets by language\n')
tweets_by_lang = tweets['lang'].value_counts()
fig, ax = plt.subplots()
ax.tick_params(axis='x', labelsize=15)
ax.tick_params(axis='y', labelsize=10)
ax.set_xlabel('Languages', fontsize=15)
ax.set_ylabel('Number of tweets' , fontsize=10)
ax.set_title('Top 5 languages', fontsize=15, fontweight='bold')
tweets_by_lang[:5].plot(ax=ax, kind='bar', color='red')
plt.show('tweet_by_lang')


#Analyzing Tweets by Country
print('Analyzing tweets by country\n')
tweets_by_country = tweets['country'].value_counts()
fig, ax = plt.subplots()
ax.tick_params(axis='x', labelsize=15)
ax.tick_params(axis='y', labelsize=10)
ax.set_xlabel('Countries', fontsize=15)
ax.set_ylabel('Number of tweets' , fontsize=10)
ax.set_title('Top 5 countries', fontsize=15, fontweight='bold')
tweets_by_country[:5].plot(ax=ax, kind='bar', color='blue')
plt.show('tweet_by_country')


#Adding programming languages columns to the tweets DataFrame
print('Adding programming languages tags to the data\n')
tweets['python'] = tweets['text'].apply(lambda tweet: word_in_text('python', tweet))
tweets['javascript'] = tweets['text'].apply(lambda tweet: word_in_text('javascript', tweet))
tweets['ruby'] = tweets['text'].apply(lambda tweet: word_in_text('ruby', tweet))


#Analyzing Tweets by programming language: First attempt
print('Analyzing tweets by programming language: First attempt\n')
prg_langs = ['python', 'javascript', 'ruby']
tweets_by_prg_lang = [tweets['python'].value_counts()[True], tweets['javascript'].value_counts()[True], tweets['ruby'].value_counts()[True]]
x_pos = list(range(len(prg_langs)))
width = 0.8
fig, ax = plt.subplots()
plt.bar(x_pos, tweets_by_prg_lang, width, alpha=1, color='g')
ax.set_ylabel('Number of tweets', fontsize=15)
ax.set_title('Ranking: python vs. javascript vs. ruby (Raw data)', fontsize=10, fontweight='bold')
ax.set_xticks([p + 0.4 * width for p in x_pos])
ax.set_xticklabels(prg_langs)
plt.grid()
plt.show('tweet_by_prg_language_1')


#Targeting relevant tweets
print('Targeting relevant tweets\n')
tweets['programming'] = tweets['text'].apply(lambda tweet: word_in_text('programming', tweet))
tweets['tutorial'] = tweets['text'].apply(lambda tweet: word_in_text('tutorial', tweet))
tweets['relevant'] = tweets['text'].apply(lambda tweet: word_in_text('programming', tweet) or word_in_text('tutorial', tweet))


#Analyzing Tweets by programming language: Second attempt
print('Analyzing tweets by programming language: First attempt\n')
tweets_by_prg_lang = [tweets[tweets['relevant'] == True]['python'].value_counts()[True], 
                  tweets[tweets['relevant'] == True]['javascript'].value_counts()[True], 
                  tweets[tweets['relevant'] == True]['ruby'].value_counts()[True]]
x_pos = list(range(len(prg_langs)))
width = 0.8
fig, ax = plt.subplots()
plt.bar(x_pos, tweets_by_prg_lang, width,alpha=1,color='g')
ax.set_ylabel('Number of tweets', fontsize=15)
ax.set_title('Ranking: python vs. javascript vs. ruby (Relevant data)', fontsize=10, fontweight='bold')
ax.set_xticks([p + 0.4 * width for p in x_pos])
ax.set_xticklabels(prg_langs)
plt.grid()
plt.show('tweet_by_prg_language_2')


#Extracting Links
tweets['link'] = tweets['text'].apply(lambda tweet: extract_link(tweet))
tweets_relevant = tweets[tweets['relevant'] == True]
tweets_relevant_with_link = tweets_relevant[tweets_relevant['link'] != '']

print('\nBelow are some Python links that we extracted\n')
print(tweets_relevant_with_link[tweets_relevant_with_link['python'] == True]['link'].head())

if name=='main':
main()

But it shows these graphs ![figure_1](https://cloud.githubusercontent.com/assets/23469906/22321042/d33498f0-e3bb-11e6-8f2c-104e3708e88b.png) ![figure_1-1](https://cloud.githubusercontent.com/assets/23469906/22321043/d3370270-e3bb-11e6-9c3a-bf72f0fc0227.png) and shows following error============== RESTART: C:\Users\User\Desktop\analyze_tweets.py ==============
Reading Tweets

Structuring Tweets

Analyzing tweets by language

Analyzing tweets by country

Adding programming languages tags to the data

Traceback (most recent call last):
File "C:\Users\User\Desktop\analyze_tweets.py", line 137, in
main()
File "C:\Users\User\Desktop\analyze_tweets.py", line 83, in main
tweets['python'] = tweets['text'].apply(lambda tweet: word_in_text('python', tweet))
File "E:\WinPython-64bit-3.5.2.3Qt5\python-3.5.2.amd64\lib\site-packages\pandas\core\series.py", line 2292, in apply
mapped = lib.map_infer(values, f, convert=convert_dtype)
File "pandas\src\inference.pyx", line 1207, in pandas.lib.map_infer (pandas\lib.c:66116)
File "C:\Users\User\Desktop\analyze_tweets.py", line 83, in
tweets['python'] = tweets['text'].apply(lambda tweet: word_in_text('python', tweet))
File "C:\Users\User\Desktop\analyze_tweets.py", line 15, in word_in_text
text = text.lower()
AttributeError: 'map' object has no attribute 'lower'

`

@bossiernesto
Copy link

Hi, when structuring the tweets, you may need to cast the iterators to list as i did on a fork branch i have for this project https://github.com/bossiernesto/Twitter_Analytics/blob/master/analyze.py#L48 in order to make it work with python3 after migrating the script with 2to3.

@rupam2402
Copy link

The map function wont work directly , you need to convert it in list , i too faced the same issue while doing such code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants