API
cleaning
¶
cleaning_functions
¶
clean_address(raw)
¶
Given a raw address, first conduct baseline address cleaning, then identify and return the components of the address. Where possible, infer correct city and state value from the zip code given.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raw
|
str
|
A raw address. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
A dictionary of address components. |
Source code in src/chainlink/cleaning/cleaning_functions.py
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 |
|
clean_names(raw)
¶
Given a raw name string, clean the name and return it. Contains conditional logic based on the source of the data to handle data-specific cleaning cases. Returns none if the name resembles a list of excluded strings. Strips most non-alphanumeric characters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raw
|
str
|
A raw name string. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str | None
|
A cleaned name string. |
Source code in src/chainlink/cleaning/cleaning_functions.py
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 |
|
clean_zipcode(raw)
¶
Modified from the function written by Anthony Moser of the deseguys project.
Returns a 5-digit zipcode from a string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raw
|
any
|
A zipcode. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
A 5-digit zipcode or an empty string. |
Source code in src/chainlink/cleaning/cleaning_functions.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
|
identify_state_city(zipcode)
¶
Use zipcode to look up state and city info using the uszipcode API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
zipcode
|
str
|
A zipcode. |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
tuple
|
A tuple of city and state, or (None, None) if the lookup failed. |
Source code in src/chainlink/cleaning/cleaning_functions.py
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 |
|
predict_org(name)
¶
Given a string, predict whether or not the string is an organization name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
An entity name. |
required |
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
1 if the name is an organization, 0 if the name is an individual. |
Source code in src/chainlink/cleaning/cleaning_functions.py
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 |
|
remove_initial_I(raw)
¶
Remove the "I" or I" present for some names in corporation and LLC data where name was incorrectly entered in the style of "I, John Smith" instead of just "John Smith"
Source code in src/chainlink/cleaning/cleaning_functions.py
321 322 323 324 325 326 327 328 329 330 331 |
|
link
¶
link_generic
¶
create_across_links(db_path, new_schema, existing_schema, link_exclusions)
¶
For each entity in the existing_db list, create links between the new entity and the existing entity.
for old_entity in existing_db
-find all the name links old_entity.name to new_entity.name, etc. -find all the address links old_entity.address to new_entity.address, etc. -match by raw address string -match by clean street string -if street id matches, match by unit -match street name and number if zipcode matches
Returns: None
Source code in src/chainlink/link/link_generic.py
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 |
|
create_tfidf_across_links(db_path, new_schema, existing_schema, link_exclusions)
¶
create all fuzzy links across new entity and existing entity
Returns: None
Source code in src/chainlink/link/link_generic.py
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 |
|
create_tfidf_within_links(db_path, schema_config, link_exclusions)
¶
create tfidf links within entity
Returns: None
Source code in src/chainlink/link/link_generic.py
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 |
|
create_within_links(db_path, schema_config, link_exclusions)
¶
Creates exact string matches on name and address fields for entity and entity.
For each file find the links with the file
-find all the name links includes name1 to name1, name1 to name2, etc. -find all the address links includes address1 to address1, address1 to address2, etc. -match by raw address string -match by clean street string -if street id matches, match by unit -match street name and number if zipcode matches -find all name and address links across tables within the entity
Returns: None
Source code in src/chainlink/link/link_generic.py
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 |
|
link_utils
¶
execute_address_fuzzy_link(db_path, left_entity, left_table, left_ent_id, left_address_col, right_entity, right_table, right_ent_id, right_address_col, tfidf_table='link.tfidf_staging', skip_address=False, link_exclusions=None)
¶
fuzzy address matching
Source code in src/chainlink/link/link_utils.py
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 |
|
execute_fuzzy_link(db_path, left_entity, left_table, left_ent_id, left_name_col, right_entity, right_table, right_ent_id, right_name_col, tfidf_table='link.tfidf_staging', link_exclusions=None)
¶
Given two tables and a tfidf matching entity table, create a fuzzy match between the two tables. Creates a match column called {left_entity}{left_table}{left_name_col}{right_entity}{right_table}{right_name_col}_fuzzy_match and appends to link table link.{left_entity}{right_entity}
Source code in src/chainlink/link/link_utils.py
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 |
|
execute_match(db_path, match_type, left_entity, left_table, left_matching_col, left_matching_id, left_ent_id, right_entity, right_table, right_matching_col, right_matching_id, right_ent_id, skip_address=False, link_exclusions=None)
¶
Exact matches between two column in two tables. Creates a match column called {left_entity}{left_table}{left_matching_col}{right_entity}{right_table}{right_matching_col}{match_type} and appends to link table link.{left_entity}_{right_entity}
Returns: None
Source code in src/chainlink/link/link_utils.py
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 |
|
execute_match_address(db_path, left_entity, left_table, left_address, left_ent_id, right_entity, right_table, right_address, right_ent_id, skip_address=False, link_exclusions=None)
¶
given a two address columns, match the addresses: * match by raw address string * match by clean street string * if street id matches, match by unit * match street name and number if zipcode matches
Creates four match columns called {left_entity}{left_table}{left_matching_col}{right_entity}{right_table}{right_matching_col}{match_type} and appends to link table link.{left_entity}_{right_entity}
Returns: None
Source code in src/chainlink/link/link_utils.py
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 |
|
execute_match_processing(db_conn, link_table, out_temp_table_name, id_col_1, match_name_col, id_col_2)
¶
Steps to run after matches are created. append matches to link table, set null matches to 0, and drop temp table of matches runs in execute_match()
Returns: None
Source code in src/chainlink/link/link_utils.py
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 |
|
execute_match_unit(db_path, left_entity, right_entity, street_match_to_check, left_table, left_address, left_ent_id, right_table, right_address, right_ent_id, skip_address=False, link_exclusions=None)
¶
Given two address columns, if street id matches, match by unit.
Creates a match column called {left_entity}{left_table}{left_address}{right_entity}{right_table}{right_address}_unit_match and appends to link table link.{left_entity}{right_entity}
Source code in src/chainlink/link/link_utils.py
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 |
|
generate_combos_within_across_tables(name_idx, address_idx=None)
¶
create all possible combinations of across tables in the same entity, but do not include combos within the same table if address_idx is not empty, also create across combos between address tables
Source code in src/chainlink/link/link_utils.py
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 |
|
generate_tfidf_links(db_path, table_location='entity.name_similarity', source_table_name=None)
¶
create a table of tfidf matches between two entities and adds to db
Returns: None
Source code in src/chainlink/link/link_utils.py
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 |
|
query_append_to_links(link_table_exists, link_table, table_to_append, id_col1, id_col2)
¶
query to append links to link table runs in execute_match_processing()
Source code in src/chainlink/link/link_utils.py
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 |
|
tfidf_utils
¶
adjust_and_replace(string)
¶
replace specified words with blanks and other words with their corresponding values for ngrams
Source code in src/chainlink/link/tfidf_utils.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
|
clean_matches(matches_df)
¶
remove self matches and duplicates in match dataframe
Returns: pl.DataFrame
Source code in src/chainlink/link/tfidf_utils.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
|
database_query(db_path, table_name=None, limit=None)
¶
queries entities for comparison
Source code in src/chainlink/link/tfidf_utils.py
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 |
|
get_matches_df(sparse_matrix, name_vector, top=None)
¶
create a matches dataframe given matrix of ngrams references sparse_matrix - matrix from vectorized comparison calculations name_vector - list of names to compare id_vector - id of distinct name from entities list
Source code in src/chainlink/link/tfidf_utils.py
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 |
|
ngrams(string, n=3)
¶
split string into substrings of length n, return list of substrings
Source code in src/chainlink/link/tfidf_utils.py
162 163 164 165 166 167 168 |
|
superfast_tfidf(entity_list, id_col='name_id', entity_col='entity')
¶
returns sorted list of top matched names
Source code in src/chainlink/link/tfidf_utils.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
load
¶
load_generic
¶
load_generic(db_path, schema_config, bad_addresses)
¶
Loads a generic file into the database.
Reads config file, loops through each file listed, cleans the data, creates a unique id for name, street, and street_name, loads into cleaned files into a database using the schema name from the config file, and lastly updates the entity name files.
Returns None.
Source code in src/chainlink/load/load_generic.py
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 |
|
load_utils
¶
clean_generic(df, config)
¶
Cleans the name and address for a generic file. Appends a new column with the cleaned name and address.
Returns a pl.DataFrame
Source code in src/chainlink/load/load_utils.py
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 |
|
create_id_col(df, col)
¶
Adds an id column to the DataFrame using pl.Series.hash function
Returns a pl.DataFrame
Source code in src/chainlink/load/load_utils.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
|
execute_flag_bad_addresses(db_conn, table, address_col, bad_addresses)
¶
Flags rows with bad addresses as provided by user
Source code in src/chainlink/load/load_utils.py
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 |
|
load_to_db(df, table_name, db_conn, schema)
¶
Loads parquet file into table in database.
Parameters¶
filepath : str Directory of parquet file to load onto database. table_name : str Name of resulting table in database. db_conn : object Connection object to desired duckdb database. schema : str Name of schema for resulting table in database.
Returns¶
None
Source code in src/chainlink/load/load_utils.py
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 |
|
update_entity_ids(df, entity_id_col, db_conn)
¶
Adds new ids to the entity schema table. If the value is already in the table, it is not added.
Returns None
Source code in src/chainlink/load/load_utils.py
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 |
|
validate_input_data(df, table_config)
¶
Validates input data against configuration requirements
Source code in src/chainlink/load/load_utils.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
|
main
¶
chainlink(config, config_path=DIR / 'configs/config.yaml')
¶
Given a correctly formatted config file, * load in any schemas in the config that are not already in the database * create within links for each new schema * create across links for each new schema with all existing schemas
Returns true if the database was created successfully.
Source code in src/chainlink/main.py
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 |
|
main(config=typer.Argument(DIR / 'config' / 'chainlink_config.yaml', exists=True, readable=True))
¶
Given a correctly formatted config file, * load in any schemas in the config that are not already in the database * create within links for each new schema * create across links for each new schema with all existing schemas
Returns 'True' if the database was created successfully.
Source code in src/chainlink/main.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
|
utils
¶
add_schema_config(config)
¶
Helper to add a schema to an existing config
Source code in src/chainlink/utils.py
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
|
add_table_config(config, schema_name)
¶
Helper to add a table to an existing schema
Source code in src/chainlink/utils.py
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 |
|
check_table_exists(db_conn, schema, table_name)
¶
check if a table exists
Returns: bool
Source code in src/chainlink/utils.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
|
create_config()
¶
Helper to create config file from user input if not pre created
Source code in src/chainlink/utils.py
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 |
|
export_tables(db_path, data_path)
¶
export all tables from database to parquet files in {data_path}/export directory
Returns: None
Source code in src/chainlink/utils.py
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 |
|
load_config(file_path)
¶
load yaml config file, clean up column names
Returns: dict
Source code in src/chainlink/utils.py
39 40 41 42 43 44 45 46 47 48 49 |
|
setup_logger(name, log_file, level=logging.DEBUG)
¶
To setup as many loggers as you want
from https://stackoverflow.com/questions/11232230/logging-to-two-files-with-different-settings¶
Source code in src/chainlink/utils.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
update_config(db_path, config, config_path)
¶
update config by adding in all existing link columns and last updated time. writes config back out to config.yaml
Returns: None
Source code in src/chainlink/utils.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
|
validate_config(config)
¶
Validates the configuration against a schema
Source code in src/chainlink/utils.py
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 |
|