Author name: Vikram Chiluka

Python Program to Find Nearest Palindrome of a Number

Palindrome:

If a number reads the same both forward and backward, it is called a Palindrome number. And the insane part is that it doesn’t just apply to numbers. Even if a string reads the same in both directions, it is always a Palindrome!

For Example –  121, 131, etc

when we reverse 121 we get the same 121. Hence it is a Palindrome number.

In this article, let us look at how to use Python to find the nearest palindrome to a given number if the provided number isn’t a palindrome.

Examples:

Example1:

Input:

Given Number = 620

Output:

The Nearest palindrome of the given number{ 620 } is:
626

Example2:

Input:

Given Number = 120

Output:

The Nearest palindrome of the given number{ 120 } is:
121

Program to Find Nearest Palindrome of a Number in Python

Using the slicing operator in Python, we can determine whether a given number or string is a palindrome. We will also use the slicing operator to get the nearest palindrome of an integer. A given number’s nearest palindrome is found by adding 1 to the number until it is a palindrome. This is accomplished by recursively using a while loop. When the condition is satisfied, the number is output.

Method #1: Using Slicing (Static Input)

Approach:

  • Create a function say Nearest_palindrome() which accepts the given number as an argument
  • Loop recursively using the while loop
  • Check if the Reverse of the given number is equal to the given number using slicing and if conditional statement.
  • If it is true, then return that corresponding number.
  • Increment the value of a given number by 1 i.e, add 1 to the given number until it becomes a palindrome
  • Give the number as static input and store it in a variable.
  • Pass the given number to the above created Nearest_palindrome() function and print the result.
  • The Exit of the Program.

Below is the implementation:

# Create a function say Nearest_palindrome() which accepts the given number as an argument
def Nearest_palindrome(gvn_numb):
    # Loop recursively using the while loop
    while(1):
        # Check if the Reverse of the given number is equal to the given number using slicing 
        # and if conditional statement.
        if gvn_numb ==int(str(gvn_numb)[::-1]):
            # If it is true, then return that corresponding number.
            return gvn_numb
        # Increment the value of given number by 1 i.e, add 1 to the given number until
        # it becomes a palindrome
        gvn_numb+=1

# Give the number as static input and store it in a variable.
gvn_numb = 120
# Pass the given number to the above created Nearest_palindrome() function and
# print the result.
print("The Nearest palindrome of the given number{", gvn_numb, "} is:")
print(Nearest_palindrome(gvn_numb))

Output:

The Nearest palindrome of the given number{ 120 } is:
121

Method #2: Using Slicing (User Input)

Approach:

  • Create a function say Nearest_palindrome() which accepts the given number as an argument
  • Loop recursively using the while loop
  • Check if the Reverse of the given number is equal to the given number using slicing and if conditional statement.
  • If it is true, then return that corresponding number.
  • Increment the value of a given number by 1 i.e, add 1 to the given number until it becomes a palindrome
  • Give the number as user input using the int(input()) function and store it in a variable.
  • Pass the given number to the above created Nearest_palindrome() function and print the result.
  • The Exit of the Program.

Below is the implementation:

# Create a function say Nearest_palindrome() which accepts the given number as an argument
def Nearest_palindrome(gvn_numb):
    # Loop recursively using the while loop
    while(1):
        # Check if the Reverse of the given number is equal to the given number using slicing 
        # and if conditional statement.
        if gvn_numb ==int(str(gvn_numb)[::-1]):
            # If it is true, then return that corresponding number.
            return gvn_numb
        # Increment the value of given number by 1 i.e, add 1 to the given number until
        # it becomes a palindrome
        gvn_numb+=1

# Give the number as as user input using the int(input()) function and store it in a variable.
gvn_numb = int(input("Enter some random number = "))
# Pass the given number to the above created Nearest_palindrome() function and
# print the result.
print("The Nearest palindrome of the given number{", gvn_numb, "} is:")
print(Nearest_palindrome(gvn_numb))

Output:

Enter some random number = 620
The Nearest palindrome of the given number{ 620 } is:
626

NOTE: Because we can’t use the slice operator on an integer, we first typecast it to string. The string is then typecast to an integer since we need to iterate the loop again by adding 1 if it is not a palindrome.

Python Program to Find Nearest Palindrome of a Number Read More »

How to Separate Alphabets and Numbers in a String using regular expression in Python?

In this article, let us look at how to use regular expressions in Python to split numbers and alphabets in a number of ways. This is extremely helpful in competitive programming. The technique gets quite simple when regular expressions are used.

