WT and DA Labbook
WT and DA Labbook
LABCOURSE III
SECTION I
Web Technologies II
SEMESTER-II
Co-ordinators
PREPARED BY
Prof. Shaikh A.M. H.P.T. Arts & R.Y.K. Science College, Nashik
Prof. Malani P.S. K.K.Wagh Arts,Commerce,Science & Computer Science College, Nashik
Page2 T.Y.B.Sc.(Comp.Sc.)Lab-III,Sem-II
About The WorkBook
Objectives –
1) Students should carry this book during practical sessions of Computer Science.
2) Students should maintain separate journal for the source code and outputs.
3) Students should read the topics mentioned in reading section of this Book before coming
for practical.
4) Students should solve all exercises which are selected by Practical in-charge as a part of
journal activity.
2 Incomplete 1
3 Late complete 2
4 Needs improvement 3
5 Complete 4
6 Well-done 5
Page3 T.Y.B.Sc.(Comp.Sc.)Lab-III,Sem-II
Instructions to the practical in-charge
1. Explain the assignment and related concepts in around ten minutes using white board if
required or by demonstrating the software.
6. The value should also be entered on assignment completion page of respected lab
course.
Page4 T.Y.B.Sc.(Comp.Sc.)Lab-III,Sem-II
PHP Semester – II
4 AJAX
Total out of 25
Total out of 05
University Exam Seat Number__________ has successfully completed the course work for CS - 368
Page5 T.Y.B.Sc.(Comp.Sc.)Lab-III,Sem-II
Assignment No 1. Cookies and Session
PHP Cookies
Cookie is a small piece of information stored as a file in the user's browser by the web server. Once created,
cookie is sent to the web server as header information with every HTTP request.
User can use cookie to save any data but it should not exceed 1K(1024 bytes) in size.
6
Real world Use of Cookies
1. To store user information like when he/she visited, what pages were visited on the website etc, so that
next time the user visits your website you can provide a better user experience.
2. To store basic website specific information to know this is not the first visit of user.
3. You can use cookies to store number of visits or view counter.
Types of Cookies
There are two types of cookies :
1. Session Cookie: This type of cookies are temporary and are expire as soon as the session ends or the
browser is closed.
2. Persistent Cookie: To make a cookie persistent we must provide it with an expiration time. Then the
cookie will only expire after the given expiration time, until then it will be a valid cookie.
syntax :
setcookie(name, value, expire, path, domain, secure)
The first argument which defines the name of the cookie is mandatory, rest all are optional arguments.
name Used to specify the name of the cookie. It is a mandatory argument. Name of the
cookie must be a string.
value Used to store any value in the cookie. It is generally saved as a pair with name. For
example, name is userid and value is 7007, the userid for any user.
expire Used to set the expiration time for a cookie. if you do not provide any value, the
cookie will be treated as a session cookie and will expire when the browser is closed.
7
path Used to set a web URL in the cookie. If set, the cookie will be accessible only from
that URL. To make a cookie accessible through a domain, set '/' as cookie path.
domain The domain of your web application. It can be used to limit access of cookie for sub-
domains.
secure If you set this to 1, then the cookie will be available and sent only over HTTPS
connection.
So if we want to create a cookie to store the name of the user who visited your website, and set an expiration
time of a week, then we can do it like this,
<?php
setcookie("username", "iamabhishek", time()+60*60*24*7);
?>
To access a stored cookie we use the $_COOKIE global variable, and can use the isset() method to check
whether the cookie is set or not.
Example :
<?php
// set the cookie
setcookie ("username", "iamabhishek", time()+60*60*24*7);
?>
<html>
<body>
<?php
// check if the cookie exists
if(isset($_COOKIE["username"]))
{
echo "Cookie set with value: ".$_COOKIE["username"];
}
else
{
echo "cookie not set!";
}
?>
</body> 8
</html>
So by providing the name of the cookie inside the square brackets with the global variable $_COOKIE[ ] we can
access the cookie.
NOTE: setcookie() function should be placed before the starting HTML tag(<html>).
<?php
// updating the cookie
?>
<html>
<body>
<?php
// check if the cookie exists
if(isset($_COOKIE["username"]))
}
else
{
echo "cookie not set!";
?>
</body>
</html>
9
Delete a Cookie in PHP
To delete/remove a cookie, we need to expire the cookie, which can be done by updating the cookie using
the setcookie() function with expiration date in past.
<?php
// updating the cookie
?>
<html>
<body>
<?php
echo "cookie username is deleted!";
?>
</body>
</html>
1. Web applications which require a user to login, use session to store user information, so that on every
webpage related information can be displayed to the user.
2. In eCommerce web stores, shopping cart is generally saved as part of session.
10
Start a Session in PHP
In PHP we can start a session by using the session_start() function. And data is stored in the session using
session variable, which can be assigned different values using global variable $_SESSION
Using the function session_start() we initialize the session, in which we can store information using the session
variable $_SESSION.
Example :
first_page.php
<?php
// start the session
session_start();
$_SESSION["username"] = "iamabhishek";
$_SESSION["userid"] = "1";
?>
<html>
<body>
<?php
echo "Session variable is set.";
?>
<a href="second_page.php">Go to Second Page</a>
</body>
</html>
NOTE: The function session_start() should be the first statement of the page, before any HTML tag.
<?php
// start the session 11
session_start();
$username = $_SESSION["username"];
$userid = $_SESSION["userid"];
?>
<html>
<body>
<?php
echo "Username is: ".$username."<br/>";
?>
</body>
</html>
Output :
Username is: iamabhishek
User id is: 1
session_start () function is used to initialize a new session and to fetch the ongoing session(if already started),
and then, using the $_SESSION global variable, we can either set new values into the session or get the saved
values.
If there are too many values stored in the session, and you don't know which one do you want to get, you can
use the below code to print all the current session variable data.
Example :
<?php
// start the session
session_start();
?>
<html>
<body>
<?php
12
print_r ($_SESSION);
?>
</body>
</html>
Output :
Array (
[userid] => 1
<?php
// start the session
session_start ();
?>
<html>
<body>
<?php
echo "Username is: ".$username."<br/>";
?>
</body>
</html>
13
Destroy a Session in PHP
To clean the session variable or to remove all the stored values from the session variable we can use the
function session_unset () and to detroy the session, we use session_destroy () function.
<?php
// start the session
session_start ();
?>
<html>
<body>
<?php
// clean the session variable
session_unset ();
session_destroy ();
?>
</body>
</html>
Set A
1. Write a PHP script to keep track of number of times the web page has been access.
[Use session and cookies]
2. Write a PHP script to change the preferences of your web page like font style, font size, font color,
background color using cookie. Display selected setting on next web page and actual implementation
(with new settings) on third page.
Set B
1. Write a PHP script to accept username and password. If in the first three chances, username and
password entered is correct then display second form with “Welcome message” otherwise display error
message. [Use Session]
2. Write a PHP script to accept Employee details (Eno, Ename, Address) on first page. On second page
accept earning (Basic, DA, HRA). On third page print Employee information (Eno, Ename, Address,
Basic, DA, HRA, Total) [ Use Session]
14
Set C
1. Crete a form to accept customer information ( Name, Addr, MobNo). Once the customer
information is accepted, accept product information in the next form (ProdName, Qty,Rate).
Generate the bill for the customer in the next form. Bill should contain the customer information and the
information of the products entered.
15
Assignment No 2. XML documents and DOM
Syntax:
university.css :
uname
{
color:black;
font-family:copperplate Gothoic Light;
font-size:16 pt;
font:bold;
}
city
{
color:yellow;
font-family:Arial;
font-size:12 pt;
font:bold;
}
rank
{
color:yellow;
font-family:Arial;
font-size:16 pt;
font:bold;
}
university.xml :
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="university.css"?>
<university>
<univ>
<uname>Pune University</uname>
<city>Pune</city>
<rank>2</rank>
</univ>
<univ>
<uname>Kolhapur University</uname>
<city>Kolhapur</city>
<rank>4</rank>
</univ>
</university> 16
Create xml file through PHP code :
<?php
$str =<<<XML
<?xml version="1.0"encoding="ISO-8859-1"?>
<BookStore>
<Books>
<PHP>
<Title>Programming in PHP</Title>
<Publication>O'RELLY</Publication>
</PHP>
<PHP>
<Title>Beginners PHP</Title>
<Publication>2000</Publication>
</PHP>
</Books>
</BookStore>
XML;
$fname="bookstore.xml";
$fp = fopen($fname,"w");
fwrite($fp, $str);
fclose($fp);
echo "created filename is:".$fname;
?>
What is DOM?
The W3C DOM provides a standard set of objects for HTML and XML documents, and a standard interface for
accessing and manipulating them.
The W3C DOM is separated into different parts (Core, XML, and HTML) and different levels (DOM Level
1/2/3):
Core DOM - defines a standard set of objects for any structured document
XML DOM - defines a standard set of objects for XML documents
HTML DOM - defines a standard set of objects for HTML documents
XML Parsing
To read and update - create and manipulate - an XML document, you will need an XML parser.
Tree-based parser: This parser transforms an XML document into a tree structure. It analyzes the whole
document, and provides access to the tree elements
17
Event-based parser : Views an XML document as a series of events. When a specific event occurs, it
calls a function to handle it.
DomDocument Example :
Create XML File “ Book.xml “
<BookInfo>
<book>
<bookno>1</bookno>
<bookname>JAVA</bookname>
<price>250</price>
<year>2006</year>
</book>
<book>
<bookno>2</bookno>
<bookname>PHP</bookname>
<authorname> S.Kadam</authorname>
<price>350</price>
<year>2009</year>
</book>
<book>
<bookno>3</bookno>
<bookname>C</bookname>
18
<authorname> Denis Ritchie</authorname>
<price>500</price>
<year>1971</year>
</book>
<book>
<bookno>4</bookno>
<bookname>C++</bookname>
<price>400</price>
<year>1994</year>
<book>
<bookno>5</bookno>
<bookname>DATABASE</bookname>
<price>700</price>
<year>2001</year>
</book>
</BookInfo>
$dom=new DomDocument();
$dom->load("book.xml");
$bname=$dom->getElementsByTagName("bookname");
foreach($bname as $b)
echo "<b>$b->textContent<b><br><br>";
} 19
?>
What is SimpleXML?
SimpleXML is new in PHP 5. It is an easy way of getting an element's attributes and text, if you know the XML
document's layout.
Compared to DOM or the Expat parser, SimpleXML just takes a few lines of code to read text data from an
element.
Elements - Are converted to single attributes of the SimpleXMLElement object. When there's more than
one element on one level, they're placed inside an array
Attributes - Are accessed using associative arrays, where an index corresponds to the attribute name
Element Data - Text data from elements are converted to strings. If an element has more than one text
node, they will be arranged in the order they are found
SimpleXML is fast and easy to use when performing basic tasks like:
20
Simple XML Example:
<empdetails>
<employee>
<empno>1</empno>
<empname>Sagar</empname>
<salary>20000</salary>
<designation>Clerk</designation>
</employee>
<employee>
<empno>2</empno>
<empname>Shubham</empname>
<salary>30000</salary>
<designation>Manager</designation>
</employee>
<employee>
<empno>3</empno>
<empname>Abhishekh</empname>
<salary>500000</salary>
<designation>CEO</designation>
</employee>
</empdetails>
21
Read contents of Employee.xml through php using simpleXML
Employee.php :
<?php
$xml=simplexml_load_file("Employee.xml");
foreach($xml->employee as $emp)
?>
Set A :
3) Write a PHP script to generate an XML file in the following format in PHP.
<?xml version="1.0" encoding="UTF-8"?>
<BookInfo>
<book>
<bookno>1</bookno>
<bookname>JAVA</bookname>
<authorname> Balguru Swami</authorname>
<price>250</price>
<year>2006</year>
</book>
<book>
<bookno>2</bookno>
22
<bookname>C</bookname>
<authorname> Denis Ritchie</authorname>
<price>500</price>
<year>1971</year>
</book>
</BookInfo>
Set B
1. Write PHP script to read above created “book.xml” file into simpleXML object. Display attributes and elements
.(Hint L simple_xml_load_file() function )
2. Write a PHP script to read “Movie.xml” file and print all MovieTitle and ActorName of file using DOMDocument
Parser. “Movie.xml” file should contain following information with at least 5 records with values.
MovieInfo
MovieNo, MovieTitle, ActorName , ReleaseYear
Set C
1. Create a XML file which gives details of movies available in “Movie CD Store “ from following categories
a) Classical
b) Horror
c) Action
Elements in each category are in the following format
<Category>
<MovieTitle> ……………. </ MovieTitle >
<ActorName> ……………….</ActorName>
<ReleaseYear> ………………… </ReleaseYear>
</Category>
Save the file with name “movies.xml”
23
ASSIGNMENT NO. : 4 JAVASCRIPT and jquery
Javascript is basically designed to create interactivity with HTML pages. It enables you to read and change the
content of HTML controls and also enables you to load a specific page depending upon the client’s request. It
helps you to do certain validations on client side.
Variables: Value of a variable can change during the script. No need to declare a variable.
Conditional statements and loops: In Javascript , the syntax of Conditional statements and loops are same as
that of PHP, C programming.
Javascript Object
Objects in Javascript are divided into three categories.
1. Built-in objects
2. Browser Objects
3. User-defined objects.
Built-in Objects:
Array, string, math, Date are commonly used built-in objects.
Array: Using Keyword new, you can create an instance of the object.
For ex. Varmyarray=new Aarray();
String: String object is used to manipulate a stored piece of text.
Var text =”PHP and Javascript”
Document.write(text.length);
Math: This object is used to perform common mathematical tasks .
Javascript provides eight mathematical values that can be accessed from the
math object.
These are
Math.E
Math.PI
Math.SQRT2
Math.SQRT 1_2
Math.LN2
Math.LN10
Math.LOG2E
Math.LOG10E
Ex. Round method is used to round a number
Document.write(Math.round(4.7))
Date: This object works with date and time
VarmyDate=new Date()
myDate.setFullYear(2015, 0, 20)
Browser Objects :
BOM is a collection of objects that interact with the browser window. These objects include the Window object,
history object, location object, navigator object, screen object and document object.The window object
method is the top object in BOM hierarchy. The window24 object is used to move, resize windows , create a new
windows. Window object is also used to create dialogue boxes such as alert boxes. Some commonly used methods
of window object are open, close, confirm, alert, prompt etc.
2) getElementByTagName() method:
This method returns all elements with the specified tag name.
Refer the examples given below.
1) simplejavascript example
<!DOCTYPE html>
<html>
<body>
<script type=”text/javascript”>
Document.write(“Hello World!”)
</script>
</body>
</html>
2) javascript example using 'alert box ' window object.
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
window.alert(5 + 6);
</script>
</body>
</html>
3)javascript example using 'document.write method'.
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<button onclick="document.write(5 + 6)">Try it</button>
</body>
</html>
Popup boxes :JavaScript has three kind of popup boxes: Alert box, Confirm
box, and Prompt box.
Alert Box: An alert box is often used if you want to make sure information
comes through to the user.
When an alert box pops up, the user will have to click "OK" to proceed.
25
Syntax
window.alert("sometext");
The window.alert() method can be written without the window prefix.
Example
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display an alert box:</p>
<button onclick="myFunction()">Try it</button>
<script>
functionmyFunction() {
alert("I am an alert box!");
}
</script>
</body>
</html>
Confirm Box :A confirm box is often used if you want the user to verify or accept something. When a confirm
box pops up, the user will have to click either "OK" or "Cancel" to proceed. If the user clicks "OK", the box
returns true. If the user clicks "Cancel", the box returns false.
Syntax
window.confirm("sometext");
The window.confirm() method can be written without the window prefix.
Example
var r = confirm("Press a button");
if (r == true) {
x = "You pressed OK!";
} else {
x = "You pressed Cancel!";
}
Prompt Box :A prompt box is often used if you want the user to input a value before entering a page. When a
prompt box pops up, the user will have to click either "OK" or
"Cancel" to proceed after entering an input value. If the user clicks "OK" the box returns the input value. If the
user clicks "Cancel" the box returns null.
Syntax
window.prompt("sometext","defaultText");
The window.prompt() method can be written without the window prefix.
Example
var person = prompt("Please enter your name", "Harry Potter");
if (person != null) {
document.getElementById("demo").innerHTML =
"Hello " + person + "! How are you today?";
}
HTML Events
An HTML event can be something the browser does, or something a user
does.
Here are some examples of HTML events:
An HTML web page has finished loading
An HTML input field was changed
An HTML button was clicked
Often, when events happen, you may want to do something.
26
JavaScript lets you execute code when events are detected.
HTML allows event handler attributes, with JavaScript code, to be added to
HTML elements.
With single quotes:
<some-HTML-element some-event='some JavaScript'>
With double quotes:
<some-HTML-element some-event="some JavaScript">
This is the easiest way to create a JavaScript Object. Using an object literal, you both define and create an object
in one statement.
An object literal is a list of name: value pairs (like age:50) inside curly braces {}.
The following example creates a new JavaScript object with four properties:
<!DOCTYPE html>
<html>
<body>
<p>Creating a JavaScript Object.</p>
<p id="demo"></p>
<script>
27
var person = {
firstName : "John",
lastName : "Doe",
age : 50,
eyeColor : "blue"
};
document.getElementById("demo").innerHTML =
person.firstName + " is " + person.age + " years old.";
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var person = new Object();
person.firstName = "John";
person.lastName = "Doe";
person.age = 50;
person.eyeColor = "blue";
document.getElementById("demo").innerHTML =
person.firstName + " is " + person.age + " years old.";
</script>
</body>
</html>
functionfunctionName(parameters) {
28
code to be executed
}
Declared functions are not executed immediately. They are "saved for later use", and will be executed later,
when they are invoked (called upon).
Example
<!DOCTYPE html>
<html>
<body>
<p>This example calls a function which performs a calculation,
and returns the result:</p>
<p id="demo"></p>
<script>
functionmyFunction(a, b) {
return a * b;
}
document.getElementById("demo").innerHTML =
myFunction(4, 3);
</script>
</body>
</html>
jQuery
What is jQuery?
jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is to make it much easier
to use JavaScript on your website. jQuery takes a lot of common tasks that require many lines of JavaScript code
to accomplish, and wraps them into methods that you can call with a single line of code.
jQuery also simplifies a lot of the complicated things from JavaScript, like AJAX calls and DOM manipulation.
The jQuery library contains following features:
HTML/DOM manipulation
CSS manipulation
HTML event methods
Effects and animations
AJAX
Utilities
The jQuery library is a single JavaScript file, and you reference it with the HTML <script> tag (notice that
the <script> tag should be inside the <head> section):
<head>
<script src="jquery-3.5.1.min.js"></script>
</head>
29
jQuery CDN
If you don't want to download and host jQuery yourself, you can include it from a CDN (Content Delivery
Network).Google is an example of someone who host jQuery:
Google CDN:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
But this will execute only when you are online. So if you have to run your scripts offline you have to use the
library option, ie
<head>
<script src="jquery-3.5.1.min.js"></script>
</head>
jQuery Syntax
The jQuery syntax is tailor-made for selecting HTML elements and performing some action on the element(s).
Examples:
jQuery Selectors
jQuery selectors allow you to select and manipulate HTML element(s). jQuery selectors are used to "find" (or
select) HTML elements based on their name, id, classes, types, attributes, values of attributes and much more.
It's based on the existing CSS Selectors, and in addition, it has some own custom selectors.
All selectors in jQuery start with the dollar sign and parentheses: $().
30
The element Selector
The jQuery element selector selects elements based on the element name.
You can select all <p> elements on a page like this:
$("p")
Example
<!DOCTYPE html>
<html>
<head>
<script src="jquery-3.5.1.min.js">
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me to hide paragraphs</button>
</body>
</html>
Syntax Description
$("a[target='_blank']") Selects all <a> elements with a target attribute value equal to
"_blank"
32
$("a[target!='_blank']") Selects all <a> elements with a target attribute value NOT
equal to "_blank"
$("*")
<!DOCTYPE html>
<html>
<head>
<script src=“jquery-3.5.1.min.js”></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("*").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body> </html>
$(this)
<!DOCTYPE html>
<html>
<head>
<script src=“jquery-3.5.1.min.js”></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$(this).hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
33
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>
</html>
$("p.intro")
<!DOCTYPE html>
<html>
<head>
<script src=“jquery-3.5.1.min.js”></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p.intro").hide();
});
});
</script>
</head>
<body>
<button>Click me</button>
</body>
</html>
$("p:first")
<!DOCTYPE html>
<html>
<head>
<script src=“jquery-3.5.1.min.js”></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p:first").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p> 34
<button>Click me</button>
</body>
</html>
$("ulli:first")
<!DOCTYPE html>
<html>
<head>
<script src=“jquery-3.5.1.min.js”></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("ulli:first").hide();
});
});
</script>
</head>
<body>
<p>List 1:</p>
<ul>
<li>Coffee</li>
<li>Milk</li>
<li>Tea</li>
</ul>
<p>List 2:</p>
<ul>
<li>Coffee</li>
<li>Milk</li>
<li>Tea</li>
</ul>
<button>Click me</button>
</body>
</html>
$("ulli:first-child")
<!DOCTYPE html>
<html>
<head>
<script src=“jquery-3.5.1.min.js”></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("ulli:first-child").hide();
});
});
</script> 35
</head>
<body>
<p>List 1:</p>
<ul>
<li>Coffee</li>
<li>Milk</li>
<li>Tea</li>
</ul>
<p>List 2:</p>
<ul>
<li>Coffee</li>
<li>Milk</li>
<li>Tea</li>
</ul>
<button>Click me</button>
</body>
</html>
$("[href]")
<!DOCTYPE html>
<html>
<head>
<script src=“jquery-3.5.1.min.js”></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("[href]").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<p><a href="https://www.w3schools.com/html/">HTML Tutorial</a></p>
<p><a href="https://www.w3schools.com/css/">CSS Tutorial</a></p>
<button>Click me</button>
</body>
</html>
36
$("a[target='_blank']")
<!DOCTYPE html>
<html>
<head>
<script src=“jquery-3.5.1.min.js”></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("a[target='_blank']").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<p><a href="https://www.w3schools.com/html/" target="_blank">HTML Tutorial</a></p>
<p><a href="https://www.w3schools.com/css/">CSS Tutorial</a></p>
<button>Click me</button>
</body>
</html>
$("a[target!='_blank']")
<!DOCTYPE html>
<html>
<head>
<script src=“jquery-3.5.1.min.js”></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("a[target!='_blank']").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<p><a href="https://www.w3schools.com/html/" target="_blank">HTML Tutorial</a></p>
<p><a href="https://www.w3schools.com/css/">CSS Tutorial</a></p>
<button>Click me</button>
</body></html> 37
$(":button")
<!DOCTYPE html>
<html>
<head>
<script src=“jquery-3.5.1.min.js”></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$(":button").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>
</html>
$("tr:even")
$("tr:odd")
38
jQuery after() Method
The jQueryafter() method inserts content (new or existing DOM elements) after target element(s) which is
specified by a selector.
Syntax:
$('selector expression').after('content');
First of all, specify a selector to get the reference of target element(s) after which you want to add the content
and then call after() method. Pass the content string as a parameter. Content string can be any valid HTML
element.
<!DOCTYPE html>
<html>
<head>
<script src="jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function () {
});
</script>
<style>
div{
border: 1px solid;
background-color:red;
margin: 2px 0 2px 0;
}
</style>
</head>
<body>
<h1>Demo: jQueryafter() method </h1>
<div id="div1">div 1
</div>
<div id="div2">div 2
</div>
</body>
</html>
jQuery before() Method
The jQuerybefore() method inserts content (new or existing DOM elements) before target element(s) which is
specified by a selector.
Syntax:
$('selector expression').before('content');
Specify a selector to get the reference of target element(s) before which you want to add the content and then
call before() method. Pass the content string that can be any valid HTML element as parameter.
<!DOCTYPE html>
<html>
<head>
<script src="jquery-3.5.1.min.js"></script>
<script>
39
$(document).ready(function () {
});
</script>
<style>
div{
border: 1px solid;
background-color:red;
margin: 2px 0 2px 0;
}
</style>
</head>
<body>
<h1>Demo: jQuerybefore() method </h1>
<div id="div1">div 1
</div>
jQuery append() Method
The jQueryappend() method inserts content to the end of target element(s) which is specified by a selector.
Syntax:
$('selector expression').append('content');
First specify a selector expression to get the reference of an element(s) to which you want to append content,
then call append() method and pass content string as a parameter.
<!DOCTYPE html>
<html>
<head>
<script src="jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function () {
$('p').append('World!');
});
</script>
</head>
<body>
<h1>Demo: jQueryappend() method </h1>
<p>Hello </p>
</body>
</html>
});
</script>
</head>
<body>
<h1>Demo: jQueryprepend() method </h1>
<div>
<label>This is div.</label>
</div>
</body>
</html>
$('label').remove();
});
</script>
</head>
<body>
<h1>Demo: jQueryremove() method </h1>
<div>This is div.
<label>This is label.</label>
</div>
</body>
</html>
jQueryreplaceAll() Method
The jQueryreplaceAll() method replaces all target elements with specified element(s).
Syntax:
$('content string').replaceAll('selector expression');
Here, syntax is different. First specify a content string as replacement element(s) and then call replaceAll()
method with selector expression to specify a target element(s).
<!DOCTYPE html>
41
<html>
<head>
<script src="jquery-3.5.1.min.js"></script> <script>
$(document).ready(function () {
$('<span>This is span</span>').replaceAll('p');
});
</script>
</head>
<body>
<h1>Demo: jQueryreplaceAll() method </h1>
<div>
<p>This is paragraph.</p>
</div>
<p>This is another paragraph.</p>
</body>
</html>
jQuery wrap() Method
The jQuerywrap() method wrap each target element with specified content element.
Syntax:
$('selector expression').wrap('content string');
Specify a selector to get target elements and then call wrap method and pass content string to wrap the target
element(s).
<!DOCTYPE html>
<html>
<head>
<script src="jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function () {
$('span').wrap('<p></p>');
});
</script>
</head>
<body>
<h1>Demo: jQuerywrap() method </h1>
<div>
<span>This is span.</span>
</div>
<span>This is span.</span>
</body>
</html>
42
Example of appending in paragraph text and in unordered list in a given HTML document using jQuery
selectors.
<!DOCTYPE html>
<html>
<head>
<script src=“jquery-3.5.1.min.js”></script>
<script>
$(document).ready(function(){
$("#btn1").click(function(){
$("p").append(" <b>Appended text</b>.");
});
$("#btn2").click(function(){
$("ul").append("<li>Appended item</li>");
});
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<ul>
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
</ul>
<button id="btn1">Append text</button>
<button id="btn2">Append list items</button>
</body>
</html>
$("#btn2").click(function(){
$("p").after("<i>After</i>");
});
});
</script>
</head>
<body>
</body>
</html>
SET A:
1) Write a javascript to display message ‘Exams are near, have you started preparing for?’ using alert,
prompt and confirm boxes. Accept proper input from user and display messages accordingly.
2) Add or append in paragraph text and also in the numbered(ordered) list in a given HTML document
using jQuery selectors.
[Hint : Use Append( ) method]
SET B:
1) Write a javascript function to validate username and password for a membership form.
2) To insert text before and after an image using jQuery.
[Hint : Use before( ) and after( )]
SET C:
1) Write a Javascript program to accept name of student, change font color to red, font size to 18 if student
name is present otherwise on clicking on empty text box display image which changes its size (Use
onblur, onload, onmousehover, onmouseclick, onmouseup)
2) Remove div section elements after clicking on button using jQuery.
[Hint : Use #id selector]
Assignment Evaluation
44
ASSIGNMENT NO. 4: AJAX
AJAX is not a new programming language, but a new way to use existing standards.
AJAX is the art of exchanging data with a server, and updating parts of a web page - without
reloading the whole page.
XMLHttpRequest is a JavaScript object capable of calling the server and capturing its
response. It is used to send HTTP or HTTPS requests to a web server and load the server
response data back into the script.
All modern browsers (IE, Firefox, Chrome, Safari, and Opera) have a built- in XMLHttpRequest
object.
xmlhttp=new XMLHttpRequest();
When a request to a server is sent, we want to perform some actions based on the
response.
The onreadystatechange event is triggered every time the readyState
changes.
The readyState property holds the status of the XMLHttpRequest.
Three important properties of the XMLHttpRequest object:
Property Description
onreadystatechange
Stores a function (or the name of a function) to be
called automatically each time the readyState property
changes
readyState Holds the status of the XMLHttpRequest.
Changes from 0 to 4:
0: request not initialized
1: server connection established
2: request received
3: processing request
4: request finished and response is ready
Status 200: "OK"
404: Page not found
In the onreadystatechange event, we specify what will happen when the server response is
ready to be processed. 45
The onreadystatechange event is triggered five times (0-4), one time for each change in
readyState.
To get the response from a server, use the responseText or responseXML property of the
XMLHttpRequest object. The responseText property returns the response as a string.
To send a request to a server, we use the open() and send() methods of the XMLHttpRequest
object:
Method Description
open(method,url,async)
Specifies the type of request, the URL, and if the
request should be handled asynchronously or not.
Example 1:
Write a Ajax program to search Student Name according to the character typed and display list using
array.
<?php
$n=$_GET['n'];
$a=array();
$a[]="sonal";
$a[]="sanjay";
$a[]="anant";
$a[]=”anushka”
$a[]=”kajal”
foreach($a as $v)
{
46
$s=substr($v,0,strlen($n));
if($s===$n)
echo "$v<br>";
?>
<html> <body>
<script type="text/javascript">
function display()
var n= document.getElementById('n').value;
x.send( );
x.onreadystatechange = function()
document.getElementById("show").innerHTML = x.responseText;
</script>
</body>
</html>
Output :
Write AJAX program to print movie details by selecting an Actor’s name. Create tables Movie and Actor
with 1:M cardinality as follows:
Actor (ano,aname)
[Use PostgreSQL]
Solution :
<html>
<body>
<script type="text/javascript">
function display()
var n= document.getElementById('n').value;
x.send( );
x.onreadystatechange = function()
document.getElementById("show").innerHTML = x.responseText;
</script>
</body>
</html>
<?php
$aname=$_POST['aname'];
if($rs!=null)
echo"<table border=0>";
while($row=pg_fetch_row($rs))
echo"<tr>";
echo"<td>".$row[0]."</td>";
echo"<td>".$row[1]."</td>";
echo"</tr>";
echo"</table>";
else
pg_close($con);
?>Output :
Run majax.php file on browser enter actor name in text box and see output
49
Set A
1. Write AJAX program to read contact.dat file and print the contents of the file in a tabular
format when the user clicks on print button. Contact.dat file should contain srno, name,
residence number, mobile number, Address. [Enter at least 3 record in contact.dat file]
2. Write AJAX program where the user is requested to write his or her name in a text box,
and the server keeps sending back responses while the user is typing. If the user name is
not entered then the message displayed will be, “Stranger, please tell me your name!”. If
the name is Rohit, Virat, Dhoni, Ashwin or Harbhajan , the server responds with “Hello,
master <user name>!”. If the name is anything else, the message will be “<user name>, I
don’t know you!”.
Set B
Set C
1. Write Ajax program to fetch suggestions when is user is typing in a textbox. (eg like
google suggestions. Hint create array of suggestions and matching string will be displayed)
2. Write Ajax program to get book details from XML file when user select a book name.
Create XML file for storing details of book(title, author, year, price).
Assignment Evaluation
(Designed by: Ms. Sarita Byagar, Indira College of Commerce and Science, Pune)
50
ASSIGNMENT 5: PHP Framework CODEIGNITER
CodeIgniter is a powerful PHP framework with a very small footprint, built for developers
who need a simple and elegant toolkit to create full-featured web applications.
The Model represents your data structures. Typically, your model classes will contain
functions that help you retrieve, insert and update information in your database.
The View is information that is being presented to a user. A View will normally be a
web page, but in CodeIgniter, a view can also be a page fragment like a header or
footer. It can also be an RSS page, or any other type of “page”.
The Controller serves as an intermediary between the Model, the View, and any other
resources needed to process the HTTP request and generate a web page.
CodeIgniter requires PHP version 5.4 or newer and MySQL 5.1 or newer with mysqli and
pdo drivers so make sure that your system meets CodeIgniter system requirements.
1. Download codeigniter
The next thing you need to do is to navigate to your server’s directory root and
download the current version of CodeIgniter.
cd /var/www/wget
https://github.com/bcit-ci/CodeIgniter/archive/3.0.1.zip
unzip 3.0.1.zip
51
Rename the directory to be more user-friendly:
mv /var/www/CodeIgniter-3.0.1 /var/www/codeigniter
nano /etc/apache2/sites-enabled/000-default
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/codeigniter/
ServerName yourdomain.com
ServerAlias www.yourdomain.com
<Directory /var/www/codeigniter/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog /var/log/httpd/yourdomain.com-error_log
CustomLog /var/log/httpd/yourdomain.com-access_log common
</VirtualHost>
52
5. Create new database
Now, create a MySQL database for CodeIgniter:
Once you create the MySQL database you need to change the database connectivity senttigs
to the settings needed to access your newly created database.
nano /var/www/codeigniter/application/config/database.php
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => '',
'password' => '',
'database' => '',
);
Here, you need to enter your database connectivity settings. Save the file and close it.
nano /var/www/codeigniter/application/config/config.php
$config['base_url'] = 'http://yourdomain.com';
Once you enter your domain name, save the file and close it.
http://www.codeigniter.com/user_guide/
53
Set A
1. Create a CSS file to apply the following styling for an HTML document.
Background color: blue,
H1
Color : red,
Font-family : Verdana,
Font-size : 8
Color : green,
Font-family : Monospace,
Font-size :10
2. Add a Javascript file in codeigniter. The javascript code should check whether a
number is positive or negative.
Set B
1. Create a table student having attributes (rollno, name, class). Assume appropriate
data types for the attributes. Using Codeigniter , connect to the database and
insert minimum 5 records in it.
2. For the above table student, display all its records using Codeigniter.
Set C
1. Create a form to accept personal details of customer (name, age, address). Once
the personal information is accepted, on the next page accept order details such as
(product name, quantity). Display the personal details and order details on the
third page. (Use cookies) 54
2. Write a PHP script to accept Employee details ( Eno, Ename, address) on first
page. On second page accept earning (Basic, Da, HRA). On third page print
Employee information( ENO,Ename, Address, BASIC, DA, HRA, TOTAL) (Use
Session)
Assignment Evaluation
(Designed by: Ms. Sarita Byagar, Indira College of Commerce and Science, Pune)
55
T.Y.B.Sc. (Computer Science)
Semester – VI
WORKBOOK FOR
CS – 368
SECTION II
Practical Course on DATA ANALYTICS
Total Marks : / 15
Converted Marks : /
Signature of Incharge
Date:
Types of Regression
There are various types of regressions which are used in data science and machine learning. Each type has its
own importance on different scenarios, but at the core, all the regression methods analyze the effect of the
independent variable on dependent variables. Here in this assignment we will learn Linear Regression and
Logistic Regression in detail.
Linear Regression:
• Linear regression is a statistical regression method which is used for predictive analysis.
• It is one of the very simple and easy algorithms which works on regression and shows the relationship
between the continuous variables.
• It is used for solving the regression problem in machine learning.
• Linear regression shows the linear relationship between the independent variable (X-axis) and the
dependent variable (Y-axis), hence called linear regression.
• If there is only one input variable (x), then such linear regression is called simple linear regression.
And if there is more than one input variable, then such linear regression is called multiple linear
regression.
• The relationship between variables in the linear regression model can be explained using the below
image. Here we are predicting the salary of an employee on the basis of the year of experience.
Logistic Regression:
• Logistic regression is another supervised learning algorithm which is used to solve the classification
problems. In classification problems, we have dependent variables in a binary or discrete format such
as 0 or 1.
• Logistic regression algorithm works with the categorical variable such as 0 or 1, Yes or No, True or
False, Spam or not spam, etc.
• It is a predictive analysis algorithm which works on the concept of probability.
• Logistic regression is a type of regression, but it is different from the linear regression algorithm in the
term how they are used.
• Logistic regression uses sigmoid function or logistic function which is a complex cost function. This
sigmoid function is used to model the data in logistic regression. The function can be represented as:
• It uses the concept of threshold levels, values above the threshold level are rounded up to 1, and values
below the threshold level are rounded up to 0.
Self-Activity
6. Residual analysis(Check the results of model fitting to know whether the model is satisfactory)
plt.scatter(X_test,y_test,color="green") # Plot a graph with X_test vs y_test
plt.plot(X_train,regressor.predict(X_train),color="red",linewidth=3) # Regressior line showing
plt.title('Regression(Test Set)')
plt.xlabel('HP')
plt.ylabel('MSRP')
plt.show()
Here we plot a scatter plot graph between X_test and y_test datasets and we draw a regression line.
plt.scatter(X_train,y_train,color="blue") # Plot a graph with X_train vs y_train
plt.plot(X_train,regressor.predict(X_train),color="red",linewidth=3) # Regressior line showing
plt.title('Regression(training Set)')
Sample Example -
Goal is to build a logistic regression model in Python in order to determine whether candidates would get
admitted to a prestigious university.
Here, there are two possible outcomes: Admitted (represented by the value of ‘1’) vs. Rejected (represented
by the value of ‘0’).
You can then build a logistic regression in Python, where:
• The dependent variable represents whether a person gets admitted; and
• The 3 independent variables are the GMAT score, GPA and Years of work experience
2. Reading and understanding the data(eventually do appropriate transformations- cleaning, filling nulls,
duplicates, etc…)
data = pd.read_csv("C:\TYBSC\Student_Score.csv") # dataset
6. Print test data and predicted data Predictions on the test set
Diving Deeper into the Results -> print two components in the python code:
print (x_test)
print (y_pred)
The prediction was also made for those 10 records (where 1 = admitted, while 0 = rejected):
In the actual dataset (from step-1), you’ll see that for the test data, we got the correct results 8 out of 10
times:
SET B
2. Use the iris dataset. Write a Python program to view some basic statistical details like percentile,
mean, std etc. of the species of 'Iris-setosa', 'Iris-versicolor' and 'Iris-virginica'. Apply logistic
regression on the dataset to identify different species (setosa, versicolor, verginica) of Iris
flowers given just 4 features: sepal and petal lengths and widths.. Find the accuracy of the
model.
Assignment Evaluation
Objectives
● To understand the impact of finding frequent patterns from large datasets.
● To learn the Apriori Algorithm which is used for frequent itemsets mining.
● To understand Association Rule Mining.
● To write and learn implementation of such concepts with Python.
Reading
You should read the following topics before starting this exercise:
● Why Pre-processing is must before analysis of data.
● What is support, confidence and lift.
● Learn definitions such as frequent itemsets, association between things, Apriori
Property of sets.
● Basic understanding of libraries supported in Python for performing these tasks.
Ready Reference
Frequent Itemset Mining: Finding frequent patterns, associations, correlations, or causal
structures among sets of items or objects in transaction databases, relational databases, and
other information repositories.
Association Mining searches for frequent items in the data-set. In frequent mining usually the
interesting associations and correlations between item sets in transactional and relational
databases are found.
If there are 2 items X and Y purchased frequently then it is good to put them together in stores
or provide some discount offer on one item on purchase of other item. This can really increase
the sales. For example it is likely to find that if a customer buys Milk and bread he/she also
buys Butter. So the association rule is [‘milk]^[‘bread’]=>[‘butter’].
Applications: Market Basket Analysis is one of the key techniques used by large retailers to
uncover associations between item, catalog design, loss-leader analysis, clustering,
classification, recommendation systems, etc.
Apriori is an algorithm for frequent item set mining and association rule learning over
relational databases. The name of the algorithm is based on the fact that the algorithm uses
prior knowledge of frequent itemset properties. Apriori employs an iterative approach known
as a levelwise search, where k-itemsets are used to explore (k+1)-itemsets
Support:
The support of item I is defined as the ratio between the number of transactions containing the
item I by the total number of transactions expressed as :
Confidence:
This is measured by the proportion of transactions with item I1, in which item I2 also appears.
Given that the item on the left hand side (antecedent) is purchased then the item on the right
hand side(consequent) would also be purchased.
Lift:
Lift is the ratio between the confidence and support expressed as :
Lift (antecedent => consequent) = 1 means that there is no correlation within the itemset, > 1
means that there is a positive correlation within the itemset, i.e., products in the
itemset, antecedent, and consequent, are more likely to be bought together, < 1 means that there
is a negative correlation within the itemset, i.e., products in itemset, antecedent, and consequent,
are unlikely to be bought together.
1. Define the minimum support and confidence for the association rule
2. Take all the subsets in the transactions with higher support than the minimum support
3. Take all the rules of these subsets with higher confidence than minimum confidence
4. Sort the association rules in the decreasing order of lift.
5. Visualize the rules along with confidence and support.
In this assignment you will analyze collections of market baskets and will determine frequent
itemsets and association rules present in the collections.
Python libraries
Python has many libraries for apriori implementation.
i. Mlxtend (apriori)
ii. Apyori (apriori)
iii. pypi (efficient_apriori)
The apriori module from mlxtend library provides fast and efficient apriori implementation.
Parameters
• df : One-Hot-Encoded DataFrame or DataFrame that has 0 and 1 or True and False as
values
• min_support : Floating point value between 0 and 1 that indicates the minimum support
required for an itemset to be selected.
# of observation with item / total observation# of observation with item / total observation
• use_colnames : This allows to preserve column names for itemset making it more
readable.
• max_len : Max length of itemset generated. If not set, all possible lengths are evaluated.
• verbose : Shows the number of iterations if >= 1 and low_memory is True. If =1 and
low_memory is False , shows the number of combinations.
• low_memory :
• If True, uses an iterator to search for combinations above min_support. Note that while
low_memory=True should only be used for large dataset if memory resources are limited,
because this implementation is approx. 3–6x slower than the default.
The function returns a pandas DataFrame with columns ['support', 'itemsets'] of all itemsets
that are >= min_support and < than max_len (if max_len is not None).
Leverage computes the difference between the observed frequency of A and C appearing
together and the frequency that would be expected if A and C were independent. A leverage
value of 0 indicates independence.
A high conviction value means that the consequent is highly depending on the antecedent.
Self-Activity
Dataset Sources
https://www.kaggle.com/datasets/sivaram1987/association-rule-learningapriori
https://github.com/shivang98/Market-Basket-Optimization
https://www.kaggle.com/datasets/hemanthkumar05/market-basket-optimization
https://www.kaggle.com/datasets/irfanasrullah/groceries
Lab Assignments
SET A:
1. Create the following dataset in python
SET B:
SET C:
Write a python code to implement the apriori algorithm. Test the code on any standard dataset.
Assignment Evaluation
Objectives
• To understand the concept of sentiment analysis.
• To learn various methodologies for analysis on text including text analytics, tokenization, frequency
distribution, stopwords, stemming, lemmatization, part-of-speech tagging.
• To write the Python scripts using various libraries for sentiment analysis using natural language processing
toolkit and classifying emotions on basis of labels i.e. Positive, Negative and Neutral. Also to use wordcloud
package for words comparison.
• To perform analysis on social media data such as Facebook, Twitter, YouTube.
• To graphically represent the analyzed data.
Reading
You should read the following topics before starting the exercise :
What is the need of doing data analysis using natural language processing. Basics of Python libraries such as
pandas, matplotlib, numpy, scikit-learn, nltk, VADER tool to perform the data analysis.
Ready Reference
Python Libraries for performing text and Sentiment Analysis :
Natural Language Toolkit (NLTK) :
NLTK is a Python Package for performing Natural Language Processing on human language data which is
mostly unstructured. It mainly focuses on analyzing textual data. It supports different natural language
processing algorithms such as Tokenization, Frequency Distribution, Stopwords, Lexicon Normalization,
Stemming, Lemmatization, POS Tagging. These are considered as pre-processing steps to perform text
analytics.
Installation of NLTK : You can use any IDE to perform Python programming for the following tasks. Here
Spyder IDE is used.
After running the above script, a screen will come to download the packages. Here click on download to
download all the supporting NLTK packages.
You can also download all NLTK packages using Python statement :
nltk.download(‘all’)
If all the packages are not needed, then individual packages can also be installed by passing its name in
nltk.download().
Syntax : nltk.download(‘package_name’)
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
For example : nltk.download(‘punkt’)
Tokenization : It is the first step to perform text analytics. Tokenization means breaking down a textual
paragraph into small chunks such as words or sentences. It is classified into two sections :
Sentence Tokenization and Word Tokenization : Sentence Tokenization breaks the text into sentences
whereas Word Tokenization breaks the text into words.
Example :
Output :
Tokenized Sentences :
['Hello all, Welcome to Python Programming Academy.', 'Python Programming Acade
my is a nice platform to learn new programming skills.', 'It is difficult to get
enrolled in this Academy.']
Tokenized Words :
['Hello', 'all', ',', 'Welcome', 'to', 'Python', 'Programming', 'Academy', '.',
'Python', 'Programming', 'Academy', 'is', 'a', 'nice', 'platform', 'to', 'learn'
, 'new', 'programming', 'skills', '.', 'It', 'is', 'difficult', 'to', 'get', 'en
rolled', 'in', 'this', 'Academy', '.']
Frequency Distribution :
The frequency distribution helps to understand how many words have occurred how many times in the
given textual data.
Example :
# Import word_tokenize
from nltk.tokenize import word_tokenize
# Import FreqDist package belonging to nltk.probability
from nltk.probability import FreqDist
# Textual data for word tokenization
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
paragraph_text="""Hello all, Welcome to Python Programming Academy. Python
Programming Academy is a nice platform to learn new programming skills. It is
difficult to get enrolled in this Academy."""
# Word Tokenization
tokenized_words=word_tokenize(paragraph_text)
# Pass the tokenized words to FreqDist
frequency_distribution=FreqDist(tokenized_words)
print(frequency_distribution)
Output :
<FreqDist with 24 samples and 32 outcomes>
To find most common words using Frequency Distribution, add the following lines in above code :
print(frequency_distribution.most_common(2))
Output :
Output :
Stopwords : Stopwords are considered as Noise in textual data. For example if text is containing words
such as is, are, am, a, this, the, an etc. then they are treated as stopwords.
These stopwords needs to be removed from actual text for further processing. Using NLTK, first identify
and create a list of stopwords in given text. Then remove it from the original content. Before working with
stopwords, make sure to download it by using following :
import nltk
nltk.download('stopwords')
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
To check list all Stopwords :
from nltk.corpus import stopwords
# It will find the stowords in English language.
stop_words_data=set(stopwords.words("english"))
print(stop_words_data)
Output :
{'wouldn', 'down', 'was', 'any', 'themselves', 'on', 'how', 'y', 'them', 'do',
'as', "couldn't", 'wasn', 'can', 'yourself', "mightn't", 'm', "wasn't", 'yours',
"haven't", 'have', 'their', 'from', 'with', 'through', 'been', 'couldn', 'here',
'your', 'above', 'same', 'ours', 'now', 'isn', 'that', 'just', 'further',
'only', "won't", 'having', 'these', 'won', 'himself', 'ourselves', 'which',
"you're", 'while', 'of', "doesn't", "should've", "mustn't", 'hadn', 'are',
'not', 'he', 'she', 'am', 'an', 'most', 'whom', 'where', 'than', 'didn',
"isn't", 'shouldn', 'what', 'mustn', 'some', 'very', 'should', 'ain', "you'd",
'yourselves', 'own', 'but', 'we', 't', 'out', 'such', 'in', 've', 'this',
'shan', 'about', 'over', 'both', 'all', 'why', 'i', 'being', "wouldn't", 'll',
'myself', 'between', 'has', "didn't", 'hers', 'hasn', "she's", 'other', 'if',
'itself', 'below', "aren't", 'too', 'under', 'herself', 'be', 'after', 'off',
're', 'during', 'until', 'our', "shouldn't", 'into', 'don', 'again', 'nor',
'needn', "that'll", "weren't", 'no', 'so', 'then', 'before', 'his', 'its',
'few', 'doing', "don't", "you'll", "hadn't", 'because', 'there', 'did', 'my',
"needn't", "it's", 'they', 'for', 'does', 'is', 'a', 'against', 'who', 'and',
"shan't", 'o', 'weren', 'him', 'or', 'theirs', 'were', 'had', 'doesn', 'you',
'haven', 'those', 'me', 'when', 's', 'd', 'it', 'up', 'by', 'each', 'once',
'aren', "you've", 'her', "hasn't", 'to', 'more', 'will', 'mightn', 'the', 'at',
'ma'}
Removing Stopwords :
The above words in the output are predefined stopwords in English Language. If either of these words occur in
a user-defined textual data, then it can be removed as follows :
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
# Textual data to remove stopwords
paragraph_text="""Hello all, Welcome to Python Programming Academy. Python
Programming Academy is a nice platform to learn new programming skills. It is
difficult to get enrolled in this Academy."""
# Word Tokenization
tokenized_words=word_tokenize(paragraph_text)
# It will find the stowords in English language.
stop_words_data=set(stopwords.words("english"))
# Create a stopwords list to filter it from original text
filtered_words_list=[]
for words in tokenized_words:
if words not in stop_words_data:
filtered_words_list.append(words)
print("Tokenized Words : \n",tokenized_words,"\n")
print("Filtered Words : \n",filtered_words_list,"\n")
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
Output :
Tokenized Words :
Filtered Words :
Stemming : Stemming is a process of linguistics normalization to reduce words to their word root or divide
the derivational affixes. For example : writing, wrote, written can stemmed or reduced as write.
Example :
# Same code as previous example to remove stop words from tokenized words
from nltk.stem import PorterStemmer
porter_stemmer=PorterStemmer()
stemmed_text_words=[]
for words in filtered_words_list:
stemmed_text_words.append(porter_stemmer.stem(words))
print("Filtered Words : \n",tokenized_words,"\n")
print("Stemmed Words : \n",stemmed_text_words,"\n")
Lemmatization : Lemmatization is a process of removing words to their base words which is linguistically
correct lemmas. For example : “Running” word will be lemmatized to “run”. Before that download the
package “wordnet” belonging to nltk as follows :
import nltk
nltk.download('wordnet')
# Lemmatization
from nltk.stem.wordnet import WordNetLemmatizer
lemmatizer=WordNetLemmatizer()
word_text="running"
print("Lemmatized Word : ",lemmatizer.lemmatize(word_text,"v"))
Output :
Lemmatized Word : run
POS Tagging : The POS (Part-of-Speech) tagging is basically used to identify the grammatical group of
given words i.e. Noun, Pronoun, Verb, Adjective, Adverbs etc. on the basis of its context.
Before that download the package “averaged_perceptron_tagger” belonging to nltk as follows :
import nltk
nltk.download('averaged_perceptron_tagger')
# Part-of-Speech Tagging
import nltk
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
from nltk.tokenize import word_tokenize
text_data="Hello all, Welcome to Python programming"
tokenized_data=word_tokenize(text_data)
print(nltk.pos_tag(tokenized_data))
Output :
[('Hello', 'NNP'), ('all', 'DT'), (',', ','), ('Welcome', 'NNP'), ('to', 'TO'),
('Python', 'NNP'), ('programming', 'NN')]
Text Summarization :
Text summarization is an NLP technique that extracts text from a large amount of data. It is the process of
identifying the most important meaningful information in a document and compressing it into a shorter
version by preserving its meaning. Types: Extractive summarization and Abstractive summarization
To perform extractive summarization, we calculate the sentence weights and choose the first ‘n’ sentences
with maximum weight. The weights are calculated on the basis of the word frequencies
Steps:
1. Preprocess the text
2. Create the word frequency table
3. Tokenize the sentence
4. Score the sentences: Term frequency
5. Generate the summary
Sample code
import nltk
nltk.download('all')
#Preprocessing
import re
text="""
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
# Creating a frequency table of words
wordfreq = {}
for word in words:
if word in stopWords:
continue
if word in wordfreq:
wordfreq[word] += 1
else:
wordfreq[word] = 1
#Compute the weighted frequencies
maximum_frequency = max(wordfreq.values())
for word in wordfreq.keys():
wordfreq[word] = (wordfreq[word]/maximum_frequency)
# Creating a dictionary to keep the score # of each sentence
sentences = sent_tokenize(text)
sentenceValue = {}
for sentence in sentences:
for word, freq in wordfreq.items():
if word in sentence.lower():
if sentence in sentenceValue:
sentenceValue[sentence] += freq
else:
sentenceValue[sentence] = freq
import heapq
summary = ''
summary_sentences = heapq.nlargest(4, sentenceValue, key=sentenceValue.get)
summary = ' '.join(summary_sentences)
print(summary)
import nltk
nltk.download('vader_lexicon')
Examples : Let’s consider some text statements expressing different emotions and analyzing them using
VADER.
Example 1 :
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
vader_analyzer=SentimentIntensityAnalyzer()
text1="I am feeling good" # The text is positive.
print(vader_analyzer.polarity_scores(text1))
Output :
{'neg': 0.0, 'neu': 0.185, 'pos': 0.815, 'compound': 0.5267}
It has given ‘pos’ value as 0.815 which is maximum of all the other values since the statement is positive.
Similarly, we can check it on other emotions as well.
Example 2 :
Output :
Example 3 : Consider the following example to get the overall rating about a statement
i.e. overall whether it is positive, negative or neutral.
Output :
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
Word cloud is basically a data visualization technique to represent the textual content where the size of each
visualized word implies its importance, frequency and intensity. It is a good tool to visualize the text and
perform sentiment analysis to find the frequency of words having positive, negative or neutral emotions.
Now to perform sentiment analysis on above dataset and creating a wordcloud, consider the following code :
(Here, we will represent Positive words with green color, Negative words with red color and Neutral words
with white color)
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
# Create dictionaries to store positive and negative words with polarity.
positive_words=dict()
negative_words=dict()
# Sentiment Analysis
sentiment_analyzer=SentimentIntensityAnalyzer()
for i in words:
if not i.lower() in stop_words_data: # It will remove stopwords.
polarity=sentiment_analyzer.polarity_scores(i)
if polarity['compound']>=0.05: # Positive Sentiment
positive_words[i]=polarity['compound']
if polarity['compound']<=-0.05: # Negative Sentiment
negative_words[i]=polarity['compound']
# Append the positive and negative words from dictionaries to lists i.e.
positive[] and negative[]
for key,value in positive_words.items():
positive.append(key)
for key,value in negative_words.items():
negative.append(key)
# Create a dictionary to mention the colors : green for positive and red for
negative
coloured_words={"green":positive,"red":negative}
def get_colour(self,word):
try:
colour=next(
colour for (colour,words) in self.coloured_words
if word in words)
except StopIteration:
colour=self.default
return colour
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
# To print the plot
word_cloud=WordCloud(collocations=False,background_color='black').generate(movie
s_reviews1)
# Neutral words will be visible as black
group_color=ColourAssignment(coloured_words, 'white')
word_cloud.recolor(color_func=group_color)
plt.figure()
plt.imshow(word_cloud, interpolation="bilinear")
plt.axis("off")
plt.show()
Output :
When it comes to social media such as Twitter, Facebook, YouTube etc., bulk of data is available which is
needed to be examined or analyzed for interpreting the opinions of people conveyed in different formats. It
basically puts the subjective information in the form emotions.
To get the tweets through twitter API, Twitter account is needed and App is to be registered. Follow the below
steps :
First create a Twitter account if you do not have one. Visit to https://twitter.com/i/flow/signup and create
an account. Existing account can also be used.
Now create an App on Twitter Developer using following link :
https://developer.twitter.com/en/apps
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
Now click on “Create an App” button to create an application to get the API key for
credentials. It will ask to apply for a Developer Account.
Click on Apply and continue. And then answer the questions visible on the screen.
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
After submitting the request, you will receive a message from twitter to get the Email
confirmation. Then we can get the keys. Visit the following link for App creation.
https://developer.twitter.com/en/portal/register/welcome
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
Libraries used for twitter data analysis :
1. tweepy : It is a Python library which is used to access the Twitter API. To install
tweepy, use the following command :
pip install tweepy
Or you can also use the “Bearer Token” to perform authentication. For this code, Bearer
Token is used. If you want to use another approach, refer this :
https://docs.tweepy.org/en/stable/authentication.html. You can find Bearer Token here :
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
Before using Bearer Token, make sure to use “Elevated” section of App. When first time
app gets created, it comes with “Essential”. But to use Bearer Token directly, “Elevated” is
to be used.
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
In case if the token gets expired, then it can be regenerated as well. Add the following lines
of code :
auth=tweepy.OAuth2BearerHandler("Your Bearer Token")
api=tweepy.API(auth)
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
# Get the tweets on the basis of Hash Tags or Keywords.
search_tag=input("Enter the Hash Tag or Keyword for which you want to get the
tweets : ")
no_of_tweets=int(input("How many tweets you want ? "))
# Iterate over the tweets.
tweets=tweepy.Cursor(api.search_tweets, q=search_tag).items(no_of_tweets)
# Create a list to store all the tweets.
tweet_list=[]
for tweet in tweets:
tweet_list.append(tweet.text)
print(tweet_list)
Output : (Example)
Enter the Hash Tag or Keyword for which you want to get the tweets : #sadhguru
More Analysis on Twitter Data : We can further perform different analysis on gathered
data as follows :
First select the user ID on which analysis is to be done.
Then we can find various information related to tweets such as 'created_at', 'id',
'id_str', 'text', 'truncated', 'entities', 'metadata', 'source', 'in_reply_to_status_id',
'in_reply_to_status_id_str', 'in_reply_to_user_id', 'in_reply_to_user_id_str',
'in_reply_to_screen_name', 'user', 'geo', 'coordinates', 'place', 'contributors',
'retweeted_status', 'is_quote_status', 'retweet_count', 'favorite_count', 'favorited',
'retweeted', 'lang', 'possibly_sensitive'.
Example :
# Select a specific user by using a twitter user ID.
user_id=input("Enter a Twitter user ID : ")
no_of_tweets=int(input("How many tweets you want ? "))
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
for tweet_info in tweets:
print("Tweet ID : ",tweet_info.id) # Tweet ID
print("Created at : ",tweet_info.created_at) # Date on which tweet is created.
print("Tweet : ",tweet_info.full_text) # Tweet
print("Retweet count : ",tweet_info.retweet_count) # Number of retweets on each
tweet.
print("\n")
Output : (Example)
Tweet ID : 1502113329103487012
Created at : 2022-03-11 02:45:00+00:00
Tweet : Kriya Yoga requires nothing but dedication towards the practice. As you
refine your energies, there is no way you can remain untransformed.
#SadhguruQuotes https://t.co/byjrSIld2u
Retweet count : 1696
Tweet ID : 1501771371562471426
Created at : 2022-03-10 04:06:11+00:00
Tweet : Congratulations @CISFHQrs for your courageous & committed
contribution to Nation Building for more than five decades. Bharat is proud
& grateful for your stellar service. May you continue to inspire Peace &
Prosperity. Best Wishes. –Sg #CISFRaisingDay2022
Retweet count : 1595
Tweet ID : 1501750941187551232
Created at : 2022-03-10 02:45:00+00:00
Tweet : You cannot change the past. You can only experience the present moment.
The future must be crafted the way you want. #SadhguruQuotes
https://t.co/eTCAmU3gOl
Retweet count : 2510
Tweet ID : 1501624364889825281
Created at : 2022-03-09 18:22:02+00:00
Tweet : Machel, #VelliangiriMountains are a Cascade of Grace. Their Power has
empowered millions & will continue to empower future populations. Wonderful
your #Sadhanapada culminated here; it was beautiful to have you & Renee.
Journey on- sing, dance, also transform lives. Blessings. –Sg
https://t.co/y2qV6EBM2k
Retweet count : 1483
Visualizing Twitter Data : We can visualize the twitter data in multiple ways on the
basis of attributes returned by Twitter API.
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
Example : To visualize the number of re-tweets on each tweet :
First create a DataFrame so that it will become easy to get the attributes of Twitter API.
Then create a plot (e.g. Pie Plot) to get the number of re-tweets on each tweet.
import tweepy
import pandas as pd
import matplotlib.pyplot as plt
twitter_data={'id':tweet_id,'created_at':tweet_created_at,'full_text':tweet_full_text,'r
etweet_count':tweet_retweet_count,'favorite_count':tweet_favorite_count}
# DataFrame
twitter_dataframe=pd.DataFrame(twitter_data)
# Plotting Pie Graph for retweets on each tweet.
twitter_dataframe['retweet_count'].plot.pie()
plt.show()
Output :
Enter a Twitter user ID : Tesla
How many tweets you want ? 10
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
Now to plot the likes and re-tweets received on each tweet, add the following script :
Consider the following script to plot the Time Series for likes and re-tweets along with
dates on which the tweets were published.
# Time Series
time_likes=pd.Series(data=twitter_dataframe['favorite_count'].values,index=twitter_dataf
rame['created_at'])
time_likes.plot(figsize=(16,4),label="likes",legend=True,color="magenta")
time_retweets=pd.Series(data=twitter_dataframe['retweet_count'].values,index=twitter_dat
aframe['created_at'])
time_retweets.plot(figsize=(16,4),label="retweets",legend=True,color="blue")
plt.show()
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
You can also get information regarding tweets as total number of likes and re-tweets on
each tweet, which tweet has maximum count of likes and got maximum re-tweets.
Output : (Example)
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
Enter a Twitter user ID : SadhguruJV
How many tweets you want ? 5
Total number of tweets : 5
Total number of likes on each tweet : 16863
Total number of retweets on each tweet : 6180
Number of likes for most liked tweet : 5964
Number of retweets for the most retweeted tweet : 2207
Tweet ID : 1502475716935442442
Created at : 2022-03-12 02:45:00+00:00
Tweet : Only if you invest your emotions in what matters to you, will life become
powerful and really meaningful. #SadhguruQuotes https://t.co/EWJ2Aneqps
Retweet count : 447
Favorite count : 1247
Tweet ID : 1502375448885432326
Created at : 2022-03-11 20:06:34+00:00
Tweet : #SaveSoil #MoU #CARICOM
@GastonBrowne @AntiguaOpm @SkerritR @PhilipJPierreLC @pmharriskn @antiguagov
@SaintLuciaGov @skngov @molwynjoseph @SamMarshallMP @machelmontano @armandarton
@GlobalCitizenFo @cpsavesoil @PMOIndia https://t.co/RMXpcgW12d
Retweet count : 461
Favorite count : 1026
Tweet ID : 1502375423451164672
Created at : 2022-03-11 20:06:28+00:00
Tweet : A historic moment marked by the first #SaveSoil MoUs signed by the pearls of
the ocean. Governments of Antigua & Barbuda, Dominica, St Lucia, and St Kitts &
Nevis — may your commitment to soil revitalization be an inspiration to the rest of the
world. -Sg @CARICOMorg #CARICOM https://t.co/0glWuMlFBy
Retweet count : 1074
Favorite count : 2806
Tweet ID : 1502151438419464196
Created at : 2022-03-11 05:16:26+00:00
Tweet : Sir Vivian Richards & Lord Ian Botham - a joy to meet you during my Antigua
visit for the #SaveSoil movement. Your achievements in cricket & beyond are
commendable. Please join me in restoring our world’s Soil, the basis of all Life on
Earth. -Sg @ivivianrichards @BeefyBotham https://t.co/M53Ckhu0Lg
Retweet count : 2207
Favorite count : 5964
Tweet ID : 1502113329103487012
Created at : 2022-03-11 02:45:00+00:00
Tweet : Kriya Yoga requires nothing but dedication towards the practice. As you refine
your energies, there is no way you can remain untransformed. #SadhguruQuotes
https://t.co/byjrSIld2u
Retweet count : 1991
Favorite count : 5820
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
Sentiment Analysis on Twitter Data : We can also perform sentiment analysis on gathered twitter data.
Here two libraries will be needed, i.e. TextBlob and Vader.
1. textblob : It is a Python library which is used for processing textual data. It is built on top of NLTK
module and offers a simple API to access its methods to perform basic Natural Language Processing
tasks. To install textblob, use the following command :
pip install textblob
2. VADER description has already been given in previous topic for Sentiment Analysis using NLTK.
positive_tweets=0
negative_tweets=0
neutral_tweets=0
polarity_of_tweets=0
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
compound_score=polarity_score['compound']
polarity_of_tweets+=analysis.sentiment.polarity
if negative_score>positive_score:
negative_tweets_list.append(tweet.text)
negative_tweets+=1
elif positive_score>negative_score:
positive_tweets_list.append(tweet.text)
positive_tweets+=1
elif positive_score==negative_score:
neutral_tweets_list.append(tweet.text)
neutral_tweets+=1
positive_tweets=sentiment_percentage(positive_tweets,no_of_tweets)
negative_tweets=sentiment_percentage(negative_tweets,no_of_tweets)
neutral_tweets=sentiment_percentage(neutral_tweets,no_of_tweets)
polarity_of_tweets=sentiment_percentage(polarity_of_tweets,no_of_tweets)
positive_tweets=format(positive_tweets,'.1f')
negative_tweets=format(negative_tweets,'.1f')
neutral_tweets=format(neutral_tweets,'.1f')
Output :
Enter the Hash Tag or Keyword for which you want to get the tweets : amazonIN
Positive Tweets :
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
["Back to my weekend's favourite activity. Some insights\n\n56 days,\n12
emails\n>30 calls.\n\nAnd the amazing collaboratio… https://t.co/38JoPHM0aw",
'@amazonIN i am unable to rest my Amazon password and i am trying to call
180030001593 on this number but every tim… https://t.co/QNEeqtA1cm', 'RT
@amazonIN: Soundbar Days is back with exciting offers & great discount from
popular brands! Get up to 55% off on bestselling soundbars,…', '@amazonIN
@amazon @AmitAgarwal \nAmazon app is not performing well like add to cart, save
later , move to cart obse… https://t.co/DPnt2Zg2pL']
Negative Tweeets :
Neutral Tweets :
Downloading the twitter datasets online : The online available datasets containing
Twitter data can be downloaded and different analytics can be performed on it.
Example : https://www.kaggle.com/crowdflower/twitter-user-gender-classification
Self-Activity : Download and analyze the data using above link and apply different
analytics techniques on it.
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
Getting data using Facebook Access Token
Now create an app to get the token to be used for further processing.
Click on Create App and then select the type of app to be created. Multiple options will
be available i.e. Business, Consumer, Instant Games, Gaming, Workplace, None. You can
read the details and select an option. If Business option available in list is selected, then it
creates an app which manages business assets like Pages, Events, Groups, Ads,
Messenger and Instagram Graph API using the available business permissions, features
and products.
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
After an app gets created, you can get the Access Token as follows :
Go to : https://developers.facebook.com/tools/explorer/
Now click on Generate Access Token. Proceed to the next step by clicking on
Continue.
The Access Token will be visible in Access Token input box.
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
Now allow the necessary permissions to access the Facebook pages as well.
Click on “Add a Permission” dropdown and select the permissions from it.
From “User or Pages” dropdown select “Get User Token” again to get the Token with
revised permissions.
Now if we want to see the details of publically available Facebook users or pages,
change the request in the request url box.
Example : If we want to get the details of Facebook Page “Sanganak Academy” then
change the name of page as : SanganakAcademy?fields=id,name. Before that,
make sure to allow the permissions for accessing that page as well.
You can see the posts on this page as well by changing the url as :
SanganakAcademy?fields=id,name,posts. Same you can do to access other
information as well.
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
The Facebook data can be accessed using Python script as follows :
Import the necessary libraries :
import requests
import time
import pickle
import random
Provide Access Token and get the URL to access the data :
# Access Token
access_token="Your Access Token"
# In Graph URL, provide the correct version of Graph API. Here currently I am using
v13.0
graphURL="https://graph.facebook.com/v13.0/"
# Request URL to get the relevant data of Facebook Page Sanganak Academy.
# You can access any other page by using its ID as well.
requestURL="SanganakAcademy?fields=id,name,posts{message,created_time,comments.limit(
0).summary(true), likes.limit(0).summary(true)}"
actual_url=graphURL+requestURL
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
Create a DataFrame using Pandas library.
# Create a dataframe using pandas library
import pandas as pd
fb_dataframe=pd.DataFrame(received_data)
fb_dataframe=pd.json_normalize(received_data)
print(fb_dataframe)
# Columns
print("Columns in Dataframe : ")
for col in fb_dataframe.columns:
print(col)
Here columns in the dataframe are :
message
created_time
id
comments.data
comments.summary.order
comments.summary.total_count
comments.summary.can_comment
likes.data
likes.summary.total_count
likes.summary.can_like
likes.summary.has_liked
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
time_likes=pd.Series(data=fbdata_with_dates['likes.summary.total_count'].values,index
=fbdata_with_dates['created_time'])
time_likes.plot(figsize=(16,4),label="likes",legend=True)
plt.show()
And then click on “Generate Access Token”. Now copy the generated access token
and use it for further analysis.
Creating a Facebook post : To post something on Facebook Page wall, use put_object()
method of Facebook Graph API.
import facebook
access_token="Your Page Access Token"
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
fb=facebook.GraphAPI(access_token)
fb.put_object(parent_object='me', connection_name='feed', message='Hello all...Welcome
to Sanganak Academy')
Here, 511268930590718 is the Facebook Post ID. To get “Facebook Page ID”, Open the
Facebook Page and in About section, you will find the Facebook Page ID.
Now combine Facebook Page ID and Facebook Post ID as pageid_postid. For example : If
Page ID = 12345 and Post ID = 511268930590718, then combine it as
12345_511268930590718.
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
Liking a post :
Syntax : graph_api_object.put_like(object_id = ‘post_id’)
Example :
# Liking a facebook post.
fb.put_like(object_id='12345_511268930590718')
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
(Refer this https://facebook-sdk.readthedocs.io/en/latest/api.html for more information
about Facebook SDK.)
sns.set_style('darkgrid')
matplotlib.rcParams['font.size'] = 14
matplotlib.rcParams['figure.figsize'] = (12, 5)
matplotlib.rcParams['figure.facecolor'] = '#00000000'
fig = plt.figure()
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
axis2 = fig.add_axes([0.8, 0, 0.75, 0.75], aspect=1)
# Pie chart for reactors
pie_vars = ['Likers','Dislikers','Commenters']
pie_values =
[youtube_data['likes'].sum(),youtube_data['dislikes'].sum(),youtube_data['comment_count'
].sum()]
axis2.pie(pie_values,labels=pie_vars,autopct='%1.2f%%')
axis2.set_title("Types of reactors")
plt.show()
i=0
for i in range(youtube_data.shape[0]):
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
date_time_obj = datetime.datetime.strptime(youtube_data['publish_time'].at[i],'%Y-
%m-%dT%H:%M:%S.000Z')
youtube_data['publish_time'].at[i] = date_time_obj
i = i+1
date=[]
year=[]
month=[]
day=[]
for i in range(youtube_data.shape[0]):
d = youtube_data['publish_time'][i].date()
y = youtube_data['publish_time'][i].date().year
m = youtube_data['publish_time'][i].date().month
days = youtube_data['publish_time'][i].date().day
date.append(d) # Storing dates
year.append(y) # Storing years
month.append(m) # Storing months
day.append(d) # Storing days
i = i+1
youtube_data.drop(['publish_time'], inplace=True,axis=1)
youtube_data['publish_time']=date
youtube_data['year']=year
youtube_data['month'] = month
youtube_data['day'] = day
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
# Year wise statistics of dislikes.
plt.scatter(youtube_data['year'], youtube_data['dislikes'], c="red")
plt.xlabel("Year")
plt.ylabel("Dislikes")
plt.show()
Lab Assignments
SET A
1. Consider any text paragraph. Preprocess the text to remove any special characters and digits. Generate
the summary using extractive summarization process.
2. Consider any text paragraph. Remove the stopwords. Tokenize the paragraph to extract words and
sentences. Calculate the word frequency distribution and plot the frequencies. Plot the wordcloud of
the text.
3. Consider the following review messages. Perform sentiment analysis on the messages.
i. I purchased headphones online. I am very happy with the product.
ii. I saw the movie yesterday. The animation was really good but the script was ok.
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
iii. I enjoy listening to music
iv. I take a walk in the park everyday
4. Perform text analytics on WhatsApp data :
Write a Python script for the following :
i. First Export the WhatsApp chat of any group. Read the exported “.txt” file using open() and read()
functions.
ii. Tokenize the read data into sentences and print it.
iii. Remove the stopwords from data and perform lemmatization.
iv. Plot the wordcloud for the given data.
Set B
1. Consider the following dataset :
https://www.kaggle.com/datasets/prasertk/top-1000-instagram-influencers
Write a Python script for the following :
i. Read the dataset and find the top 5 Instagram influencers from India.
ii. Find the Instagram account having least number of followers.
iii. Read the column “Category”, remove stopwords and plot the wordcloud to find the keywords which
will imply that in which category maximum accounts are created.
iv. Group the Instagram accounts category wise.
v. Visualize the dataset and plot the relationship between Followers and Authentic engagement columns.
Set C
Q.2 Write a Python script to read the Tweets using Twitter API and tweepy library to perform the
following tasks :
i. Authenticate Twitter API (Using Bearer Token)
ii. Get the tweets using Keywords or Hash Tags.
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde
iii. Find the total number of likes and retweets on each tweet.
iv. Find the most liked tweet and print its text.
v. Visualize the tweets and plot the time series for likes and retweets along with dates on which tweets
are published.
1. Import and format the data into a DataFrame using pandas library. Example : For working with Facebook
Posts, read the JSON file available in “Posts” folder as :
import pandas as pd
facebook_dataframe=pd.read_json("your_posts.json")
Similarly you can work with other downloaded data and read the JSON files available in them.
2. Now perform data cleaning operation on created dataframe and remove unnecessary columns.
3. Perform multiple statistical analysis such as finding the posts by date, number of likes on a post, comments
on a post.
4. Perform sentiment analysis to find the polarity scores and classify the posts text in three categories i.e.
positive, negative and neutral posts.
Assignment Evaluation
DATA ANALYTICS ASSIGNMENT 3 | Prepared by: Dr. Harsha Patil, Prof. Kamakshi Goyal, Dr. Poonam Ponde