Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python sentence splitter

# -*- coding: utf-8 -*-
import re
alphabets= "([A-Za-z])"
prefixes = "(Mr|St|Mrs|Ms|Dr)[.]"
suffixes = "(Inc|Ltd|Jr|Sr|Co)"
starters = "(Mr|Mrs|Ms|Dr|Hes|Shes|Its|Theys|Theirs|Ours|Wes|Buts|Howevers|Thats|Thiss|Wherever)"
acronyms = "([A-Z][.][A-Z][.](?:[A-Z][.])?)"
websites = "[.](com|net|org|io|gov)"

def split_into_sentences(text):
    text = " " + text + "  "
    text = text.replace("
"," ")
    text = re.sub(prefixes,"1<prd>",text)
    text = re.sub(websites,"<prd>1",text)
    if "Ph.D" in text: text = text.replace("Ph.D.","Ph<prd>D<prd>")
    text = re.sub("s" + alphabets + "[.] "," 1<prd> ",text)
    text = re.sub(acronyms+" "+starters,"1<stop> 2",text)
    text = re.sub(alphabets + "[.]" + alphabets + "[.]" + alphabets + "[.]","1<prd>2<prd>3<prd>",text)
    text = re.sub(alphabets + "[.]" + alphabets + "[.]","1<prd>2<prd>",text)
    text = re.sub(" "+suffixes+"[.] "+starters," 1<stop> 2",text)
    text = re.sub(" "+suffixes+"[.]"," 1<prd>",text)
    text = re.sub(" " + alphabets + "[.]"," 1<prd>",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(".",".<stop>")
    text = text.replace("?","?<stop>")
    text = text.replace("!","!<stop>")
    text = text.replace("<prd>",".")
    sentences = text.split("<stop>")
    sentences = sentences[:-1]
    sentences = [s.strip() for s in sentences]
    return sentences
Comment

python sentence splitter

>>> from nltk import tokenize
>>> p = "Good morning Dr. Adams. The patient is waiting for you in room number 3."

>>> tokenize.sent_tokenize(p)
['Good morning Dr. Adams.', 'The patient is waiting for you in room number 3.']
Comment

PREVIOUS NEXT
Code Example
Python :: basic calculator in python 
Python :: generate new secret key django 
Python :: opencv waitkey example 
Python :: change colorbar size and place python 
Python :: convert url to base64 image py 
Python :: python get attributes of class 
Python :: df = df.reset_index(level=0) 
Python :: 2 distinct numbers random number generator python 
Python :: pickle load pickle file 
Python :: python Pyramid Patterns 
Python :: how to determine python project parent dir 
Python :: integer colomn to datetime 
Python :: python print percent sign 
Python :: how to catch ctrl c in python 
Python :: how to make label background transparent in tkinter 
Python :: python merge two dictionaries in a single expression 
Python :: shebang python 
Python :: django secure variable 
Python :: numpy count occurrences in array 
Python :: where to find location of where python is installed linux 
Python :: if list of columns exist pandas 
Python :: in pandas how to start an index from a specific number 
Python :: python file count 
Python :: Get Current Date using today method 
Python :: pygame key pressed once 
Python :: seaborn bar plot 
Python :: list with numbers between 2 values by 
Python :: pickle.dump python 
Python :: python raw string 
Python :: hardest python questions 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =