-
Notifications
You must be signed in to change notification settings - Fork 0
/
#Pooling.py
49 lines (41 loc) · 1.31 KB
/
#Pooling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 3 16:38:04 2019
@author: khan
"""
import csv
#global dictonary for hashtags and tweets
All_hashtags = {}
All_hashtag_count = {}
def removeHashtags(tweet):
ls_tweet = tweet.split()
hashtags = []
for word in ls_tweet:
if word[0] == "#":
hashtags.append(word[1:])
if hashtags:
for tag in hashtags:
ls_tweet.remove("#" + tag)
return hashtags, " ".join(ls_tweet)
def detectHashtag(tweet):
hashtags, new_tweet = removeHashtags(tweet)
if hashtags:
for tag in hashtags:
if tag in All_hashtags.keys():
All_hashtags[tag] = All_hashtags[tag] + " " + new_tweet
All_hashtag_count[tag] += 1
else:
All_hashtags[tag] = new_tweet
All_hashtag_count[tag] = 1
tweets = []
with open('english_tweets.txt', mode = 'r') as f:
csv_reader = csv.reader((x.replace('\0', '') for x in f), delimiter = ',')
for row in csv_reader:
tweets.append(row[1])
for tweet in tweets:
detectHashtag(tweet)
with open("hashtag_pooled_tweets.txt", mode = 'w') as f:
csv_f = csv.writer(f, delimiter=',')
for key in All_hashtags.keys():
csv_f.writerow([key, All_hashtags[key]])