NOT being able to do it

NOT being able to do it

de Castro Ângela -
Кількість відповідей: 5

Since I'm having problems with R. I'm leaving the python code:# Import necessary libraries

import pandas as pd
import re
import nltk
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from wordcloud import WordCloud
import matplotlib.pyplot as plt
# Download NLTK data
nltk.download('stopwords')
nltk.download('wordnet')
# Sample data related to clean energy
data = {
    'id': [1, 2, 3, 4, 5],
    'contents': [
        "Solar energy is one of the most promising renewable energy sources available today.",
        "Wind power has the potential to provide a significant portion of the world's electricity needs.",
        "Hydropower is a well-established clean energy source that harnesses the power of moving water.",
        "Geothermal energy offers a sustainable and reliable energy source by tapping into the heat of the Earth's core.",
        "Bioenergy involves the use of organic materials, such as plants and waste, to generate electricity and heat."
    ]
}
# Create DataFrame
df = pd.DataFrame(data)
# Data Cleaning
def clean_text(text):
    text = re.sub(r'<[^>]+>', '', text)  # Remove HTML tags
    text = re.sub(r'\s+', ' ', text)  # Remove extra spaces
    text = re.sub(r'[^\w\s]', '', text)  # Remove punctuation
    text = text.lower()  # Convert to lowercase
    return text
df['cleaned'] = df['contents'].apply(clean_text)
# Text Preprocessing
stop_words = set(stopwords.words('english'))
lemmatizer = WordNetLemmatizer()
def preprocess(text):
    tokens = text.split()
    tokens = [lemmatizer.lemmatize(word) for word in tokens if word not in stop_words]
    return ' '.join(tokens)
df['processed'] = df['cleaned'].apply(preprocess)
# Combine all processed text into one string for word cloud generation
text = ' '.join(df['processed'])
# Generate Word Cloud
wordcloud = WordCloud(width=800, height=400, background_color ='white').generate(text)
# Plot the Word Cloud
plt.figure(figsize=(10, 5))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.title('Word Cloud of Clean Energy Texts')
plt.show()


У відповідь на de Castro Ângela

Re: NOT being able to do it

Sá Guimarães Anna -
Hello, Ângela!
I also couldn´t do it with R console, but ended up to complete the task in VS code with Python. Requires installation of Python, some extensions for VS code and some packages. Was my first contact with Python, but at least it worked.
Good luck, I hope you can do it
Best regards
Anna
У відповідь на Sá Guimarães Anna

Re: NOT being able to do it

Pascoa Antonio -
Hi all. Me either. R is not finding the packages promptly and require more time. I did try python now as i use to use under Linux bash without GUI, But require much more time i think to adjust all details (installation issues) or old pc.
У відповідь на Pascoa Antonio

Re: NOT being able to do it

Manuel José -
Good morning Angela de Castro
I tested your code with PyCharm and got the following:

 
I had to install the nltk and wordcloud libraries
You can download it from the link: https://www.jetbrains.com/pycharm/
With this code, just change the text in 'contents': and it's ready to work. For example, I changed the text to IOT and got:

 Best Regards,
José Manuel