NLTK in Python:
NLTK is a Python toolkit for working with natural language processing (NLP). It provides us with a large number of test datasets for various text processing libraries. NLTK can be used to perform a variety of tasks such as tokenizing, parse tree visualization, and so on.
Tokenization
Tokenization is the process of dividing a large amount of text into smaller pieces/parts known as tokens. These tokens are extremely valuable for detecting patterns and are regarded as the first stage in stemming and lemmatization. Tokenization also aids in the replacement of sensitive data elements with non-sensitive data elements.
Natural language processing is utilized in the development of applications such as text classification, intelligent chatbots, sentiment analysis, language translation, and so on. To attain the above target, it is essential to consider the pattern in the text.
Natural Language Toolkit features an important module called NLTK tokenize sentences, which is further divided into sub-modules.
- word tokenize
- sentence tokenize
tokenize.regexp() Function:
We can extract tokens from strings using regular expressions with the tokenize.regexp() function of python.
Syntax:
tokenize.regexp()
Parameters: This method doesn’t accept any parameters
Return Value:
The array of tokens using regular expression is returned by the tokenize.regexp() Function
NLTK tokenize.regexp() Function in Python
Method #1: Using regexp() Function (Static Input)
Approach:
- Import RegexpTokenizer() method from tokenize of nltk module using the import keyword
- Create a reference/Instance variable(Object) for the RegexpTokenizer Class by passing gaps as True as an argument to it.
- Give the string as static input and store it in a variable.
- Pass the above-given string to the tokenize() function to extract tokens from the given string using regular expressions
- Store it in another variable.
- Print the above result.
- The Exit of the Program.
Below is the implementation:
# Import RegexpTokenizer() method from tokenize of nltk module using the import keyword from nltk.tokenize import RegexpTokenizer # Creating a reference/Instance variable(Object) for the RegexpTokenizer Class by passing # gaps as True. tkn = RegexpTokenizer('\s+', gaps = True) # Give the string as static input and store it in a variable. gvn_str = "Hello this is Python-programs" # Pass the above given string to the tokenize() function to extract tokens from the # given string using regular expressions # Store it in another variable. rslt = tkn.tokenize(gvn_str) # Print the above result print(rslt)
Output:
['Hello', 'this', 'is', 'Python-programs']
Method #2: Using regexp() Function (User Input)
Approach:
- Import RegexpTokenizer() method from tokenize of nltk module using the import keyword
- Create a reference/Instance variable(Object) for the RegexpTokenizer Class by passing gaps as True as an argument to it.
- Give the string as user input using the input() function and store it in a variable.
- Pass the above-given string to the tokenize() function to extract tokens from the given string using regular expressions
- Store it in another variable.
- Print the above result.
- The Exit of the Program.
Below is the implementation:
# Import RegexpTokenizer() method from tokenize of nltk module using the import keyword from nltk.tokenize import RegexpTokenizer # Creating a reference/Instance variable(Object) for the RegexpTokenizer Class by passing # gaps as True as an argument to it. tkn = RegexpTokenizer('\s+', gaps = True) # Give the string as user input using the input() function and store it in a variable. gvn_str = input("Enter some random string = ") # Pass the above given string to the tokenize() function to extract tokens from the # given string using regular expressions # Store it in another variable. rslt = tkn.tokenize(gvn_str) # Print the above result print(rslt)
Output:
Enter some random string = welcome to Python programs ['welcome', 'to', 'Python', 'programs']