-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
182 lines (147 loc) · 5.76 KB
/
data.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
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
180
181
# coding: utf-8
# In[ ]:
#generate graphs with chunk_size, batch size
import psycopg2
import pandas as pd
import nltk
import re #This is to parse the HTML code from the given text
from sshtunnel import SSHTunnelForwarder #This is to connect to the local Database
def getCharAuthorData(authors, doc, documentTable = 'aman_content', chunk_size = 1000):
df = pd.DataFrame()
conn = None
output = []
i = 1
#nltk.download('punkt')
try:
with SSHTunnelForwarder(("srn01.cs.cityu.edu.hk", 22),
ssh_username='stylometry',
ssh_password='stylometry',
remote_bind_address=('localhost', 5432),
local_bind_address=('localhost', 5400)):
conn = psycopg2.connect(user="stylometry", password="stylometry",
database="stylometry", host="localhost", port=5400)
cur = conn.cursor()
query = "SELECT author_id, doc_content FROM " + str(documentTable) + " WHERE author_id IN ("
flag = False
for auth in authors:
if not flag:
query = query + str(auth)
flag = True
else:
query = query + ", " + str(auth)
query = query + ") AND doc_id NOT IN ("
flag = False
for doc_id in doc:
if not flag:
query = query + str(doc_id)
flag = True
else:
query = query + ", " + str(doc_id)
query = query + ") ;"
cur.execute(query)
print("Execution completed")
rows = cur.fetchall()
print("Read completed")
print("Number of rows: %s" % (len(rows)))
for row in rows:
#tokens = nltk.word_tokenize(row[1])
temp = re.sub('<[^<]+?>', '', row[1])
temp = temp.replace("\r\n","")
temp = temp.replace("\n","")
chars = list(temp)
chunk1 = []
for x in chars:
if (i < chunk_size):
chunk1.append(x)
i += 1
else:
chunk1.append(x)
xx = ''.join(chunk1)
xx = str(xx)
chunk1 = []
output.append([row[0], xx])
i = 1
if len(chunk1) > 0:
xx = ''.join(chunk1)
xx = str(xx)
chunk1 = []
output.append([row[0], xx])
i = 1
df = pd.DataFrame(output, columns=["author_id", "doc_content"])
print(df.dtypes)
print("Data Frame created: Shape: %s" % (str(df.shape)))
except psycopg2.Error as e:
if conn:
conn.rollback()
print('Error %s' % e)
sys.exit(1)
finally:
if conn is not None:
conn.close()
# print df
return df
def getCharDocData(authors, doc, documentTable = 'aman_content', chunk_size = 1000):
df = pd.DataFrame()
conn = None
output = []
i = 1
#nltk.download('punkt')
try:
with SSHTunnelForwarder(("srn01.cs.cityu.edu.hk", 22),
ssh_username='stylometry',
ssh_password='stylometry',
remote_bind_address=('localhost', 5432),
local_bind_address=('localhost', 5400)):
conn = psycopg2.connect(user="stylometry", password="stylometry",
database="stylometry", host="localhost", port=5400)
cur = conn.cursor()
query = "SELECT author_id, doc_content FROM " + str(documentTable) + " WHERE"
query += " doc_id = '" + str(doc) + "' ;"
cur.execute(query)
print("Execution completed")
rows = cur.fetchall()
print("Read completed")
print("Number of rows: %s" % (len(rows)))
for row in rows:
#tokens = nltk.word_tokenize(row[1])
temp = re.sub('<[^<]+?>', '', row[1])
temp = temp.replace("\r\n","")
temp = temp.replace("\n","")
chars = list(temp)
chunk1 = []
for x in chars:
if (i < chunk_size):
chunk1.append(x)
i += 1
else:
chunk1.append(x)
xx = ''.join(chunk1)
xx = str(xx)
chunk1 = []
output.append([row[0], xx])
i = 1
if len(chunk1) > 0:
xx = ''.join(chunk1)
xx = str(xx)
chunk1 = []
output.append([row[0], xx])
i = 1
df = pd.DataFrame(output, columns=["author_id", "doc_content"])
print(df.dtypes)
print("Data Frame created: Shape: %s" % (str(df.shape)))
except psycopg2.Error as e:
if conn:
conn.rollback()
print('Error %s' % e)
sys.exit(1)
finally:
if conn is not None:
conn.close()
# print df
return df
# In[ ]:
'''
authors=[123, 80, 75]
doc = 204
df = getCharAuthorData(authors, doc, documentTable = 'aman_content', chunk_size = 1000)
'''