MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / clean_text

Function clean_text

src/licensedcode/match_spdx_lid.py:358–391  ·  view source on GitHub ↗

Return a text suitable for SPDX license identifier detection cleaned from certain leading and trailing punctuations and normalized for spaces.

(text)

Source from the content-addressed store, hash-verified

356
357
358def 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(

Callers 4

test_clean_lineMethod · 0.90
test_clean_line_nugetMethod · 0.90
prepare_textFunction · 0.70

Calls 5

is_markup_textFunction · 0.90
demarkup_textFunction · 0.90
replaceMethod · 0.45
joinMethod · 0.45
splitMethod · 0.45

Tested by 3

test_clean_lineMethod · 0.72
test_clean_line_nugetMethod · 0.72