-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_summarizer.py
35 lines (27 loc) · 1.18 KB
/
base_summarizer.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
from operator import attrgetter
from collections import namedtuple
from utils import ItemsCount
SentenceInfo = namedtuple("SentenceInfo", ("sentence", "order", "rating",))
class BaseSummarizer(object):
def __call__(self, document, sentences_count):
raise NotImplementedError("This method should be overriden in subclass")
@staticmethod
def normalize_word(word):
return word.lower()
@staticmethod
def _get_best_sentences(sentences, count, rating, *args, **kwargs):
rate = rating
if isinstance(rating, dict):
assert not args and not kwargs
rate = lambda s: rating[s]
infos = (SentenceInfo(s, o, rate(s, *args, **kwargs))
for o, s in enumerate(sentences))
# sorting sentences by rating in descending order
infos = sorted(infos, key=attrgetter("rating"), reverse=True)
# getting `count` first best rated sentences
if not isinstance(count, ItemsCount):
count = ItemsCount(count)
infos = count(infos)
# sorting sentences by their order in document
infos = sorted(infos, key=attrgetter("order"))
return tuple(i.sentence for i in infos)