OneCompiler

DS18

192

Q. 1) Write a Java Script Program to print Fibonacci numbers on onclick event. [Marks 15]

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Fibonacci Numbers</title> <script> function generateFibonacci() { var num1 = 0; var num2 = 1; var fibonacciSeries = num1 + ", " + num2;
var count = parseInt(prompt("Enter the number of Fibonacci numbers to generate:"));
if (count < 2) {
    alert("Please enter a number greater than 1.");
    return;
}

for (var i = 2; i < count; i++) {
    var nextNum = num1 + num2;
    fibonacciSeries += ", " + nextNum;
    num1 = num2;
    num2 = nextNum;
}

alert("Fibonacci Series: " + fibonacciSeries);

}
</script>

</head> <body>

<button onclick="generateFibonacci()">Generate Fibonacci Numbers</button>

</body> </html>

Q. 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.
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.probability import FreqDist
import matplotlib.pyplot as plt
from wordcloud import WordCloud

Sample text paragraph

text = """
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.
"""

Remove stopwords

stop_words = set(stopwords.words("english"))
words = word_tokenize(text.lower())
filtered_words = [word for word in words if word.isalpha() and word not in stop_words]

Tokenize the paragraph to extract words and sentences

tokens = word_tokenize(text)
sentences = text.split(".")

Calculate word frequency distribution

freq_dist = FreqDist(filtered_words)

Plot the frequencies

plt.figure(figsize=(10, 5))
freq_dist.plot(20, cumulative=False)
plt.title("Word Frequency Distribution")
plt.xlabel("Word")
plt.ylabel("Frequency")
plt.show()

Plot the wordcloud

wordcloud = WordCloud(width=800, height=400, background_color="white").generate(text)
plt.figure(figsize=(10, 5))
plt.imshow(wordcloud, interpolation='bilinear')
plt.title("Word Cloud")
plt.axis("off")
plt.show()