Spaces:
Runtime error
Runtime error
File size: 5,265 Bytes
ac750db a804ced ac750db a804ced ac750db a804ced ac750db a804ced ac750db a804ced ac750db a804ced ac750db a804ced ac750db a804ced ac750db a804ced ac750db 1fdb52f ac750db 1fdb52f ac750db 1fdb52f ac750db 1fdb52f ac750db a804ced ac750db a804ced ac750db a804ced 1fdb52f ac750db a804ced ac750db a804ced ac750db a804ced ac750db a804ced ac750db a804ced ac750db a804ced ac750db |
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 |
import re
import nltk
import numpy as np
import gensim
import spacy
import math
from collections import Counter
try:
from src.clean import clean_license_text
from src.read_data import read_file
except:
from clean import clean_license_text
from read_data import read_file
NEGATION_WEIGHT = 0.2
nlp = spacy.load("en_core_web_sm")
modal_verbs = {
"can",
"may",
"must",
"shall",
"will",
# "could",
# "might",
"should",
"would"
}
neg_modal = {
"cannot",
"may not",
"must not",
"shall not",
"will not",
# "could not",
# "might not",
"should not",
"would not"
}
# TODO Move these structures to another file
license_stopwords = {
",",
"(",
")",
"software",
"license",
"work",
# "copyright",
"program",
# "use",
# "copy",
"source",
# "may",
# "terms",
"code",
# "without",
# "free",
# "distribute",
# "rights",
# "notice",
# "shall",
"provided",
# "permission",
# "including",
"version",
"library",
# "condition",
"covered",
# "must",
"public",
# "modify",
# "distribution",
# "warranty",
}.union(nlp.Defaults.stop_words) - modal_verbs
negation_words = {
"no",
"not",
"non"
}
# TODO: Consider adding these words to the vocab:
# no-charge
#
#
#
#
verbs = [
"permit", "copy", "modify", "change", "sell", "reproduce",
"transfer", "rent", "lease", "assign", "sublet", "distribute",
"redistribute", "allow", "require", "merge", "publish", "use",
"include", "grant", "run", "affirm", "propagate", "acknowledge"
]
neg_verbs = [f"not-{verb}" for verb in verbs]
properties_dict = {
"0.1": [
],
"0.2": ["everyone"],
"0.3": ["irrevocable"],
"0.4": [],
"0.5": [],
"0.6": [
"distribution", "redistribution",
"permission", "modification",
"copyright",
"permission",
"limitation",
"free", "charge",
"warranty",
"term", "terms", "condition",
"right",
"sublicense",
"commercial", "non-commercial",
"exception"
],
"0.7": verbs + [
],
"0.8": [],
"0.9": neg_verbs + [],
"1.0": [],
"3.0": modal_verbs
}
properties_scores = {
"0.1": 0.1,
"0.2": 0.2,
"0.3": 0.3,
"0.4": 0.4,
"0.5": 0.5,
"0.6": 0.6,
"0.7": 0.7,
"0.8": 0.8,
"0.9": 0.9,
"1.0": 1.0,
"3.0": 3.0
}
def lemmatize_tokens(sent):
# TODO: Docstrings
"""Each word in input sentence is converted to lemma"""
lemmas = list()
nlp_sent = [token.lemma_.lower().strip() for token in nlp(sent)]
for tok_i, token in enumerate(nlp_sent):
if (token
and token not in license_stopwords
and token not in negation_words):
if tok_i > 0 and nlp_sent[tok_i-1] in negation_words:
lemmas.append(f"{nlp_sent[tok_i-1]}-{token}")
elif tok_i > 1 and nlp_sent[tok_i-1] in " -" and nlp_sent[tok_i-2] in negation_words:
lemmas.append(f"{nlp_sent[tok_i-2]}-{token}")
else:
lemmas.append(token)
return lemmas
def custom_textrank_summarizer(license_text,
min_sent_len=3,
summary_len=0.3,
debug=False):
"""
TODO: Doctrings
"""
sent_scores = Counter()
cleaned_license_text, definitions = clean_license_text(license_text)
cleaned_license_sentences = re.split('(\n{2,}|\.)', cleaned_license_text)
cleaned_license_sentences = [
text.strip() for text in cleaned_license_sentences
if text.strip() not in ["", ".", "\n", "\n\n"]
]
summary_len = math.ceil(summary_len*len(cleaned_license_sentences))
if debug:
print(f"summary length:{summary_len}")
print(cleaned_license_sentences)
for sent_i, sent in enumerate(cleaned_license_sentences):
if sent_i < 0:
continue
if len(sent.split()) < min_sent_len:
continue
score = 0
lemmatized_tokens = lemmatize_tokens(sent)
if debug:
print("-"*50)
print(f"\nOriginal Sentence = {sent}")
print(f"\n{sent_i}. Lemmatized_tokens = {lemmatized_tokens}")
word_count = Counter([tok for tok in lemmatized_tokens])
for prop, prop_words in properties_dict.items():
prop_score = 0
imp_words = list()
for prop_i, prop_word in enumerate(prop_words):
if prop_word in word_count.keys():
prop_score += properties_scores[prop]
imp_words.append(prop_word)
if debug:
print(prop, "=", imp_words, "=", prop_score)
score += prop_score
sent_scores[sent] = score / len(lemmatized_tokens)
if debug:
print(f"Sentence score: {sent_scores[sent]}")
print()
if debug:
print(sent_scores)
sorted_sent_scores = sent_scores.most_common()
summary = ".\n".join(sent for sent, score in sorted_sent_scores[:summary_len])
return summary, definitions |