re library/RegEx Module:

Python includes the re package, which can be used to interact with Regular Expressions.
A RegEx, or Regular Expression, is a character sequence that forms a search pattern.

RegEx can be used to determine whether or not a string contains the specified search pattern.

To work with this library we should first import it.

re.findall() method:

The findall() function to separate things out. This method gives a list of all non-overlapping matches in a string. From left to right, the string is parsed. This method takes three arguments.

re.findall(pattern, string, flags=0)

Parameters

pattern: It is the pattern that the user needs from the given string.

string: It is the string given as input.

Separating Alphabets and Numbers in a String using regular expression in Python

Example1

Approach:

  • Import re module using the import keyword
  • Give the string as static input and store it in a variable
  • Get all the numbers in a given string using findall() function and store it in a variable.
  • Here ‘\d’represents digits.
  • Get all the alphabets in a given string using findall() function and and store it in another variable.
  • Here ‘[a-zA-Z]’ represents alphabets(lower, uppercase)
  • Print all the numbers present in a given string.
  • Print all the alphabets present in a given string.
  • The Exit of the Program.

Below is the implementation:

# Import re module using the import keyword
import re
# Give the string as static input and store it in a variable
gvn_str = "Python1213prog786rams"
# Get all the numbers in a given string using findall() function
# and store it in a variable.
# Here '\d'represents digits.
numbrs = re.findall(r'\d', gvn_str)
# Get all the alphabets in a given string using findall() function and
# and store it in another variable.
# Here '[a-zA-Z]' represents alphabets(lower, uppercase)
alphabets = re.findall(r'[a-zA-Z]', gvn_str)
# Print all the numbers present in a given string
print("The numbers present in a given string{", gvn_str, "}:")
print(numbrs)
# Print all the alphabets present in a given string
print("The alphabets present in a given string{", gvn_str, "}:")
print(alphabets)

Output:

The numbers present in a given string{ Python1213prog786rams }:
['1', '2', '1', '3', '7', '8', '6']
The alphabets present in a given string{ Python1213prog786rams }:
['P', 'y', 't', 'h', 'o', 'n', 'p', 'r', 'o', 'g', 'r', 'a', 'm', 's']

Example2: Separating Numbers and Alphabets in the specified Pattern Format

Here two lists are returned, the first of which will be made up of numbers. The numbers in this list that are present without any characters in between are grouped as a single number. The same is true for the second list, which is made up of alphabets. If there are no numbers between the alphabets, they are combined together as a string.

import re
gvn_str = "Python1213prog786rams"
numbrs = re.findall(r'\d+', gvn_str)
alphabets = re.findall(r'[a-zA-Z]+', gvn_str)
# Print all the numbers present in a given string in the specified pattern format
print(
    "The numbers present in a given string{", gvn_str, "} in the specified pattern:")
print(numbrs)
# Print all the alphabets present in a given string in the specified pattern format
print(
    "The alphabets present in a given string{", gvn_str, "} in the specified pattern:")
print(alphabets)

Output:

The numbers present in a given string{ Python1213prog786rams } in the specified pattern:
['1213', '786']
The alphabets present in a given string{ Python1213prog786rams } in the specified pattern:
['Python', 'prog', 'rams']

Example3

Here it returns two lists. Only the index positions where there is a number are filled with a number in the first list; all other index places are filled with empty characters. The same holds true for the alphabets in the second list.

import re
gvn_str = "Python1213prog786rams"
numbrs = re.findall(r'\d*', gvn_str)
alphabets = re.findall(r'[a-zA-Z]*', gvn_str)
# Print all the numbers present in a given string in the specified pattern format
print(
    "The numbers present in a given string{", gvn_str, "} in the specified pattern:")
print(numbrs)
# Print all the alphabets present in a given string in the specified pattern format
print(
    "The alphabets present in a given string{", gvn_str, "} in the specified pattern:")
print(alphabets)

Output:

The numbers present in a given string{ Python1213prog786rams } in the specified pattern:
['', '', '', '', '', '', '1213', '', '', '', '', '786', '', '', '', '', '']
The alphabets present in a given string{ Python1213prog786rams } in the specified pattern:
['Python', '', '', '', '', 'prog', '', '', '', 'rams', '']

How to Separate Alphabets and Numbers in a String using regular expression in Python? Read More »

Python Program to Print Ladder Pattern

Let us look at the code for how to Print a Ladder Pattern in Python. It is quite simple and interesting

Given a number N, the task is to print the ladder with N steps (frets) using ‘*’. The ladder will have a gap of 3 spaces between the two side rails.

