diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..2ce747c Binary files /dev/null and b/.DS_Store differ 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..49a53a6 --- /dev/null +++ b/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb @@ -0,0 +1,390 @@ +{ + "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", + "\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": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Grocery list: bananas, bread, brownie mix, broccoli.'" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "food_list = ['Bananas', 'Chocolate', 'bread', 'diapers', 'Ice Cream', 'Brownie Mix', 'broccoli']\n", + "food_list_lower = []\n", + "food_list_b = []\n", + "\n", + "# Your code here:\n", + "for i in food_list:\n", + " food_list_lower.append(i.lower())\n", + "\n", + "for i in food_list_lower:\n", + " if i[0] == 'b':\n", + " food_list_b.append(i)\n", + "\n", + "'Grocery list: ' + (\", \".join(food_list_b)) + '.'" + ] + }, + { + "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": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the radius of a circle: 5\n" + ] + }, + { + "data": { + "text/plain": [ + "'The area of the circle with radius: 5, is: 21.99'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import math\n", + "\n", + "string1 = \"The area of the circle with radius: \"\n", + "string2 = \"is: \"\n", + "\n", + "def area(radius, pi = math.pi):\n", + " area = round(pi*(radius^2), 2)\n", + " return(string1 + str(radius) + \", \" + string2 + \" \" + str(area))\n", + "\n", + "radius = int(input('Enter the radius of a circle: '))\n", + "\n", + "area(radius, pi = math.pi)" + ] + }, + { + "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": 5, + "metadata": {}, + "outputs": [], + "source": [ + "from collections import Counter" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Counter({'I': 4, 'say': 3, 'Some': 2, 'in': 2, 'fire': 2, 'ice': 2, 'of': 2, 'the': 1, 'world': 1, 'will': 1, 'end': 1, 'From': 1, 'what': 1, 'have': 1, 'tasted': 1, 'desire': 1, '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_1 = \"\"\"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", + "poem_1 = poem_1.replace(\",\", \"\")\n", + "poem_1 = poem_1.replace(\".\", \"\")\n", + "poem_1 = poem_1.replace(\"I’ve\", \"I have\")\n", + "\n", + "poem_list_1 = list(poem_1.split())\n", + "\n", + "cnt = Counter()\n", + "\n", + "for i in poem_list_1:\n", + " cnt[i] += 1\n", + " \n", + "print(cnt)" + ] + }, + { + "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": 7, + "metadata": {}, + "outputs": [], + "source": [ + "import re" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'grow', 'not', 'fears', 'my', 'see', 'knew', 'had', 'day', 'outstretched', 'bright', 'waterd', 'tears', 'with', 'morning', 'friend', 'he', 'smiles', 'told', 'wiles', 'bore', 'beheld', 'mine', 'veild', 'did', 'tree', 'when', 'beneath', 'both', 'wrath', 'foe', 'shine', 'i', 'apple', 'till', 'garden', 'stole', 'pole', 'glad', 'sunned', 'night', 'that', 'into', 'soft', 'deceitful', 'angry', 'grew', 'end', 'was'}\n" + ] + } + ], + "source": [ + "blacklist = ['and', 'as', 'an', 'a', 'the', 'in', 'it']\n", + "blacklist_set = set(blacklist)\n", + "\n", + "poem_2 = \"\"\"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_2 = re.sub(r'[&,\\.;:]', \"\", poem_2)\n", + "\n", + "poem_list_2 = list(poem_2.split())\n", + "poem_list_lower = []\n", + "\n", + "for i in poem_list_2:\n", + " poem_list_lower.append(i.lower())\n", + " \n", + "poem_set = set(poem_list_lower)\n", + "\n", + "not_in_blacklist = poem_set - blacklist_set\n", + "print(not_in_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": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['T', 'P']\n" + ] + } + ], + "source": [ + "poem_3 = \"\"\"The apparition of these faces in the crowd;\n", + "Petals on a wet, black bough.\"\"\"\n", + "\n", + "# Your code here:\n", + "print(re.findall(r'[A-Z]', poem_3))" + ] + }, + { + "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": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['123abc', 'abc123', 'JohnSmith1', 'ABBY4']\n" + ] + } + ], + "source": [ + "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", + "data_w_numbers = []\n", + "# Your code here:\n", + "\n", + "for i in data:\n", + " if (re.search('[0-9]+', i)):\n", + " data_w_numbers.append(i)\n", + " \n", + "print(data_w_numbers)" + ] + }, + { + "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": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['123abc', 'abc123', 'JohnSmith1']\n" + ] + } + ], + "source": [ + "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", + "data_w_lower_case_and_numbers = []\n", + "# Your code here:\n", + "\n", + "for i in data:\n", + " if (re.search('[0-9]+', i)) and (re.search('[a-z]+', i)):\n", + " data_w_lower_case_and_numbers.append(i)\n", + " \n", + "print(data_w_lower_case_and_numbers)" + ] + } + ], + "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.9.5" + } + }, + "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..2dcdf13 100644 --- a/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb @@ -70,13 +70,64 @@ "Now let's define the `docs` array that contains the paths of `doc1.txt`, `doc2.txt`, and `doc3.txt`." ] }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import glob" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['doc1.txt', 'doc3.txt', 'doc2.txt']\n" + ] + } + ], "source": [ - "docs = ['doc1.txt', 'doc2.txt', 'doc3.txt']" + "pwd = '/Users/diegoperezo97/Documents/Ironhack – Data Analytics Bootcamp/Module 1/Week 1/Day 2/lab-string-operations/your-code/'\n", + "os.chdir(pwd)\n", + "file_extention = '.txt'\n", + "file_names = [file for file in glob.glob(f'*{file_extention}')]\n", + "print(file_names)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['doc1.txt', 'doc3.txt', 'doc2.txt']\n" + ] + } + ], + "source": [ + "docs = []\n", + "\n", + "for file in file_names:\n", + " docs.append(file)\n", + " \n", + "print(docs)" ] }, { @@ -88,13 +139,21 @@ }, { "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", + "\n", + "# Le estás dando los índices de la lista docs y esos índices dentro del arreglo de docs ya tiene los nombre. \n", + "# Estás leyendo los archivos leyendo los nombres de los índices\n", + "for doc in range(len(docs)):\n", + " with open(docs[doc], \"r\") as file:\n", + " for line in file:\n", + " stripped_line = line.strip()\n", + " corpus.append(stripped_line)" ] }, { @@ -106,9 +165,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Ironhack is cool.', 'I am a student at Ironhack.', 'I love Ironhack.']\n" + ] + } + ], "source": [ "print(corpus)" ] @@ -136,11 +203,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ - "# Write your code here" + "import re" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack is cool', 'i am a student at ironhack', 'i love ironhack']\n" + ] + } + ], + "source": [ + "# Write your code here\n", + "corpus = [x.lower().replace('.', '') for x in corpus]\n", + "print(corpus)" ] }, { @@ -152,7 +238,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -172,29 +258,60 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ - "# Write your code here" + "from sklearn.feature_extraction import _stop_words" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 10, "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['beside', 'though', 'me', 'an', 'few', 'before', 'if', 'please', 'not', 'afterwards', 'others', 'do', 'whether', 'sixty', 'more', 'no', 'wherein', 'nine', 'everything', 'however', 'each', 'hereby', 'during', 'fifty', 'interest', 'whereupon', 'anyway', 'still', 'show', 'yours', 'here', 'become', 'whereby', 'may', 'forty', 'even', 'because', 'move', 'noone', 'out', 'cry', 'see', 'our', 'detail', 'whither', 'thick', 'any', 'five', 'whatever', 'whom', 'anything', 'your', 'seemed', 'from', 'is', 'indeed', 'amoungst', 'keep', 'you', 'on', 'part', 'my', 'but', 'enough', 'couldnt', 'by', 'eleven', 'somehow', 'the', 'both', 'themselves', 'formerly', 'he', 'thereafter', 'seems', 'un', 'found', 'are', 'alone', 'done', 'eight', 'would', 'whence', 'up', 'towards', 'further', 'than', 'via', 'con', 'never', 'below', 'something', 'became', 'fire', 'between', 'again', 'everyone', 'once', 'am', 'to', 'together', 'can', 'thence', 'too', 'behind', 'herself', 'neither', 'above', 'yet', 'beforehand', 'except', 'becomes', 'own', 'ltd', 'as', 'should', 'that', 'two', 'next', 'will', 'these', 'and', 'whole', 'first', 'sometimes', 'such', 'amount', 'at', 'either', 'fill', 'nobody', 'whose', 'although', 'someone', 'might', 'front', 'last', 'full', 'therein', 'when', 'or', 'anywhere', 'across', 'thereupon', 'nevertheless', 'hereupon', 'thru', 'could', 'system', 'several', 'three', 'must', 'otherwise', 'down', 'almost', 'co', 'nor', 'everywhere', 'upon', 'along', 'per', 'over', 'mostly', 'himself', 'amongst', 'for', 'be', 'take', 'find', 'rather', 'every', 'whereas', 'under', 'wherever', 'mill', 'thin', 'yourself', 'call', 'etc', 'were', 'so', 'less', 'had', 'a', 'hundred', 'this', 'name', 'after', 'some', 'de', 'also', 'somewhere', 'those', 'him', 'anyone', 'eg', 'why', 'latterly', 'been', 'give', 'has', 'same', 'off', 'ten', 'around', 'go', 'describe', 'side', 'bill', 're', 'among', 'being', 'put', 'with', 'another', 'where', 'therefore', 'cannot', 'becoming', 'hereafter', 'onto', 'against', 'into', 'through', 'which', 'they', 'her', 'meanwhile', 'former', 'besides', 'how', 'often', 'latter', 'else', 'always', 'beyond', 'ie', 'now', 'inc', 'six', 'there', 'hers', 'in', 'thereby', 'until', 'nowhere', 'seem', 'fifteen', 'serious', 'already', 'them', 'namely', 'elsewhere', 'seeming', 'anyhow', 'made', 'least', 'then', 'since', 'one', 'itself', 'top', 'herein', 'whenever', 'mine', 'due', 'whoever', 'ever', 'hence', 'many', 'bottom', 'none', 'sincere', 'its', 'twenty', 'thus', 'it', 'yourselves', 'ours', 'was', 'she', 'cant', 'empty', 'moreover', 'twelve', 'other', 'what', 'without', 'well', 'their', 'get', 'perhaps', 'within', 'back', 'third', 'his', 'hasnt', 'four', 'about', 'whereafter', 'while', 'we', 'who', 'all', 'toward', 'only', 'of', 'throughout', 'sometime', 'very', 'much', 'have', 'ourselves', 'most', 'myself', 'i', 'us', 'nothing']\n" + ] + } + ], "source": [ - "Print `bag_of_words`. You should see: \n", - "\n", - "```['ironhack', 'is', 'cool', 'i', 'love', 'am', 'a', 'student', 'at']```\n", - "\n", - "If not, fix your code in the previous cell." + "stop_words = list(_stop_words.ENGLISH_STOP_WORDS)\n", + "print(stop_words)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, "outputs": [], + "source": [ + "# Write your code here\n", + "for text in range(len(corpus)):\n", + " words = re.split(\" \", (corpus[text]))\n", + " for word in words:\n", + " if word in bag_of_words or word in stop_words:\n", + " pass\n", + " else:\n", + " bag_of_words.append(word)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack', 'cool', 'student', 'love']\n" + ] + } + ], "source": [ "print(bag_of_words)" ] @@ -208,31 +325,62 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack is cool', 'i am a student at ironhack', 'i love ironhack']\n" + ] + } + ], "source": [ - "term_freq = []\n", + "corpus_l = []\n", "\n", - "# Write your code here" + "for text in range(len(corpus)):\n", + " corpus_l.append((corpus[text]))\n", + " \n", + "print(corpus_l)" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 14, "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1, 1, 0, 0], [1, 0, 1, 0], [1, 0, 0, 1]]\n" + ] + } + ], "source": [ - "Print `term_freq`. You should see:\n", + "term_freq = []\n", "\n", - "```[[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]]```" + "# Write your code here\n", + "for j in range(len(corpus_l)):\n", + " term_freq_b = []\n", + " for i in bag_of_words:\n", + " if i in corpus_l[j].split():\n", + " term_freq_b.append(1)\n", + " else:\n", + " term_freq_b.append(0)\n", + " term_freq.append(term_freq_b)\n", + " \n", + "print(term_freq)" ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "print(term_freq)" + "Print `term_freq`. You should see:\n", + "\n", + "```[[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]]```" ] }, { @@ -268,7 +416,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ @@ -323,7 +471,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.9.5" } }, "nbformat": 4, diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index 4302084..49a53a6 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,25 @@ }, { "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" + "\n", + "# Your code here:\n", + "(\" \".join(str_list)) + '.'" ] }, { @@ -50,12 +63,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'Grocery list: bananas, bread, brownie mix, broccoli.'" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "food_list = ['Bananas', 'Chocolate', 'bread', 'diapers', 'Ice Cream', 'Brownie Mix', 'broccoli']\n", - "# Your code here:\n" + "food_list_lower = []\n", + "food_list_b = []\n", + "\n", + "# Your code here:\n", + "for i in food_list:\n", + " food_list_lower.append(i.lower())\n", + "\n", + "for i in food_list_lower:\n", + " if i[0] == 'b':\n", + " food_list_b.append(i)\n", + "\n", + "'Grocery list: ' + (\", \".join(food_list_b)) + '.'" ] }, { @@ -69,28 +104,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the radius of a circle: 5\n" + ] + }, + { + "data": { + "text/plain": [ + "'The area of the circle with radius: 5, is: 21.99'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import math\n", "\n", - "string1 = \"The area of the circle with radius:\"\n", - "string2 = \"is:\"\n", - "radius = 4.5\n", + "string1 = \"The area of the circle with radius: \"\n", + "string2 = \"is: \"\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", - " \n", - " \n", - "# Your output string here:" + "def area(radius, pi = math.pi):\n", + " area = round(pi*(radius^2), 2)\n", + " return(string1 + str(radius) + \", \" + string2 + \" \" + str(area))\n", + "\n", + "radius = int(input('Enter the radius of a circle: '))\n", + "\n", + "area(radius, pi = math.pi)" ] }, { @@ -106,11 +153,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ - "poem = \"\"\"Some say the world will end in fire,\n", + "from collections import Counter" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Counter({'I': 4, 'say': 3, 'Some': 2, 'in': 2, 'fire': 2, 'ice': 2, 'of': 2, 'the': 1, 'world': 1, 'will': 1, 'end': 1, 'From': 1, 'what': 1, 'have': 1, 'tasted': 1, 'desire': 1, '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_1 = \"\"\"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", @@ -120,7 +184,19 @@ "Is also great\n", "And would suffice.\"\"\"\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "poem_1 = poem_1.replace(\",\", \"\")\n", + "poem_1 = poem_1.replace(\".\", \"\")\n", + "poem_1 = poem_1.replace(\"I’ve\", \"I have\")\n", + "\n", + "poem_list_1 = list(poem_1.split())\n", + "\n", + "cnt = Counter()\n", + "\n", + "for i in poem_list_1:\n", + " cnt[i] += 1\n", + " \n", + "print(cnt)" ] }, { @@ -132,13 +208,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], + "source": [ + "import re" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'grow', 'not', 'fears', 'my', 'see', 'knew', 'had', 'day', 'outstretched', 'bright', 'waterd', 'tears', 'with', 'morning', 'friend', 'he', 'smiles', 'told', 'wiles', 'bore', 'beheld', 'mine', 'veild', 'did', 'tree', 'when', 'beneath', 'both', 'wrath', 'foe', 'shine', 'i', 'apple', 'till', 'garden', 'stole', 'pole', 'glad', 'sunned', 'night', 'that', 'into', 'soft', 'deceitful', 'angry', 'grew', 'end', 'was'}\n" + ] + } + ], "source": [ "blacklist = ['and', 'as', 'an', 'a', 'the', 'in', 'it']\n", + "blacklist_set = set(blacklist)\n", "\n", - "poem = \"\"\"I was angry with my friend; \n", + "poem_2 = \"\"\"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", @@ -158,7 +252,19 @@ "In the morning glad I see; \n", "My foe outstretched beneath the tree.\"\"\"\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "poem_2 = re.sub(r'[&,\\.;:]', \"\", poem_2)\n", + "\n", + "poem_list_2 = list(poem_2.split())\n", + "poem_list_lower = []\n", + "\n", + "for i in poem_list_2:\n", + " poem_list_lower.append(i.lower())\n", + " \n", + "poem_set = set(poem_list_lower)\n", + "\n", + "not_in_blacklist = poem_set - blacklist_set\n", + "print(not_in_blacklist)" ] }, { @@ -172,14 +278,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['T', 'P']\n" + ] + } + ], "source": [ - "poem = \"\"\"The apparition of these faces in the crowd;\n", + "poem_3 = \"\"\"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(r'[A-Z]', poem_3))" ] }, { @@ -191,13 +306,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['123abc', 'abc123', 'JohnSmith1', 'ABBY4']\n" + ] + } + ], "source": [ "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", + "data_w_numbers = []\n", + "# Your code here:\n", "\n", - "# Your code here:\n" + "for i in data:\n", + " if (re.search('[0-9]+', i)):\n", + " data_w_numbers.append(i)\n", + " \n", + "print(data_w_numbers)" ] }, { @@ -213,12 +342,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['123abc', 'abc123', 'JohnSmith1']\n" + ] + } + ], "source": [ "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", - "# Your code here:\n" + "data_w_lower_case_and_numbers = []\n", + "# Your code here:\n", + "\n", + "for i in data:\n", + " if (re.search('[0-9]+', i)) and (re.search('[a-z]+', i)):\n", + " data_w_lower_case_and_numbers.append(i)\n", + " \n", + "print(data_w_lower_case_and_numbers)" ] } ], @@ -238,7 +382,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.9.5" } }, "nbformat": 4, diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index d76069e..2dcdf13 100644 --- a/your-code/challenge-2.ipynb +++ b/your-code/challenge-2.ipynb @@ -70,13 +70,64 @@ "Now let's define the `docs` array that contains the paths of `doc1.txt`, `doc2.txt`, and `doc3.txt`." ] }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import glob" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['doc1.txt', 'doc3.txt', 'doc2.txt']\n" + ] + } + ], "source": [ - "docs = ['doc1.txt', 'doc2.txt', 'doc3.txt']" + "pwd = '/Users/diegoperezo97/Documents/Ironhack – Data Analytics Bootcamp/Module 1/Week 1/Day 2/lab-string-operations/your-code/'\n", + "os.chdir(pwd)\n", + "file_extention = '.txt'\n", + "file_names = [file for file in glob.glob(f'*{file_extention}')]\n", + "print(file_names)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['doc1.txt', 'doc3.txt', 'doc2.txt']\n" + ] + } + ], + "source": [ + "docs = []\n", + "\n", + "for file in file_names:\n", + " docs.append(file)\n", + " \n", + "print(docs)" ] }, { @@ -88,13 +139,21 @@ }, { "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", + "\n", + "# Le estás dando los índices de la lista docs y esos índices dentro del arreglo de docs ya tiene los nombre. \n", + "# Estás leyendo los archivos leyendo los nombres de los índices\n", + "for doc in range(len(docs)):\n", + " with open(docs[doc], \"r\") as file:\n", + " for line in file:\n", + " stripped_line = line.strip()\n", + " corpus.append(stripped_line)" ] }, { @@ -106,9 +165,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Ironhack is cool.', 'I am a student at Ironhack.', 'I love Ironhack.']\n" + ] + } + ], "source": [ "print(corpus)" ] @@ -136,11 +203,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ - "# Write your code here" + "import re" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack is cool', 'i am a student at ironhack', 'i love ironhack']\n" + ] + } + ], + "source": [ + "# Write your code here\n", + "corpus = [x.lower().replace('.', '') for x in corpus]\n", + "print(corpus)" ] }, { @@ -152,7 +238,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -172,29 +258,60 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ - "# Write your code here" + "from sklearn.feature_extraction import _stop_words" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 10, "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['beside', 'though', 'me', 'an', 'few', 'before', 'if', 'please', 'not', 'afterwards', 'others', 'do', 'whether', 'sixty', 'more', 'no', 'wherein', 'nine', 'everything', 'however', 'each', 'hereby', 'during', 'fifty', 'interest', 'whereupon', 'anyway', 'still', 'show', 'yours', 'here', 'become', 'whereby', 'may', 'forty', 'even', 'because', 'move', 'noone', 'out', 'cry', 'see', 'our', 'detail', 'whither', 'thick', 'any', 'five', 'whatever', 'whom', 'anything', 'your', 'seemed', 'from', 'is', 'indeed', 'amoungst', 'keep', 'you', 'on', 'part', 'my', 'but', 'enough', 'couldnt', 'by', 'eleven', 'somehow', 'the', 'both', 'themselves', 'formerly', 'he', 'thereafter', 'seems', 'un', 'found', 'are', 'alone', 'done', 'eight', 'would', 'whence', 'up', 'towards', 'further', 'than', 'via', 'con', 'never', 'below', 'something', 'became', 'fire', 'between', 'again', 'everyone', 'once', 'am', 'to', 'together', 'can', 'thence', 'too', 'behind', 'herself', 'neither', 'above', 'yet', 'beforehand', 'except', 'becomes', 'own', 'ltd', 'as', 'should', 'that', 'two', 'next', 'will', 'these', 'and', 'whole', 'first', 'sometimes', 'such', 'amount', 'at', 'either', 'fill', 'nobody', 'whose', 'although', 'someone', 'might', 'front', 'last', 'full', 'therein', 'when', 'or', 'anywhere', 'across', 'thereupon', 'nevertheless', 'hereupon', 'thru', 'could', 'system', 'several', 'three', 'must', 'otherwise', 'down', 'almost', 'co', 'nor', 'everywhere', 'upon', 'along', 'per', 'over', 'mostly', 'himself', 'amongst', 'for', 'be', 'take', 'find', 'rather', 'every', 'whereas', 'under', 'wherever', 'mill', 'thin', 'yourself', 'call', 'etc', 'were', 'so', 'less', 'had', 'a', 'hundred', 'this', 'name', 'after', 'some', 'de', 'also', 'somewhere', 'those', 'him', 'anyone', 'eg', 'why', 'latterly', 'been', 'give', 'has', 'same', 'off', 'ten', 'around', 'go', 'describe', 'side', 'bill', 're', 'among', 'being', 'put', 'with', 'another', 'where', 'therefore', 'cannot', 'becoming', 'hereafter', 'onto', 'against', 'into', 'through', 'which', 'they', 'her', 'meanwhile', 'former', 'besides', 'how', 'often', 'latter', 'else', 'always', 'beyond', 'ie', 'now', 'inc', 'six', 'there', 'hers', 'in', 'thereby', 'until', 'nowhere', 'seem', 'fifteen', 'serious', 'already', 'them', 'namely', 'elsewhere', 'seeming', 'anyhow', 'made', 'least', 'then', 'since', 'one', 'itself', 'top', 'herein', 'whenever', 'mine', 'due', 'whoever', 'ever', 'hence', 'many', 'bottom', 'none', 'sincere', 'its', 'twenty', 'thus', 'it', 'yourselves', 'ours', 'was', 'she', 'cant', 'empty', 'moreover', 'twelve', 'other', 'what', 'without', 'well', 'their', 'get', 'perhaps', 'within', 'back', 'third', 'his', 'hasnt', 'four', 'about', 'whereafter', 'while', 'we', 'who', 'all', 'toward', 'only', 'of', 'throughout', 'sometime', 'very', 'much', 'have', 'ourselves', 'most', 'myself', 'i', 'us', 'nothing']\n" + ] + } + ], "source": [ - "Print `bag_of_words`. You should see: \n", - "\n", - "```['ironhack', 'is', 'cool', 'i', 'love', 'am', 'a', 'student', 'at']```\n", - "\n", - "If not, fix your code in the previous cell." + "stop_words = list(_stop_words.ENGLISH_STOP_WORDS)\n", + "print(stop_words)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, "outputs": [], + "source": [ + "# Write your code here\n", + "for text in range(len(corpus)):\n", + " words = re.split(\" \", (corpus[text]))\n", + " for word in words:\n", + " if word in bag_of_words or word in stop_words:\n", + " pass\n", + " else:\n", + " bag_of_words.append(word)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack', 'cool', 'student', 'love']\n" + ] + } + ], "source": [ "print(bag_of_words)" ] @@ -208,31 +325,62 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack is cool', 'i am a student at ironhack', 'i love ironhack']\n" + ] + } + ], "source": [ - "term_freq = []\n", + "corpus_l = []\n", "\n", - "# Write your code here" + "for text in range(len(corpus)):\n", + " corpus_l.append((corpus[text]))\n", + " \n", + "print(corpus_l)" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 14, "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1, 1, 0, 0], [1, 0, 1, 0], [1, 0, 0, 1]]\n" + ] + } + ], "source": [ - "Print `term_freq`. You should see:\n", + "term_freq = []\n", "\n", - "```[[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]]```" + "# Write your code here\n", + "for j in range(len(corpus_l)):\n", + " term_freq_b = []\n", + " for i in bag_of_words:\n", + " if i in corpus_l[j].split():\n", + " term_freq_b.append(1)\n", + " else:\n", + " term_freq_b.append(0)\n", + " term_freq.append(term_freq_b)\n", + " \n", + "print(term_freq)" ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "print(term_freq)" + "Print `term_freq`. You should see:\n", + "\n", + "```[[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]]```" ] }, { @@ -268,7 +416,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ @@ -323,7 +471,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.9.5" } }, "nbformat": 4,