From 1954f3f403629a41c1fa670987a81e7d3f502fa2 Mon Sep 17 00:00:00 2001 From: hiler-hutter Date: Mon, 28 Mar 2022 10:58:08 +0200 Subject: [PATCH] string operations done --- .../challenge-1-checkpoint.ipynb | 412 ++++++++++++++++++ .../challenge-2-checkpoint.ipynb | 100 ++++- your-code/challenge-1.ipynb | 188 ++++++-- your-code/challenge-2.ipynb | 100 ++++- 4 files changed, 723 insertions(+), 77 deletions(-) create mode 100644 your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb diff --git a/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb b/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb new file mode 100644 index 0000000..51bad83 --- /dev/null +++ b/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb @@ -0,0 +1,412 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# String Operations Lab\n", + "\n", + "**Before your start:**\n", + "\n", + "- Read the README.md file\n", + "- Comment as much as you can and use the resources in the README.md file\n", + "- Happy learning!" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import re" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge 1 - Combining Strings\n", + "\n", + "Combining strings is an important skill to acquire. There are multiple ways of combining strings in Python, as well as combining strings with variables. We will explore this in the first challenge. In the cell below, combine the strings in the list and add spaces between the strings (do not add a space after the last string). Insert a period after the last string." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Durante un tiempo no estuvo segura de si su marido era su marido.'" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "str_list = ['Durante', 'un', 'tiempo', 'no', 'estuvo', 'segura', 'de', 'si', 'su', 'marido', 'era', 'su', 'marido']\n", + "# Your code here:\n", + "\" \".join(str_list) + \".\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the cell below, use the list of strings to create a grocery list. Start the list with the string `Grocery list: ` and include a comma and a space between each item except for the last one. Include a period at the end. Only include foods in the list that start with the letter 'b' and ensure all foods are lower case." + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['bananas', 'bread', 'brownie', 'broccoli']\n" + ] + } + ], + "source": [ + "food_list = ['Bananas', 'Chocolate', 'bread', 'diapers', 'Ice Cream', 'Brownie Mix', 'broccoli']\n", + "# Your code here:\n", + "grocery_list = [i.lower() for i in food_list] #creating the grocery list and making all foods in lower case\n", + "\n", + "grocery_list = \" \".join(grocery_list) + \".\" #combininig all the strings\n", + "\n", + "grocery_list = re.findall(r\"b.+?\\b\",grocery_list) #including foods in the list that start with the letter 'b'\n", + "\n", + "print(grocery_list)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the cell below, write a function that computes the area of a circle using its radius. Compute the area of the circle and insert the radius and the area between the two strings. Make sure to include spaces between the variable and the strings. \n", + "\n", + "Note: You can use the techniques we have learned so far or use f-strings. F-strings allow us to embed code inside strings. You can read more about f-strings [here](https://www.python.org/dev/peps/pep-0498/)." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The area of the circle with radius: 4.5 is: 63.62\n" + ] + } + ], + "source": [ + "import math\n", + "\n", + "string1 = \"The area of the circle with radius:\"\n", + "string2 = \"is:\"\n", + "radius = 4.5\n", + "\n", + "def area(x, pi = math.pi):\n", + " # This function takes a radius and returns the area of a circle. We also pass a default value for pi.\n", + " # Input: Float (and default value for pi)\n", + " # Output: Float\n", + " \n", + " area_circle = round(pi*(x**2),2) #circle area formula\n", + " \n", + " return print(f\"{string1} {x} {string2} {area_circle}\") #returning the strings with values and output\n", + "\n", + "area(4.5)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge 2 - Splitting Strings\n", + "\n", + "We have first looked at combining strings into one long string. There are times where we need to do the opposite and split the string into smaller components for further analysis. \n", + "\n", + "In the cell below, split the string into a list of strings using the space delimiter. Count the frequency of each word in the string in a dictionary. Strip the periods, line breaks and commas from the text. Make sure to remove empty strings from your dictionary." + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Some': 2, 'say': 3, 'the': 1, 'world': 1, 'will': 1, 'end': 1, 'in': 2, 'fire': 2, 'ice': 2, 'From': 1, 'what': 1, 'I’ve': 1, 'tasted': 1, 'of': 2, 'desire': 1, 'I': 3, 'hold': 1, 'with': 1, 'those': 1, 'who': 1, 'favor': 1, 'But': 1, 'if': 1, 'it': 1, 'had': 1, 'to': 1, 'perish': 1, 'twice': 1, 'think': 1, 'know': 1, 'enough': 1, 'hate': 1, 'To': 1, 'that': 1, 'for': 1, 'destruction': 1, 'Is': 1, 'also': 1, 'great': 1, 'And': 1, 'would': 1, 'suffice': 1}\n" + ] + } + ], + "source": [ + "poem = \"\"\"Some say the world will end in fire,\n", + "Some say in ice.\n", + "From what I’ve tasted of desire\n", + "I hold with those who favor fire.\n", + "But if it had to perish twice,\n", + "I think I know enough of hate\n", + "To say that for destruction ice\n", + "Is also great\n", + "And would suffice.\"\"\"\n", + "\n", + "# Your code here:\n", + "list_strings = re.split(\"[,.\\s\\n\\r]\", poem) #splitting the strings\n", + "\n", + "list_strings = [word for word in list_strings if word != ''] #deleting empty strings\n", + "\n", + "word_freq_dict = {item:list_strings.count(item) for item in list_strings} #creating the dictionary and counting the frecuency\n", + "\n", + "print(word_freq_dict)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the cell below, find all the words that appear in the text and do not appear in the blacklist. You must parse the string but can choose any data structure you wish for the words that do not appear in the blacklist. Remove all non letter characters and convert all words to lower case." + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "i was angry with my friend; \n", + "i told my wrath, my wrath did end.\n", + "i was angry with my foe: \n", + "i told it not, my wrath did grow. \n", + "\n", + "and i waterd it in fears,\n", + "night & morning with my tears: \n", + "and i sunned it with smiles,\n", + "and with soft deceitful wiles. \n", + "\n", + "and it grew both day and night. \n", + "till it bore an apple bright. \n", + "and my foe beheld it shine,\n", + "and he knew that it was mine. \n", + "\n", + "and into my garden stole, \n", + "when the night had veild the pole; \n", + "in the morning glad i see; \n", + "my foe outstretched beneath the tree.\n", + "i was angry with my friend \n", + "i told my wrath my wrath did end\n", + "i was angry with my foe \n", + "i told it not my wrath did grow \n", + "\n", + "and i waterd it in fears\n", + "night & morning with my tears \n", + "and i sunned it with smiles\n", + "and with soft deceitful wiles \n", + "\n", + "and it grew both day and night \n", + "till it bore an apple bright \n", + "and my foe beheld it shine\n", + "and he knew that it was mine \n", + "\n", + "and into my garden stole \n", + "when the night had veild the pole \n", + "in the morning glad i see \n", + "my foe outstretched beneath the tree\n", + "['i', 'was', 'angry', 'with', 'my', 'friend', 'i', 'told', 'my', 'wrath', 'my', 'wrath', 'did', 'end', 'i', 'was', 'angry', 'with', 'my', 'foe', 'i', 'told', 'it', 'not', 'my', 'wrath', 'did', 'grow', 'and', 'i', 'waterd', 'it', 'in', 'fears', 'night', '&', 'morning', 'with', 'my', 'tears', 'and', 'i', 'sunned', 'it', 'with', 'smiles', 'and', 'with', 'soft', 'deceitful', 'wiles', 'and', 'it', 'grew', 'both', 'day', 'and', 'night', 'till', 'it', 'bore', 'an', 'apple', 'bright', 'and', 'my', 'foe', 'beheld', 'it', 'shine', 'and', 'he', 'knew', 'that', 'it', 'was', 'mine', 'and', 'into', 'my', 'garden', 'stole', 'when', 'the', 'night', 'had', 'veild', 'the', 'pole', 'in', 'the', 'morning', 'glad', 'i', 'see', 'my', 'foe', 'outstretched', 'beneath', 'the', 'tree']\n", + "['i', 'was', 'angry', 'with', 'my', 'friend', 'told', 'wrath', 'did', 'end', 'foe', 'not', 'grow', 'waterd', 'fears', 'night', '&', 'morning', 'tears', 'sunned', 'smiles', 'soft', 'deceitful', 'wiles', 'grew', 'both', 'day', 'till', 'bore', 'apple', 'bright', 'beheld', 'shine', 'he', 'knew', 'that', 'mine', 'into', 'garden', 'stole', 'when', 'had', 'veild', 'pole', 'glad', 'see', 'outstretched', 'beneath', 'tree']\n" + ] + } + ], + "source": [ + "blacklist = ['and', 'as', 'an', 'a', 'the', 'in', 'it']\n", + "\n", + "poem = \"\"\"I was angry with my friend; \n", + "I told my wrath, my wrath did end.\n", + "I was angry with my foe: \n", + "I told it not, my wrath did grow. \n", + "\n", + "And I waterd it in fears,\n", + "Night & morning with my tears: \n", + "And I sunned it with smiles,\n", + "And with soft deceitful wiles. \n", + "\n", + "And it grew both day and night. \n", + "Till it bore an apple bright. \n", + "And my foe beheld it shine,\n", + "And he knew that it was mine. \n", + "\n", + "And into my garden stole, \n", + "When the night had veild the pole; \n", + "In the morning glad I see; \n", + "My foe outstretched beneath the tree.\"\"\"\n", + "\n", + "# Your code here:\n", + "lower_in_poem = poem.lower() #putting in lowercase all the strings\n", + "print(poem_lower)\n", + "\n", + "replacing_poem = re.sub(r\"[;:\\\\.,\\\\]\", \"\", poem_lower) #deleting white space and special characters\n", + "print(replacing_poem)\n", + "\n", + "splitting_poem = poem_replace.split() #putting the text into a list with split\n", + "print(splitting_poem)\n", + "\n", + "new_words_poem = [] #creating a empty list\n", + "\n", + "#for looping to print all the words in the poem that are not in the blacklist\n", + "for word in splitting_poem:\n", + " if word not in blacklist and word not in new_words_poem:\n", + " new_words_poem.append(word)\n", + "print(new_words_poem)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge 3 - Regular Expressions\n", + "\n", + "Sometimes, we would like to perform more complex manipulations of our string. This is where regular expressions come in handy. In the cell below, return all characters that are upper case from the string specified below." + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['T', 'P']\n" + ] + } + ], + "source": [ + "poem = \"\"\"The apparition of these faces in the crowd;\n", + "Petals on a wet, black bough.\"\"\"\n", + "\n", + "# Your code here:\n", + "print(re.findall('[A-Z]', poem))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the cell below, filter the list provided and return all elements of the list containing a number. To filter the list, use the `re.search` function. Check if the function does not return `None`. You can read more about the `re.search` function [here](https://docs.python.org/3/library/re.html)." + ] + }, + { + "cell_type": "code", + "execution_count": 113, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "123abc abc123 JohnSmith1 ABBY4 JANE\n", + "['123', '123', '1', '4']\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "None\n" + ] + } + ], + "source": [ + "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", + "\n", + "# Your code here:\n", + "data_string = \" \".join(data) #joining the strings\n", + "print(data_string)\n", + "\n", + "print(re.findall(\"\\d+\", data_string)) #printing the numbers with findall\n", + "\n", + "print(re.search(r'\\d+', data_string)) #printing the numbers with search\n", + "\n", + "#for loop to find out the matches position patterns\n", + "for value in data:\n", + " print(re.search('\\\\d',value))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Bonus Challenge - Regular Expressions II\n", + "\n", + "In the cell below, filter the list provided to keep only strings containing at least one digit and at least one lower case letter. As in the previous question, use the `re.search` function and check that the result is not `None`.\n", + "\n", + "To read more about regular expressions, check out [this link](https://developers.google.com/edu/python/regular-expressions)." + ] + }, + { + "cell_type": "code", + "execution_count": 114, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\n", + "\n", + "None\n" + ] + } + ], + "source": [ + "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", + "# Your code here:\n", + "#for value in data:\n", + " #print(re.search('\\\\d',value))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.3" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb b/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb index d76069e..1b27966 100644 --- a/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb @@ -72,7 +72,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -88,13 +88,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ - "corpus = []\n", - "\n", - "# Write your code here\n" + "corpus = [] #empty array\n", + "\n", + "# Write your code here\n", + "for i in range(1,4): #loop to get the strings that are in docs\n", + " with open(f'doc{i}.txt', 'r') as fr:\n", + " doc = fr.readline()\n", + " corpus.append(doc)\n", + " " ] }, { @@ -106,9 +111,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Ironhack is cool.', 'I love Ironhack.', 'I am a student at Ironhack.']\n" + ] + } + ], "source": [ "print(corpus)" ] @@ -136,11 +149,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['ironhack is cool', 'i love ironhack', 'i am a student at ironhack']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Write your code here" + "# Write your code here\n", + "#1. Remove punctuation from the strings\n", + "corpus = [doc.lower().replace('.','') for doc in corpus]\n", + "corpus" ] }, { @@ -152,7 +179,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -172,11 +199,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ - "# Write your code here" + "joining_corpus = ' '.join(corpus) #joining the strings in corpus\n", + "\n", + "bag_of_words = (joining_corpus.split()) #splitting the joining_corpus and add it into the bag_of_words \n", + "\n", + "corpus_bag_of_words = [] #defining empty array to append the words that are in bag_of_words\n", + "\n", + "for i in bag_of_words: #loop in bag_of_words\n", + " if i not in corpus_bag_of_words: #sub loop in corpus_bag_of_words\n", + " corpus_bag_of_words.append(i)\n", + "bag_of_words = corpus_bag_of_words" ] }, { @@ -192,9 +228,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack', 'is', 'cool', 'i', 'love', 'am', 'a', 'student', 'at']\n" + ] + } + ], "source": [ "print(bag_of_words)" ] @@ -208,13 +252,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, "outputs": [], "source": [ - "term_freq = []\n", + "term_freq = [] #empty array to add the count of the strings in corpus and bag_of_words\n", "\n", - "# Write your code here" + "# Write your code here\n", + "for i in corpus: \n", + " for word in bag_of_words:\n", + " term_freq.append(i.split().count(word))\n", + " " ] }, { @@ -228,9 +276,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1]\n" + ] + } + ], "source": [ "print(term_freq)" ] @@ -309,7 +365,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -323,7 +379,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.10.3" } }, "nbformat": 4, diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index 4302084..c4e1e6c 100644 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -15,7 +15,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -33,12 +33,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'Durante un tiempo no estuvo segura de si su marido era su marido.'" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "str_list = ['Durante', 'un', 'tiempo', 'no', 'estuvo', 'segura', 'de', 'si', 'su', 'marido', 'era', 'su', 'marido']\n", - "# Your code here:\n" + "# Your code here:\n", + "\" \".join(str_list) + \".\"" ] }, { @@ -50,12 +62,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['bananas', 'bread', 'brownie', 'broccoli']\n" + ] + } + ], "source": [ "food_list = ['Bananas', 'Chocolate', 'bread', 'diapers', 'Ice Cream', 'Brownie Mix', 'broccoli']\n", - "# Your code here:\n" + "# Your code here:\n", + "grocery_list = [i.lower() for i in food_list] #creating the grocery list and making all foods in lower case\n", + "\n", + "grocery_list = \" \".join(grocery_list) + \".\" #combininig all the strings\n", + "\n", + "grocery_list = re.findall(r\"b.+?\\b\",grocery_list) #including foods in the list that start with the letter 'b'\n", + "\n", + "print(grocery_list)" ] }, { @@ -69,9 +96,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 119, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The area of the circle with radius: 4.5 is: 63.62\n" + ] + } + ], "source": [ "import math\n", "\n", @@ -84,13 +119,9 @@ " # Input: Float (and default value for pi)\n", " # Output: Float\n", " \n", - " # Sample input: 5.0\n", - " # Sample Output: 78.53981633\n", - " \n", - " # Your code here:\n", - " \n", - " \n", - "# Your output string here:" + " area_circle = round(pi*(x**2),2) #circle area formula\n", + " return print(f\"{string1} {x} {string2} {area_circle}\") #returning the strings with values and output\n", + "area(4.5)" ] }, { @@ -106,9 +137,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Some': 2, 'say': 3, 'the': 1, 'world': 1, 'will': 1, 'end': 1, 'in': 2, 'fire': 2, 'ice': 2, 'From': 1, 'what': 1, 'I’ve': 1, 'tasted': 1, 'of': 2, 'desire': 1, 'I': 3, 'hold': 1, 'with': 1, 'those': 1, 'who': 1, 'favor': 1, 'But': 1, 'if': 1, 'it': 1, 'had': 1, 'to': 1, 'perish': 1, 'twice': 1, 'think': 1, 'know': 1, 'enough': 1, 'hate': 1, 'To': 1, 'that': 1, 'for': 1, 'destruction': 1, 'Is': 1, 'also': 1, 'great': 1, 'And': 1, 'would': 1, 'suffice': 1}\n" + ] + } + ], "source": [ "poem = \"\"\"Some say the world will end in fire,\n", "Some say in ice.\n", @@ -120,7 +159,14 @@ "Is also great\n", "And would suffice.\"\"\"\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "list_strings = re.split(\"[,.\\s\\n\\r]\", poem) #splitting the strings\n", + "\n", + "list_strings = [word for word in list_strings if word != ''] #deleting empty strings\n", + "\n", + "word_freq_dict = {item:list_strings.count(item) for item in list_strings} #creating the dictionary and counting the frecuency\n", + "\n", + "print(word_freq_dict)" ] }, { @@ -132,9 +178,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 120, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['i', 'was', 'angry', 'with', 'my', 'friend', 'told', 'wrath', 'did', 'end', 'foe', 'not', 'grow', 'waterd', 'fears', 'night', '&', 'morning', 'tears', 'sunned', 'smiles', 'soft', 'deceitful', 'wiles', 'grew', 'both', 'day', 'till', 'bore', 'apple', 'bright', 'beheld', 'shine', 'he', 'knew', 'that', 'mine', 'into', 'garden', 'stole', 'when', 'had', 'veild', 'pole', 'glad', 'see', 'outstretched', 'beneath', 'tree']\n" + ] + } + ], "source": [ "blacklist = ['and', 'as', 'an', 'a', 'the', 'in', 'it']\n", "\n", @@ -158,7 +212,23 @@ "In the morning glad I see; \n", "My foe outstretched beneath the tree.\"\"\"\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "lower_in_poem = poem.lower() #putting in lowercase all the strings\n", + "#print(poem_lower)\n", + "\n", + "replacing_poem = re.sub(r\"[;:\\\\.,\\\\]\", \"\", poem_lower) #deleting white space and special characters\n", + "#print(replacing_poem)\n", + "\n", + "splitting_poem = poem_replace.split() #putting the text into a list with split\n", + "#print(splitting_poem)\n", + "\n", + "new_words_poem = [] #creating a empty list\n", + "\n", + "#for looping to print all the words in the poem that are not in the blacklist\n", + "for word in splitting_poem:\n", + " if word not in blacklist and word not in new_words_poem:\n", + " new_words_poem.append(word)\n", + "print(new_words_poem)" ] }, { @@ -172,14 +242,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 78, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['T', 'P']\n" + ] + } + ], "source": [ "poem = \"\"\"The apparition of these faces in the crowd;\n", "Petals on a wet, black bough.\"\"\"\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "print(re.findall('[A-Z]', poem))" ] }, { @@ -191,13 +270,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 116, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\n", + "\n", + "None\n" + ] + } + ], "source": [ "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "data_string = \" \".join(data) #joining the strings\n", + "#print(data_string)\n", + "\n", + "#print(re.findall(\"\\d+\", data_string)) #printing the numbers with findall\n", + "\n", + "#print(re.search(r'\\d+', data_string)) #printing the numbers with search\n", + "\n", + "#for loop to find out the matches position patterns\n", + "for value in data:\n", + " print(re.search('\\\\d',value))" ] }, { @@ -213,18 +314,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 114, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\n", + "\n", + "None\n" + ] + } + ], "source": [ "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", - "# Your code here:\n" + "# Your code here:\n", + "for value in data:\n", + " print(re.search('\\\\d',value))" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -238,7 +360,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.10.3" } }, "nbformat": 4, diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index d76069e..1b27966 100644 --- a/your-code/challenge-2.ipynb +++ b/your-code/challenge-2.ipynb @@ -72,7 +72,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -88,13 +88,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ - "corpus = []\n", - "\n", - "# Write your code here\n" + "corpus = [] #empty array\n", + "\n", + "# Write your code here\n", + "for i in range(1,4): #loop to get the strings that are in docs\n", + " with open(f'doc{i}.txt', 'r') as fr:\n", + " doc = fr.readline()\n", + " corpus.append(doc)\n", + " " ] }, { @@ -106,9 +111,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Ironhack is cool.', 'I love Ironhack.', 'I am a student at Ironhack.']\n" + ] + } + ], "source": [ "print(corpus)" ] @@ -136,11 +149,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['ironhack is cool', 'i love ironhack', 'i am a student at ironhack']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Write your code here" + "# Write your code here\n", + "#1. Remove punctuation from the strings\n", + "corpus = [doc.lower().replace('.','') for doc in corpus]\n", + "corpus" ] }, { @@ -152,7 +179,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -172,11 +199,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ - "# Write your code here" + "joining_corpus = ' '.join(corpus) #joining the strings in corpus\n", + "\n", + "bag_of_words = (joining_corpus.split()) #splitting the joining_corpus and add it into the bag_of_words \n", + "\n", + "corpus_bag_of_words = [] #defining empty array to append the words that are in bag_of_words\n", + "\n", + "for i in bag_of_words: #loop in bag_of_words\n", + " if i not in corpus_bag_of_words: #sub loop in corpus_bag_of_words\n", + " corpus_bag_of_words.append(i)\n", + "bag_of_words = corpus_bag_of_words" ] }, { @@ -192,9 +228,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack', 'is', 'cool', 'i', 'love', 'am', 'a', 'student', 'at']\n" + ] + } + ], "source": [ "print(bag_of_words)" ] @@ -208,13 +252,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, "outputs": [], "source": [ - "term_freq = []\n", + "term_freq = [] #empty array to add the count of the strings in corpus and bag_of_words\n", "\n", - "# Write your code here" + "# Write your code here\n", + "for i in corpus: \n", + " for word in bag_of_words:\n", + " term_freq.append(i.split().count(word))\n", + " " ] }, { @@ -228,9 +276,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1]\n" + ] + } + ], "source": [ "print(term_freq)" ] @@ -309,7 +365,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -323,7 +379,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.10.3" } }, "nbformat": 4,