Examples:

Example1:

Input:

Given N = 5

Output:

*   *
*   *
*****
*   *
*   *
*****
*   *
*   *
*****
*   *
*   *
*****
*   *
*   *
*****
*   *
*   *

Example2:

Input:

Given N = 3

Output:

*   *
*   *
*****
*   *
*   *
*****
*   *
*   *
*****
*   *
*   *

Program to Print Ladder Pattern in Python

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of steps(frets) of a ladder as static input and store it in a variable.
  • Loop till the given number of steps using the for loop
  • Print “* *”
  • Check if the iterator value is less than the given number of steps(gvn_N) using the if conditional statement
  • If it is true, then print “*****” i.e, frets.
  • The Exit of the Program.

Below is the implementation:

# Give the number of steps of a ladder as static input and store it in a variable.
gvn_N = 5
# Loop till the given number of steps using the for loop
for k in range(gvn_N+1):
    # Print "*   *" 
    print("*   *")   
    print("*   *")

    # Check if the iterator value is less than given number of steps(gvn_N) using the 
    # if conditional statement
    if k<gvn_N:
        # If it is true, then print "*****" i.e, frets
        print("*****")

Output:

*   *
*   *
*****
*   *
*   *
*****
*   *
*   *
*****
*   *
*   *
*****
*   *
*   *
*****
*   *
*   *

Method #2: Using For loop (User Input)

Approach:

  • Give the number of steps(frets) of a ladder as user input using the int(input()) function and store it in a variable.
  • Loop till the given number of steps using the for loop
  • Print “* *”
  • Check if the iterator value is less than the given number of steps(gvn_N) using the if conditional statement
  • If it is true, then print “*****” i.e, frets.
  • The Exit of the Program.

Below is the implementation:

# Give the number of steps(frets) of a ladder as user input using the int(input()) function 
# and store it in a variable.
gvn_N = int(input("Enter some random number = "))
# Loop till the given number of steps using the for loop
for k in range(gvn_N+1):
    # Print "*   *" 
    print("*   *")   
    print("*   *")

    # Check if the iterator value is less than given number of steps(gvn_N) using the 
    # if conditional statement
    if k<gvn_N:
        # If it is true, then print "*****" i.e, frets
        print("*****")

Output:

Enter some random number = 3
*   *
*   *
*****
*   *
*   *
*****
*   *
*   *
*****
*   *
*   *

 

Python Program to Print Ladder Pattern Read More »

How to Find Battery Percentage and Charging Status in Windows and Linux Using Python

In this article, we will use Python to calculate the Battery percentage(%).

The psutil library is used in this program to obtain Battery information.

psutil library in python:

The python system and process utilities are abbreviated as psutil.

In Python, the psutil Library is used to obtain information on active(ongoing) processes and system utilization information in, such as CPU, memory, and so on.

Because the psutil Library is not built-in, we must install it before we can use it.

Use the below command to install the psutil library

Installation

pip install psutil

sensors_battery() function:

To obtain information about the Battery, we will utilize the sensors battery() method present in the psutil Library.

Syntax:

 psutil.sensors_battery()

The sensors_battery() method returns battery status information as a named tuple. The following information is included in the battery status:

percent:- It represents the percentage of battery life remaining(battery left).

secsleft:- This is the battery’s capacity in seconds.

power_plugged:- This variable indicates the status of the battery’s charging. If the battery is charging, it is True; otherwise, it is False.

If the battery status cannot be found, the value of power plugged is set to None.

If the battery cannot be found, the psutil.sensors_battery() method gives None.

Find Battery Percentage and Charging Status in Windows and Linux in Python

Approach:

  • Import psutil module using the import keyword
  • Get the named tuple with battery information using the sensors_battery() function of the psutil module.
  • Print the battery percentage by applying the percent attribute on the above battery information
  • Using batteryinformation.power_plugged, we can determine the charging status.
  • Check if the batteryinformation.power_plugged is equal to True using the if conditional statement
  • If it is true, then print “The battery of the system is Charging!!!”
  • Else check if the batteryinformation.power_plugged is equal to False using the elif conditional statement
  • If the condition is true, then print “The battery of the system is NOT Charging/Discharging”
  • The Exit of the Program.

Below is the implementation:

