<jupyter_start><jupyter_text><jupyter_code>from bs4 import BeautifulSoup as soup
from urllib.request import urlopen as uReq
import csv
rotten_urls = ['https://www.rottentomatoes.com/tv/peaky_blinders/s05/reviews?type=&sort=']*2
page = 1
while (page<=2):
  url_pos = page-1
  rotten_urls[url_pos] = rotten_urls[url_pos] + '&page='+ str(page)
  page = page+1
rotten_urls
reviews = []
for url in rotten_urls:
  uClient = uReq(url)
  page_html = uClient.read()
  uClient.close()
  page_soup = soup(page_html, 'html.parser')
  reviews = reviews + [review.text for review in page_soup.find_all("div",{"class":"critic__review-quote"})]
len(reviews)
reviews
#write the csv file with reviews
with open ('rotten_reviews.csv', 'w') as myfile:
  wr = csv.writer(myfile , quoting = csv.QUOTE_ALL)
  wr.writerow(reviews)<jupyter_output><empty_output><jupyter_text>**CHECKPOINT: Run the code above only one time, after that only run after this point**<jupyter_code>import csv
import nltk
nltk.download('stopwords')
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')

from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize

from wordcloud import WordCloud, ImageColorGenerator
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
#read the csv file with reviews for check
with open ('rotten_reviews.csv', 'r') as f:
  reader = csv.reader(f)
  reviews = list(reader)[0]
len(reviews)
stop_words = set(stopwords.words('english'))
word_tokens=[]
for review in reviews:
  word_tokens = word_tokens + word_tokenize(review)

filtered_word_reviews = [w for w in word_tokens if not w in stop_words]
print(word_tokens[:10])
print(filtered_word_reviews[:10])
len(filtered_word_reviews)
def drawWordCloud(words, filename):
  text = ''
  for w in words:
    text = text + ' ' + w
  
  mask = np.array(Image.open("peaky.jpeg"))
  image_colors = ImageColorGenerator(mask)

  #take relative word frequencies into account, lower max_font_size
  wordcloud = WordCloud(background_color="white", max_words = 1000,mask = mask).generate(text)

  fig, axes = plt.subplots(1, 2, figsize=( 16,20))
  axes[0].imshow(mask, cmap=plt.cm.gray, interpolation = "bilinear")
  axes[1].imshow(wordcloud.recolor(color_func=image_colors, random_state=3),
                 interpolation ='bilinear')
  
  for ax in axes:
    ax.set_axis_off()
    plt.savefig(filename+ ".png", format="png", dpi=200)
    plt.show()
drawWordCloud(filtered_word_reviews, 'wc-peaky')
adjs = [word for (word, pos) in nltk.pos_tag(filtered_word_reviews) if(pos[:2] == 'JJ')]
print (adjs[:10])
drawWordCloud(adjs, 'wc-peaky-adjs')
import googletrans
from googletrans import Translator
translator = Translator()
translations = translator.translate(adjs[:100], dest='pt')
adjs_pt = []
for translation in translations:
    adjs_pt.append(translation.text)
#ajds_pt
drawWordCloud(adjs_pt, "wc-joker-adjs-pt")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12598695502917953, 'fraction_numerical': 0.012015104703055269, 'mean_word_length': 5.74537037037037, 'pattern_counts': {'":': 1, '<': 10, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '556182', 'license': 'MIT', 'n_tokens_mistral': 1088, 'n_tokens_neox': 1043, 'n_words': 243, 'path': 'Sentiment_Analysis_Peaky_Blinders_S05_review.ipynb', 'repo_name': 'cassiolm/Data_Analysis', 'size': '2913'}