-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclusterProcessor.py
More file actions
179 lines (142 loc) · 6 KB
/
clusterProcessor.py
File metadata and controls
179 lines (142 loc) · 6 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
import json
import re
import shutil
import os
from ast import literal_eval as make_tuple
sys.path.insert(0, '/home/cs179g/Tweet-Processor')
import libtp
from pyspark.sql import SQLContext
from pyspark import SparkConf
from pyspark import SparkContext
from pyspark.sql.types import *
from pyspark_cassandra import CassandraSparkContext
with open('/home/cs179g/Tweet-Processor/filteredTweets.txt') as d:
contents = d.readlines()
contents = dict([ tuple(x.strip().split(',')) for x in contents ])
def normalize(datum):
return re.sub('[^A-Za-z .0-9]+', '', datum)
def ParseData(datum):
data = datum.split(", \"")
more = data[1].split("\", ")
del data[1]
data = data + more
hashtag = str(normalize(data[0].split(',')[0])[1:])
location = str(normalize(data[0].split(',')[1]))
obscenityOverall = str(normalize(data[3].split(', ')[0]))
obscenityPure = str(normalize(data[3].split(', ')[1]))
emotion = str(data[1][:-2].replace("u", ""))
sentiment = str(data[2].replace("u", ""))
return [(hashtag, location, obscenityOverall, obscenityPure, emotion, sentiment)]
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Poor mans map reduce stuff~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def removeTargets(target):
isolatedHashtags = parseHashtagsOnly(target)
for hashtag in isolatedHashtags:
if contents.has_key(hashtag):
return False
return True
def removeAgain(tweet, relevants):
isolatedHashtags = parseHashtagsOnly(tweet)
for hashtag in isolatedHashtags:
if relevants.has_key(hashtag):
return True
return False
def parseHashtagsOnly(tweet):
beg = tweet.find('"hashTags"') + 13
end = tweet.find('"full_name"') - 3
hashtags = tweet[beg:end].replace('"', '').split(',')
return [hashtag.lower() for hashtag in hashtags]
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~USED IN FIRST PASS MAP REDUCE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Detects the presence of a hashtag for the spark filter
#@Param tweet object
#@Return true if there is a hashtag
def hasHashtag(tweet):
if tweet.find('"hashTags":[]') == -1:
return True
elif tweet.find('"hashTags":[]') != -1:
return False
#Parses the tweet, returning a sequence of tweets
#@Param tweet object
#@Return sequence of pairs (hashtag, tweet_message)
def parseHashtags(tweet):
beg = tweet.find('"hashTags"') + 13
end = tweet.find('"full_name"') - 3
hashtags = tweet[beg:end].replace('"', '').split(',')
beg = tweet.find('"message"') + 10
end = tweet.find('"timestamp"') - 1
message = tweet[beg:end]
message = message.replace('\n', ' ')
beg = tweet.find('"full_name"') + 13
end = tweet.find('"possibly_sensitive"') - 2
location = tweet[beg:end].split(',')
if len(location) == 1 or len(location) == 0:
location = 'Not enough information'
elif len(location[1].strip()) == 2:
if location[1].strip() in states[1]:
location = states[1][location[1].strip()] + ' (' + location[1].strip() + ')'
else:
location = 'Non-USA'
else:
if location[0].strip() in states[0]:
location = location[0] + ' (' + states[0][location[0].strip()] + ')'
else:
location = 'Non-USA'
return list(map(lambda hashtag: (hashtag.lower() + ',' + location, message), hashtags))
def loadDictionary(path):
a = ''
with open(path) as d:
a = d.readlines()
invcontent = [tuple((content.split(',')[1].strip(), content.split(',')[0].strip())) for content in a]
states = dict([ tuple(x.strip().split(',')) for x in a ])
statesinv = dict(invcontent)
return (states, statesinv)
states = loadDictionary('/home/cs179g/Tweet-Processor/states.txt')
#Load the states into a bidirectional dictionary for ease of access and data formatting
#states = loadDictionary('/home/cs179g/Tweet-Processor/states.txt')
######################################################MAIN##########################################################
if __name__ == "__main__":
if len(sys.argv) != 2:
print ("ERROR NOT ENOUGH ARGS")
exit(-1)
conf = SparkConf().setAppName("Processor")
sc = CassandraSparkContext(conf=conf)
sqlContext = SQLContext(sc)
print('Removing old sequence files...')
#shutil.rmtree('/home/cs179g/orderedTweets')
#shutil.rmtree('~/relevantHashtags')
shutil.rmtree('/home/cs179g/relevantTweets')
files = "/home/cs179g/tweets7g/" + (",/home/cs179g/tweets7g/".join(os.listdir("/home/cs179g/tweets7g")))
textFile = sc.textFile(files)
orderedTweets = textFile.filter(hasHashtag)\
.filter(removeTargets)\
.flatMap(parseHashtags)\
.map( lambda x: (x[0].split(',')[0], 1))\
.reduceByKey(lambda a, b: a+b)\
.sortBy(lambda x: x[1], ascending=False)\
# .saveAsSequenceFile('/home/cs179g/orderedTweets')
# smallest = sc.sequenceFile('/home/cs179g/orderedTweets').takeOrdered(40, key = lambda x: -x[1])[39][1]
smallest = orderedTweets.takeOrdered(40, key = lambda x: -x[1])[39][1]
relevantHashtags = orderedTweets\
.filter(lambda x: x[1] >= smallest)\
.map(lambda x: (x[0], x[1]))\
.sortBy(lambda x: x[1], ascending=False)\
# .saveAsSequenceFile('/home/cs179g/relevantHashtags')
# relevants = sc.sequenceFile('/home/cs179g/relevantHashtags').collect()
relevants = relevantHashtags.collect()
cont = dict([ tuple(x) for x in relevants ])
relevantTweets = textFile.filter(hasHashtag)\
.filter(removeTargets)\
.filter(lambda x: removeAgain(x, cont))\
.map(lambda x: (x, 0))\
.saveAsSequenceFile('/home/cs179g/relevantTweets')
hashtagData = sc.textFile(sys.argv[1])\
.flatMap(ParseData)\
.map(lambda row: {'hashtag': row[0],
'location': row[1],
'obscenityoverall': row[2],
'obscenitypure': row[3],
'emotion': row[4],
'sentiment': row[5]}).collect()
#sc.parallelize(hashtagData).saveToCassandra(keyspace='database_t', table='test')