forked from yask123/Summarize-it
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbase_summarizer.py
40 lines (31 loc) · 1.39 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
36
37
38
39
40
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division, print_function, unicode_literals
from collections import namedtuple
from operator import attrgetter
from utils import ItemsCount
import logging
logging.basicConfig(level=logging.INFO)
SentenceInfo = namedtuple("SentenceInfo", ("sentence", "order", "rating",))
class BaseSummarizer(object):
def __init__(self, ):
self.logger = logging.getLogger(__name__)
def __call__(self, document, sentences_count):
raise NotImplementedError("This method should be overriden in subclass")
def normalize_word(self, word):
return word.lower()
def _get_best_sentences(self, sentences, count, rating, *args, **kwargs):
rate = rating
self.logger.info("Sentences are %s" % sentences)
infos = (SentenceInfo(s, o, rate(s, *args, **kwargs))
for o, s in enumerate(sentences))
# sort sentences by rating in descending order
infos = sorted(infos, key=attrgetter("rating"), reverse=True)
# get `count` first best rated sentences
count = ItemsCount(count)
# if not isinstance(count, ItemsCount):
# count = ItemsCount(count)
infos = count(infos)
# sort sentences by their order in document
infos = sorted(infos, key=attrgetter("order"))
return tuple(i.sentence for i in infos)