Models
File implementing logic of the models
HuggingFaceValidator
Check fair housing violation with HuggingFace model
Source code in app/models.py
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 | |
__init__(model_path, pretrained_model)
Init HuggingFace model and tokenizer
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_path |
str
|
path to the trained HuggingFace model |
required |
pretrained_model |
str
|
HuggingFace pretrained model for tokenizer |
required |
Source code in app/models.py
510 511 512 513 514 515 516 517 518 519 520 521 | |
load_huggingface_model(model_path)
staticmethod
Load HuggingFace model from path
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_path |
str
|
The path to HuggingFace model |
required |
Returns:
| Name | Type | Description |
|---|---|---|
model |
model
|
HuggingFace model |
Source code in app/models.py
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 | |
sentences_encoder(sentences)
Tokenize sentences for HuggingFace transformer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sentences |
list
|
List of sentences |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ids |
tensor
|
tokens ids |
attention_masks |
tensor
|
tokens attention masks |
Source code in app/models.py
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 | |
validate(sentences)
Function for calculating predictions on a set of text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sentences |
list
|
List of sentences in remark |
required |
Returns:
| Name | Type | Description |
|---|---|---|
probabilities |
list
|
Probability of each sentence prediction |
Source code in app/models.py
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 | |
Preprocessor
Preprocessor for preprocessing input text and sentence segmentation.
Source code in app/models.py
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 | |
__init__()
Init re for html tags and spacy.sentencizer
Source code in app/models.py
25 26 27 28 29 30 | |
get_sentences(text, lowercase=False)
Extract sentences from text
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text |
str
|
input text |
required |
lowercase |
bool
|
lowercase sentences if true |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
sentences |
list
|
Initial sentences in list |
clean_sentences |
list
|
Preprocessed list of sentences |
locations |
list
|
Locations of the sentences in remark |
Source code in app/models.py
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 | |
preprocess_text(text)
Preprocess raw text. Remove html tags
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text |
str
|
input text |
required |
Returns:
| Name | Type | Description |
|---|---|---|
clean_text |
str
|
text without html tags |
Source code in app/models.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
ProfanityValidator
Check banned words
Source code in app/models.py
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 | |
__init__(censor_words_path=config.CENSORED_WORLDS_PATH)
Init profanity lib with banned words file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
censor_words_path |
str
|
path of the censor words. |
CENSORED_WORLDS_PATH
|
Source code in app/models.py
405 406 407 408 409 410 411 412 413 414 415 | |
validate(sentences)
Validate if possible censored words are available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sentences |
list[str]
|
input list of sentences |
required |
Returns:
| Name | Type | Description |
|---|---|---|
predictions |
ndarray[int]
|
list of binary predictions |
words |
list[str]
|
profanity words |
Source code in app/models.py
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 | |
RuleBasedValidator
Validating text with spacy matcher.
Source code in app/models.py
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 | |
__init__()
Init spacy Matcher
Source code in app/models.py
166 167 168 169 170 171 | |
check_competitors(sentences, urduc)
Validate input sentences
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
urduc |
int
|
identifier for urduc |
required |
sentences |
list[str]
|
input list of sentences |
required |
Returns:
| Name | Type | Description |
|---|---|---|
predictions |
(list[int])
|
list of binary predictions |
names |
list[int]
|
list of company names |
Source code in app/models.py
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 | |
check_name_variation(sentence, variation, max_length)
Check if pattern exist in the given sentence. Function takes tokens of the pattern and check if increasing sequence of occurrence is exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sentence |
str
|
input sentence |
required |
variation |
list[str]
|
list of strings (competitor pattern) |
required |
max_length |
int
|
number for filtering sequences where np.diff(seq)<max_length |
required |
Returns:
| Name | Type | Description |
|---|---|---|
indices |
list[int]
|
indices of the pattern |
Source code in app/models.py
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 | |
collect_matches(matches, doc, patterns=None)
staticmethod
Collected tokens from matches
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matches |
Union[List[Tuple[int, int, int]], List[Span]]
|
list of matches with match_id and indices |
required |
doc |
spacy doc object
|
spacy doc object for sentence |
required |
patterns |
list[str]
|
list of strings for collecting specific patterns |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
matched_phrases |
list[str]
|
matched phrases |
Source code in app/models.py
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 | |
construct_patterns()
Construct violation patterns. competitors patterns - case sensitive strict_violations - not case sensitive
Source code in app/models.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | |
find_word_indices(sentence, word)
staticmethod
Find word all occurrences in the sentence with generator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sentence |
str
|
input sentence |
required |
word |
str
|
word to search |
required |
Returns:
| Type | Description |
|---|---|
python generator object
|
|
Source code in app/models.py
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | |
validate(sentences, urduc)
Validate input sentences
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
urduc |
int
|
identifier for urduc |
required |
sentences |
list[str]
|
input list of sentences |
required |
Returns:
| Name | Type | Description |
|---|---|---|
result |
list
|
list of binary predictions |
Source code in app/models.py
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 | |
validate_competitors(sentences, urduc=2)
Validate competitors
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sentences |
input sentences |
required | |
urduc |
enum for urduc |
2
|
Returns:
| Name | Type | Description |
|---|---|---|
predictions |
(list[int])
|
predictions for each sentence |
names |
list[str]
|
banned words |
Source code in app/models.py
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 | |
SpacyValidator
Source code in app/models.py
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 | |
load_spacy_model(model_path)
staticmethod
Load spacy model from path
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_path |
str
|
The path of spacy model |
required |
Returns:
| Name | Type | Description |
|---|---|---|
model |
spacy model
|
model |
Source code in app/models.py
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 | |
validate(sentences, threshold)
Function for calculating predictions on a set of text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sentences |
list
|
List of sentences in remark |
required |
threshold |
float
|
threshold of the violation prediction |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool_predictions |
list
|
Prediction of each sentence in boolean format |
probabilities |
list
|
Probability of each prediction |
Source code in app/models.py
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 | |
Validator
Class for interacting with the spacy model+strict violations
Source code in app/models.py
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 | |
validate(sentences, clean_sentences, urduc, threshold=config.PREDICTION_THRESHOLD)
Validate sentences with RuleBasedValidator,RuleBasedValidator and SpacyValidator
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
urduc |
identifier for urduc |
required | |
sentences |
list of input sentences |
required | |
clean_sentences |
list of preprocessed sentences |
required | |
threshold |
threshold for calculating predictions |
PREDICTION_THRESHOLD
|
Returns:
| Name | Type | Description |
|---|---|---|
final_predictions |
list[bool]
|
1 if sentence is valid |
final_probabilities |
list[float]
|
the probability of validation |
violations |
list[str]
|
list of violations |
Source code in app/models.py
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 | |
validate_competitors_and_profanity(preprocessed_sentences, text, urduc)
Validate sentences with RuleBasedValidator,RuleBasedValidator and SpacyValidator
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preprocessed_sentences |
list[str]
|
list of preprocessed sentences |
required |
urduc |
int
|
identifier for urduc |
required |
text |
str
|
input text (not preprocessed) |
required |
Returns:
| Name | Type | Description |
|---|---|---|
pred |
bool
|
0 for violation |
violations |
set[str]
|
set of violations |
Source code in app/models.py
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 | |
get_nested_pattern(violations, case_sensitive=True)
Construct spacy patterns from the list of strings. Flag case_sensitive will be constructing case-sensitive patters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
violations |
list[str]
|
input list of strings |
required |
case_sensitive |
bool
|
True for case-sensitive patters. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
pattern |
list[list[dict[str, any]]]
|
constructed spacy pattern |
Source code in app/models.py
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 | |
split_list_of_strings(strings, tokenize=False)
Split list of strings to tokens
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokenize |
bool
|
tokenize with Spacy or split with spaces |
False
|
strings |
list[str]
|
input list of strings |
required |
Returns:
| Name | Type | Description |
|---|---|---|
result |
list[list[str]]
|
nested list with split items |
Source code in app/models.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |