-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.py
134 lines (103 loc) · 3.85 KB
/
pipeline.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
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
import json
import os
import pickle
import ebayapi
import glob
import argparse
from multiprocessing import Pool
from alchemyapi import AlchemyAPI
def ExtractEntity(text):
# Create the AlchemyAPI Object
alchemyapi = AlchemyAPI()
response = alchemyapi.entities('text', text, {'sentiment': 1})
if response['status'] == 'OK':
for entity in response['entities']:
print('text: ', entity['text'].encode('utf-8'))
print('type: ', entity['type'])
print('relevance: ', entity['relevance'])
else:
print('Error in entity extraction call: ', response['statusInfo'])
def ExtractKeyword(text):
alchemyapi = AlchemyAPI()
response = alchemyapi.keywords('text', text, {'sentiment': 1})
results = []
if response['status'] == 'OK':
for keyword in response['keywords']:
results.append(keyword['text'])
# print('text: ', keyword['text'].encode('utf-8'))
# print('relevance: ', keyword['relevance'])
else:
print('Error in keyword extaction call: ', response['statusInfo'])
return results
def ExtractTaxonomy(text):
alchemyapi = AlchemyAPI()
response = alchemyapi.taxonomy('text', text)
results = []
if response['status'] == 'OK':
for category in response['taxonomy']:
results.append(category['label'] + ' : ' + category['score'])
else:
print('Error in taxonomy call: ', response['statusInfo'])
return results
def ExtractImageTag(image_url):
alchemyapi = AlchemyAPI()
response = alchemyapi.imageTagging('url', image_url)
results = []
if response['status'] == 'OK':
for keyword in response['imageKeywords']:
results.append(keyword['text'])
# print(keyword['text'], ' : ', keyword['score'])
print('')
else:
print('Error in image tagging call: ', response['statusInfo'])
def ProcessTweet(tweet, dataPath):
# make dir for a tweet
tweet_dir = dataPath+'/'+tweet['user']['screen_name']+'/'+tweet['id_str']
if os.path.isdir(tweet_dir) == False:
os.mkdir(tweet_dir)
# save tweet text
tweet_info = open(tweet_dir+'/tweet.txt', 'w')
# # get image url
# imageTags = []
# if 'media' in tweet['entities']:
# imageTags = ExtractImageTag(tweet['entities']['media'][0]['expanded_url'])
# print(imageTags)
keywords = ExtractKeyword(tweet['text'])
# print(kewwords)
# ExtractEntity(tweet['text'])
category = ExtractTaxonomy(tweet['text'])
# ExtractImageTag(image_url)
tweet_info.write(tweet['text']+'\n\n')
tweet_info.write(', '.join(keywords)+'\n')
tweet_info.write(', '.join(category)+'\n')
tweet_info.close()
# Search on ebay for sales data
if len(keywords) > 0:
ebayapi.SearchEbay(keywords, tweet_dir)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('datapath', help='Path of data files. For example "./data"')
parser.add_argument('limit', default=0, type=int, help='Limit of number of tweets for each account.')
args = parser.parse_args()
dataPath = args.datapath
t_limit = args.limit
pool = Pool(10)
files = glob.glob(dataPath+"/*.t")
# print(files)
for f in files:
tweets = pickle.load(open(f, 'rb'))
# make dir for an account
account_dir = dataPath+'/'+tweets[0]['user']['screen_name']
print(account_dir)
if os.path.isdir(account_dir)==False:
os.mkdir(account_dir)
if t_limit == 0:
for t in tweets:
ProcessTweet(t, dataPath)
else:
maxnumber = min(t_limit, len(tweets))
for i in range(maxnumber):
ProcessTweet(tweets[i], dataPath)
# print(t['created_at'])
# print(t['retweet_count'])
# print(t['text'].encode('utf-8'))