-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_mapper.py
181 lines (159 loc) · 6.55 KB
/
base_mapper.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
import os
import sys
import argparse
import json
import re
from datetime import datetime
import time
import signal
import random
#=========================
class base_library():
#----------------------------------------
def __init__(self, mappingStandardsFile, isoCountrySize = 3):
self.initialized = True
self.statPack = {}
if not os.path.exists(mappingStandardsFile):
print('')
print('File %s is missing!' % mappingStandardsFile)
print('')
self.initialized = False
return
try: self.mapping_standards = json.load(open(mappingStandardsFile,'r', encoding='latin-1'))
except json.decoder.JSONDecodeError as err:
print('')
print('JSON error %s in %s' % (err, mappingStandardsFile))
print('')
self.initialized = False
return
if 'ORGANIZATION_TOKENS' not in self.mapping_standards:
self.mapping_standards['ORGANIZATION_TOKENS'] = {}
if 'PERSON_TOKENS' not in self.mapping_standards:
self.mapping_standards['PERSON_TOKENS'] = {}
if 'STATE_CODES' not in self.mapping_standards:
self.mapping_standards['STATE_CODES'] = {}
if 'COUNTRY_CODES' not in self.mapping_standards:
self.mapping_standards['COUNTRY_CODES'] = {}
if 'BAD_VALUES' not in self.mapping_standards:
self.mapping_standards['BAD_VALUES'] = {}
#--supported date formats
self.dateFormats = []
self.dateFormats.append("%Y-%m-%d")
self.dateFormats.append("%m/%d/%Y")
self.dateFormats.append("%d/%m/%Y")
self.dateFormats.append("%d-%b-%Y")
self.dateFormats.append("%Y")
self.dateFormats.append("%Y-%M")
self.dateFormats.append("%m-%Y")
self.dateFormats.append("%m/%Y")
self.dateFormats.append("%b-%Y")
self.dateFormats.append("%b/%Y")
self.dateFormats.append("%m-%d")
self.dateFormats.append("%m/%d")
self.dateFormats.append("%b-%d")
self.dateFormats.append("%b/%d")
self.dateFormats.append("%d-%m")
self.dateFormats.append("%d/%m")
self.dateFormats.append("%d-%b")
self.dateFormats.append("%d/%b")
#--set iso country code size
if isoCountrySize not in (2, 3):
print('')
print('The ISO Country size must be 2 or 3.')
print('')
self.initialized = False
self.isoCountrySize = 'ISO' + str(isoCountrySize)
#----------------------------------------
def formatDate(self, dateString, outputFormat = None):
for dateFormat in self.dateFormats:
try: dateValue = datetime.strptime(dateString, dateFormat)
except: pass
else:
if not outputFormat:
if len(dateString) == 4:
outputFormat = '%Y'
elif len(dateString) in (5,6):
outputFormat = '%m-%d'
elif len(dateString) in (7,8):
outputFormat = '%Y-%m'
else:
outputFormat = '%Y-%m-%d'
return datetime.strftime(dateValue, outputFormat)
return None
#---------------------------------------
def isoCountryCode(self, countryString):
countryString = countryString.replace('.', '').upper()
if countryString in self.mapping_standards['COUNTRY_CODES']:
return self.mapping_standards['COUNTRY_CODES'][countryString.upper()][self.isoCountrySize]
elif ',' in countryString:
countryString = countryString[countryString.rfind(',')+1:].strip()
if countryString in self.mapping_standards['COUNTRY_CODES']:
return self.mapping_standards['COUNTRY_CODES'][countryString.upper()][self.isoCountrySize]
#---------------------------------------
def isoStateCode(self, stateString):
if stateString.upper() in self.mapping_standards['STATE_CODES']:
return self.mapping_standards['STATE_CODES'][stateString.upper()]
#-----------------------------------
def isCompanyName(self, nameString):
if nameString:
for token in nameString.lower().replace('.',' ').replace(',',' ').split():
if token.upper() in self.mapping_standards['ORGANIZATION_TOKENS']:
return True
return False
#----------------------------------------
def dictKeysUpper(self, jsonDataIn):
return {k.upper():v for k,v in jsonDataIn.items()}
#----------------------------------------
def updateStat(self, cat1, cat2, example = None):
if cat1 not in self.statPack:
self.statPack[cat1] = {}
if cat2 not in self.statPack[cat1]:
self.statPack[cat1][cat2] = {}
self.statPack[cat1][cat2]['count'] = 0
self.statPack[cat1][cat2]['count'] += 1
if example:
if 'examples' not in self.statPack[cat1][cat2]:
self.statPack[cat1][cat2]['examples'] = []
if example not in self.statPack[cat1][cat2]['examples']:
if len(self.statPack[cat1][cat2]['examples']) < 5:
self.statPack[cat1][cat2]['examples'].append(example)
else:
randomSampleI = random.randint(2,4)
self.statPack[cat1][cat2]['examples'][randomSampleI] = example
return
#----------------------------------------
def attrInList(jsonAttr, attrList):
for attrName in attrList:
if attrName in jsonAttr:
return True
return False
#----------------------------------------
def pause(question='PRESS ENTER TO CONTINUE ...'):
""" pause for debug purposes """
try: response = input(question)
except KeyboardInterrupt:
response = None
global shutDown
shutDown = True
return response
#----------------------------------------
def signal_handler(signal, frame):
print('USER INTERUPT! Shutting down ... (please wait)')
global shutDown
shutDown = True
return
#----------------------------------------
if __name__ == "__main__":
appPath = os.path.dirname(os.path.abspath(sys.argv[0]))
global shutDown
shutDown = False
signal.signal(signal.SIGINT, signal_handler)
procStartTime = time.time()
progressInterval = 10000
#--test the instance
baseLibrary = base_library(appPath + os.path.sep + 'base_variants.json')
if baseLibrary.initialized:
print('')
print('successfully initialized!')
print('')
sys.exit()