<s> from __future__ import absolute_import <EOL> from __future__ import division , print_function , unicode_literals <EOL> try : <EOL> from StringIO import StringIO <EOL> except ImportError : <EOL> from io import StringIO <EOL> from os . path import dirname , join , abspath <EOL> from sumy . nlp . tokenizers import Tokenizer <EOL> from sumy . _compat import to_string , to_unicode <EOL> from sumy . models . dom import ObjectDocumentModel , Paragraph , Sentence <EOL> _TOKENIZER = Tokenizer ( "<STR_LIT>" ) <EOL> def expand_resource_path ( path ) : <EOL> return join ( abspath ( dirname ( __file__ ) ) , to_string ( "<STR_LIT:data>" ) , to_string ( path ) ) <EOL> def load_resource ( path ) : <EOL> path = expand_resource_path ( path ) <EOL> with open ( path , "<STR_LIT:rb>" ) as file : <EOL> return to_unicode ( file . read ( ) ) <EOL> def build_document ( * sets_of_sentences ) : <EOL> paragraphs = [ ] <EOL> for sentences in sets_of_sentences : <EOL> sentence_instances = [ ] <EOL> for sentence_as_string in sentences : <EOL> sentence = build_sentence ( sentence_as_string ) <EOL> sentence_instances . append ( sentence ) <EOL> paragraphs . append ( Paragraph ( sentence_instances ) ) <EOL> return ObjectDocumentModel ( paragraphs ) <EOL> def build_document_from_string ( string ) : <EOL> sentences = [ ] <EOL> paragraphs = [ ] <EOL> for line in string . strip ( ) . splitlines ( ) : <EOL> line = line . lstrip ( ) <EOL> if line . startswith ( "<STR_LIT>" ) : <EOL> sentences . append ( build_sentence ( line [ <NUM_LIT:2> : ] , is_heading = True ) ) <EOL> elif not line : <EOL> paragraphs . append ( Paragraph ( sentences ) ) <EOL> sentences = [ ] <EOL> else : <EOL> sentences . append ( build_sentence ( line ) ) <EOL> paragraphs . append ( Paragraph ( sentences ) ) <EOL> return ObjectDocumentModel ( paragraphs ) <EOL> def build_sentence ( sentence_as_string , is_heading = False ) : <EOL> return Sentence ( sentence_as_string , _TOKENIZER , is_heading ) </s>
