-
Notifications
You must be signed in to change notification settings - Fork 1
/
JRCFileParserService.py
58 lines (52 loc) · 1.88 KB
/
JRCFileParserService.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
'''
Created on Jan 30, 2017
@author: Subhasis
'''
import csv
from MongoManager import MongoManager
class JRCFileParserService(object):
'''
This class takes care of reading the input file parsing the text line by line and pushing it into MongoDB.
'''
def __init__(self, file_path, db_config, schema, table, batch_size):
self.file_path = file_path
self.manager = MongoManager(schema, table, batch_size, db_config)
def process(self):
print "Reading File ", self.file_path
count_record = 0
entity_count = 0
similar_record = []
previous_record_id = '0'
with open(self.file_path, 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter='\t')
for row in reader:
if previous_record_id != row[0]:
self.manager.pushRecords(self.getInsertObject(similar_record))
entity_count += 1
similar_record = []
similar_record.append(row)
previous_record_id = row[0]
count_record += 1
self.manager.pushRecords(self.getInsertObject(similar_record))
print "Records Processed ", count_record
print "Entity Processed ", entity_count
return self.manager.flushBatch()
def getInsertObject(self, data_list):
d = {}
d['id'] = int(data_list[0][0])
d['type'] = 'UNKNOWN'
if data_list[0][1] == 'P':
d['type'] = 'PERSON'
if data_list[0][1] == 'O':
d['type'] = 'ORGANIZATION'
variations = []
compare_strings = []
for r in data_list:
v = {}
v['lang'] = r[2]
v['name'] = r[3]
variations.append(v)
compare_strings.append(r[3].lower())
d['variations'] = variations
d['compare_strings'] = compare_strings
return d