CoolFace
Apppublic

alpertml/TopicModelingForSummarization

sourceHugging Facecreativeml-openrail-mupdated 3y agoView on Hugging Face
0likes
spacy_utilities.py64 linesDownload Raw Back to src
1### Imports2import spacy3from spacy.lang.en import English4from spacy import displacy5 6import pandas as pd7 8import traceback9 10class SpacySegmentizer:11    ##==========================================================================================================12    """13    Definition of attributes14    """15    __nlp_SpaCy = None16    ##==========================================================================================================17    """18    Function: __init__19    """20    def __init__(self):21        if self.__nlp_SpaCy == None:22            print("Initializing spacy")23            self.initialize_spacy()24    ##==========================================================================================================25    """26    Function: initialize_spacy27    """28    def initialize_spacy(self):29        try:30            self.__nlp_SpaCy = English()31            #self.__nlp_spacy = spacy.load("en_core_web_sm")32            self.__nlp_SpaCy.add_pipe("sentencizer")33            #nlp.add_pipe("sentencizer", config={"punct_chars":[".", ";"]})34        except Exception as excmsg:35            print(f"An error happens in initialize_spacy(...) {traceback.format_exc()}.")36            self.__nlp_SpaCy = None37        return self.__nlp_SpaCy38    ##==========================================================================================================39    """40    Function: segment_into_sentences41    """42    def segment_into_sentences(self, src_text="", _format=""):43        intermediate_result = None44 45        if isinstance(src_text, str):46            intermediate_result = [s for s in (self.__nlp_SpaCy(src_text)).sents]47        elif isinstance(src_text, list):48            intermediate_result = list()49 50            for sent in src_text:51                intermediate_result.extend([s for s in (self.__nlp_SpaCy(sent)).sents])52 53        if _format == "str":54            sentences_new_doc = list()55    56            for intsent in intermediate_result:57                sentences_new_doc.append(" ".join([str(s) for s in intsent]))58            59            return sentences_new_doc60        else:61            return intermediate_result62    ##==========================================================================================================63 64##==========================================================================================================