<s> from __future__ import absolute_import <EOL> from __future__ import division , print_function , unicode_literals <EOL> from collections import defaultdict <EOL> from . . nlp . stemmers import null_stemmer <EOL> from . _summarizer import AbstractSummarizer <EOL> from . edmundson_cue import EdmundsonCueMethod <EOL> from . edmundson_key import EdmundsonKeyMethod <EOL> from . edmundson_title import EdmundsonTitleMethod <EOL> from . edmundson_location import EdmundsonLocationMethod <EOL> _EMPTY_SET = frozenset ( ) <EOL> class EdmundsonSummarizer ( AbstractSummarizer ) : <EOL> _bonus_words = _EMPTY_SET <EOL> _stigma_words = _EMPTY_SET <EOL> _null_words = _EMPTY_SET <EOL> def __init__ ( self , stemmer = null_stemmer , cue_weight = <NUM_LIT:1.0> , key_weight = <NUM_LIT:0.0> , <EOL> title_weight = <NUM_LIT:1.0> , location_weight = <NUM_LIT:1.0> ) : <EOL> super ( EdmundsonSummarizer , self ) . __init__ ( stemmer ) <EOL> self . _ensure_correct_weights ( cue_weight , key_weight , title_weight , <EOL> location_weight ) <EOL> self . _cue_weight = float ( cue_weight ) <EOL> self . _key_weight = float ( key_weight ) <EOL> self . _title_weight = float ( title_weight ) <EOL> self . _location_weight = float ( location_weight ) <EOL> def _ensure_correct_weights ( self , * weights ) : <EOL> for w in weights : <EOL> if w < <NUM_LIT:0.0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> @ property <EOL> def bonus_words ( self ) : <EOL> return self . _bonus_words <EOL> @ bonus_words . setter <EOL> def bonus_words ( self , collection ) : <EOL> self . _bonus_words = frozenset ( map ( self . stem_word , collection ) ) <EOL> @ property <EOL> def stigma_words ( self ) : <EOL> return self . _stigma_words <EOL> @ stigma_words . setter <EOL> def stigma_words ( self , collection ) : <EOL> self . _stigma_words = frozenset ( map ( self . stem_word , collection ) ) <EOL> @ property <EOL> def null_words ( self ) : <EOL> return self . _null_words <EOL> @ null_words . setter <EOL> def null_words ( self , collection ) : <EOL> self . _null_words = frozenset ( map ( self . stem_word , collection ) ) <EOL> def __call__ ( self , document , sentences_count ) : <EOL> ratings = defaultdict ( int ) <EOL> if self . _cue_weight > <NUM_LIT:0.0> : <EOL> method = self . _build_cue_method_instance ( ) <EOL> ratings = self . _update_ratings ( ratings , method . rate_sentences ( document ) ) <EOL> if self . _key_weight > <NUM_LIT:0.0> : <EOL> method = self . _build_key_method_instance ( ) <EOL> ratings = self . _update_ratings ( ratings , method . rate_sentences ( document ) ) <EOL> if self . _title_weight > <NUM_LIT:0.0> : <EOL> method = self . _build_title_method_instance ( ) <EOL> ratings = self . _update_ratings ( ratings , method . rate_sentences ( document ) ) <EOL> if self . _location_weight > <NUM_LIT:0.0> : <EOL> method = self . _build_location_method_instance ( ) <EOL> ratings = self . _update_ratings ( ratings , method . rate_sentences ( document ) ) <EOL> return self . _get_best_sentences ( document . sentences , sentences_count , ratings ) <EOL> def _update_ratings ( self , ratings , new_ratings ) : <EOL> assert len ( ratings ) == <NUM_LIT:0> or len ( ratings ) == len ( new_ratings ) <EOL> for sentence , rating in new_ratings . items ( ) : <EOL> ratings [ sentence ] += rating <EOL> return ratings <EOL> def cue_method ( self , document , sentences_count , bunus_word_value = <NUM_LIT:1> , stigma_word_value = <NUM_LIT:1> ) : <EOL> summarization_method = self . _build_cue_method_instance ( ) <EOL> return summarization_method ( document , sentences_count , bunus_word_value , <EOL> stigma_word_value ) <EOL> def _build_cue_method_instance ( self ) : <EOL> self . __check_bonus_words ( ) <EOL> self . __check_stigma_words ( ) <EOL> return EdmundsonCueMethod ( self . _stemmer , self . _bonus_words , self . _stigma_words ) <EOL> def key_method ( self , document , sentences_count , weight = <NUM_LIT:0.5> ) : <EOL> summarization_method = self . _build_key_method_instance ( ) <EOL> return summarization_method ( document , sentences_count , weight ) <EOL> def _build_key_method_instance ( self ) : <EOL> self . __check_bonus_words ( ) <EOL> return EdmundsonKeyMethod ( self . _stemmer , self . _bonus_words ) <EOL> def title_method ( self , document , sentences_count ) : <EOL> summarization_method = self . _build_title_method_instance ( ) <EOL> return summarization_method ( document , sentences_count ) <EOL> def _build_title_method_instance ( self ) : <EOL> self . __check_null_words ( ) <EOL> return EdmundsonTitleMethod ( self . _stemmer , self . _null_words ) <EOL> def location_method ( self , document , sentences_count , w_h = <NUM_LIT:1> , w_p1 = <NUM_LIT:1> , w_p2 = <NUM_LIT:1> , w_s1 = <NUM_LIT:1> , w_s2 = <NUM_LIT:1> ) : <EOL> summarization_method = self . _build_location_method_instance ( ) <EOL> return summarization_method ( document , sentences_count , w_h , w_p1 , w_p2 , w_s1 , w_s2 ) <EOL> def _build_location_method_instance ( self ) : <EOL> self . __check_null_words ( ) <EOL> return EdmundsonLocationMethod ( self . _stemmer , self . _null_words ) <EOL> def __check_bonus_words ( self ) : <EOL> if not self . _bonus_words : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def __check_stigma_words ( self ) : <EOL> if not self . _stigma_words : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def __check_null_words ( self ) : <EOL> if not self . _null_words : <EOL> raise ValueError ( "<STR_LIT>" ) </s>
