forked from OSM-DM-KGP/Narmada-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseTweetsAndAdd2DB.py
More file actions
140 lines (119 loc) · 4.29 KB
/
Copy pathparseTweetsAndAdd2DB.py
File metadata and controls
140 lines (119 loc) · 4.29 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
import location
import pudb
import json
from nltk.tokenize import word_tokenize
from pymongo import MongoClient
from datetime import datetime
client = MongoClient('mongodb://127.0.0.1:27017')
db = client["narmada"]
tweet = db['tweets']
def parseTweet(text_org, tweet_link, tweet_id,timestamp):
print('helo')
text = text_org.lower()
places = location.return_location_list(text)
each_loc = [place[0] for place in places]
resources = {
"oxygen": "Oxygen",
"o2": "Oxygen",
"ventilator": "Ventilator",
"bed": "Beds",
"icu": "Beds",
"remdes": "Remdesivir",
"plasma": "Plasma",
"consultation": "Doctor",
"ambulance": "Ambulance"
}
tokenized_text = word_tokenize(text)
print("\nOrig tokenized text:" + str(tokenized_text))
for i in reversed(range(1, len(tokenized_text))):
# pu.db
word = tokenized_text[i]
word_prev = tokenized_text[i - 1]
if "#" in word_prev:
del tokenized_text[i]
print("\nNew tokenized text:" + str(tokenized_text))
text = ""
for word in tokenized_text:
text = text+word+" "
places_to_remove = []
resource_text = ""
for resource in resources:
if resource in each_loc:
places_to_remove.append(each_loc.index(resource))
if resource in text:
resource_text = resource_text+resources[resource]+" "
places_to_remove.sort(reverse=True)
for ptr in places_to_remove:
del places[ptr]
# print("\n\n\nText: "+str(text_org))
print("\nLocation: ",places)
print("\nResources: "+resource_text)
# make places in db format
locations = {}
for place in places:
name_of_place = place[0]
arr_of_co_ords = place[1]
if len(arr_of_co_ords) == 1:
locations[name_of_place] = {
'lat': arr_of_co_ords[0][0],
'long': arr_of_co_ords[0][1]
}
else:
for i in range(len(arr_of_co_ords)):
locations[name_of_place + '_' + str(i+1)] = {
'lat': arr_of_co_ords[i][0],
'long': arr_of_co_ords[i][1],
}
print('locations in DB ', locations)
if "availab" in text:
classification_type = 'Availability'
print("\nType: Availability")
elif "need" in text or "require" in text:
classification_type = 'Need'
print("\nType: Need")
else:
classification_type = 'Other'
print("\nType: Other")
# if "need" in text or "require" in text:
# print("\nType: Need")
# elif "availab" in text or len(resource_text) != 0:
# print("\nType: Availability")
# else:
# print("\nType: Other")
# inserting in mongodb
if classification_type != 'Other':
try:
if db.tweets.count_documents({ "TweetId": tweet_id }) == 0:
db.tweets.insert_one({
'text': text_org,
'Classification': classification_type,
'ResourceWords': resource_text.split(" "),
"Locations": locations,
"isCompleted": False,
"Timestamp": timestamp,
"TweetLink": tweet_link,
"TweetId": tweet_id
})
print('successfully added in DB')
else:
print('already there in db')
except Exception as e:
print('Error in adding to DB is ',e)
# For testing uncomment below
# parseTweet('I need oxygen and bed in goa and hyderabad')
with open('latest_tweets.jsonl') as f:
json_lines = f.readlines()
for json_line in json_lines:
try:
parsed_json_line = json.loads(json_line)
tweet = parsed_json_line['tweet']
tweet_link = parsed_json_line['link']
tweet_id = parsed_json_line['id_str']
timestamp = parsed_json_line['datetime']
print('============> parsing currently ', tweet)
parseTweet(tweet, tweet_link, tweet_id, timestamp)
except Exception as e:
print('Failed for some thing')
with open('cronjob_last_time', 'w') as f:
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
f.write(current_time)