Sentence Boundary Detection
Sentence boundary detection is the method of detecting where one sentence ends and another begins. If you are thinking that this sounds pretty easy, as a period (.) or a question mark (?) denotes the end of a sentence and the beginning of another sentence, then you are wrong. There can also be instances where the letters of acronyms are separated by full stops, for instance. Various analyses need to be performed at a sentence level; detecting the boundaries of sentences is essential.
An exercise will provide us with a better understanding of this process.
Exercise 1.11: Sentence Boundary Detection
In this exercise, we will extract sentences from a paragraph. To do so, we'll be using the sent_tokenize() method, which is used to detect sentence boundaries. The following steps need to be performed:
- Open a Jupyter Notebook.
- Insert a new cell and add the following code to import the necessary libraries:
import nltk
from nltk.tokenize import sent_tokenize
- Use the sent_tokenize() method to detect sentences in some given text. Insert a new cell and add the following code to implement this:
def get_sentences(text):
return sent_tokenize(text)
get_sentences("We are reading a book. Do you know who is "\
"the publisher? It is Packt. Packt is based "\
"out of Birmingham.")
This code generates the following output:
['We are reading a book.'
'Do you know who is the publisher?'
'It is Packt.',
'Packt is based out of Birmingham.']
- Use the sent_tokenize() method for text that contains periods (.) other than those found at the ends of sentences:
get_sentences("Mr. Donald John Trump is the current "\
"president of the USA. Before joining "\
"politics, he was a businessman.")
The code will generate the following output:
['Mr. Donald John Trump is the current president of the USA.',
'Before joining politics, he was a businessman.']
As you can see in the code, the sent_tokenize method is able to differentiate between the period (.) after "Mr" and the one used to end the sentence. We have covered all the preprocessing steps that are involved in NLP.
Note
To access the source code for this specific section, please refer to https://packt.live/2ZseU86.
You can also run this example online at https://packt.live/2CC8Ukp.
Now, using the knowledge we've gained, let's perform an activity.
Activity 1.01: Preprocessing of Raw Text
We have a text corpus that is in an improper format. In this activity, we will perform all the preprocessing steps that were discussed earlier to get some meaning out of the text.
Note
The text corpus, file.txt, can be found at this location: https://packt.live/30cu54z
After downloading the file, place it in the same directory as the notebook.
Follow these steps to implement this activity:
- Import the necessary libraries.
- Load the text corpus to a variable.
- Apply the tokenization process to the text corpus and print the first 20 tokens.
- Apply spelling correction on each token and print the initial 20 corrected tokens as well as the corrected text corpus.
- Apply PoS tags to each of the corrected tokens and print them.
- Remove stop words from the corrected token list and print the initial 20 tokens.
- Apply stemming and lemmatization to the corrected token list and then print the initial 20 tokens.
- Detect the sentence boundaries in the given text corpus and print the total number of sentences.
Note
The solution to this activity can be found on page 366.
We have learned about and achieved the preprocessing of given data. By now, you should be familiar with what NLP is and what basic preprocessing steps are needed to carry out any NLP project. In the next section, we will focus on the different phases of an NLP project.
- 大規(guī)模數(shù)據(jù)分析和建模:基于Spark與R
- 數(shù)據(jù)庫基礎與應用:Access 2010
- SQL Server 2008數(shù)據(jù)庫應用技術(第二版)
- Python數(shù)據(jù)分析、挖掘與可視化從入門到精通
- SQL查詢:從入門到實踐(第4版)
- 大數(shù)據(jù)導論
- 算法與數(shù)據(jù)中臺:基于Google、Facebook與微博實踐
- 數(shù)據(jù)庫系統(tǒng)原理及應用教程(第4版)
- Microsoft Power BI數(shù)據(jù)可視化與數(shù)據(jù)分析
- 智能數(shù)據(jù)時代:企業(yè)大數(shù)據(jù)戰(zhàn)略與實戰(zhàn)
- 數(shù)據(jù)庫技術實用教程
- 大數(shù)據(jù)架構(gòu)商業(yè)之路:從業(yè)務需求到技術方案
- Instant Autodesk AutoCAD 2014 Customization with .NET
- 數(shù)據(jù)修復技術與典型實例實戰(zhàn)詳解(第2版)
- Web Services Testing with soapUI