# Import psutil module using the import keyword 
import psutil
# Get the named tuple with battery information using the sensors_battery() 
# function of the psutil module.
batteryinformation  = psutil.sensors_battery()
# Print the battery percentage by applying percent attribute on the above battery information 
print("The Battery percentage of the sytem  = ",batteryinformation.percent)
# Using batteryinformation.power_plugged, we can determine the charging status.
# Check if the batteryinformation.power_plugged is equal to True using the if conditional statement
if batteryinformation.power_plugged == True :
    # If it is true, then print "The battery of the system is Charging!!!"
    print("The battery of the system is Charging!!!")
# Else check if the batteryinformation.power_plugged is equal to False using the elif conditional statement
elif batteryinformation.power_plugged == False:
    # If the condition is true, then print "The battery of the system is NOT Charging/Discharging"
    print("The battery of the system is NOT Charging/Discharging")

Output:

The Battery percentage of the sytem = 55
The battery of the system is Charging!!!

Execution Image:

showing battery percentage and charging status

How to Find Battery Percentage and Charging Status in Windows and Linux Using Python Read More »

Python howdoi

howdoi in Python:

Python howdoi is a command-line tool that is extremely useful for newcomers. We may use this tool to find answers to numerous programming operations, such as printing a list in Python, putting comments, and so on. We can perform all of this from the command line. Python howdoi basically collects data from the top answers from stack overflow and tells us what to do. Because it scrapes data from the internet, we must have an active internet connection to use it.

howdoi will answer all of your programming and coding questions. For example, obtaining syntactic help, looking for libraries for a certain purpose, resolving issues, using pre-defined functions and their applications, and so on.

Syntax:

pip install howdoi

Output:

Collecting howdoi
Downloading PyGithub-1.55-py3-none-any.whl (291 kB)
|████████████████████████████████| 291 kB 8.2 MB/s 
Requirement already satisfied: click in /usr/local/lib/python3.7/dist-packages
 (from keep->howdoi) (7.1.2)
manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl (856 kB)
|████████████████████████████████| 856 kB 46.0 MB/s
Successfully built howdoi
Installing collected packages: pynacl, pyjwt, deprecated, terminaltables, 
PyGithub, cssselect, pyquery, keep, cachelib, howdoi
Successfully installed PyGithub-1.55 cachelib-0.6.0 cssselect-1.1.0 
deprecated-1.2.13 howdoi-2.0.19 keep-2.10.1 pyjwt-2.3.0 pynacl-1.5.0
 pyquery-1.4.3 terminaltables-3.1.10

How should it be used?

Now that we’re ready to utilize the Python howdoi tool, open a command prompt and type ‘howdoi’ followed by our query, as shown below:

howdoi  'QUERY'

This will provide us with an answer to our query.

Optional Arguments That can be used with the howdoi

  • -h:- Display this help message and then exit
  • -p POS:- choose the answer in the specified position (default: 1)
  • -al:- Display the entire answer text
  • -l:- Display only the answer link
  • -c:- Enable colorized output
  • -n NUM ANSWERS:- The number of responses(answers) to Return
  • -C:- clean the cache
  • -v:- shows the current version of howdoi

howdoi in Python

Using this tool we can find the answers to our queries. Very Exciting Right!!!!!

Examples:

1)How to Create Fibonnacci Series in Python

C:\Users\VIKRAM>howdoi create fibonnaci series in python

Output:

def F(n):
if n == 0: return 0
elif n == 1: return 1
else: return F(n-1)+F(n-2)

2)How to print current date and time in python

C:\Users\VIKRAM>howdoi print current date and time in python

Output:

>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2009, 1, 6, 15, 8, 24, 78915)

>>> print(datetime.datetime.now())
2009-01-06 15:08:24.789150

3) How to convert list to string in python

C:\Users\VIKRAM>howdoi convert list to string in python

Output:

list1 = ['1', '2', '3']
str1 = ''.join(list1)

4)How to Use howdoi python

C:\Users\VIKRAM>howdoi use howdoi

Output:

Here are a few popular howdoi commands

>>> howdoi print hello world in python (default query)

>>> howdoi print hello world in python -a (read entire answer)

>>> howdoi print hello world in python -n [number] (retrieve n number of answers)

>>> howdoi print hello world in python -l (display only a link to where the answer is from

>>> howdoi print hello world in python -c (Add colors to the output)

>>> howdoi print hello world in python -e (Specify the search engine you want to use e.g google,bing)

5)How to get the link of an Answer

C:\Users\VIKRAM>howdoi -l print only even numbers in python

Output:

https://stackoverflow.com/questions/64901321/print-even-numbers-n-number-of-times-using-while-loop

Here -l gives the link of the solution to the query given default from the Stackoverflow

 

 

 

Python howdoi Read More »

