-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoopPDF.py
More file actions
908 lines (673 loc) · 32.2 KB
/
Copy pathLoopPDF.py
File metadata and controls
908 lines (673 loc) · 32.2 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
#!/usr/bin/env python
# coding: utf-8
# In[1]:
#loops through each text file and creates array with all of them
import os
masterLineArray=[] #master array of all the stuff, use lineArray[0] for each one
masterTxtName=[] #masterArray for all the text names
for filename in os.scandir():
if filename.is_file() and filename.path.endswith('.txt') and filename.path.endswith("_Table.txt")== False: #makes sure it is text file
with open(filename.path) as f:
lineArray=f.readlines()
masterLineArray.append(lineArray)
masterTxtName.append(filename.path[2:])
#creates two corresponding lists, one with text names and one with the text arrays
# In[ ]:
# In[2]:
results=[]
addThis={}
completeARList=[]
completeDRList=[]
masterCounter=0
#for lineArray in masterLineArray:
while masterCounter<len(masterLineArray):
lineArray=masterLineArray[masterCounter]
addThis={}
#adds pdfname
addThis["PDFName"]=masterTxtName[masterCounter][:-3]+"pdf" #strips txt and replaces with pdf
# EXTRACTING INFORMATION SECTION BELOW
#extracts gender
#should work in theory, not executed yet since test is blank
counter=0
while counter<len(lineArray):
if("Gender" in lineArray[counter]):
if("DOB" in lineArray[counter]):
gender=lineArray[counter+1][lineArray[counter].find("Gender"):lineArray[counter].find("LexID")]
addThis["Gender"]= gender
counter=counter+1
#extracts SSN by alternate method through voter registration
SSNCounter=0
RegistrantLine=0 #need to reset variables before each iteration
VoterLine=0
while SSNCounter<len(lineArray):
if("Registrant Information" in lineArray[SSNCounter]):
RegistrantLine=SSNCounter
if("Voter Information" in lineArray[SSNCounter]):
VoterLine=SSNCounter
SSNCounter+=1
newLoop=RegistrantLine
while(newLoop<=VoterLine):
if("SSN" in lineArray[newLoop]):
SSN=lineArray[newLoop][lineArray[newLoop].find(":")+1:]
addThis["SSN"]=SSN.strip()
newLoop+=1
#extracts emails
emailMax=6
emailCounter=0
while emailCounter<len(lineArray):
if("Email" in lineArray[emailCounter]):
if("SSN" in lineArray[emailCounter]): #nested if statements helps extract email line
i=1
while i<=emailMax:
emailNum= "Email"+str(i)
emailHolder=lineArray[emailCounter+i][lineArray[emailCounter].find("Email"):]
#processing to remove \n at the end of each email
#apparently \n is treated as one charcter?????
if(emailHolder[-1:] == "\n"):
emailHolder=emailHolder[:-1]
addThis[emailNum]=emailHolder
# print(emailNum, emailHolder)
i=i+1
emailCounter=emailCounter+1
#extracts date of birth month and year
DOBcounter=0
while DOBcounter<len(lineArray):
if("DOB" in lineArray[DOBcounter]):
if("SSN" in lineArray[DOBcounter] and "Gender" in lineArray[DOBcounter]):
#bad formatting in text file forces me to go down 2 lines instead of 1 line
DOB=lineArray[DOBcounter+2][lineArray[DOBcounter].find("DOB"):lineArray[DOBcounter].find("Gender")]
DOBcounter=DOBcounter+1
#splicing DOB
#Question- any specification on formatting to clean data??? Ex: 4 vs april
DOBMonth=DOB[:DOB.find("/")]
DOBYear=DOB[DOB.find("/")+1:]
addThis["DOBMonth"]=DOBMonth
addThis["DOBYear"]=DOBYear.strip()
# print("DOBMonth=" +DOBMonth)
# print("DOBYear="+ DOBYear)
############## new lexID section
oldPDFName= addThis["PDFName"]
newPDFName=oldPDFName.replace(".pdf","_Table.txt")
f = open(newPDFName,'r')
tableFile=f.readlines() #array of strings
IDCounter=0
while IDCounter<len(tableFile):
if("LexID" in tableFile[IDCounter]):
if("Email" in tableFile[IDCounter]):
lexID=tableFile[IDCounter+2][tableFile[IDCounter].find("LexID"):tableFile[IDCounter].find("Email")]
addThis["LexID"]=lexID.strip()
IDCounter=IDCounter+1
#######################end new lexID
#Extracting current!!! Address and county
#finding endpoint using additional personal information as endpoint marker
endCounter=0
endpoint=0
while endCounter<len(lineArray):
if("ADDITIONAL PERSONAL INFORMATION" in lineArray[endCounter]):
endpoint=endCounter
endCounter+=1
addyCounter=0
while addyCounter<len(lineArray):
if("Address" in lineArray[addyCounter]):
if("County" in lineArray[addyCounter] and "Phone" in lineArray[addyCounter]):
a=1
currentAddress=''
b=endpoint-addyCounter-1
lineCounter=0
while a <= b:
currentAddress+=lineArray[addyCounter+a][lineArray[addyCounter].find("Address"):lineArray[addyCounter].find("County")].strip()
if lineArray[addyCounter+a][lineArray[addyCounter].find("Address"):lineArray[addyCounter].find("County")].strip() != '':
currentAddress+=" " #adds single space
lineCounter+=1
if "COUNTY" in lineArray[addyCounter+a]:
addThis["County"]=lineArray[addyCounter+a][lineArray[addyCounter].find("Address"):lineArray[addyCounter].find("County")].strip()
a+=1
addyCounter+=1
# print("CurrentAddress="+ currentAddress)
addThis["CurrentAddress"]= currentAddress.replace(addThis["County"],"").strip()
#adds to county from this section
# countyCounter=0
#while countyCounter<len(tableFile):
# if("Full Name" in tableFile[countyCounter] and "County" in tableFile[countyCounter] and "Address" in tableFile[counterCounter]):
# print(tableFile[countyCounter+2])
#extracts phone
#currently extracting only the one from the beginning records page
#note here- some minor formatting differences between pdf and text file
phoneCounter=0
while phoneCounter<len(lineArray):
if("Phone" in lineArray[phoneCounter]):
if("County" in lineArray[phoneCounter] and "Address" in lineArray[phoneCounter]): #makes sure it is the beginning record page
phoneNum=lineArray[phoneCounter+1][lineArray[phoneCounter].find("Phone"):]
phoneCounter=phoneCounter+1
# print("phone number ="+ phoneNum)
addThis["PhoneNumber"]=phoneNum.strip()
#extracting name
#bad formatting issue, seems Full Name on additional information is always in line with "COUNTY" though
addThis["FirstName"]=addThis["PDFName"][:addThis["PDFName"].find("_")].strip()
addThis["LastName"]=addThis["PDFName"][addThis["PDFName"].find("_")+1:-4].strip()
addThis["FullName"]=addThis["FirstName"]+' ' + addThis["LastName"]
#reading excel file with moodys linked in data
#using panda to analyze and work with the excel data
import pandas as pd
workbook = pd.read_excel('Moodys_LinkedIn_Data.xlsx')
workbook.head()
analystData=workbook.loc[:,["id","analyst"]]
analystDataList=analystData.values.tolist()
#installed fuzzywuzzy package
from fuzzywuzzy import fuzz
#fuzzy matching to analyst name
#adding analystname and ID to list
fullName= addThis["FullName"]
holdRatio=0
for item in analystDataList:
uncleanName=item[1]
if uncleanName[-5:] == ", CFA": #strips CFA
uncleanName=uncleanName[:-5]
uncleanName=uncleanName.replace('.', '') #strips periods
ratio=fuzz.ratio(fullName.lower(),uncleanName.lower())
if ratio>holdRatio:
holdRatio=ratio
addThis["analystName"]=uncleanName
addThis["analystID"]=item[0]
addThis["fuzzRatio"]=holdRatio
############################################# all address date range section
#address date range- detailed address information
# needs work with multiple addresses
# might be a challenge, bad formatting causes this to be difficult
#calculates number of addresses
# addressCounter=0
# while addressCounter<len(lineArray):
# if("Address Summary" in lineArray[addressCounter]):
# #print(lineArray[addressCounter][lineArray[addressCounter].find("-")+1:lineArray[addressCounter].find("records")])
# numAddresses=lineArray[addressCounter][lineArray[addressCounter].find("-")+1:lineArray[addressCounter].find("records")]
# numAddresses=int(numAddresses)
# addressCounter+=1
#finds beginning point and endpoint
#beginning point = "Address Details"
# beginCounter=0
# startPoint=0
# while beginCounter<len(lineArray):
# if("Address Details" in lineArray[beginCounter]):
# startPoint=beginCounter
# beginCounter+=1
#create two arrays, one for the addresses and one for the date ranges
# k=1
# searchThis=str(k)+":"
# addyArray=[]
# dateArray=[]
# while startPoint<len(lineArray):
# if searchThis in lineArray[startPoint]:
# #print(lineArray[startPoint])
# addressHolder=lineArray[startPoint][2:]
# if("Dates" in addressHolder):
# addressHolder=addressHolder[:addressHolder.find("Dates")] # just some cleaning
# if(" " in addressHolder):
# addressHolder=addressHolder[:addressHolder.find(" ")] #cleaning- removes a bunch of spaces and then phone numer
# addyArray.append(addressHolder.strip()) # more cleaning
# k+=1
# if k>numAddresses: #if k is larger than number of address, k gets changed to null and thus rendered useless
# k='null'
# searchThis=str(k)+":"
# if("Dates" in lineArray[startPoint] and "Phone" in lineArray[startPoint]):
# #dateRange= "None"
# dateRange=lineArray[startPoint+1][lineArray[startPoint].find("Dates"):lineArray[startPoint].find("Phone")] #goes down one line and takes date range, might be blank
# dateArray.append(dateRange.strip())
# startPoint+=1
#fixing error if date array length is less than address array length
#adds blanks until len(addyArray) matches len of dateArray
# if len(addyArray)>len(dateArray):
# while len(addyArray)>len(dateArray):
# dateArray.append("")
#adding these dates to address Key value pair dictionairy
# e=0
# addressRangeList=[]
# while e<len(addyArray):
# holdingDict={}
# holdingDict["PropertyAddress"]=addyArray[e].replace(":","").strip()
# holdingDict["PropertyAddress"]=holdingDict["PropertyAddress"].strip()
# holdingDict["DateRange"]=dateArray[e]
# addressRangeList.append(holdingDict)
# e+=1
#print("Address Range List")
#print(addressRangeList)
#take each dictionairy in address range list and merge it with analyst dictionairy (addThis)
# fullAnalystAddressList=[]
# for tempDict in addressRangeList:
# tempHolder={**addThis,**tempDict} #merges into tempHolder dict
# fullAnalystAddressList.append(tempHolder)
####################### end address date range section
#######new property address section
#address date range- detailed address information
# needs work with multiple addresses
# might be a challenge, bad formatting causes this to be difficult
#calculates number of addresses
addressCounter=0
while addressCounter<len(tableFile):
if("Address Summary" in tableFile[addressCounter]):
numAddresses=tableFile[addressCounter][tableFile[addressCounter].find("-")+1:tableFile[addressCounter].find("records")]
numAddresses=int(numAddresses)
addressCounter+=1
#finds beginning point and endpoint
#beginning point = "Address Details"
beginCounter=0
startPoint=0
while beginCounter<len(tableFile):
if("Address Details" in tableFile[beginCounter]):
startPoint=beginCounter
beginCounter+=1
#create two arrays, one for the addresses and one for the date ranges
k=1
searchThis=str(k)+":"
addyArray=[]
dateArray=[]
while startPoint<len(tableFile):
if searchThis in tableFile[startPoint]:
#print(lineArray[startPoint])
addressHolder=tableFile[startPoint][2:]
if("Dates" in addressHolder):
addressHolder=addressHolder[:addressHolder.find("Dates")] # just some cleaning
##replaces double spaces with single spaces
addressHolder=addressHolder.replace(" ",' ')
addressHolder=addressHolder.replace(" ",' ')
addressHolder=addressHolder.replace(" ",' ')
addressHolder=addressHolder.replace(" ",' ')
addressHolder=addressHolder.replace(" ",' ')
addressHolder=addressHolder.replace(" ",' ')
addressHolder=addressHolder.replace(" ",' ')
addressHolder=addressHolder.replace(" ",' ')
addressHolder=addressHolder.replace(" ",' ')
addressHolder=addressHolder.replace(" ",' ')
addressHolder=addressHolder.replace(" ",' ')
addressHolder=addressHolder.replace(" ",' ')
addyArray.append(addressHolder.strip()) # more cleaning
k+=1
if k>numAddresses: #if k is larger than number of address, k gets changed to null and thus rendered useless
k='null'
searchThis=str(k)+":"
if("Dates" in tableFile[startPoint] and "Phone" in tableFile[startPoint]):
#dateRange= "None"
#print(tableFile[startPoint+2])
dateRange=tableFile[startPoint+2][tableFile[startPoint].find("Dates"):tableFile[startPoint].find("Phone")] #goes down one line and takes date range, might be blank
dateArray.append(dateRange.strip())
startPoint+=1
#fixing error if date array length is less than address array length
#adds blanks until len(addyArray) matches len of dateArray
# if len(addyArray)>len(dateArray):
# while len(addyArray)>len(dateArray):
# dateArray.append("")
#adding these dates to address Key value pair dictionairy
e=0
addressRangeList=[]
while e<len(addyArray):
holdingDict={}
holdingDict["PropertyAddress"]=addyArray[e].replace(":","").strip()
holdingDict["PropertyAddress"]=holdingDict["PropertyAddress"].strip()
holdingDict["DateRange"]=dateArray[e]
addressRangeList.append(holdingDict)
e+=1
#print("Address Range List")
#print(addressRangeList)
#take each dictionairy in address range list and merge it with analyst dictionairy (addThis)
fullAnalystAddressList=[]
for tempDict in addressRangeList:
tempHolder={**addThis,**tempDict} #merges into tempHolder dict
fullAnalystAddressList.append(tempHolder)
#############end new property address section
#results.append(addThis)
#analystsOnly.appaddend(addThis)
#print(analystsOnly)
results=results + fullAnalystAddressList
#print(addThis)
### new assessmentStart and assessmentEnd section begin
tempCounter=0
assessmentStart=0
assessmentEnd=0
firstInstance=True
firstEndInstance=True
while tempCounter < len(lineArray):
if("Assessment Record" in lineArray[tempCounter] or "Deed Record" in lineArray[tempCounter]):
if firstInstance: #finds the first instance of assessment record or deed record and sets it as assessment start
assessmentStart=tempCounter
firstInstance= False
tempCounter+=1
tempCounter=0
while tempCounter < len(lineArray):
if("Boats - " in lineArray[tempCounter] or "Potential Relatives -" in lineArray[tempCounter]):
if firstEndInstance and tempCounter>assessmentStart: #finds first ending instance of boats or potential relatives
#note- boat should come first before potential relatives
assessmentEnd=tempCounter
firstEndInstance= False
tempCounter+=1
#print(assessmentStart, assessmentEnd)
#### end assessmentstart and assessment end section
#looping through the real property section the numRecord times and assessing each '#:'
recordDict={}
i=1
#creating assessment and deed key value pair dictionairy list
assessmentRecords=[]
deedRecords=[]
####
count=assessmentStart
search=str(i)+ ":"
while count<assessmentEnd:
if(search in lineArray[count]):
#print("Found "+ search)
if(lineArray[count][lineArray[count].find(search)+2:lineArray[count].find("Record")].strip()=="Assessment"):
#assessment records
addThis2={} #key value pair dictionairy to be added
endpoint=count+1
while lineArray[endpoint].find("Record for")==-1 and lineArray[endpoint].find("Boat")==-1 and lineArray[endpoint].find("Potential Relatives")==-1:# returns -1 if not found, loops until it finds "Record for"
endpoint+=1
recordDict["Assessment"+str(search)]=[]
#loops to extract each interesting variable
temp=count
while temp<endpoint:
if("Address" in lineArray[temp]):
addThis2["ARAddress"]=lineArray[temp][lineArray[temp].find(":")+1:].strip()
if("County" in lineArray[temp]):
addThis2["ARCounty"]=lineArray[temp][lineArray[temp].find(":")+1:].strip()
if("Recording Date" in lineArray[temp]):
addThis2["ARRecordingDate"]=lineArray[temp][lineArray[temp].find(":")+1:].strip()
if("Sale Date" in lineArray[temp]):
addThis2["ARSaleDate"]=lineArray[temp][lineArray[temp].find(":")+1:].strip()
if("Sale Price" in lineArray[temp]):
addThis2["ARSalePrice"]=lineArray[temp][lineArray[temp].find(":")+1:].strip()
if("Assessed Value" in lineArray[temp]):
addThis2["ARAssessedValue"]=lineArray[temp][lineArray[temp].find(":")+1:].strip()
if("Market Land Value" in lineArray[temp]):
addThis2["ARMarketLandValue"]=lineArray[temp][lineArray[temp].find(":")+1:].strip()
if("Market Improvement Value" in lineArray[temp]):
addThis2["ARMarketImprovementValue"]= lineArray[temp][lineArray[temp].find(":")+1:].strip()
if("Total Market Value" in lineArray[temp]):
addThis2["ARTotalMarketValue"]=lineArray[temp][lineArray[temp].find(":")+1:].strip()
temp+=1
assessmentRecords.append(addThis2)
# print("Assessment")
if(lineArray[count][lineArray[count].find(search)+2:lineArray[count].find("Record")].strip()=="Deed"):
#deed records
addThis3={}
endpoint=count+1
while(lineArray[endpoint].find("Record for")==-1 and lineArray[endpoint].find("Boat")==-1 and lineArray[endpoint].find("Potential Relatives")==-1):# returns -1 if not found, loops until it finds "Record for"
endpoint+=1
recordDict["Deed"+str(search)]=[]
#loops to extract each interesting variable
temp=count
while temp<endpoint:
if("Address" in lineArray[temp]):
addThis3["DRAddress"]=lineArray[temp][lineArray[temp].find(":")+1:].strip()
if("Contract Date" in lineArray[temp]):
#print(lineArray[temp][lineArray[temp].find(":")+1:])
addThis3["DRContractDate"]=lineArray[temp][lineArray[temp].find(":")+1:].strip()
if("Recording Date" in lineArray[temp]):
#print(lineArray[temp][lineArray[temp].find(":")+1:])
addThis3["DRRecordingDate"]=lineArray[temp][lineArray[temp].find(":")+1:].strip()
if("Loan Amount" in lineArray[temp]):
#print(lineArray[temp][lineArray[temp].find(":")+1:])
addThis3["DRLoanAmount"]=lineArray[temp][lineArray[temp].find(":")+1:].strip()
if("Loan Type" in lineArray[temp]):
#print(lineArray[temp][lineArray[temp].find(":")+1:])
addThis3["DRLoanType"]=lineArray[temp][lineArray[temp].find(":")+1:].strip()
if("Title Company" in lineArray[temp]):
#print(lineArray[temp][lineArray[temp].find(":")+1:])
addThis3["DRTitleCompany"]=lineArray[temp][lineArray[temp].find(":")+1:].strip()
if("Transaction Type" in lineArray[temp]):
#print(lineArray[temp][lineArray[temp].find(":")+1:])
addThis3["DRTransactionType"]=lineArray[temp][lineArray[temp].find(":")+1:].strip()
if("Description" in lineArray[temp]):
#print(lineArray[temp][lineArray[temp].find(":")+1:])
addThis3["DRDescription"]=lineArray[temp][lineArray[temp].find(":")+1:].strip()
if("Lender Information" in lineArray[temp]):# lender information is formatted differently so go down a line to extract
#print(lineArray[temp+1].find(":")+1)
addThis3["DRLenderInformation"]=lineArray[temp+1].find(":")+1
temp+=1
deedRecords.append(addThis3)
#print("Deed")
i+=1
search=str(i)+":"
count+=1
#creating a single key value pair dictionairy for each address
#compares assessment records and merges ones with similiar address
cleanAR=[]
cleanDR=[]
a=0
while a<len(assessmentRecords):
base=assessmentRecords[a]
b=a+1
while b<len(assessmentRecords)-a:
comparison=assessmentRecords[b]
ratio=fuzz.ratio(base["ARAddress"].lower(),comparison["ARAddress"].lower())
if(ratio>80): #levenhstein ration >80
agregaEsto={**base,**comparison}
cleanAR.append(agregaEsto)
b+=1
a+=1
#compares deedrecords and merges one with similiar address
a=0
while a<len(deedRecords):
base=deedRecords[a]
b=a+1
while b<len(deedRecords)-a:
comparison=deedRecords[b]
ratio=fuzz.ratio(base["DRAddress"].lower(),comparison["DRAddress"].lower())
if(ratio>80): #levenhstein ration >80
agregaEsto={**base,**comparison}
cleanDR.append(agregaEsto)
b+=1
a+=1
#if clean records are empty, then it sets them equal to the original record lists because it means no match
if(len(cleanAR)==0):
cleanAR=assessmentRecords
if(len(cleanDR)==0):
cleanDR=deedRecords
#print(cleanAR)
#print(cleanDR)
### creation of master list by merging addThis (dictionairy of identifying information) with cleanAR/DR
for thing in cleanAR:
addAR={**addThis,**thing}
completeARList.append(addAR)
for thing in cleanDR:
addDR={**addThis,**thing}
completeDRList.append(addDR)
masterCounter+=1
# In[23]:
for item in analystsOnly:
print(item)
# In[27]:
for item in results:
print(item)
# In[19]:
#writing master AR and DRList with the correct logic
import csv
csv_columns=["analystName","analystID","fuzzRatio","PDFName", "FullName", "FirstName", "LastName","County","PhoneNumber","SSN","DOBMonth","DOBYear","Gender","LexID","Email1","Email2","Email3","Email4","Email5","Email6","CurrentAddress","PropertyAddress","DateRange"]
AR_columns=["ARAddress","ARCounty","ARRecordingDate","ARSaleDate","ARSalePrice","ARAssessedValue","ARMarketLandValue","ARMarketImprovementValue","ARTotalMarketValue"]
DR_columns=["DRAddress","DRContractDate","DRRecordingDate","DRLoanAmount","DRLoanType","DRTitleCompany","DRTransactionType","DRDescription","DRLenderInformation"]
all_columns=csv_columns+AR_columns+DR_columns
masterList=[]
for addyDict in results: #cycles through ALL addresses
mergeThis={}
for ARDict in completeARList:
tempRatio= fuzz.ratio(ARDict["ARAddress"].lower().strip(),addyDict["PropertyAddress"].lower().strip()) #levenhstein ratio
if tempRatio> 90: #arbitrary set point, set very high due to mismatches
addyDict={**addyDict,**ARDict}
for DRDict in completeDRList:
holdingRatio= fuzz.ratio(DRDict["DRAddress"].lower().strip(),addyDict["PropertyAddress"].lower().strip())
if holdingRatio>90:
addyDict={**addyDict,**DRDict}
masterList.append(addyDict)
#print(addyDict)
#creating master excel
csv_file="alphabetizedMasterARDR.csv"
masterList=sorted(masterList,key=lambda x: x['FirstName'])
try:
with open(csv_file,'w') as csvfile:
writer=csv.DictWriter(csvfile,fieldnames=all_columns)
writer.writeheader()
for data in masterList:
writer.writerow(data)
except IOError:
print("IOError")
# In[ ]:
# In[5]:
#writing analysts only file
#for item in analystsOnly:
#print(item)
import csv
csv_columns=["analystName","analystID","fuzzRatio","PDFName", "FullName", "FirstName", "LastName","County","PhoneNumber","SSN","DOBMonth","DOBYear","Gender","LexID","Email1","Email2","Email3","Email4","Email5","Email6","CurrentAddress","PropertyAddress","DateRange"]
dict_data=analystsOnly
#print(dict_data)
csv_file ="AnalystsOnly.csv"
try:
with open(csv_file,'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
writer.writeheader()
for data in dict_data:
writer.writerow(data)
except IOError:
print("I/O error")
# In[12]:
cleanAnalystsOnly=[]
#for item in analystsOnly:
# item.pop("PropertyAddress",None)
# if item not in cleanAnalystsOnly:
# cleanAnalystsOnly.append(item)
for i in range(len(analystsOnly)):
if analystsOnly[i] not in analystsOnly[i + 1:]:
cleanAnalystsOnly.append(analystsOnly[i])
print(cleanAnalystsOnly)
# In[ ]:
# In[ ]:
# In[ ]:
# In[12]:
#writing masterAR and DR list with reversed logic
import csv
csv_columns=["analystName","analystID","fuzzRatio","PDFName", "FullName", "FirstName", "LastName","County","PhoneNumber","SSN","DOBMonth","DOBYear","Gender","LexID","Email1","Email2","Email3","Email4","Email5","Email6","CurrentAddress","PropertyAddress","DateRange"]
AR_columns=["ARAddress","ARCounty","ARRecordingDate","ARSaleDate","ARSalePrice","ARAssessedValue","ARMarketLandValue","ARMarketImprovementValue","ARTotalMarketValue"]
DR_columns=["DRAddress","DRContractDate","DRRecordingDate","DRLoanAmount","DRLoanType","DRTitleCompany","DRTransactionType","DRDescription","DRLenderInformation"]
all_columns=csv_columns+AR_columns+DR_columns
#merge every dict in results and merge every dict in completeARList and completeDRList
#we reversed the logic####
masterList=[]
trueMasterList=[]
for ARDict in completeARList:
largestRatio=0 #finds highest levenhstein ratio
mergeThis={}
for addyDict in results:
tempRatio= fuzz.ratio(ARDict["ARAddress"].lower().strip(),addyDict["PropertyAddress"].lower().strip())
if tempRatio>largestRatio:
mergeThis=addyDict
largestRatio=tempRatio
mergedDict={}
mergedDict={**ARDict,**mergeThis}
print(mergedDict)
masterList.append(mergedDict)
for DRDict in completeDRList:
print("DRDict")
largeRatio=0
mergeEse={}
for largeDict in masterList:
holdingRatio=fuzz.ratio(DRDict["DRAddress"].lower().strip(),largeDict["PropertyAddress"].lower().strip())
if holdingRatio>largeRatio:
mergeEse=largeDict
largeRatio=holdingRatio
mergeDict={}
mergeDict={**DRDict,**mergeEse}
trueMasterList.append(mergeDict)
#creating master excel
csv_file="masterAR_DR.csv"
try:
with open(csv_file,'w') as csvfile:
writer=csv.DictWriter(csvfile,fieldnames=all_columns)
writer.writeheader()
for data in trueMasterList:
writer.writerow(data)
except IOError:
print("IOError")
# In[2]:
#dictionairy tests
empty={}
testDict={"key1":"hey","key2":"hello"}
merger={**empty,**testDict}
print(merger)
merge2={**testDict,**empty}
print(merge2)
# In[11]:
#writing completeARList and completeDRList to excel
import csv
csv_columns=["analystName","analystID","fuzzRatio","PDFName", "FullName", "FirstName", "LastName","County","PhoneNumber","SSN","DOBMonth","DOBYear","Gender","LexID","Email1","Email2","Email3","Email4","Email5","Email6","CurrentAddress","PropertyAddress","DateRange"]
AR_columns=["ARAddress","ARCounty","ARRecordingDate","ARSaleDate","ARSalePrice","ARAssessedValue","ARMarketLandValue","ARMarketImprovementValue","ARTotalMarketValue"]
DR_columns=["DRAddress","DRContractDate","DRRecordingDate","DRLoanAmount","DRLoanType","DRTitleCompany","DRTransactionType","DRDescription","DRLenderInformation"]
#creating AR excel
csv_file="AR.csv"
allARColumns=csv_columns+AR_columns
try:
with open(csv_file,'w') as csvfile:
writer=csv.DictWriter(csvfile,fieldnames=allARColumns)
writer.writeheader()
for data in completeARList:
writer.writerow(data)
except IOError:
print("I/O error")
#creating DR excel
csv_file="DR.csv"
allDRColumns=csv_columns+DR_columns
try:
with open(csv_file,'w') as csvfile:
writer=csv.DictWriter(csvfile,fieldnames=allDRColumns)
writer.writeheader()
for data in completeDRList:
writer.writerow(data)
except IOError:
print("IOError")
# In[3]:
#converting to CSV file, creating analysts.csv
import csv
csv_columns=["analystName","analystID","fuzzRatio","PDFName", "FullName", "FirstName", "LastName","County","PhoneNumber","SSN","DOBMonth","DOBYear","Gender","LexID","Email1","Email2","Email3","Email4","Email5","Email6","CurrentAddress","PropertyAddress","DateRange"]
dict_data=results
#print(dict_data)
csv_file ="AnalystsAndAllAddresses.csv"
try:
with open(csv_file,'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
writer.writeheader()
for data in dict_data:
writer.writerow(data)
except IOError:
print("I/O error")
# In[ ]:
# In[ ]:
# In[ ]:
#don't run below bc it generates the text files with -layout formula
# In[1]:
# creates table version with -table
import os, sys, subprocess
for filename in os.scandir():
if filename.is_file():
filePath=filename.path
pdfName=filePath[filePath.find('.')+2:filePath.find(".pdf")]
txtFileName= pdfName+"_Table.txt"
command= "C:\\Users\\evany_cdhq038\\OneDrive\\Desktop\\Economics_Research\\xpdf-tools-win-4.03\\bin64\\pdftotext.exe -table C:\\Users\\evany_cdhq038\\OneDrive\\Desktop\\Economics_Research\\All_Reports\\"
command= command + pdfName+".pdf " + txtFileName
print(command)
p= subprocess.Popen(command,shell=True, stdout=subprocess.PIPE)
# In[ ]:
# In[2]:
import os
import sys
import subprocess
for filename in os.scandir():
if filename.is_file():
filePath=filename.path
pdfName=filePath[filePath.find('.')+2:filePath.find(".pdf")]
txtFileName= pdfName+".txt"
command= "C:\\Users\\evany_cdhq038\\OneDrive\\Desktop\\Economics_Research\\xpdf-tools-win-4.03\\bin64\\pdftotext.exe -layout C:\\Users\\evany_cdhq038\\OneDrive\\Desktop\\Economics_Research\\All_Reports\\"
command= command + pdfName+".pdf " + txtFileName
print(command)
p= subprocess.Popen(command,shell=True, stdout=subprocess.PIPE)
# In[ ]:
try:
q = subprocess.Popen([PDFTOTEXT_PATH,'-layout',pdfPath,'-'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
pdfText, err = q.communicate()
except:
print 'pdftotext failed'
# In[ ]:
PDFTOTEXT_PATH = '/usr/local/bin/pdftotext'