DS25


Q. 1) Write a script to create “cricket.xml” file with multiple elements as shown below:
<CricketTeam>
<Team country=”Australia”>
<player>__</player>
<runs>
</runs>
<wicket>
</wicket>
</Team>
</CricketTeam>
Write a script to add multiple elements in “cricket.xml” file of category, country=”India”. [Marks 15]

<?php // Function to create initial cricket.xml file with Australia team function create_cricket_xml() { $doc = new DOMDocument(); $doc->formatOutput = true; $cricketTeam = $doc->createElement('CricketTeam'); $doc->appendChild($cricketTeam); $team = $doc->createElement('Team'); $team->setAttribute('country', 'Australia'); $cricketTeam->appendChild($team); $player = $doc->createElement('player', 'Player Name'); $team->appendChild($player); $runs = $doc->createElement('runs', '0'); $team->appendChild($runs); $wicket = $doc->createElement('wicket', '0'); $team->appendChild($wicket); $doc->save('cricket.xml'); echo "cricket.xml file created with Australia team.<br><br>"; } // Function to add multiple elements in cricket.xml file of category country="India" function add_elements_to_cricket_xml() { $doc = new DOMDocument(); $doc->load('cricket.xml'); $cricketTeam = $doc->documentElement; $indiaTeam = $doc->createElement('Team'); $indiaTeam->setAttribute('country', 'India'); // Add multiple elements for India team for ($i = 1; $i <= 3; $i++) { $player = $doc->createElement('player', "Player $i"); $indiaTeam->appendChild($player); $runs = $doc->createElement('runs', rand(10, 100)); // Random runs $indiaTeam->appendChild($runs); $wicket = $doc->createElement('wicket', rand(0, 5)); // Random wickets $indiaTeam->appendChild($wicket); } $cricketTeam->appendChild($indiaTeam); $doc->save('cricket.xml'); echo "Elements added to cricket.xml file for India team.<br><br>"; } // Create cricket.xml file with Australia team if it doesn't exist if (!file_exists('cricket.xml')) { create_cricket_xml(); } // Add elements for India team add_elements_to_cricket_xml(); ?>

Q. 2) Consider the following dataset :
Write a Python script for the following :
i. Read the dataset and perform data cleaning operations on it.
ii. ii. Tokenize the comments in words. iii. Perform sentiment analysis and find the percentage of
positive, negative and neutral comments..
import pandas as pd
from textblob import TextBlob

Read the dataset

df = pd.read_csv('covid_2021_1.csv')

Data cleaning operations (if needed)

Tokenize the comments in words

def tokenize_comments(comment):
return TextBlob(comment).words

df['tokenized_comments'] = df['comment'].apply(tokenize_comments)

Perform sentiment analysis

def get_sentiment(comment):
analysis = TextBlob(comment)
if analysis.sentiment.polarity > 0:
return 'Positive'
elif analysis.sentiment.polarity == 0:
return 'Neutral'
else:
return 'Negative'

df['sentiment'] = df['comment'].apply(get_sentiment)

Calculate percentage of positive, negative, and neutral comments

sentiment_counts = df['sentiment'].value_counts(normalize=True) * 100

print("Percentage of Comments by Sentiment:")
print(sentiment_counts)