From 47cb734bc31be67f6cce97873d530eeee866ebc6 Mon Sep 17 00:00:00 2001 From: henri123lemoine Date: Mon, 6 Feb 2023 20:49:55 -0500 Subject: [PATCH] Added helper functions for sentence-splitting and article splitting --- src/helper.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/helper.py diff --git a/src/helper.py b/src/helper.py new file mode 100644 index 0000000..e1bab8d --- /dev/null +++ b/src/helper.py @@ -0,0 +1,61 @@ +import re +from typing import List + +# FROM https://stackoverflow.com/a/31505798/16185542 +# -*- coding: utf-8 -*- +alphabets= "([A-Za-z])" +prefixes = "(Mr|St|Mrs|Ms|Dr)[.]" +suffixes = "(Inc|Ltd|Jr|Sr|Co)" +starters = "(Mr|Mrs|Ms|Dr|Prof|Capt|Cpt|Lt|He\s|She\s|It\s|They\s|Their\s|Our\s|We\s|But\s|However\s|That\s|This\s|Wherever)" +acronyms = "([A-Z][.][A-Z][.](?:[A-Z][.])?)" +websites = "[.](com|net|org|io|gov|edu|me)" +digits = "([0-9])" + +def split_into_sentences(text): + text = " " + text + " " + text = text.replace("\n"," ") + text = text.replace("?!", "?") + text = re.sub(prefixes,"\\1",text) + text = re.sub(websites,"\\1",text) + text = re.sub(digits + "[.]" + digits,"\\1\\2",text) + if "..." in text: text = text.replace("...","") + if "Ph.D" in text: text = text.replace("Ph.D.","PhD") + text = re.sub("\s" + alphabets + "[.] "," \\1 ",text) + text = re.sub(acronyms+" "+starters,"\\1 \\2",text) + text = re.sub(alphabets + "[.]" + alphabets + "[.]" + alphabets + "[.]","\\1\\2\\3",text) + text = re.sub(alphabets + "[.]" + alphabets + "[.]","\\1\\2",text) + text = re.sub(" "+suffixes+"[.] "+starters," \\1 \\2",text) + text = re.sub(" "+suffixes+"[.]"," \\1",text) + text = re.sub(" " + alphabets + "[.]"," \\1",text) + if "”" in text: text = text.replace(".”","”.") + if "\"" in text: text = text.replace(".\"","\".") + if "!" in text: text = text.replace("!\"","\"!") + if "?" in text: text = text.replace("?\"","\"?") + text = text.replace(".",".") + text = text.replace("?","?") + text = text.replace("!","!") + text = text.replace("",".") + + sentences = text.split("") + sentences = sentences[:-1] + sentences = [s.strip() for s in sentences] + + if sentences == []: + sentences = [text.strip()] + return sentences + + +def split_article(text: str) -> List[str]: # THIS IS COMPLETELY BROKEN AND WRONG. TODO: FIX IT. + # Receives one text (str) and returns a list of sections (List[str]), each section being a few appended paragraphs that do not exceed 1000 words. + # This is done to avoid the 8000 token limit of OpenAI embeddings. + sections = [] + section = "" + paragraphs = text.split('\n') + for paragraph in paragraphs: + if paragraph == "": continue + if len(section.split()) + len(paragraph.split()) > 1000 or len(section) + len(paragraph) > 7000: + sections.append(section) + section = "" + section += f"{paragraph}\n" + sections.append(section) + return sections \ No newline at end of file