How to Check Version of Installed Python from cmd in Windows?

Let us see how to use cmd (Command Prompt) to determine which version of Python is installed on your Windows OS system.

Checking Python Version from cmd in Windows

Steps to be followed:

  • To access the command line, first right-click the Windows logo in the bottom left corner of your computer screen and pick “Command Prompt” from the list. (OR)
  • Simply type “cmd” into the Windows search box and select the first option that appears in the search results.
  • Now, in the Command Prompt window, enter the following command:
  • python --version (OR)
    python
  • Press Enter

Thus, if Python is installed on your system and the path is also specified in the Environment Variables, the first command will directly display the version of Python installed on your operating system, as shown below.

When you type the first command python –version it directly displays the version

C:\Users\VIKRAM>python --version
Python 3.9.1

If you run the second command, “python,” this command will run Python.exe and display the version number as shown below:

C:\Users\VIKRAM>python
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

Checking Python version in Windows

If Python is not properly installed on your machine, you will observe the following:

'python' is not recognized as an internal or external command, operable program or batch file.

In this scenario, you must first download Python from https://www.python.org/downloads/ and add it to your path.

Check Python version Using sys.version Method

For this approach, the user must import the sys library and use the from sys.version command, which returns the user’s current Python version.

# Import sys module using the import keyword
import sys
# Get the current version of the user using the sys.version command
print("User Current Version:-", sys.version)

Output:

Current Version of User : 3.10.0 (tags/v3.10.0:b494f59, Oct 4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)]

Checking Python version using sys module

Check Python version Using python_version() Method

This function can be accessed by importing the platform library and will always return the running user’s Python version as a string.

# Import python_version from platform module using the import keyword
from platform import python_version
# Get the current version of the user using the python_version() function
print("User Current Version:-", python_version())

Output:

User Current Version:- 3.7.12

Check Python version Using python -V command

This approach is one of the easiest of all the methods since it uses one of the built-in commands in Python to obtain the current Python version.

command:

python -V

Output:

C:\Users\VIKRAM> python -V
Python 3.9.1

 

How to Check Version of Installed Python from cmd in Windows? Read More »

Python Holidays Library

Let us see about the holidays library here.

holidays library in Python:

The holidays library will help to determine whether or not a particular day is a holiday in various countries. Only public holidays are visible to us.

Syntax:

class holidays.HolidayBase(years=[], expand=True, observed=True, prov=None, state=None)

Parameters

years:

The years argument specifies an iterable list of integers that should be generated automatically by the holiday object. This is only used when the expand parameter is set to False. The years parameter’s default value is [].

expand:

The expand is a boolean argument that specifies whether or not the holidays should be appended to the holidays object in new years. True is the default value.

observed:

It is also a boolean argument. When set to True, it includes the observed day of a holiday that falls on a weekend, if applicable. True is the default value.

prov:

It is a string that represents a province

state: 

It is a string representing a state.

Methods of holidays module

get(key, default = None):

It returns the name of the holiday on the specified date in the key parameter. If more than one holiday falls on the same day, the names are separated by commas.

getlist(key):

It returns a list of names for holidays.

pop(key, default = None):

It removes the key from the holidays object.

Before we work with this module, we should first install it.

Installation:

pip install holidays

Python Holidays Library

Printing the holidays in the specified Year

Approach:

  • Import holidays module using the import keyword
  • Instantiate the holidays.India() and pass some random year as an argument to it to get the India holidays for the given year(here it returns as a dictionary).
  • Loop in the above holidays dictionary using the items() function and the for loop
  • Print the holiday date and the occasion for the year given.
  • The Exit of the Program.

Below is the implementation:

# Import holidays module using the import keyword
import holidays

# Instantiate the holidays.India() and pass some random year as an argument to it
# to get the India holidays for the given year(here it returns as a dictionary).
Indiaholidays = holidays.India(years=2021)
# Loop in the above holidays dictionary using the items() function and the for loop
for holiday_date, occasion in Indiaholidays.items():
   # Print the holiday date and the occasion for the year given.
   print(f'{holiday_date}: {occasion}')

Output:

2021-01-14: Makar Sankranti / Pongal
2021-01-26: Republic Day
2021-08-15: Independence Day
2021-10-02: Gandhi Jayanti
2021-05-01: Labour Day
2021-12-25: Christmas

Checking if the given day is a holiday or Not

