-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrake.py
356 lines (265 loc) · 12.1 KB
/
rake.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
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
# -*- coding: utf-8 -*-
# Implementation of RAKE - Rapid Automtic Keyword Exraction algorithm
# as described in:
# Rose, S., D. Engel, N. Cramer, and W. Cowley (2010).
# Automatic keyword extraction from indi-vidual documents.
# In M. W. Berry and J. Kogan (Eds.), Text Mining: Applications and Theory.unknown: John Wiley and Sons, Ltd.
#
# NOTE: The original code (from https://github.com/aneesha/RAKE)
# has been extended by a_medelyan (zelandiya)
# with a set of heuristics to decide whether a phrase is an acceptable candidate
# as well as the ability to set frequency and phrase length parameters
# important when dealing with longer documents
from __future__ import absolute_import
from __future__ import print_function
import re
import operator
import six
from six.moves import range
from nltk import stem
import json
import numpy as np
debug = False
test = False
def is_number(s):
try:
float(s) if '.' in s else int(s)
return True
except ValueError:
return False
def load_stop_words(stop_word_file):
"""
Utility function to load stop words from a file and return as a list of words
@param stop_word_file Path and file name of a file containing stop words.
@return list A list of stop words.
"""
stop_words = []
for line in open(stop_word_file):
if line.strip()[0:1] != "#":
for word in line.split(): # in case more than one per line
stop_words.append(word)
return stop_words
def separate_words(text, min_word_return_size):
"""
Utility function to return a list of all words that are have a length greater than a specified number of characters.
@param text The text that must be split in to words.
@param min_word_return_size The minimum no of characters a word must have to be included.
"""
splitter = re.compile('[^a-zA-Z0-9_\\+\\-/]')
words = []
for single_word in splitter.split(text):
current_word = single_word.strip().lower()
#leave numbers in phrase, but don't count as words, since they tend to invalidate scores of their phrases
if len(current_word) > min_word_return_size and current_word != '' and not is_number(current_word):
words.append(current_word)
return words
def split_sentences(text):
"""
Utility function to return a list of sentences.
@param text The text that must be split in to sentences.
"""
sentence_delimiters = re.compile(u'[\\[\\]\n.!?,;:\t\\-\\"\\(\\)\\\u2019\u2013]')
sentences = sentence_delimiters.split(text)
return sentences
def build_stop_word_regex(stop_word_file_path):
stop_word_list = load_stop_words(stop_word_file_path)
stop_word_regex_list = []
for word in stop_word_list:
word_regex = '\\b' + word + '\\b'
stop_word_regex_list.append(word_regex)
stop_word_pattern = re.compile('|'.join(stop_word_regex_list), re.IGNORECASE)
return stop_word_pattern
def generate_candidate_keywords(sentence_list, stopword_pattern, min_char_length=1, min_words_length=1, max_words_length=3, min_keyphrase_frequency =1):
phrase_list = []
raw_list = []
for s in sentence_list:
tmp = re.sub(stopword_pattern, '|', s.strip())
phrases = tmp.split("|")
for phrase in phrases:
phrase = phrase.strip().lower()
raw_list.append(phrase)
for phraz in raw_list:
if phraz != "" and is_acceptable(phraz, min_char_length, min_words_length, max_words_length):
if min_keyphrase_frequency > 1 and raw_list.count(phraz) < min_keyphrase_frequency:
continue
else:
phrase_list.append(phraz)
return phrase_list
def is_acceptable(phrase, min_char_length, min_words_length, max_words_length):
words = phrase.split()
# a phrase must have a min length in characters
for word in words:
if len(word) < min_char_length:
return 0
# a phrase must have a max number of words
words = phrase.split()
if len(words) > max_words_length or len(words) < min_words_length:
return 0
digits = 0
alpha = 0
for i in range(0, len(phrase)):
if phrase[i].isdigit():
digits += 1
elif phrase[i].isalpha():
alpha += 1
# a phrase must have at least one alpha character
if alpha == 0:
return 0
# a phrase must have more alpha than digits characters
if digits > alpha:
return 0
return 1
def spell_check(sentence_list):
with open('dico_spell.json', 'r') as f:
dico_spell = json.load(f)
for i in range(len(sentence_list)):
sentence_split = sentence_list[i].split(' ')
for k in range(len(sentence_split)):
if sentence_split[k] in dico_spell.keys():
sentence_split[k] = dico_spell[sentence_split[k]]
sentence_list[i] = str(' '.join(sentence_split))
return sentence_list
def handle_neg(candidate):
candidate = candidate.lower()
neg_items = ['not a lot of', 'not', 'no', 'non', 'not', 'nor', 'free of', 'not too', 'not to', 'clear of', 'free of']
for neg_item in neg_items:
candidate = candidate.replace(neg_item, 'no')
word_list = candidate.split(' ')
if '' in word_list:
word_list.remove('')
to_remove = []
new_phrases = []
cpt=0
while cpt < len(word_list):
if word_list[cpt][-4:] == 'less':
new_phrases.append('no_'+word_list[cpt][:-4])
to_remove += [word_list[cpt]]
cpt += 1
continue
if cpt+1 < len(word_list):
if word_list[cpt+1] == 'free':
new_phrases.append('no_'+word_list[cpt])
to_remove += [word_list[cpt], word_list[cpt+1]]
cpt += 2
continue
if word_list[cpt] == 'no' and cpt+2 < len(word_list):
if word_list[cpt+2] == 'or' and cpt+4 < len(word_list):
if word_list[cpt+4] != 'or':
new_phrases.append('no_'+word_list[cpt+1])
new_phrases.append('no_'+word_list[cpt+3])
to_remove += [word_list[cpt], word_list[cpt+1], word_list[cpt+2], word_list[cpt+3]]
cpt += 4
continue
else:
new_phrases.append('no_'+word_list[cpt+1])
new_phrases.append('no_'+word_list[cpt+3])
new_phrases.append('no_'+word_list[cpt+5])
to_remove += [word_list[cpt], word_list[cpt+1], word_list[cpt+2], word_list[cpt+3], word_list[cpt+4], word_list[cpt+5]]
cpt += 6
continue
elif word_list[cpt+2] == 'or' and cpt+4 >= len(word_list):
new_phrases.append('no_'+word_list[cpt+1])
new_phrases.append('no_'+word_list[cpt+3])
to_remove += [word_list[cpt], word_list[cpt+1], word_list[cpt+2], word_list[cpt+3]]
cpt += 4
continue
else:
new_phrases.append('no_'+word_list[cpt+1])
to_remove += [word_list[cpt], word_list[cpt+1]]
cpt += 2
continue
elif word_list[cpt] == 'no' and cpt+1 < len(word_list):
new_phrases.append('no_'+word_list[cpt+1])
to_remove += [word_list[cpt], word_list[cpt+1]]
cpt += 2
continue
else:
cpt += 1
continue
to_keep = [el for el in word_list if el not in to_remove]
new_candidate = to_keep + new_phrases
return ' '.join(new_candidate)
def handle_neg_list(sentence_list):
sent_handle_neg = []
for candidate in sentence_list:
sent_handle_neg.append(handle_neg(candidate))
return sent_handle_neg
def stem_candidate_keywords(phrase_list):
stemmer = stem.PorterStemmer()
#Stem phrase_list and track
phrase_list_stem = []
track_stem = {}
for phrase in phrase_list:
spl = phrase.split(' ')
phrase_stem = ''
for item in spl:
if len(phrase_stem)==0:
phrase_stem += stemmer.stem(item)
else:
phrase_stem += ' '+stemmer.stem(item)
phrase_list_stem.append(str(phrase_stem))
track_stem[phrase] = str(phrase_stem)
#Compute reverse tracking
track_stem_rev = {}
for phrase, stem_phrase in track_stem.iteritems():
if stem_phrase not in track_stem_rev.keys():
track_stem_rev[stem_phrase] = [phrase]
else:
track_stem_rev[stem_phrase].append(phrase)
#Compute final list of keyphrase candidates
final_list = list(set(phrase_list_stem))
final_list = [track_stem_rev[str(phrase)][0] for phrase in final_list]
return final_list, phrase_list_stem, track_stem
def calculate_word_metrics(phrase_list, tradeoff):
word_frequency = {}
word_degree = {}
word_score = {}
keyphrase_counts = {}
for phrase in phrase_list:
keyphrase_counts.setdefault(phrase, 0)
keyphrase_counts[phrase] += 1
word_list = separate_words(phrase, 0)
for word in word_list:
word_frequency.setdefault(word, 0)
word_frequency[word] += 1
word_degree.setdefault(word, 0)
word_degree[word] += len(word_list) - 1
for word in word_frequency.keys():
word_score.setdefault(word, 0)
if word_degree[word] != 0:
word_score[word] = word_frequency[word] / np.power(word_degree[word], tradeoff)
else:
word_score[word] = word_frequency[word]
keyphrase_stem_freq = {kp: float(sc)/len(phrase_list) for (kp, sc) in keyphrase_counts.iteritems()}
return word_score, keyphrase_stem_freq
def generate_candidate_keyphrase_scores(final_list, word_scores, keyphrase_stem_freq, track_stem):
keyphrase_candidates = {}
for phrase in final_list:
phrase_stem = track_stem[phrase]
keyphrase_candidates.setdefault(phrase, 0)
word_list = separate_words(phrase_stem, 0)
candidate_score = 0
for word in word_list:
candidate_score += word_scores[word]
keyphrase_candidates[phrase] = float(candidate_score) / len(word_list)
keyphrase_candidates = {kp: (sc, keyphrase_stem_freq[track_stem[kp]]) for (kp, sc) in keyphrase_candidates.iteritems()}
return keyphrase_candidates
class Rake(object):
def __init__(self, stop_words_path, min_char_length=1, min_words_length = 1, max_words_length=3, min_keyphrase_frequency=1, tradeoff=0.8):
self.__stop_words_path = stop_words_path
self.__stop_words_pattern = build_stop_word_regex(stop_words_path)
self.__min_char_length = min_char_length
self.__min_words_length = min_words_length
self.__max_words_length = max_words_length
self.__min_keyphrase_frequency = min_keyphrase_frequency
self.__tradeoff = tradeoff
def run(self, text):
sentence_list = split_sentences(text)
sentence_list_check = spell_check(sentence_list)
sentence_list_neg_melt = handle_neg_list(sentence_list_check)
phrase_list = generate_candidate_keywords(sentence_list_neg_melt, self.__stop_words_pattern, self.__min_char_length, self.__min_words_length, self.__max_words_length, self.__min_keyphrase_frequency)
final_list, phrase_list_stem, track_stem = stem_candidate_keywords(phrase_list)
word_scores, keyphrase_stem_freqs = calculate_word_metrics(phrase_list_stem, self.__tradeoff)
keyphrase_candidates = generate_candidate_keyphrase_scores(final_list, word_scores, keyphrase_stem_freqs, track_stem)
sorted_keywords = sorted(six.iteritems(keyphrase_candidates), key=operator.itemgetter(1), reverse=True)
return sorted_keywords