Posts

Identifying noun in a sentence using NLTK and getting its description using TextBlob Python (Simple Chat-bot script to get answer of question starting with "what" keyword)

Image
  About this Post In this post I will be sharing a simple python script to identify noun in a sentence and getting its description. Prerequisite Python 2 / Python 3 Python Packages Required NLTK (to identify noun in a sentence) TextBlob (To get word description and spell check) Python Code import nltk from textblob import Word , TextBlob # function to return all nouns in a sentence def is_noun (sentence): noun_list = [] tokenized = nltk.word_tokenize(sentence) print (nltk.pos_tag(tokenized)) for word , pos in nltk.pos_tag(tokenized): if pos[: 2 ] == 'NN' : noun_list.append(word) return noun_list # function to test if spelling is correct def spell_check (sentence): return sentence == TextBlob(sentence).correct() # function to check question starts with what def check_start_with_what (sentence): return sentence.find( 'what ' ) == 0 question = 'what is a rabbit?' if spell_check(question): if check_start_with_what(

Sending Email Using Python Script (Gmail )

Image
   About this Post In this post I will be sharing a simple python script to send email using Python SMTP module. I am taking Gmail example. Prerequisite Python 2 / Python 3 Enable Less Secure App Login (Gmail)-  https://myaccount.google.com/lesssecureapps Python Code import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart def mail_using_gmail (): from_email = '-------@gmail.com' to_email = "-------@gmail.com" # Create message container - the correct MIME type is multipart/alternative. msg = MIMEMultipart( 'alternative' ) msg[ 'Subject' ] = "Testing Email Using Python" msg[ 'From' ] = from_email msg[ 'To' ] = to_email # Create the body of the message (a plain-text and an HTML version). text = "This is a test email." part1 = MIMEText(text , 'plain' ) msg.attach(part1) # SMTP client session mail = smtplib.SMTP_SSL( 

Basic of Python - Variable and Memory Address

Image
  About this Post In this post I will be demonstrating how python works using example of variables and their memory addresses. Explanation with Python Code Examples #create a variable with value 10 var = 10 # id function is used to get the memory address  print(id(var)) #1712357776 - memory address #assign var_new equals to var var_new = var #var_new and var are now attached to same memory address print(id(var_new)) #1712357776 - memory address #reassign value of var  var = 20 # var gets detached from memory address of 10 and then attached to the memory address of 20 .We can see different memory address now print(id(var)) #1712358096 -memory address #var_new has old memory address because it is attached to memory address of 10 print(id(var_new)) # 1712357776 - memory address print(var) #20 print(var_new) #10 Now rather than reassigning value we will try to update the variable value. For this we can take list / dictionary example.  # taking a list variable list_var = [1,2,3,4] print(id(

Face Detection And Blur Face Using Face-Recognition and Pillow Python Package

Image
  About this Post In this post I will be sharing a simple python script to detect faces in image using face-recognition package and blurring the face using Pillow package  Prerequisite Python 2 / Python 3 Python Packages Required face-recognition -  https://pypi.org/project/face-recognition/  (if you are getting error while installing it download dlib whl  file and install it first . Pillow (Blur Image)-  https://pypi.org/project/Pillow/ Sample Image Link https://www.pexels.com/photo/group-of-people-smiling-3756513/ Python Code from PIL import Image, ImageDraw from PIL import ImageFilter import face_recognition image_path = "test.jpeg" # Open an image im = Image.open(image_path) # Load the jpg file into a numpy array - face recognition image = face_recognition.load_image_file(image_path) def get_face_in_image(loaded_image):     # face_locations method returns list of coordinates (top, right, bottom, left) of faces found     faces = face_recognition.face_locations(loaded_image

Highlight Text In PDF With Different Colors Using Python

Image
 About this Post In this post I will be sharing a simple python script which will highlight text with different colors in PDF. Prerequisite Python 2 / Python 3 Python Package Required PyMuPDF Sample File Link -  https://easyupload.io/2hiobb Python Code #fitz is used to highlight text in PDF import fitz from fitz.utils import getColor #we need to read pdf file as binary with open("sample.pdf", "rb") as f:     file = f.read() doc = fitz.open('pdf', file) #function for highlighting text with color def highlight(document, text, color_name):     for i in range(len(document)):          #looping through pages one by one (here we are having only one page in sample PDF)           page = document[i]          # searchFor is a page method that search text and based on finding returns list of Rect value which  is used for highlighting         text_instances = page.searchFor(text.strip())            #here we are defining color for highlighting         color = {"strok

How To Convert HTML Page Into PDF File Using Python

Image
About this Post In this post I will be sharing a simple python script to create a flask application where we can upload html file and script will convert it into PDF file. Prerequisite Python 2 / Python 3 Python Packages Required Flask (To run the application) Xhtml2Pdf (Package used to convert html to pdf) HTML Links That Can Be Chosen As Sample File https://www.w3schools.com/html/html_examples.asp https://www.w3schools.com/html/tryit.asp?filename=tryhtml_table_class2  ( I used this example) Python Code from flask import Flask, flash, request, redirect, url_for, make_response from io import BytesIO, StringIO from xhtml2pdf import pisa #only html file is allowed to upload ALLOWED_EXTENSIONS = set(['html']) #flask app app = Flask(__name__) #function to check allowed file def allowed_file(filename):     return '.' in filename and \            filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS #function to convert html into pdf def create_pdf(sourceHtml):