Approach:

  • Import time from datetime module using the import keyword
  • Import holidays module using the import keyword
  • Call the India() method of the holidays method to get the India holidays (here it returns as a dictionary).
  • Give the date as static input and store it in a variable.
  • Check if the given date is a holiday or NOT in the above holidays dictionary using in,
    if conditional statements.
  • If it is true, then print the Occassion/holiday name of the given date using the get() method
  • Else print the given date is NOT a Holiday.
  • The Exit of the Program.

Below is the implementation:

# Import time from datetime module using the import keyword
from datetime import time
# Import holidays module using the import keyword
import holidays
# Call the India() method of the holidays method to get the India holidays.
# (here it returns as a dictionary).
Indiaholidays = holidays.India()
# Give the date as static input and store it in a variable.
gvn_date = '2021-12-25'
# Check if the given date is a holiday or NOT in the above holidays dictionary using in, 
# if conditional statements.
if gvn_date in Indiaholidays:
    # If it is true, then print the Occassion/holiday name of the given date using the get() method
    print(Indiaholidays.get(gvn_date))
else:
    # Else print the given date is NOT a Holiday
    print("The given date {", gvn_date, "} is NOT a Holiday")

Output:

Christmas

To return the holiday date as a List

The get_list() methods returns the output as a list.

Approach:

  • Import time from datetime module using the import keyword
  • Import holidays module using the import keyword
  • Call the India() method of the holidays method to get the India holidays (here it returns as a dictionary).
  • Give the date as user input using the input() function and store it in a variable.
  • Check if the given date is a holiday or NOT in the above holidays dictionary using in, if conditional statements.
  • If it is true, then print the Occassion/holiday name of the given date using the get_list() method
    as a list.
  • Else print the given date is NOT a Holiday.
  • The Exit of the Program.

Below is the implementation:

# Import time from datetime module using the import keyword
from datetime import time
# Import holidays module using the import keyword
import holidays
# Call the India() method of the holidays method to get the India holidays.
# (here it returns as a dictionary).
Indiaholidays = holidays.India()
# Give the date as user input using the input() function and store it in a variable.
gvn_date = input("Enter some random date:")
# Check if the given date is a holiday or NOT in the above holidays dictionary using in, 
# if conditional statements.
if gvn_date in Indiaholidays:
    # If it is true, then print the Occassion/holiday name of the given date using the get_list() method
    # as a list.
    print(Indiaholidays.get_list(gvn_date))
else:
    # Else print the given date is NOT a Holiday
    print("The given date {", gvn_date, "} is NOT a Holiday")

Output:

Enter some random date:2021-01-26
['Republic Day']
Enter some random date:2015-03-02
The given date { 2015-03-02 } is NOT a Holiday

Python Holidays Library Read More »

Python nltk.sent_tokenize() Function

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.

nltk.sent_tokenize() Function in Python:

The method sent_tokenize() is to used split the text into sentences.

The sent tokenize sub-module is provided for the above. The obvious question is why phrase tokenization is required when word tokenization is available. Assume you need to count the average number of words every sentence; how will you do it? To determine the ratio, you’ll need both the NLTK sentence tokenizer and the NLTK word tokenizer. Because the answer is numerical, such output is useful for machine learning.

Syntax:

sent_tokenize(text)

Advantages of sentence tokenization

The benefits of using NLTK for sentence tokenization are described below.

  • NLTK allows you to perform text data mining on sentences.
  • The NLTK sentence tokenization method compares different text corporas at the sentence level.
  • Sentence tokenization with NLTK allows you to see how many sentences are used in various sources of text, such as websites, books, and papers.
  • The NLTK “sent tokenize” function allows you to observe how sentences are linked to one another and which bridge words are used.
  • It is feasible to perform an overall sentiment analysis for the sentences using the NLTK sentence tokenizer.
  • One of the advantages of NLTK sentence tokenization is the ability to do Semantic Role Labeling on the sentences in order to understand how the sentences are related to one another.

nltk.sent_tokenize() Function in Python

Method #1: Using sent_tokenize Function (Static Input)

Approach:

  • Import sent_tokenize() function from tokenize of the nltk module using the import keyword
  • Give the string as static input and store it in a variable.
  • Pass the above-given string as an argument to the sent_tokenize() function to tokenize into sentences and print the result.
  • The Exit of the Program.

Below is the implementation:

Output:

['hello this is Python-programs!', 'good morning...']

Here, the text is tokenized into sentences. By putting all of the sentences into a list and tokenizing them with NLTK, you can see which sentences are related to which ones, the average word count per sentence, and the unique sentence count.

Method #2: Using sent_tokenize Function (User Input)

