-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_cleaner.py
69 lines (41 loc) · 1.54 KB
/
data_cleaner.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
# -*- coding: utf-8 -*-
# Author: chen
# Created at: 6/12/19 10:20 AM
import data_helper as dh
import pandas as pd
def generate_text(dataframe):
"""Return combined texts and titles"""
body = nonetostr(list(dataframe.text))
titles = nonetostr(list(dataframe.title))
for i,x in enumerate(titles):
if x == None:
titles[i] = ''
texts = [body[i] + titles[i] for i, x in enumerate(titles)]
return texts
def nonetostr(text_list):
"""Change all none type elements to str"""
for i,x in enumerate(text_list):
if x == None:
text_list[i] = ''
return text_list
def searchkw(text_list, kw_lists):
"""Search for targeted elements in the list based on the lw_lists"""
mentions_list = []
for i, x in enumerate(text_list):
if any(n in x.lower() for n in kw_lists):
mentions_list.append(i)
return mentions_list
main_stream = dh.load_json("data/Avengers/avengers.json")
texts = generate_text(main_stream)
mentions = searchkw(texts, ['lbj', 'lebron', 'lebron james'])
# Get all posts related to James
james_dataframe = main_stream.iloc[mentions]
james_main_id = list(james_dataframe.sub_id)
# Generate James related
james_reply_stream = pd.DataFrame([])
for i in range(8):
df = dh.load_json('data/NBA_1904/reply_stream_{}.json'.format(i+1))
for i in range(len(df)):
if df.iloc[i].link_id in james_main_id:
james_reply_stream = james_reply_stream.append(df.iloc[i])
print("{}, Done".format(i))