Spaces:
Runtime error
Runtime error
File size: 18,857 Bytes
cedd239 a804ced cedd239 19dab1b a804ced 19dab1b cedd239 19dab1b a804ced cedd239 19dab1b e41b03f a804ced 19dab1b e41b03f 19dab1b e41b03f 19dab1b e41b03f a804ced e41b03f a804ced 19dab1b a804ced 19dab1b a804ced 19dab1b a804ced 19dab1b cedd239 a804ced cedd239 19dab1b a804ced 19dab1b e41b03f a804ced 19dab1b a804ced 19dab1b a804ced 19dab1b e41b03f 19dab1b e41b03f a804ced e41b03f a804ced 19dab1b a804ced e41b03f a804ced cedd239 19dab1b cedd239 a804ced cedd239 a804ced 95fa18f 19dab1b e41b03f 19dab1b a804ced 19dab1b e41b03f 95fa18f 19dab1b e41b03f 19dab1b a804ced 19dab1b a804ced 19dab1b e41b03f a804ced e41b03f 19dab1b e41b03f a804ced 19dab1b a804ced 19dab1b a804ced 19dab1b cedd239 a804ced e41b03f 19dab1b e41b03f a804ced 19dab1b a804ced 19dab1b 95fa18f 19dab1b e41b03f 19dab1b 95fa18f 19dab1b 95fa18f 19dab1b 95fa18f 19dab1b a804ced 19dab1b e41b03f 19dab1b e41b03f 19dab1b cedd239 a804ced e41b03f 19dab1b a804ced 19dab1b 95fa18f 19dab1b a804ced e41b03f 19dab1b e41b03f a804ced e41b03f |
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 |
import re
import json
from bs4 import BeautifulSoup
from striprtf.striprtf import rtf_to_text
from collections import defaultdict
PARA_BREAK = "para___break"
seperator = "=" * 50
verbosity = 0
def extract_author_details(text, verbosity=0):
"""
Extracts important author information from the license text.
Parameters
----------
text : str
Raw License text.
verbosity : int, optional
The level of print statements on the output console. The default is 0.
Returns
-------
text : str
License text with author details removed.
author_details : list
A list of important author details.
"""
author_details_pattern = r"(@(author|license|copyright|package).*)"
author_details = list()
text = re.sub(author_details_pattern, lambda m: author_details.append(m.group(1)), text)
if author_details and verbosity != 0:
print(seperator)
print(seperator)
print("Following author details were extracted:")
print(seperator)
print(author_details)
print()
return text, author_details
def php_cleaner(text):
"""
Cleans the license file in PHP format.
Parameters
----------
text : str
Raw License text.
Returns
-------
str
Cleaned License text with PHP script removed.
"""
try:
return re.findall("\/\*[\S\s]*?\*\/", text)[0]
except:
return ""
# return re.findall(r"(?<=<\?php\\n\\n\/\*\*\\n \*).*(?=\\n \*\/)", text)[0]
def html_cleaner(text):
"""
Cleans the license file in HTML format.
Parameters
----------
text : str
Raw License text.
Returns
-------
str
Cleaned License text with HTML script removed.
"""
soup = BeautifulSoup(text, features="html.parser")
text = soup.body.text
if not text:
return ""
return text
def json_cleaner(text_dict):
"""
Cleans the license file in JSON format.
Parameters
----------
text_dict : dict
Dictonary as read from Raw License file.
Returns
-------
text : str
Cleaned License text with JSON format normalized to text.
"""
text = ""
for key in text_dict.keys():
if key in ("description", "license"):
text += key
text += ": "
text += str(text_dict[key])
text += ", "
return text
def rtf_cleaner(text):
"""
Cleans the license file in RTF format.
Parameters
----------
text : str
Raw License text.
Returns
-------
str
Cleaned License text with RTF script removed.
"""
return rtf_to_text(text)
def url_cleaner(text):
"""
Removes URLs from the License text.
Parameters
----------
text : str
Raw License text.
Returns
-------
str
Cleaned License text with URLs removed.
"""
return re.sub(r"\(?http\S+\)?", "", text)
def email_cleaner(text):
"""
Removes emails from the License text.
Parameters
----------
text : str
Raw License text.
Returns
-------
str
Cleaned License text with emails removed.
"""
return re.sub(r"[\w\._-]+@\w{2,}\.\w+", "", text)
def var_cleaner(text):
"""
Removes potential variable names from the License text.
Parameters
----------
text : str
Raw License text.
Returns
-------
str
Cleaned License text with variable names removed.
"""
text = re.sub(r"\$\w+", "", text)
text = re.sub(r"{[{}()\w\s._,\[\]'\"]+}", "", text)
# text = re.sub(r"[a-zA-Z\(\)_'\"]+\.[a-zA-Z_]+", "", text)
return text
def character_cleaner(text):
"""
Removes unnecessary special characters from the License text.
Parameters
----------
text : str
Raw License text.
Returns
-------
text : str
Cleaned License text with some special characters removed.
"""
text = text.replace(PARA_BREAK, f" {PARA_BREAK} ")
text = url_cleaner(text)
text = text.replace(f" {PARA_BREAK} ", PARA_BREAK)
text = email_cleaner(text)
text = var_cleaner(text)
text = re.sub("\s*(;quot;|&)\s*", " ", text)
text = re.sub("[\n]{2,}", ". ", text)
text = re.sub("[:%#<>=*\-/·\s{}]+", " ", text)
text = re.sub("[\. ]{2,}", ". ", text)
html_strs = [
"’",
"“",
"·",
"±",
"…",
"‚",
"—",
"'",
"™",
"‡",
"•",
"«",
"′",
""",
"‘",
"≈",
"″",
"½",
"§",
"£",
"¢",
"¶",
"»",
"†",
"”",
"€",
"©",
"„",
"–",
"°",
"®",
"<",
">",
"≤",
"≥",
"≠"
]
for html_str in html_strs:
text = re.sub(html_str, "", text)
return text
def isEnglish(s):
"""
Checks whether the License text is in English or not.
Parameters
----------
s : str
Raw License text.
Returns
-------
bool
True if complete License text is in English, False otherwise.
"""
try:
s.encode(encoding="utf-8").decode("ascii")
except UnicodeDecodeError:
return False
else:
return True
def split_definitions_exceptions(text, remove_exceptions, verbosity=0):
"""
Extract definitions from the License text
Parameters
----------
text : str
Raw License text.
remove_exceptions : bool
True if we want to remove exceptions from the License text, False
otherwise
verbosity : int, optional
The level of print statements on the output console. The default is 0.
Returns
-------
paras : list
A list of paragraphs from License text with definitions and exceptions
removed.
definitions : str
Definitions extracted from the License text.
exceptions : list
A list of paragraphs which contain exceptions .
"""
definitions = ""
if "Definitions" in text:
try:
def_pattern = r"([S|s]ection )?[0-9] ?[\.|-|–]? ?([A|a]dditional )?[D|d]efinitions"
after_def_pattern = r"\s+(Section )?[0-9]\.? [\.|-|–]? ?[A-Z][a-z]+"
def_pos = re.search(def_pattern, text).span()
other_start_pos = re.search(after_def_pattern, text[def_pos[1]:]).span()[0]
definitions = text[def_pos[0]: def_pos[1] + other_start_pos].strip() + "\n\n"
text = text[:def_pos[0]] + text[def_pos[1] + other_start_pos:]
except:
pass
paras, more_defs = extract_relevant_paras(
split_paras(text, verbosity=verbosity),
verbosity=verbosity
)
definitions += more_defs.strip()
definitions = "\n\n".join(split_paras(definitions, verbosity=verbosity))
paras, exceptions = get_exeptions(paras, remove_exceptions, verbosity=verbosity)
return paras, definitions, exceptions
def discard_text_after_end_tnc(text):
"""
Discards text after "END OF TERMS AND CONDITIONS"
Parameters
----------
text : str
Raw License text.
Returns
-------
str
License text with irrelavant information after "END OF TERMS AND CONDITIONS" removed.
"""
return text.split("END OF TERMS AND CONDITIONS")[0]
def clear_preamble(text):
"""
Cleans Preamble from the License text
Parameters
----------
text : str
Raw License text.
Returns
-------
text : str
License text with Preamble removed.
"""
preamble_pattern = "Preamble"
dist_and_mod_pattern = "distribution\s+and\s+modification\s+follow\.?"
if preamble_pattern in text:
preamble_split = text.split(preamble_pattern)
if len(preamble_split) != 2:
return text
try:
after_preamble_end = re.split(dist_and_mod_pattern, preamble_split[1])[1]
if len(preamble_split[0]) > 100:
text = preamble_split[0] + after_preamble_end.strip()
except:
pass
return text
def gnu_cleaner(text):
"""
Cleans GNU text such as discarding Preamble and text after end of terms
and conditions.
Parameters
----------
text : str
Raw License text.
Returns
-------
preamble_cleared_text : str
License text with irrelavant information in Preamble and text after end
of terms and conditions removed.
"""
before_end_tnc = discard_text_after_end_tnc(text)
preamble_cleared_text = clear_preamble(before_end_tnc)
return preamble_cleared_text
def preprocess_text(text):
"""
Preprocesses License text considering different License types.
Parameters
----------
text : str
Raw License text.
Returns
-------
text : str
License text with irrelavant information in Preamble and text after end
of terms and conditions removed.
"""
if "GNU" in text or "Apache" in text:
text = gnu_cleaner(text)
return text
def clean_if_else(text):
"""
Removes specific if-else conditions from the License text
Parameters
----------
text : str
Raw License text.
Returns
-------
str
Cleaned License text with if-else conditions removed.
"""
return re.sub(r"#\bif[\s\S]+?#endif\s*", "", text).strip()
def clean_comments(text):
"""
Cleans specific comment formats from the License texts
Parameters
----------
text : str
Raw License text.
Returns
-------
str
Cleaned License text with comments conditions removed.
"""
return re.sub(r"[\`'\"]{3,}[\s\S]*?[\`'\"]{3,}", "", text).strip()
def script_cleaner(text):
"""
Cleans the script text from License text to extract the main content.
Parameters
----------
text : str
Raw License text.
Returns
-------
str
Cleaned License text without scripts.
"""
try:
if "<?php" in text:
text = php_cleaner(text)
elif "</html>" in text:
text = html_cleaner(text)
elif "\\rtf" in text:
text = rtf_cleaner(text)
elif text[0] == "{" and text[-1] == "}":
text = json_cleaner(json.loads(text))
except:
pass
if not text:
return ""
text = clean_if_else(text)
text = clean_comments(text)
return text
def split_paras(text, verbosity=0):
"""
Splits the text into paragraphs.
Parameters
----------
text : str
Raw License text.
verbosity : int, optional
The level of print statements on the output console. The default is 0.
Returns
-------
paras : list
A list of split paragraphs.
"""
text = re.sub(r"\n{4,}", "\n"*4, text)
if len(re.findall("\n\n\n\n", text)) >= 2:
paras = text.split("\n\n\n\n")
paras = [re.sub(r"\n{1,3}", " ", para) for para in paras]
elif len(re.findall("\n\n", text)) >= 2:
paras = text.split("\n\n")
paras = [re.sub(r"\n", " ", para) for para in paras]
elif len(re.findall("\n", text)) >= 2:
paras = text.split("\n")
else:
paras = [text]
paras = [para.strip() for para in paras]
if verbosity != 0:
print(seperator)
print(seperator)
print("These are the split paras in the text:")
for para in paras:
if not para.strip():
continue
print(seperator)
print(para)
print()
return paras
def extract_relevant_paras(paras, verbosity=0):
"""
Extracts relevant paragraphs from the list of all paragraphs.
Parameters
----------
paras : list
A list of split paragraphs.
verbosity : int, optional
The level of print statements on the output console. The default is 0.
Returns
-------
cleaned_paras : list
A list of relevant paragraphs.
definitions : str
Definition text as extracted by the "clean_definitions_pattern", which
is to be appended to other definitons in the License text if any.
"""
cleaned_paras = list()
definitions = ""
clean_definitions_pattern = r"""\".{0,20}\".{0,40}(mean|include|refer)s?"""
if verbosity != 0:
print(seperator)
print(seperator)
print("Following paragraphs were considered unnecessary and removed:")
for para in paras:
if not para.strip():
continue
if re.search(clean_definitions_pattern, para):
definitions += para + "\n\n"
if verbosity != 0:
print(seperator)
print(para)
else:
cleaned_paras.append(para)
if verbosity != 0:
print()
definitions = definitions.strip()
return cleaned_paras, definitions
def get_all_caps(text, verbosity=0):
"""
Extracts text with all caps content from the License text.
Parameters
----------
text : str
Raw License text.
verbosity : int, optional
The level of print statements on the output console. The default is 0.
Returns
-------
text : str
License text with all caps sentences removed.
all_caps : list
A list of all caps sentences from the License text.
"""
all_caps_pattern = r"([^a-z\n]{50,})"
all_caps = list()
text = re.sub(all_caps_pattern, lambda m: all_caps.append(m.group(1)), text)
text = re.sub(r"\n{3,}", "\n\n", text)
if all_caps and verbosity != 0:
print(seperator)
print(seperator)
print("Following all caps were removed from the text:")
print(all_caps)
print()
return text, all_caps
def get_exeptions(paras, remove_exceptions, verbosity=0):
"""
Extracts a list of exceptions from the License text.
Parameters
----------
paras : list
A list of paragraphs from the License text.
remove_exceptions : bool
Toggles whether or not to remove exceptions from the cleaned license
text before summarization.
verbosity : int, optional
The level of print statements on the output console. The default is 0.
Returns
-------
non_exception_paras : list
A list of all paragraphs not containing exceptions from the License text.
exceptions : list
A list of all paragraphs containing exceptions from the License text.
"""
non_exception_paras = list()
exceptions = list()
for para in paras:
if re.search("exception", para.lower()):
exceptions.append(para)
if not remove_exceptions:
non_exception_paras.append(para)
else:
non_exception_paras.append(para)
if exceptions and verbosity != 0:
print(seperator)
print(seperator)
print("Following exceptions were found in the text:")
for exception in exceptions:
print(seperator)
print(exception)
print()
return non_exception_paras, exceptions
def get_MIT_content(text):
"""
Returns the content of the MIT-like-licenses segregated into categories like
Copyright, main content, etc.
Parameters
----------
text : str
Cleaned MIT License text.
Returns
-------
dictionary
A dictionary of content from the MIT license. Keys are the type of
content and values are the License contents from License text.
"""
paras = split_paras(text)
mit_content = defaultdict(list)
for para in paras:
para = para.strip()
if len(para) < 1:
continue
if len(para.split()) <= 10 and ("Licens" in para or "licens" in para) and "Copyright" not in para:
mit_content["header"].append(para)
elif "Copyright" in para:
if "is hereby granted" in para:
mit_content["copyright+content"].append(para)
else:
mit_content["copyright"].append(para)
elif "Permission is hereby granted" in para:
mit_content["content"].append(para)
elif "The above copyright notice" in para or len(para.split()) < 18:
mit_content["sentence"].append(para)
elif get_all_caps(para)[1]:
mit_content["all_cap"].append(para)
else:
mit_content["content"].append(para)
for key, value in mit_content.items():
mit_content[key] = "\n\n".join(value)
return mit_content
def get_most_likely_license_type(text):
"""
Returns the most likely license type based on Doc2Vec scores
(similarity > 0.9).
Parameters
----------
text : str
Raw License text.
Returns
-------
str
The type of the most likely license. "Not found" if no license score is
above 0.9
"""
try:
from src.doc2vec import inference
except:
from doc2vec import inference
top1_result = inference(text).loc[0, :]
if top1_result["Scores"] > 0.9:
return top1_result["License"]
else:
return "Not Found"
def clean_license_text(text, remove_exceptions=False, verbosity=0):
"""
Cleans License text.
Parameters
----------
text : str
Raw License text.
remove_exceptions : bool, optional
Toggles whether or not to remove exceptions from the cleaned license.
The default is False.
verbosity : int, optional
The level of print statements on the output console. The default is 0.
Returns
-------
text : str
Cleaned License text.
definitions : str
Definitions extracted from the License text.
exceptions : str
Exceptions extracted from the License text.
"""
if len(text) == 0:
return text, "", ""
text, author_details = extract_author_details(text, verbosity=verbosity)
text = script_cleaner(text)
text = preprocess_text(text)
paras, definitions, exceptions = split_definitions_exceptions(
text, remove_exceptions, verbosity=verbosity
)
text = PARA_BREAK.join(paras)
text = character_cleaner(text)
text = re.sub(PARA_BREAK, "\n\n", text)
text = text.strip()
if not isEnglish(text):
if not isEnglish(" ".join(text.split()[-5:-1])):
return "", "", ""
exceptions = " ".join(exceptions)
return text, definitions, exceptions
|