Approach:

  • Import sent_tokenize() function from tokenize of the nltk module using the import keyword
  • Give the string as user input using the input() function and store it in a variable.
  • Pass the above-given string as an argument to the sent_tokenize() function to tokenize into sentences and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import sent_tokenize() function from tokenize of the nltk module using the import keyword
from nltk.tokenize import sent_tokenize

# 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 as an argument to the sent_tokenize() function to 
# tokenize into sentences and print the result.
print(sent_tokenize(gvn_str))

Output:

Enter some random string = good morning. Python-programs. welcome all
['good morning.', 'Python-programs.', 'welcome all']

Python nltk.sent_tokenize() Function Read More »

Python NLTK nltk.tokenize.ConditionalFreqDist() Function

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 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

nltk.tokenize.ConditionalFreqDist() Function:

Using nltk.tokenize.ConditionalFreqDist() function, we can count the frequency of words in a sentence.

Syntax:

tokenize.ConditionalFreqDist()

Parameters: This method doesn’t accept any parameters

Return Value:

The frequency of words in a sentence as a dictionary is returned by the ConditionalFreqDist() function

NLTK nltk.tokenize.ConditionalFreqDist() Function in Python

Method #1: Using ConditionalFreqDist() Function(Static Input)

Approach:

  • Import ConditionalFreqDist() function from the probability of nltk module using the import keyword
  • Import word_tokenize from tokenize of nltk module using the import keyword
  • Creating a reference/Instance variable(Object) for the ConditionalFreqDist Class
  • Give the string as static input and store it in a variable.
  • Pass the above-given string to the word_tokenize() function and loop in each word of it using the for loop
  • Here the word_tokenize function splits the given string into tokens i.e words
  • Get the length of the word using the len() function and store it in a variable
  • Increment the above condition(length of word) and word count by 1
  • Here index of the dictionary is the above condition(Length of the word)
  • Printing all the Conditional Freq Dictionary values.
  • The Exit of the Program.

Below is the implementation:

# Import ConditionalFreqDist() function from probability of nltk module using the import keyword
from nltk.probability import ConditionalFreqDist
# Import word_tokenize from tokenize of nltk module using the import keyword
from nltk.tokenize import word_tokenize
    
# Creating a reference/Instance variable(Object) for the ConditionalFreqDist Class
tkn = ConditionalFreqDist()
    
# Give the string as static input and store it in a variable.
gvn_str = "Python Programs Sample Codes in Python Codes"
# Pass the above given string to the word_tokenize() function and loop in each word of it using the for loop
# (Here the word_tokenize function splits the given string into tokens i.e words)
for wrd in word_tokenize(gvn_str):
  # Get the length of the word using the len() function and store it in a variable
  condition = len(wrd)
  # Increment the above condition(length of word) and word count by 1
  # Here index of dictionary is the above condition(Length of word)
  tkn[condition][wrd] += 1
  
# Printing all the Conditional Freq Dictionary values  
tkn

Output:

ConditionalFreqDist(nltk.probability.FreqDist,
{2: FreqDist({'in': 1}),
5: FreqDist({'Codes': 2}),
6: FreqDist({'Python': 2, 'Sample': 1}),
8: FreqDist({'Programs': 1})})
# Getting the Frequency dictionary of words which are having length =6
tkn[6]

Output:

FreqDist({'Python': 2, 'Sample': 1})

Method #2: Using ConditionalFreqDist() Function (User Input)

Approach:

  • Import ConditionalFreqDist() function from the probability of nltk module using the import keyword
  • Import word_tokenize from tokenize of nltk module using the import keyword
  • Creating a reference/Instance variable(Object) for the ConditionalFreqDist Class
  • Give the string as user input using the input() function and store it in a variable.
  • Pass the above-given string to the word_tokenize() function and loop in each word of it using the for loop
  • Here the word_tokenize function splits the given string into tokens i.e words
  • Get the length of the word using the len() function and store it in a variable
  • Increment the above condition(length of word) and word count by 1
  • Here index of the dictionary is the above condition(Length of the word)
  • Printing all the Conditional Freq Dictionary values.
  • The Exit of the Program.

Below is the implementation:

# Import ConditionalFreqDist() function from probability of nltk module using the import keyword
from nltk.probability import ConditionalFreqDist
# Import word_tokenize from tokenize of nltk module using the import keyword
from nltk.tokenize import word_tokenize
    
# Creating a reference/Instance variable(Object) for the ConditionalFreqDist Class
tkn = ConditionalFreqDist()
    
