# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import matplotlib.pyplot as plt
import numpy as np
import skimage.io
import skimage.morphology
from collections import Counter
import re

import nltk
# nltk.download("stopwords") # we need this only the first time we download the "stopwords"
from nltk.corpus import stopwords
# -

# # Image Size
# Let's see what is the size (in bytes) of the hamburger image

# +
hamburger_image = skimage.io.imread("data/hamburger.jpg")
print(hamburger_image.nbytes)

# Another way to calculate the size (in bytes) is to mutiply all the numbers from the shape
# Each pixel is one byte for the JPG format so we can easily calculate the number of bytes
# We can see more info here: https://www.scantips.com/basics1d.html
rows, cols, dimensions = hamburger_image.shape
print(rows * cols * dimensions)
# -

# Let's see the for all channels the mean brigthnesses. 
#
# After that we can see the dominant one.

hamburger_image_red, hamburger_image_green, hamburger_image_blue = [hamburger_image[:, :, i] for i in range(3)]
print(hamburger_image_red.mean())
print(hamburger_image_green.mean())
print(hamburger_image_blue.mean())

# # Morphology
# Let's apply a binary opening to our image.
#
# After that we need to get the count for all the white pixels.
# We use function to calculate this count

# structuring_element = np.ones((3, 3)) # first variant
structuring_element = skimage.morphology.square(3)
binary_image = skimage.morphology.binary_opening(hamburger_image_blue, structuring_element)
binary_image = binary_image.astype(int)
plt.imshow(binary_image)
plt.show()


def count_white_pixels(binary_image):
    rows, columns = binary_image.shape
    count = 0
    for row in range(0, rows):
        for value in binary_image[row]:
            if value == 1:
                count += 1;
    return count;


white_pixel_count = count_white_pixels(binary_image)
print("White pixels count:", white_pixel_count)

# # Pride and Prejudice book
#
# Let's read the book "Pride and Prejudice"

text = ""
with open("data/Pride and Prejudice.txt", "r", encoding = "utf-8") as f:
    text = f.read()
print(len(text))

# Next, we make all the text lower and make a word counter.
#
# After that we can simply answer the questions
# * How many times does the word "pride" occur in the entire Web page?
# * How many times does the word "prejudice" occur in the entire Web page?

text = text.lower()
words = re.split("\W+", text)
word_counter = Counter(words)
print("pride count:", word_counter["pride"])
print("prejudice count:", word_counter["prejudice"])

# Now it's time to remove the stopwords and to find out what's the main character's name.

stop = stopwords.words("english")
# We get all the words that aren't "stopwords"
cleaned_words = [word for word in words if word not in stop]
cleaned_words_counter = Counter(cleaned_words)
cleaned_words_counter.most_common(20)

StarcoderdataJupyterScriptsDedupFiltered