yuvi1323 / CSCE_5340_Group8_Open_Source_Project

0 stars 0 forks source link

Data visualization for the most common words #3

Closed yuvi1323 closed 3 weeks ago

yuvi1323 commented 1 month ago

Estimation Time: Medium (3 days) Acceptance Criteria:

Girishkolapalli commented 3 weeks ago

Iterated on issues realted to this and kept a limit to stopword setter to 10 keywords

`import requests import csv import matplotlib.pyplot as plt from wordcloud import WordCloud

Ask for user input

query = input("Enter a keyword to search for: ") num_articles = int(input("Enter the number of articles to retrieve: ")) filename = input("Enter a filename to save the CSV data: ")

Replace YOUR_API_KEY with your actual NewsAPI key

url = f"https://newsapi.org/v2/everything?q={query}&pageSize=100&page=1&apiKey=8f89582e7ffc45f1b73faeca278f87e5"

articles = []

Retrieve articles

num_pages = (num_articles - 1) // 100 + 1 # Calculate number of pages needed for i in range(num_pages): response = requests.get(url.format(i + 1)) if response.status_code == 200: data = response.json() articles += data["articles"] else: print("Error:", response.status_code) break

Save data to a CSV file

with open(filename + ".csv", "w", newline="", encoding="utf-8") as csvfile: fieldnames = ["title", "description"] writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() for article in articles: writer.writerow({"title": article["title"], "description": article["description"]})

Ask user if they want to see the word cloud visualization

show_wordcloud = input("Do you want to see the word cloud visualization? (yes/no): ").lower() == "yes"

If yes, generate and display the word cloud

if show_wordcloud:

Read data from CSV file

with open(filename + ".csv", encoding='utf-8') as csvfile:
    reader = csv.DictReader(csvfile)
    text = ""
    for row in reader:
        text += row['description'] + " "

# Generate word cloud
wordcloud = WordCloud(width=800, height=800, background_color='white').generate(text)

# Display the generated wordcloud image
plt.figure(figsize=(8,8), facecolor=None)
plt.imshow(wordcloud)
plt.axis("off")
plt.tight_layout(pad=0)
plt.show()

`

yuvi1323 commented 3 weeks ago

Hi @Girishkolapalli reviewed your code all issues are fine