Return a text suitable for SPDX license identifier detection cleaned from certain leading and trailing punctuations and normalized for spaces.
(text)
| 356 | |
| 357 | |
| 358 | def clean_text(text): |
| 359 | """ |
| 360 | Return a text suitable for SPDX license identifier detection cleaned from |
| 361 | certain leading and trailing punctuations and normalized for spaces. |
| 362 | """ |
| 363 | if is_markup_text(text): |
| 364 | text = demarkup_text(text) |
| 365 | |
| 366 | dangling_markup = ['</a>', '</p>', '</div>', '</licenseUrl>'] |
| 367 | for markup in dangling_markup: |
| 368 | if markup in text: |
| 369 | text = text.replace(markup, '') |
| 370 | |
| 371 | text = ' '.join(text.split()) |
| 372 | punctuation_spaces = "!\"#$%&'*,-./:;<=>?@[\\]^_`{|}~\t\r\n " |
| 373 | # remove significant expression punctuations in wrong spot: closing parens |
| 374 | # at head and opening parens or + at tail. |
| 375 | leading_punctuation_spaces = punctuation_spaces + ")+" |
| 376 | trailng_punctuation_spaces = punctuation_spaces + "(" |
| 377 | text = text.lstrip(leading_punctuation_spaces).rstrip(trailng_punctuation_spaces) |
| 378 | # try to fix some common cases of leading and trailing missing parense |
| 379 | open_parens_count = text.count('(') |
| 380 | close_parens_count = text.count(')') |
| 381 | if open_parens_count == 1 and not close_parens_count: |
| 382 | text = text.replace('(', ' ') |
| 383 | elif close_parens_count == 1 and not open_parens_count: |
| 384 | text = text.replace(')', ' ') |
| 385 | |
| 386 | if '">' in text: |
| 387 | text_fragments = text.split('">') |
| 388 | if text_fragments[1] in text_fragments[0]: |
| 389 | text = text_fragments[0] |
| 390 | |
| 391 | return ' '.join(text.split()) |
| 392 | |
| 393 | |
| 394 | _split_spdx_lid = re.compile( |