![]() |
|
[Tut] Python | Split Text into Sentences - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python | Split Text into Sentences (/thread-100399.html) |
[Tut] Python | Split Text into Sentences - xSicKxBot - 12-14-2022 Python | Split Text into Sentences Rate this post Summary: There are four different ways to split a text into sentences: Using Code: nltk Using Code: re.split() Using Code: re.findall() 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']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:
Caution: You might get an error after installing the Code: nltkCode: nltkCode: Install nltk using → pip install nltkThen 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_tokenizerMethod 2: [b]Using re.split[/b] The Code: re.split(pattern, string)Code: re.split('a', 'bbabbbab')Code: ['bb', 'bbb', 'b']Approach: Split the given string using alphanumeric separators, and use the either-or Code: (|)Code: re.split("[//.|//!|//?]", text)Code: x!=""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'] Recommended Read: Python Regex SplitMethod 3: [b]Using findall[/b] The Code: re.findall(pattern, string)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)Code: []+Code: ^Code: !Code: ?Code: . Related Read: Python re.findall() – Everything You Need to KnowMethod 4: [b]Using replace[/b] Approach: The idea here is to replace all the punctuation marks ( Code: ‘!’, ‘?’,Code: ‘.’Code: ,Code: pop()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'] 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! ![]() 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. |