# 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 word_tokenize() function and loop in each word of it using the for loop
# (Here the word_tokenize function splits the given string into tokens i.e words)
for wrd in word_tokenize(gvn_str):
  # Get the length of the word using the len() function and store it in a variable
  condition = len(wrd)
  # Increment the above condition(length of word) and word count by 1
  # Here index of dictionary is the above condition(Length of word)
  tkn[condition][wrd] += 1

# Printing all the Conditional Freq Dictionary values  
tkn

Output:

Enter some random string = good morning all good good morning hello hi hi
ConditionalFreqDist(nltk.probability.FreqDist,
{2: FreqDist({'hi': 2}),
3: FreqDist({'all': 1}),
4: FreqDist({'good': 3}),
5: FreqDist({'hello': 1}),
7: FreqDist({'morning': 2})})

Python NLTK nltk.tokenize.ConditionalFreqDist() Function Read More »

Python nltk.word_tokenize() Function

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.

nltk.word_tokenize() Function:

The “nltk.word_tokenize()” method will be used to tokenize sentences and words with NLTK.

  • word_tokenize –  To tokenize words.

NLTK Tokenization is a method of dividing a vast amount of textual data into sections in order to analyze the text’s character.

Tokenization using NLTK can be used for training machine learning models and text cleaning with Natural Language Processing. With NLTK, tokenized words and phrases can be converted into a data frame and vectorized. Tokenization with the Natural Language Tool Kit (NLTK) includes punctuation cleaning, text cleaning, vectorization of parsed text data for better lemmatization, stemming, and machine learning algorithm training.

Kit for Natural Language Processing “tokenize” is a tokenization package in Python Libray. There are two kinds of tokenization functions in the NLTK “tokenize” package.

  • word_tokenize –  To tokenize words.
  • sent_tokenize –  To tokenize sentences.

Syntax:

word_tokenize(string)

Advantages of word tokenization with NLTK

White Space Tokenization, Dictionary Based Tokenization, Rule-Based Tokenization, Regular Expression Tokenization, Penn Treebank Tokenization, Spacy Tokenization, Moses Tokenization, and Subword Tokenization are all advantages of using NLTK for word tokenization. The text normalisation procedure includes all types of word tokenization. The accuracy of language understanding algorithms is improved by normalising the text with stemming and lemmatization. The advantages and benefits of word tokenization with NLTK are listed below.

  • Easily removing stop words from corpora prior to tokenization.
  • Splitting words into sub-words to improve understanding of the text.
  • Using NLTK, removing the text disambiguate is faster and needs less coding.
  • Aside from White Space Tokenization, Dictionary Based and Rule-based Tokenization are also simple to implement.
  • NLTK makes it easy to perform Byte Pair Encoding, Word Piece Encoding, Unigram Language Model, and Setence Piece Encoding.
  • TweetTokenizer in NLTK is used to tokenize tweets that include emojis and other Twitter standards.
  • PunktSentenceTokenizer in NLTK has a pre-trained model for tokenization in several European languages.
  • NLTK includes a Multi Word Expression Tokenizer for tokenizing compound words like  “in spite of”.
  • RegexpTokenizer in NLTK is used to tokenize phrases based on regular expressions.

nltk.word_tokenize() Function in Python

Method #1: Using word_tokenize() Function (Static Input)

Approach:

  • Import word_tokenize() function from tokenize of the nltk module using the import keyword
  • Give the string as static input and store it in a variable.
  • Pass the above-given string as an argument to the word_tokenize() function to tokenize into words and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import word_tokenize() function from tokenize of the nltk module using the import keyword
from nltk.tokenize import word_tokenize

# Give the string as static input and store it in a variable.
gvn_str = "hello this is Python-programs welcome all"

# Pass the above given string as an argument to the word_tokenize() function to 
# tokenize into words and print the result.
print(word_tokenize(gvn_str))

Output:

['hello', 'this', 'is', 'Python-programs', 'welcome', 'all']

Method #2: Using word_tokenize() Function (User Input)

Approach:

  • Import word_tokenize() function from tokenize of the nltk module using the import keyword
  • Give the string as user input using the input() function and store it in a variable.
  • Pass the above-given string as an argument to the word_tokenize() function to tokenize into words and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import word_tokenize() function from tokenize of the nltk module using the import keyword
from nltk.tokenize import word_tokenize

# 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 as an argument to the word_tokenize() function to 
# tokenize into words and print the result.
print(word_tokenize(gvn_str))

Output:

Enter some random string = good morning python programs
['good', 'morning', 'python', 'programs']

 

Python nltk.word_tokenize() Function Read More »