Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python | Split Text into Sentences
1
Python | Split Text into Sentences

Rate this post

[Image: 2728.png]Summary: There are four different ways to split a text into sentences:
[Image: 1f680.png] Using
Code:
nltk
module
[Image: 1f680.png] Using
Code:
re.split()
[Image: 1f680.png] Using
Code:
re.findall()
[Image: 1f680.png] Using
Code:
replace

[b]Minimal Example[/b]

Code:
text = "God is Great! I won a lottery." # Method 1 from nltk.tokenize import sent_tokenize print(sent_tokenize(text)) # Method 2 import re res = [x for x in re.split("[//.|//!|//?]", text) if x!=""] print(res) # Method 3 res = re.findall(r"[^.!?]+", text) print(res) # Method 4 def splitter(txt, delim): for i in txt: if i in delim: txt = txt.replace(i, ',') res = txt.split(',') res.pop() return res sep = ['.', '!'] print(splitter(text, sep)) # Output: ['God is Great', ' I won a lottery']

Problem Formulation

Problem: Given a string/text containing numerous sentences; How will you split the string into sentences?

Example: Let’s visualize the problem with the help of an example.

Code:
# Input text = "This is sentence 1. This is sentence 2! This is sentence 3?" # output ['This is sentence 1', ' This is sentence 2', ' This is sentence 3']
Method 1: [b]Using nltk.tokenize[/b]

Natural Language Processing (NLP) has a process known as tokenization using which a large quantity of text can be divided into smaller parts called tokens. The Natural Language toolkit contains a very important module known as NLTK tokenize sentence which further comprises sub-modules. We can use this module and split a given text into sentences.

Code:

Code:
from nltk.tokenize import sent_tokenize text = "This is sentence 1. This is sentence 2! This is sentence 3?" print(sent_tokenize(text)) # ['This is sentence 1.', ' This is sentence 2!', ' This is sentence 3?']

Explanation: 
  • Import the
    Code:
    sent_tokenize
    module.

  • Further, the
    Code:
    sentence_tokenizer
    module allows you to parse the given sentences and break them into individual sentences at the occurrence of punctuations like periods, exclamation,  question marks, etc.

Caution: You might get an error after installing the
Code:
nltk
package. So, here’s the entire process to install
Code:
nltk
in your system.

Code:
Install nltk using → pip install nltk

Then go ahead and type the following in your Python shell:

Code:
import nltk nltk.download('punkt')

That’s it! You are now ready to use the
Code:
sentence_tokenizer
module in your code.

Method 2: [b]Using re.split[/b]

The
Code:
re.split(pattern, string)
method matches all occurrences of the pattern in the string and divides the string along the matches resulting in a list of strings between the matches. For example,
Code:
re.split('a', 'bbabbbab')
results in the list of strings
Code:
['bb', 'bbb', 'b']
.

Approach: Split the given string using alphanumeric separators, and use the either-or
Code:
(|)
metacharacter. It allows you to specify each separator within the expression like so:
Code:
re.split("[//.|//!|//?]", text)
. Thus, whenever the script encounters any of the mentioned characters specified within the pattern, it will split the given string. The expression
Code:
x!=""
ignores all the empty characters.

Code:

Code:
import re text = "This is sentence 1. This is sentence 2! This is sentence 3?" res = [x for x in re.split("[//.|//!|//?]", text) if x!=""] print(res) # ['This is sentence 1', ' This is sentence 2', ' This is sentence 3']

[Image: 1f9e9.png]Recommended Read:  Python Regex Split

Method 3: [b]Using findall[/b]

The
Code:
re.findall(pattern, string)
method scans the string from left to right, searching for all non-overlapping matches of the pattern. It returns a list of strings in the matching order when scanning the string from left to right.

Code:

Code:
import re text = "This is sentence 1. This is sentence 2! This is sentence 3?" res = re.findall(r"[^.!?]+", text) print(res) # ['This is sentence 1', ' This is sentence 2', ' This is sentence 3']

Explanation: In the expression, i.e.,
Code:
re.findall(r"[^.!?]+", text)
, all occurrences of characters are grouped except the punctuation marks.
Code:
[]+
denotes that all occurrences of one or more characters except (given by
Code:
^
) ‘
Code:
!
’, ‘
Code:
?
’, and ‘
Code:
.
’ will be returned. Thus, whenever the script finds and groups all characters until any of the mentioned characters within the square brackets are found. As soon as one of the mentioned characters is found it splits the string and finds the next group of characters.

[Image: 1f9e9.png]Related Read: Python re.findall() – Everything You Need to Know

Method 4: [b]Using replace[/b]

Approach: The idea here is to replace all the punctuation marks (
Code:
‘!’, ‘?’,
and
Code:
‘.’
) present in the given string with a comma (
Code:
,
) and then split the modified string to get the list of split substrings. The problem here is the last element returned will be an empty string. You can use the
Code:
pop()
method to remove the last element out of the list of substrings (the empty string).

Code:

Code:
def splitter(txt, delim): for i in txt: if i in delim: txt = txt.replace(i, ',') res = txt.split(',') res.pop() return res sep = ['.', '!', '?'] text = "This is sentence 1. This is sentence 2! This is sentence 3?" print(splitter(text, sep)) # ['This is sentence 1', ' This is sentence 2', ' This is sentence 3']

[Image: 1f9e9.png]Related Read: Python String replace()

Conclusion

We have successfully solved the given problem using different approaches. I hope this article helped you in your Python coding journey. Please subscribe and stay tuned for more interesting articles.

Happy coding! [Image: 1f40d.png]

Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video.
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: