-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnlp.py
167 lines (140 loc) · 4.48 KB
/
nlp.py
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
import re
import math
from collections import Counter
import settings
ideal = 20.0
stopwords = set()
def load_stopwords():
global stopwords
stopwordsFile = settings.NLP_STOPWORDS_EN
with open(stopwordsFile, 'r', encoding='utf-8') as f:
stopwords.update(set([w.strip() for w in f.readlines()]))
def summarize(title='', text='', max_sents=5):
if not text or not title or max_sents <= 0:
return []
summaries = []
sentences = split_sentences(text)
keys = keywords(text)
titleWords = split_words(title)
ranks = score(sentences, titleWords, keys).most_common(max_sents)
for rank in ranks:
summaries.append(rank[0])
summaries.sort(key=lambda summary: summary[0])
return [summary[1] for summary in summaries]
def score(sentences, titleWords, keywords):
senSize = len(sentences)
ranks = Counter()
for i, s in enumerate(sentences):
sentence = split_words(s)
titleFeature = title_score(titleWords, sentence)
sentenceLength = length_score(len(sentence))
sentencePosition = sentence_position(i + 1, senSize)
sbsFeature = sbs(sentence, keywords)
dbsFeature = dbs(sentence, keywords)
frequency = (sbsFeature + dbsFeature) / 2.0 * 10.0
totalScore = (titleFeature*1.5 + frequency*2.0 +
sentenceLength*1.0 + sentencePosition*1.0)/4.0
ranks[(i, s)] = totalScore
return ranks
def sbs(words, keywords):
score = 0.0
if (len(words) == 0):
return 0
for word in words:
if word in keywords:
score += keywords[word]
return (1.0 / math.fabs(len(words)) * score) / 10.0
def dbs(words, keywords):
if (len(words) == 0):
return 0
summ = 0
first = []
second = []
for i, word in enumerate(words):
if word in keywords:
score = keywords[word]
if first == []:
first = [i, score]
else:
second = first
first = [i, score]
dif = first[0] - second[0]
summ += (first[1] * second[1]) / (dif ** 2)
# Number of intersections
k = len(set(keywords.keys()).intersection(set(words))) + 1
return (1 / (k * (k + 1.0)) * summ)
def split_words(text):
try:
text = re.sub(r'[^\w ]', '', text) # strip special chars
return [x.strip('.').lower() for x in text.split()]
except TypeError:
return None
def keywords(text):
NUM_KEYWORDS = 10
text = split_words(text)
if text:
num_words = len(text)
text = [x for x in text if x not in stopwords]
freq = {}
for word in text:
if word in freq:
freq[word] += 1
else:
freq[word] = 1
min_size = min(NUM_KEYWORDS, len(freq))
keywords = sorted(freq.items(),
key=lambda x: (x[1], x[0]),
reverse=True)
keywords = keywords[:min_size]
keywords = dict((x, y) for x, y in keywords)
for k in keywords:
articleScore = keywords[k] * 1.0 / max(num_words, 1)
keywords[k] = articleScore * 1.5 + 1
return dict(keywords)
else:
return dict()
def split_sentences(text):
import nltk.data
nltk.download('punkt')
tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
sentences = tokenizer.tokenize(text)
sentences = [x.replace('\n', '') for x in sentences if len(x) > 10]
return sentences
def length_score(sentence_len):
return 1 - math.fabs(ideal - sentence_len) / ideal
def title_score(title, sentence):
if title:
title = [x for x in title if x not in stopwords]
count = 0.0
for word in sentence:
if (word not in stopwords and word in title):
count += 1.0
return count / max(len(title), 1)
else:
return 0
def sentence_position(i, size):
normalized = i * 1.0 / size
if (normalized > 1.0):
return 0
elif (normalized > 0.9):
return 0.15
elif (normalized > 0.8):
return 0.04
elif (normalized > 0.7):
return 0.04
elif (normalized > 0.6):
return 0.06
elif (normalized > 0.5):
return 0.04
elif (normalized > 0.4):
return 0.05
elif (normalized > 0.3):
return 0.08
elif (normalized > 0.2):
return 0.14
elif (normalized > 0.1):
return 0.23
elif (normalized > 0):
return 0.17
else:
return 0