-
Notifications
You must be signed in to change notification settings - Fork 0
/
localProcessor.py
72 lines (54 loc) · 2.35 KB
/
localProcessor.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# -*- coding: utf-8 -*-
from pyspark import SparkContext
from pyspark import SparkConf
import os
import sys
import libtp
import shutil
sys.path.insert(0, './Tweet-Sentiment')
import sentiment
import re
#Do various things for spark
SPARK_PATH = os.environ['SPARK_PATH']
logFile = "./logfile.txt"
sconf = SparkConf().set("spark.executor.memory", "7g").setAppName("processor").setMaster("local")
sc = SparkContext(conf=sconf)
#Define what files we are going to use
#In the case of this code, we are targetting our tweet directory, and joining the files together
#Then we pass into spark for the map-reduce
#files = "/home/jadixon/Documents/Senior-Design/tweets/" + (",/home/jadixon/Documents/Senior-Design/tweets/".join(os.listdir("/home/jadixon/Documents/Senior-Design/tweets")))
#textFile = sc.textFile(files)
#print ('Removing old sequence files...')
#shutil.rmtree('/home/jadixon/Documents/Senior-Design/seq')
relevants = sc.sequenceFile('/home/jadixon/Documents/Senior-Design/Archive/relevantHashtags').collect()
contents = dict([ tuple(x) for x in relevants ])
#relevantTweets = textFile.filter(libtp.hasHashtag)\
# .filter(libtp.removeTargets)\
# .filter(lambda x: libtp.removeAgain(x, contents))\
# .map(lambda x: (x, 0))\
# .saveAsSequenceFile('/home/jadixon/Documents/Senior-Design/relevantTweets')
locationWithHashTags = sc.sequenceFile('/home/jadixon/Documents/Senior-Design/relevantTweets')\
.flatMap(lambda x: libtp.parseHashtags(x[0]))\
.filter( lambda x: contents.has_key(x[0].split(',')[0]) )\
.map( lambda x: (x[0], x[1] + '\n') )\
.reduceByKey(lambda a, b: a+b)\
.saveAsSequenceFile('/home/jadixon/Documents/Senior-Design/seq')
#relevantHashtags --> hashtags
#relevantTweets --> tweets
#seq --> tweetsSubset
#414
#821
#928
#1276
#1303
#1507
#Used without spark
#documents = sc.sequenceFile('/home/jadixon/Documents/Senior-Design/seq').collect() #1968 location_hashtags
#i = 0
#print documents[1508][0]
#while i < 1968:
# test = (documents[i][0], sentiment.getSentiment(documents[i][1]), libtp.ort.rankDocument(documents[i][1]), sentiment.getEmotion(documents[i][1]))
# with open("sentimentEmotion2.txt", "a") as myfile:
# myfile.write(str(test) + '\n')
# i = i + 1
sc.stop()