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..e3cf68d --- /dev/null +++ b/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb @@ -0,0 +1,451 @@ +{ + "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": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Durante un tiempo no estuvo segura de si su marido era su marido.'" + ] + }, + "execution_count": 3, + "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": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['bread', 'broccoli']\n" + ] + }, + { + "data": { + "text/plain": [ + "'bread broccoli.'" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "food_list = ['Bananas', 'Chocolate', 'bread', 'diapers', 'Ice Cream', 'Brownie Mix', 'broccoli']\n", + "# Your code here:\n", + "grocery_list= ', '.join(food_list)+'.'\n", + "#print(grocery_list)\n", + "lista=[]\n", + "for i in range(len(food_list)):\n", + " if food_list[i].startswith('b'):\n", + " lista.append(food_list[i])\n", + "print(lista)\n", + "grocery_list=' '.join(lista)+'.'\n", + "grocery_list.lower()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Grocery list: bananas, bread, brownie mix, broccoli.\n" + ] + } + ], + "source": [ + "food_list = ['Bananas', 'Chocolate', 'bread', 'diapers', 'Ice Cream', 'Brownie Mix', 'broccoli']\n", + "lista=[]\n", + "for food in food_list:\n", + " food=food.lower()\n", + " if food.startswith('b'):\n", + " lista.append(food)\n", + " \n", + "grocery_list=', '.join(lista)+'.'\n", + "print('Grocery list:',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": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The area of the circle with radius: 4.5 is: 63.61725123519331\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", + " # Sample input: 5.0\n", + " # Sample Output: 78.53981633\n", + " \n", + " # Your code here:\n", + " a=pi*x**2\n", + " return a\n", + " \n", + "# Your output string here:\n", + "Area=area(radius)\n", + "#print(Area)\n", + "print(string1+' '+str(radius)+' '+string2+' '+str(Area))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "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": 38, + "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", + "import re\n", + "\n", + "a=poem.replace(',',\" \")\n", + "b=a.replace('.',\" \")\n", + "c=b.replace('\\n',' ')\n", + "#print(c)\n", + "palabras=c.split()\n", + "#print(palabras)\n", + "freq=[]\n", + "dic={}\n", + "for palabra in palabras:\n", + " dic[palabra]=palabras.count(palabra)\n", + "print(dic)" + ] + }, + { + "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.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['i', 'was', 'angry', 'with', 'my', 'friend', 'i', 'told', 'my', 'wrath', 'my', 'wrath', 'did', 'endi', '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", + "100\n", + "['i', 'was', 'angry', 'with', 'my', 'friend', 'i', 'told', 'my', 'wrath', 'my', 'wrath', 'did', 'endi', 'was', 'angry', 'with', 'my', 'foe', 'i', 'told', 'not', 'my', 'wrath', 'did', 'grow', 'i', 'waterd', 'fears', 'night', '&', 'morning', 'with', 'my', 'tears', 'i', 'sunned', 'with', 'smiles', 'with', 'soft', 'deceitful', 'wiles', 'grew', 'both', 'day', 'night', 'till', 'bore', 'apple', 'bright', 'my', 'foe', 'beheld', 'shine', 'he', 'knew', 'that', 'was', 'mine', 'into', 'my', 'garden', 'stole', 'when', 'night', 'had', 'veild', 'pole', 'morning', 'glad', 'i', 'see', 'my', 'foe', 'outstretched', 'beneath', 'tree']\n", + "78\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", + "poem=poem.lower()\n", + "a=poem.replace(',',\" \")\n", + "b=a.replace('.',\"\")\n", + "c=b.replace('\\n','')\n", + "d=c.replace(';','')\n", + "e=d.replace(':','')\n", + "#print(e)\n", + "\n", + "palabras=e.split()\n", + "print(palabras)\n", + "print(len(palabras))\n", + "\n", + "#tener puras palabras\n", + "nueva_lista=[]\n", + "for palabra in palabras:\n", + " if palabra in blacklist:\n", + " continue\n", + " else:\n", + " nueva_lista.append(palabra)\n", + "print(nueva_lista)\n", + "print(len(nueva_lista))\n", + "\n", + "#buscar y quitar las palabras del blacklist" + ] + }, + { + "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": 55, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['The', 'Petals']" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "poem = \"\"\"The apparition of these faces in the crowd;\n", + "Petals on a wet, black bough.\"\"\"\n", + "\n", + "# Your code here:\n", + "\n", + "import re\n", + "\n", + "re.findall(r'[A-Z].*?\\b',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": 67, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['123abc', 'abc123', 'JohnSmith1', 'ABBY4']\n" + ] + } + ], + "source": [ + "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", + "\n", + "# Your code here:\n", + "lista=[]\n", + "for elemento in data:\n", + " if re.search(r\"\\d\",elemento):\n", + " lista.append(elemento)\n", + "print(lista)" + ] + }, + { + "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": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['123abc', 'abc123']\n" + ] + } + ], + "source": [ + "import re\n", + "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", + "# Your code here:\n", + "lista=[]\n", + "for elemento in data:\n", + " if re.search(r\"\\d\",elemento) and elemento.islower()==True:\n", + " lista.append(elemento)\n", + "print(lista)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['123abc', 'abc123', 'JohnSmith1']\n" + ] + } + ], + "source": [ + "filtered_2=[elemento for elemento in data if re.search(r'[0-19]',elemento) !=None and re.search(r'[a-z]',elemento)!=None]\n", + "print(filtered_2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.8.8" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb b/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb index d76069e..f3f2805 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": 2, "metadata": {}, "outputs": [], "source": [ @@ -88,13 +88,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "corpus = []\n", "\n", - "# Write your code here\n" + "# Write your code here\n", + "for doc in docs:\n", + " f=open(doc,'r')\n", + " corpus.append(f.read())\n", + " f.close()" ] }, { @@ -106,9 +110,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "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 +148,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack is cool', 'i love ironhack', 'i am a student at ironhack']\n" + ] + } + ], "source": [ - "# Write your code here" + "# Write your code here\n", + "remove_punctuation='.,'\n", + "corpus_clean=[]\n", + "for data in corpus:\n", + " clean=data\n", + " for punctuation in remove_punctuation:\n", + " clean=clean.replace(punctuation,'')\n", + " corpus_clean.append(clean.lower())\n", + "print(corpus_clean)" ] }, { @@ -152,7 +180,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -172,11 +200,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ - "# Write your code here" + "# Write your code here\n", + "for corpus in corpus_clean:\n", + " for word in corpus.split():\n", + " if word not in bag_of_words:\n", + " bag_of_words.append(word)" ] }, { @@ -192,9 +224,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "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 +248,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "term_freq = []\n", "\n", - "# Write your code here" + "# Write your code here\n", + "for corpus in corpus_clean:\n", + " term_freq_corpus=[]\n", + " for word in bag_of_words:\n", + " term_freq_corpus.append(corpus.split().count(word))\n", + " term_freq.append(term_freq_corpus)" ] }, { @@ -323,9 +368,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.8.8" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index 4302084..e3cf68d 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": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'Durante un tiempo no estuvo segura de si su marido era su marido.'" + ] + }, + "execution_count": 3, + "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,64 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['bread', 'broccoli']\n" + ] + }, + { + "data": { + "text/plain": [ + "'bread broccoli.'" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "food_list = ['Bananas', 'Chocolate', 'bread', 'diapers', 'Ice Cream', 'Brownie Mix', 'broccoli']\n", - "# Your code here:\n" + "# Your code here:\n", + "grocery_list= ', '.join(food_list)+'.'\n", + "#print(grocery_list)\n", + "lista=[]\n", + "for i in range(len(food_list)):\n", + " if food_list[i].startswith('b'):\n", + " lista.append(food_list[i])\n", + "print(lista)\n", + "grocery_list=' '.join(lista)+'.'\n", + "grocery_list.lower()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Grocery list: bananas, bread, brownie mix, broccoli.\n" + ] + } + ], + "source": [ + "food_list = ['Bananas', 'Chocolate', 'bread', 'diapers', 'Ice Cream', 'Brownie Mix', 'broccoli']\n", + "lista=[]\n", + "for food in food_list:\n", + " food=food.lower()\n", + " if food.startswith('b'):\n", + " lista.append(food)\n", + " \n", + "grocery_list=', '.join(lista)+'.'\n", + "print('Grocery list:',grocery_list)" ] }, { @@ -69,9 +133,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The area of the circle with radius: 4.5 is: 63.61725123519331\n" + ] + } + ], "source": [ "import math\n", "\n", @@ -88,11 +160,22 @@ " # Sample Output: 78.53981633\n", " \n", " # Your code here:\n", + " a=pi*x**2\n", + " return a\n", " \n", - " \n", - "# Your output string here:" + "# Your output string here:\n", + "Area=area(radius)\n", + "#print(Area)\n", + "print(string1+' '+str(radius)+' '+string2+' '+str(Area))" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -106,9 +189,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "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,21 +211,45 @@ "Is also great\n", "And would suffice.\"\"\"\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "import re\n", + "\n", + "a=poem.replace(',',\" \")\n", + "b=a.replace('.',\" \")\n", + "c=b.replace('\\n',' ')\n", + "#print(c)\n", + "palabras=c.split()\n", + "#print(palabras)\n", + "freq=[]\n", + "dic={}\n", + "for palabra in palabras:\n", + " dic[palabra]=palabras.count(palabra)\n", + "print(dic)" ] }, { "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." + "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.\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 51, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['i', 'was', 'angry', 'with', 'my', 'friend', 'i', 'told', 'my', 'wrath', 'my', 'wrath', 'did', 'endi', '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", + "100\n", + "['i', 'was', 'angry', 'with', 'my', 'friend', 'i', 'told', 'my', 'wrath', 'my', 'wrath', 'did', 'endi', 'was', 'angry', 'with', 'my', 'foe', 'i', 'told', 'not', 'my', 'wrath', 'did', 'grow', 'i', 'waterd', 'fears', 'night', '&', 'morning', 'with', 'my', 'tears', 'i', 'sunned', 'with', 'smiles', 'with', 'soft', 'deceitful', 'wiles', 'grew', 'both', 'day', 'night', 'till', 'bore', 'apple', 'bright', 'my', 'foe', 'beheld', 'shine', 'he', 'knew', 'that', 'was', 'mine', 'into', 'my', 'garden', 'stole', 'when', 'night', 'had', 'veild', 'pole', 'morning', 'glad', 'i', 'see', 'my', 'foe', 'outstretched', 'beneath', 'tree']\n", + "78\n" + ] + } + ], "source": [ "blacklist = ['and', 'as', 'an', 'a', 'the', 'in', 'it']\n", "\n", @@ -158,7 +273,30 @@ "In the morning glad I see; \n", "My foe outstretched beneath the tree.\"\"\"\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "poem=poem.lower()\n", + "a=poem.replace(',',\" \")\n", + "b=a.replace('.',\"\")\n", + "c=b.replace('\\n','')\n", + "d=c.replace(';','')\n", + "e=d.replace(':','')\n", + "#print(e)\n", + "\n", + "palabras=e.split()\n", + "print(palabras)\n", + "print(len(palabras))\n", + "\n", + "#tener puras palabras\n", + "nueva_lista=[]\n", + "for palabra in palabras:\n", + " if palabra in blacklist:\n", + " continue\n", + " else:\n", + " nueva_lista.append(palabra)\n", + "print(nueva_lista)\n", + "print(len(nueva_lista))\n", + "\n", + "#buscar y quitar las palabras del blacklist" ] }, { @@ -172,14 +310,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 55, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['The', 'Petals']" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], "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", + "\n", + "import re\n", + "\n", + "re.findall(r'[A-Z].*?\\b',poem)" ] }, { @@ -191,13 +344,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 67, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['123abc', 'abc123', 'JohnSmith1', 'ABBY4']\n" + ] + } + ], "source": [ "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "lista=[]\n", + "for elemento in data:\n", + " if re.search(r\"\\d\",elemento):\n", + " lista.append(elemento)\n", + "print(lista)" ] }, { @@ -213,13 +379,52 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['123abc', 'abc123']\n" + ] + } + ], "source": [ + "import re\n", "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", - "# Your code here:\n" + "# Your code here:\n", + "lista=[]\n", + "for elemento in data:\n", + " if re.search(r\"\\d\",elemento) and elemento.islower()==True:\n", + " lista.append(elemento)\n", + "print(lista)" ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['123abc', 'abc123', 'JohnSmith1']\n" + ] + } + ], + "source": [ + "filtered_2=[elemento for elemento in data if re.search(r'[0-19]',elemento) !=None and re.search(r'[a-z]',elemento)!=None]\n", + "print(filtered_2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -238,9 +443,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.8.8" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index d76069e..f3f2805 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": 2, "metadata": {}, "outputs": [], "source": [ @@ -88,13 +88,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "corpus = []\n", "\n", - "# Write your code here\n" + "# Write your code here\n", + "for doc in docs:\n", + " f=open(doc,'r')\n", + " corpus.append(f.read())\n", + " f.close()" ] }, { @@ -106,9 +110,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "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 +148,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack is cool', 'i love ironhack', 'i am a student at ironhack']\n" + ] + } + ], "source": [ - "# Write your code here" + "# Write your code here\n", + "remove_punctuation='.,'\n", + "corpus_clean=[]\n", + "for data in corpus:\n", + " clean=data\n", + " for punctuation in remove_punctuation:\n", + " clean=clean.replace(punctuation,'')\n", + " corpus_clean.append(clean.lower())\n", + "print(corpus_clean)" ] }, { @@ -152,7 +180,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -172,11 +200,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ - "# Write your code here" + "# Write your code here\n", + "for corpus in corpus_clean:\n", + " for word in corpus.split():\n", + " if word not in bag_of_words:\n", + " bag_of_words.append(word)" ] }, { @@ -192,9 +224,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "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 +248,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "term_freq = []\n", "\n", - "# Write your code here" + "# Write your code here\n", + "for corpus in corpus_clean:\n", + " term_freq_corpus=[]\n", + " for word in bag_of_words:\n", + " term_freq_corpus.append(corpus.split().count(word))\n", + " term_freq.append(term_freq_corpus)" ] }, { @@ -323,9 +368,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.8.8" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 }